From 9476b4d684679eff03a8e7c9cd750196c923999b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Tue, 19 Jun 2012 23:22:44 +0200 Subject: [PATCH] native formatter based on html formatter old changes from Feb 10, 2011 --- moinformat.py | 29 +++++++ text_dokuwiki.py | 209 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 238 insertions(+) create mode 100755 moinformat.py create mode 100644 text_dokuwiki.py diff --git a/moinformat.py b/moinformat.py new file mode 100755 index 0000000..4ddc7c8 --- /dev/null +++ b/moinformat.py @@ -0,0 +1,29 @@ +#!/usr/bin/python +# -*- coding: iso-8859-1 -*- +from MoinMoin import wikimacro, wikiutil +from MoinMoin.Page import Page +from MoinMoin.parser.wiki import Parser + +from text_dokuwiki import Formatter +from MoinMoin.request import RequestCLI + +request = RequestCLI() +formatter = Formatter(request) + +text = """ += Headers = +== Header 2 == +=== Header 3 === +==== Header 4 ==== +===== Header 5 ===== +""" + +parser = Parser(text, request) + +# this needed for macros +request.formatter = formatter + +p = Page(request, "test") +formatter.setPage(p) + +parser.format(formatter) diff --git a/text_dokuwiki.py b/text_dokuwiki.py new file mode 100644 index 0000000..5c91a79 --- /dev/null +++ b/text_dokuwiki.py @@ -0,0 +1,209 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - Dokuwiki Formatter + + @copyright: 2000, 2001, 2002 by Jürgen Hermann + @copyright: 2011-2012 Elan Ruusamäe + @license: GNU GPL, see COPYING for details. +""" + +from xml.sax import saxutils +from MoinMoin.formatter.base import FormatterBase +from MoinMoin import config +from MoinMoin.Page import Page + +class Formatter(FormatterBase): + """ + Send Dokuwiki formatted data. + """ + + hardspace = ' ' + + def __init__(self, request, **kw): + apply(FormatterBase.__init__, (self, request), kw) + self._current_depth = 1 + self._base_depth = 0 + self.in_pre = 0 + self._text = None # XXX does not work with links in headings!!!!! + + def _escape(self, text, extra_mapping={"'": "'", '"': """}): + return saxutils.escape(text, extra_mapping) + + def startDocument(self, pagename): + encoding = config.charset + return '\n' % ( + encoding, self._escape(pagename)) + + def endDocument(self): + result = "" + while self._current_depth > 1: + result += "" % self._current_depth + self._current_depth -= 1 + return result + '' + + def lang(self, on, lang_name): + return ('
' % lang_name, '
')[not on] + + def sysmsg(self, on, **kw): + return ('', '')[not on] + + def rawHTML(self, markup): + return '' + markup + '' + + def pagelink(self, on, pagename='', page=None, **kw): + apply(FormatterBase.pagelink, (self, on, pagename, page), kw) + if page is None: + page = Page(self.request, pagename, formatter=self) + return page.link_to(self.request, on=on, **kw) + + def interwikilink(self, on, interwiki='', pagename='', **kw): + if on: + return '[[%s|%s' % (interwiki, pagename) + else: + return ']]' + + def url(self, on, url='', css=None, **kw): + if css: + str = ' class="%s"' % css + else: + str = '' + return ('' % (self._escape(url), str), '') [not on] + + def attachment_link(self, url, text, **kw): + return '{{%s|%s}}' % (url, text) + + def attachment_image(self, url, **kw): + return '{{%s|}' % (url,) + + def attachment_drawing(self, url, text, **kw): + return '{{%s|%s}}' % (url, text) + + def text(self, text, **kw): + self._did_para = 0 + if self._text is not None: + self._text.append(text) + return text + + def rule(self, size=0, **kw): + # size not supported + return '----\n' + + def icon(self, type): + return '' % type + + def strong(self, on, **kw): + return ['**', '**'][not on] + + def emphasis(self, on, **kw): + return ['//', '//'][not on] + + def highlight(self, on, **kw): + return ['**', '**'][not on] + + def number_list(self, on, type=None, start=None, **kw): + result = '' + if self.in_p: + result = self.paragraph(0) + return result + ['
    ', '
\n'][not on] + + def bullet_list(self, on, **kw): + result = '' + if self.in_p: + result = self.paragraph(0) + return result + ['
    ', '
\n'][not on] + + def listitem(self, on, **kw): + return ['
  • ', '
  • \n'][not on] + + def code(self, on, **kw): + return ['', ''][not on] + + def sup(self, on, **kw): + return ['', ''][not on] + + def sub(self, on, **kw): + return ['', ''][not on] + + def strike(self, on, **kw): + return ['', ''][not on] + + def preformatted(self, on, **kw): + FormatterBase.preformatted(self, on) + result = '' + if self.in_p: + result = self.paragraph(0) + return result + ['', ''][not on] + + def paragraph(self, on, **kw): + FormatterBase.paragraph(self, on) + return ['', '\n\n'][not on] + + def linebreak(self, preformatted=1): + return ['\n', '\\\n'][not preformatted] + + def heading(self, on, depth, **kw): + if on: + self._text = [] + return '\n\n' + else: + result = u'\n%s\n\n' % (u'=' * len("".join(self._text))) + self._text = None + return result + + def table(self, on, attrs={}, **kw): + return ['', '
    '][not on] + + def table_row(self, on, attrs={}, **kw): + return ['', ''][not on] + + def table_cell(self, on, attrs={}, **kw): + return ['', ''][not on] + + def anchordef(self, id): + return '' % id + + def anchorlink(self, on, name='', **kw): + id = kw.get('id',None) + extra = '' + if id: + extra = ' id="%s"' % id + return ('' % (name, extra) ,'') [not on] + + def underline(self, on, **kw): + return self.strong(on) # no underline in StyleBook + + def definition_list(self, on, **kw): + result = '' + if self.in_p: + result = self.paragraph(0) + return result + ['', ''][not on] + + def definition_term(self, on, compact=0, **kw): + return [''][not on] + + def definition_desc(self, on, **kw): + return ['', ''][not on] + + def image(self, src=None, **kw): + valid_attrs = ['src', 'width', 'height', 'alt', 'title'] + attrs = {'src': src} + for key, value in kw.items(): + if key in valid_attrs: + attrs[key] = value + return apply(FormatterBase.image, (self,), attrs) + '' + + def code_area(self, on, code_id, code_type='code', show=0, start=-1, step=-1): + return ('' % code_id, '', '' % tok_type, '', '' % tok_type, '