# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - Blockquote Parser

    Heavily inspired by Nir Soffer's marvelous Section Parser

    For proper functioning of the parser you need to install
    header.py in your MoinMoin/util directory, add some css
    formatter stuff to e.g. common.css and provide a
    translation for _('Source').

    Header.py is taken from Section Parser and modified for
    Blockquote Parser. For having more accessible layout fun
    do also install Section Parser which allows to have nice
    layout in a simple and maintainable way.

    Section Parser
    @copyright: 2005 by Nir Soffer <nirs@freeshell.org>
    @license: GNU GPL, see COPYING for details.

    Blockquote Parser
    @copyright: 2007 by Oliver Siemoneit
    @license: GNU GPL, see COPYING for details.
"""

from MoinMoin import wikiutil, i18n
from MoinMoin.util.header import Header

Dependencies = []

class Parser:
    """
        Outputs wiki markup in a html <blockquote cite=".." lang=".." dir=".."> element.
    """

    Dependencies = []
    
    def __init__(self, raw, request, **kw):
        self.raw = raw
        self.request = request
        self.form = request.form
        self._ = request.getText

    def _get_formatterName(self, formatter):
        # XXX remove this function again. It's just a workaround since FormatterBase
        # has no 'def blockquote' yet.
        import inspect
        for key, value in inspect.getmembers(formatter, inspect.isclass(formatter)):
            if key == '__module__':
                return value
        return ''

    def format(self, formatter):
        """ Send the text. """
        _ = self._
        self.header = Header(self.request, self.raw)
        # parse header
        source = self.header.get('cite', None)
        cite = ''
        if source:
            cite = ' cite="%s"' % wikiutil.escape(source)
        lang = ' lang="%s" dir="%s"' % (self.header.get('language', self.request.content_lang),
                                        i18n.getDirection(self.header.get('language', self.request.content_lang)))
        # ouput blockquote

        # XXX
        # do not use "formatter.rawHTML('<blockquote>')" but
        # "formatter.blockquote(1, cite=, lang=, dir=)... formatter.blockquote(0)"
        # so as not to violate ouput abstraction.
        try: 
            Parser = wikiutil.importPlugin(self.request.cfg, 'parser', 'text_moin_wiki', 'Parser')
            wiki_parser = Parser(self.raw[self.header.length():], self.request)

            if self._get_formatterName(formatter) == 'MoinMoin.formatter.text_html':
                self.request.write(formatter.rawHTML('<blockquote%s%s>' % (cite, lang)))       
                wiki_parser.format(formatter)
                if cite:
                    self.request.write(formatter.rawHTML('<p class="quotesource">%s: %s</p>' % (_('Source'),
                                                                                                wikiutil.escape(source))))
                self.request.write(formatter.rawHTML('</blockquote>\n'))
            else:
                wiki_parser.format(formatter)
        except:
            self.request.write(formatter.sysmsg(1) + 
                formatter.text(_('Error while trying to parse blockquote')) +
                formatter.sysmsg(0))
 
