moin2doku/text_dokuwiki.py

323 lines
9.1 KiB
Python
Raw Normal View History

2012-09-17 11:10:46 +02:00
# -*- coding: utf-8 -*-
"""
MoinMoin - Dokuwiki Formatter
2012-09-17 11:10:46 +02:00
@copyright: 2000, 2001, 2002 by Jürgen Hermann <jh@web.de>
@copyright: 2011-2012 Elan Ruusamäe <glen@delfi.ee>
@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
2012-09-16 23:51:11 +02:00
from types import *
# TODO: let base class MoinMoin/formatter/base.py handle not implemented methods
class Formatter(FormatterBase):
"""
Send Dokuwiki formatted data.
"""
hardspace = '&nbsp;'
# hardspace = ' '
def __init__(self, request, **kw):
apply(FormatterBase.__init__, (self, request), kw)
self._current_depth = 1
self._base_depth = 0
self.in_pre = 0
2012-09-16 16:31:15 +02:00
self.in_table = 0
self._text = None # XXX does not work with links in headings!!!!!
2012-06-20 00:38:14 +02:00
self.list_depth = 0
2012-09-16 17:00:26 +02:00
self.list_type = ' '
2012-06-20 01:07:26 +02:00
# dokuwiki namespace spearator, ':' or '/', see 'useslash' config
self.ns_sep = '/';
2012-06-20 00:23:36 +02:00
def _escape(self, text, extra_mapping={"'": "&apos;", '"': "&quot;"}):
return saxutils.escape(text, extra_mapping)
def startDocument(self, pagename):
encoding = config.charset
return '<?xml version="1.0" encoding="%s"?>\n<s1 title="%s">' % (
encoding, self._escape(pagename))
def endDocument(self):
result = ""
while self._current_depth > 1:
result += "</s%d>" % self._current_depth
self._current_depth -= 1
return result + '</s1>'
def lang(self, on, lang_name):
return ('<div lang="">' % lang_name, '</div>')[not on]
def sysmsg(self, on, **kw):
return ('<sysmsg>', '</sysmsg>')[not on]
def rawHTML(self, markup):
return '<html>' + markup + '</html>'
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:
2012-06-20 01:07:26 +02:00
return '[[%s%s%s|' % (interwiki, self.ns_sep, pagename)
else:
return ']]'
def url(self, on, url='', css=None, **kw):
2012-06-20 01:07:26 +02:00
return ('[[%s|' % (self._escape(url)), ']]') [not on]
def attachment_link(self, url, text, **kw):
return '{{%s|%s}}' % (url, text)
def attachment_image(self, url, **kw):
2012-09-17 00:44:39 +02:00
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):
2012-06-19 23:50:03 +02:00
# size not supported
2012-06-20 00:48:14 +02:00
if size >= 4:
return '----\n'
else:
return '-' * size + '\n'
def icon(self, type):
return '<icon type="%s" />' % 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):
2012-06-20 00:38:14 +02:00
# list type not supported
if on:
self.list_depth += 1
self.list_type = '-'
else:
self.list_depth -= 1
2012-09-16 17:00:26 +02:00
self.list_type = ' '
2012-06-20 00:38:14 +02:00
return ['', '\n'][on]
def bullet_list(self, on, **kw):
2012-06-20 00:23:36 +02:00
if on:
2012-06-20 00:38:14 +02:00
self.list_depth += 1
self.list_type = '*'
2012-06-20 00:23:36 +02:00
else:
2012-06-20 00:38:14 +02:00
self.list_depth -= 1
2012-09-16 17:00:26 +02:00
self.list_type = ' '
2012-06-20 00:23:36 +02:00
2012-06-20 00:38:14 +02:00
return ['', '\n'][on]
def listitem(self, on, **kw):
2012-06-20 01:18:09 +02:00
# somewhy blockquote uses "listitem" call
2012-06-20 00:38:14 +02:00
return [(' ' * self.list_depth * 2) + self.list_type + ' ', '\n'][not on]
def code(self, on, **kw):
2012-06-19 23:50:03 +02:00
""" `typewriter` or {{{typerwriter}}, for code blocks i hope codeblock works """
return ["''", "''"][not on]
def sup(self, on, **kw):
return ['<sup>', '</sup>'][not on]
def sub(self, on, **kw):
return ['<sub>', '</sub>'][not on]
def strike(self, on, **kw):
return ['<del>', '</del>'][not on]
def preformatted(self, on, **kw):
FormatterBase.preformatted(self, on)
result = ''
if self.in_p:
result = self.paragraph(0)
2012-06-20 00:23:36 +02:00
return result + ['<file>', '</file>\n'][not on]
def paragraph(self, on, **kw):
FormatterBase.paragraph(self, on)
2012-09-16 17:00:26 +02:00
if self.in_table or self.list_depth:
2012-09-16 16:31:15 +02:00
return ''
return ['', '\n\n'][not on]
def linebreak(self, preformatted=1):
return ['\n', '\\\n'][not preformatted]
def heading(self, on, depth, **kw):
2012-06-19 23:42:08 +02:00
# heading depth reversed in dokuwiki
2012-06-19 23:35:42 +02:00
heading_depth = 7 - depth
if on:
2012-06-19 23:35:42 +02:00
return u'%s ' % (u'=' * heading_depth)
else:
2012-09-16 19:44:11 +02:00
return u' %s\n' % (u'=' * heading_depth)
def table(self, on, attrs={}, **kw):
2012-09-16 16:31:15 +02:00
if on:
self.in_table = 1
else:
self.in_table = 0
2012-06-20 00:48:14 +02:00
return ''
def table_row(self, on, attrs={}, **kw):
return ['\n', '|'][not on]
def table_cell(self, on, attrs={}, **kw):
2012-09-16 16:31:15 +02:00
return ['|', ''][not on]
def anchordef(self, id):
2012-06-20 01:18:09 +02:00
# not supported
return ''
def anchorlink(self, on, name='', **kw):
2012-06-20 01:24:02 +02:00
# kw.id not supported, we hope the anchor matches existing heading on page
return ('[[#', ']]') [not on]
def underline(self, on, **kw):
2012-06-19 23:50:03 +02:00
return ['__', '__'][not on]
def definition_list(self, on, **kw):
result = ''
if self.in_p:
result = self.paragraph(0)
return result + ['<gloss>', '</gloss>'][not on]
def definition_term(self, on, compact=0, **kw):
return ['<label>', '</label>'][not on]
def definition_desc(self, on, **kw):
return ['<item>', '</item>'][not on]
def image(self, src=None, **kw):
valid_attrs = ['src', 'width', 'height', 'alt', 'title']
2012-06-20 01:10:32 +02:00
url = src
if '?' in url:
url += '&'
else:
url += '?'
attrs = {}
for key, value in kw.items():
if key in valid_attrs:
attrs[key] = value
2012-06-20 01:10:32 +02:00
# TODO: finish this
if attrs.has_key('width'):
url += attrs['width']
return '{{' + url + '}}'
def code_area(self, on, code_id, code_type='code', show=0, start=-1, step=-1):
2012-06-20 00:13:07 +02:00
syntax = ''
# switch for Python: http://simonwillison.net/2004/may/7/switch/
try:
syntax = {
'ColorizedPython': 'python',
'ColorizedCPlusPlus': 'cpp',
}[code_type]
except KeyError:
pass
return ('<code %s>' % syntax , '</code>')[not on]
def code_line(self, on):
return ('', '\n')[on]
def code_token(self, on, tok_type):
# not supported
return ''
2012-09-16 23:51:11 +02:00
2012-09-17 00:26:47 +02:00
def comment(self, text):
# real comments (lines with two hash marks)
if text[0:2] == '##':
return "/* %s */\n" % text[2:]
2012-09-17 00:26:47 +02:00
2012-09-18 00:11:14 +02:00
# Some kind of Processing Instruction
# http://moinmo.in/HelpOnProcessingInstructions
2012-09-17 00:26:47 +02:00
tokens = text.lstrip('#').split(None, 1)
2012-09-18 00:11:14 +02:00
if tokens[0] in ('language', 'format', 'refresh'):
2012-09-17 00:26:47 +02:00
return ''
if tokens[0] == 'acl':
# TODO: fill acl.auth.php
return ''
2012-09-18 00:11:14 +02:00
if tokens[0] == 'deprecated':
return '<note warning>This page is deprecated</note>\n'
if tokens[0] == 'redirect':
return text
2012-09-17 00:26:47 +02:00
if tokens[0] == 'pragma':
2012-09-18 00:11:14 +02:00
# TODO: can do 'description' via 'meta' dokuwiki plugin
2012-09-17 00:26:47 +02:00
return "/* pragma: %s */" % " ".join(tokens[1:])
return "/* %s */" % text.lstrip('#')
2012-09-16 23:51:11 +02:00
def macro(self, macro_obj, name, args):
def email(args):
mail = args.replace(' AT ', '@')
mail = mail.replace(' DOT ', '.')
2012-09-17 00:39:35 +02:00
return '[[%s|%s]]' % (mail, args)
2012-09-16 23:51:11 +02:00
2012-09-17 11:11:10 +02:00
def smileys(args):
return apply(FormatterBase.macro, (self, macro_obj, name, args))
2012-09-16 23:51:11 +02:00
try:
lookup = {
'BR' : '\\\\',
'MailTo' : email,
2012-09-17 00:34:39 +02:00
'GetText' : args,
2012-09-17 11:11:10 +02:00
'ShowSmileys' : smileys,
2012-09-16 23:51:11 +02:00
}[name]
except KeyError:
lookup = '/* UndefinedMacro: %s(%s) */' % (name, args)
if type(lookup) == FunctionType:
text = lookup(args)
else:
text = lookup
return text
2012-09-17 11:10:46 +02:00
def smiley(self, text):
try:
# https://www.dokuwiki.org/devel:smileys.conf
return {
# note: reverse sorted
'(./)' : u'',
':o' : ':-o',
':D' : ':-D',
'B)' : ':-D',
'B-)' : ':-D',
'|)' : '|-)',
':\\' : ':-\\',
':))' : ':-)',
':)' : ':-)',
':(' : ':-(',
':-))' : ':-)',
';)' : ';-)',
}[text]
except KeyError:
return text