annotate src/plugins/plugin_syntax_wiki_dotclear.py @ 2559:1362cabad225

plugin merge-request mercurial: better name sanitization for import
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 08:45:08 +0200
parents 0046283a285d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1808
diff changeset
1 #!/usr/bin/env python2
1806
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SàT plugin for Dotclear Wiki Syntax
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
1806
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 # XXX: ref used: http://dotclear.org/documentation/2.0/usage/syntaxes#wiki-syntax-and-xhtml-equivalent
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.i18n import _
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.log import getLogger
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 log = getLogger(__name__)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core.constants import Const as C
1807
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
26 from sat.core import exceptions
1806
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.xish import domish
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.tools import xml_tools
1807
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
29 import copy
1806
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import re
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
33 C.PI_NAME: "Dotclear Wiki Syntax Plugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
34 C.PI_IMPORT_NAME: "SYNT_DC_WIKI",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
35 C.PI_TYPE: C.PLUG_TYPE_SYNTAXE,
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
36 C.PI_DEPENDENCIES: ["TEXT-SYNTAXES"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
37 C.PI_MAIN: "DCWikiSyntax",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
38 C.PI_HANDLER: "",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
39 C.PI_DESCRIPTION: _("""Implementation of Dotclear wiki syntax""")
1806
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 }
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 NOTE_TPL = u'[{}]' # Note template
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 NOTE_A_REV_TPL = u'rev_note_{}'
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 NOTE_A_TPL = u'note_{}'
1807
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
45 ESCAPE_CHARS_BASE = r"(?P<escape_char>[][{}%|\\/*#@{{}}~$-])"
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
46 ESCAPE_CHARS_EXTRA = r"!?_+'()" # These chars are not escaped in XHTML => dc_wiki conversion,
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
47 # but are used in the other direction
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
48 ESCAPE_CHARS = ESCAPE_CHARS_BASE.format('')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
49 FLAG_UL = 'ul' # must be the name of the element
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
50 FLAG_OL = 'ol'
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
51 ELT_WITH_STYLE = ('img', 'div') # elements where a style attribute is expected
1806
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
1807
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
53 wiki = [r'\\' + ESCAPE_CHARS_BASE.format(ESCAPE_CHARS_EXTRA),
1806
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 r"^!!!!!(?P<h1_title>.+?)$",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 r"^!!!!(?P<h2_title>.+?)$",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 r"^!!!(?P<h3_title>.+?)$",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 r"^!!(?P<h4_title>.+?)$",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 r"^!(?P<h5_title>.+?)$",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 r"^----$(?P<horizontal_rule>)",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 r"^\*(?P<list_bullet>.*?)$",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 r"^#(?P<list_ordered>.*?)$",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 r"^ (?P<preformated>.*?)$",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 r"^> +?(?P<quote>.*?)$",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 r"''(?P<emphasis>.+?)''",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 r"__(?P<strong_emphasis>.+?)__",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 r"%%%(?P<line_break>)",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 r"\+\+(?P<insertion>.+?)\+\+",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 r"--(?P<deletion>.+?)--",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 r"\[(?P<link>.+?)\]",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 r"\(\((?P<image>.+?)\)\)",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 r"~(?P<anchor>.+?)~",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 r"\?\?(?P<acronym>.+?\|.+?)\?\?",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 r"{{(?P<inline_quote>.+?)}}",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 r"@@(?P<code>.+?)@@",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 r"\$\$(?P<footnote>.+?)\$\$",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 r"(?P<text>.+?)",
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 ]
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 wiki_re = re.compile('|'.join(wiki), re.MULTILINE | re.DOTALL)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 wiki_block_level_re = re.compile(r"^///html(?P<html>.+?)///\n\n|(?P<paragraph>.+?)(?:\n{2,}|\Z)", re.MULTILINE | re.DOTALL)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 class DCWikiParser(object):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 def __init__(self):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self._footnotes = None
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 for i in xrange(5):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 setattr(self,
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 'parser_h{}_title'.format(i),
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 lambda string, parent, i=i: self._parser_title(string, parent, 'h{}'.format(i)))
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 def parser_paragraph(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 p_elt = parent.addElement('p')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 self._parse(string, p_elt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def parser_html(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 wrapped_html = "<div>{}</div>".format(string)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 try:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 div_elt = xml_tools.ElementParser()(wrapped_html)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 except domish.ParserError as e:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 log.warning(u"Error while parsing HTML content, ignoring it: {}".format(e))
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 return
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 children = list(div_elt.elements())
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 if len(children) == 1 and children[0].name == 'div':
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 div_elt = children[0]
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 parent.addChild(div_elt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 def parser_escape_char(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 parent.addContent(string)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 def _parser_title(self, string, parent, name):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 elt = parent.addElement(name)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 elt.addContent(string)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 def parser_horizontal_rule(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 parent.addElement('hr')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 def _parser_list(self, string, parent, list_type):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 depth = 0
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 while string[depth:depth+1] == '*':
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 depth +=1
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 string = string[depth:].lstrip()
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 for i in xrange(depth+1):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 list_elt = getattr(parent, list_type)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 if not list_elt:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 parent = parent.addElement(list_type)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 else:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 parent = list_elt
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 li_elt = parent.addElement('li')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 self._parse(string, li_elt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 def parser_list_bullet(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 self._parser_list(string, parent, 'ul')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 def parser_list_ordered(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 self._parser_list(string, parent, 'ol')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 def parser_preformated(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 pre_elt = parent.pre
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 if pre_elt is None:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 pre_elt = parent.addElement('pre')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 else:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 # we are on a new line, and this is important for <pre/>
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 pre_elt.addContent('\n')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 pre_elt.addContent(string)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 def parser_quote(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 blockquote_elt = parent.blockquote
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 if blockquote_elt is None:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 blockquote_elt = parent.addElement('blockquote')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 p_elt = blockquote_elt.p
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 if p_elt is None:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 p_elt = blockquote_elt.addElement('p')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 else:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 string = u'\n' + string
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 self._parse(string, p_elt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 def parser_emphasis(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 em_elt = parent.addElement('em')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 self._parse(string, em_elt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 def parser_strong_emphasis(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 strong_elt = parent.addElement('strong')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 self._parse(string, strong_elt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 def parser_line_break(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 parent.addElement('br')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 def parser_insertion(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 ins_elt = parent.addElement('ins')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 self._parse(string, ins_elt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 def parser_deletion(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 del_elt = parent.addElement('del')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 self._parse(string, del_elt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 def parser_link(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 url_data = string.split(u'|')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 a_elt = parent.addElement('a')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 length = len(url_data)
1807
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
185 if length == 1:
1806
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 url = url_data[0]
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 a_elt['href'] = url
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 a_elt.addContent(url)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 else:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 name = url_data[0]
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 url = url_data[1]
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 a_elt['href'] = url
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 a_elt.addContent(name)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 if length >= 3:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 a_elt['lang'] = url_data[2]
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 if length >= 4:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 a_elt['title'] = url_data[3]
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 if length > 4:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 log.warning(u"too much data for url, ignoring extra data")
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
200
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 def parser_image(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 image_data = string.split(u'|')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 img_elt = parent.addElement('img')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
204
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 for idx, attribute in enumerate(('src', 'alt', 'position', 'longdesc')):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 try:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 data = image_data[idx]
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 except IndexError:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 break
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
210
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 if attribute != 'position':
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 img_elt[attribute] = data
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 else:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 data = data.lower()
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 if data in ('l', 'g'):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 img_elt['style'] = "display:block; float:left; margin:0 1em 1em 0"
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 elif data in ('r', 'd'):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 img_elt['style'] = "display:block; float:right; margin:0 0 1em 1em"
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 elif data == 'c':
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 img_elt['style'] = "display:block; margin-left:auto; margin-right:auto"
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 else:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 log.warning(u"bad position argument for image, ignoring it")
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
223
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 def parser_anchor(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 a_elt = parent.addElement('a')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 a_elt['id'] = string
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
227
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 def parser_acronym(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 acronym, title = string.split(u'|',1)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 acronym_elt = parent.addElement('acronym', content=acronym)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 acronym_elt['title'] = title
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
232
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 def parser_inline_quote(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 quote_data = string.split(u'|')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 quote = quote_data[0]
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 q_elt = parent.addElement('q', content=quote)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 for idx, attribute in enumerate(('lang', 'cite'), 1):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 try:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 data = quote_data[idx]
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 except IndexError:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 break
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 q_elt[attribute] = data
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
243
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 def parser_code(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 parent.addElement('code', content=string)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
246
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 def parser_footnote(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 idx = len(self._footnotes) + 1
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 note_txt = NOTE_TPL.format(idx)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 sup_elt = parent.addElement('sup')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 sup_elt['class'] = 'note'
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 a_elt = sup_elt.addElement('a', content=note_txt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 a_elt['id'] = NOTE_A_REV_TPL.format(idx)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 a_elt['href'] = u'#{}'.format(NOTE_A_TPL.format(idx))
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
255
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 p_elt = domish.Element((None, 'p'))
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 a_elt = p_elt.addElement('a', content=note_txt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 a_elt['id'] = NOTE_A_TPL.format(idx)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 a_elt['href'] = u'#{}'.format(NOTE_A_REV_TPL.format(idx))
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 self._parse(string, p_elt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 # footnotes are actually added at the end of the parsing
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 self._footnotes.append(p_elt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
263
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 def parser_text(self, string, parent):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 parent.addContent(string)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
266
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 def _parse(self, string, parent, block_level=False):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 regex = wiki_block_level_re if block_level else wiki_re
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
269
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 for match in regex.finditer(string):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 if match.lastgroup is None:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 parent.addContent(string)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 return
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 matched = match.group(match.lastgroup)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 try:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 parser = getattr(self, 'parser_{}'.format(match.lastgroup))
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 except AttributeError:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 log.warning(u"No parser found for {}".format(match.lastgroup))
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 # parent.addContent(string)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 continue
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 parser(matched, parent)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
282
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 def parse(self, string):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 self._footnotes = []
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 div_elt = domish.Element((None, 'div'))
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 self._parse(string, parent=div_elt, block_level=True)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 if self._footnotes:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 foot_div_elt = div_elt.addElement('div')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 foot_div_elt['class'] = 'footnotes'
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 # we add a simple horizontal rule which can be customized
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 # with footnotes class, instead of a text which would need
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 # to be translated
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 foot_div_elt.addElement('hr')
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 for elt in self._footnotes:
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 foot_div_elt.addChild(elt)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 return div_elt
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
297
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
298
1807
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
299 class XHTMLParser(object):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
300
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
301 def __init__(self):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
302 self.flags = None
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
303 self.toto = 0
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
304 self.footnotes = None # will hold a map from url to buffer id
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
305 for i in xrange(1,6):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
306 setattr(self,
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
307 'parser_h{}'.format(i),
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
308 lambda elt, buf, level=i: self.parserHeading(elt, buf, level)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
309 )
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
310
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
311 def parser_a(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
312 try:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
313 url = elt['href']
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
314 except KeyError:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
315 # probably an anchor
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
316 try:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
317 id_ = elt['id']
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
318 if not id_:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
319 # we don't want empty values
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
320 raise KeyError
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
321 except KeyError:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
322 self.parserGeneric(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
323 else:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
324 buf.append(u'~~{}~~'.format(id_))
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
325 return
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
326
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
327 link_data = [url]
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
328 name = unicode(elt)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
329 if name != url:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
330 link_data.insert(0, name)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
331
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
332 lang = elt.getAttribute('lang')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
333 title = elt.getAttribute('title')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
334 if lang is not None:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
335 link_data.append(lang)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
336 elif title is not None:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
337 link_data.appand(u'')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
338 if title is not None:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
339 link_data.append(title)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
340 buf.append(u'[')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
341 buf.append(u'|'.join(link_data))
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
342 buf.append(u']')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
343
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
344 def parser_acronym(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
345 try:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
346 title = elt['title']
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
347 except KeyError:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
348 log.debug(u"Acronyme without title, using generic parser")
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
349 self.parserGeneric(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
350 return
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
351 buf.append(u'??{}|{}??'.format(unicode(elt), title))
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
352
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
353 def parser_blockquote(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
354 # we remove wrapping <p> to avoid empty line with "> "
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
355 children = list([child for child in elt.children if unicode(child).strip() not in ('', '\n')])
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
356 if len(children) == 1 and children[0].name == 'p':
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
357 elt = children[0]
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
358 tmp_buf = []
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
359 self.parseChildren(elt, tmp_buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
360 blockquote = u'> ' + u'\n> '.join(u''.join(tmp_buf).split('\n'))
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
361 buf.append(blockquote)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
362
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
363 def parser_br(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
364 buf.append(u'%%%')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
365
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
366 def parser_code(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
367 buf.append(u'@@')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
368 self.parseChildren(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
369 buf.append(u'@@')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
370
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
371 def parser_del(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
372 buf.append(u'--')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
373 self.parseChildren(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
374 buf.append(u'--')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
375
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
376 def parser_div(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
377 if elt.getAttribute('class') == 'footnotes':
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
378 self.parserFootnote(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
379 else:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
380 self.parseChildren(elt, buf, block=True)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
381
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
382 def parser_em(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
383 buf.append(u"''")
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
384 self.parseChildren(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
385 buf.append(u"''")
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
386
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
387 def parser_h6(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
388 # XXX: <h6/> heading is not managed by wiki syntax
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
389 # so we handle it with a <h5/>
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
390 elt = copy.copy(elt) # we don't want to change to original element
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
391 elt.name = 'h5'
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
392 self._parse(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
393
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
394 def parser_hr(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
395 buf.append(u'\n----\n')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
396
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
397 def parser_img(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
398 try:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
399 url = elt['src']
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
400 except KeyError:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
401 log.warning(u"Ignoring <img/> without src")
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
402 return
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
403
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
404 image_data=[url]
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
405
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
406 alt = elt.getAttribute('alt')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
407 style = elt.getAttribute('style', '')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
408 desc = elt.getAttribute('longdesc')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
409
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
410 if '0 1em 1em 0' in style:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
411 position = 'L'
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
412 elif '0 0 1em 1em' in style:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
413 position = 'R'
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
414 elif 'auto' in style:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
415 position = 'C'
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
416 else:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
417 position = None
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
418
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
419 if alt:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
420 image_data.append(alt)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
421 elif position or desc:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
422 image_data.append(u'')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
423
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
424 if position:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
425 image_data.append(position)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
426 elif desc:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
427 image_data.append(u'')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
428
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
429 if desc:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
430 image_data.append(desc)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
431
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
432 buf.append(u'((')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
433 buf.append(u'|'.join(image_data))
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
434 buf.append(u'))')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
435
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
436 def parser_ins(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
437 buf.append(u'++')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
438 self.parseChildren(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
439 buf.append(u'++')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
440
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
441 def parser_li(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
442 flag = None
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
443 current_flag = None
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
444 bullets = []
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
445 for flag in reversed(self.flags):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
446 if flag in (FLAG_UL, FLAG_OL):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
447 if current_flag is None:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
448 current_flag = flag
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
449 if flag == current_flag:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
450 bullets.append(u'*' if flag == FLAG_UL else u'#')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
451 else:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
452 break
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
453
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
454 if flag != current_flag and buf[-1] == u' ':
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
455 # this trick is to avoid a space when we switch
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
456 # from (un)ordered to the other type on the same row
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
457 # e.g. *# unorder + ordered item
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
458 del buf[-1]
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
459
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
460 buf.extend(bullets)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
461
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
462 buf.append(u' ')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
463 self.parseChildren(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
464 buf.append(u'\n')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
465
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
466 def parser_ol(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
467 self.parserList(elt, buf, FLAG_OL)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
468
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
469 def parser_p(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
470 self.parseChildren(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
471 buf.append(u'\n\n')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
472
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
473 def parser_pre(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
474 pre = u''.join([child.toXml() if domish.IElement.providedBy(child) else unicode(child) for child in elt.children])
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
475 pre = u' ' + u'\n '.join(pre.split('\n'))
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
476 buf.append(pre)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
477
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
478 def parser_q(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
479 quote_data=[unicode(elt)]
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
480
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
481 lang = elt.getAttribute('lang')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
482 cite = elt.getAttribute('url')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
483
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
484 if lang:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
485 quote_data.append(lang)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
486 elif cite:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
487 quote_data.append(u'')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
488
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
489 if cite:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
490 quote_data.append(cite)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
491
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
492 buf.append(u'{{')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
493 buf.append(u'|'.join(quote_data))
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
494 buf.append(u'}}')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
495
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
496 def parser_span(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
497 self.parseChildren(elt, buf, block=True)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
498
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
499 def parser_strong(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
500 buf.append(u'__')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
501 self.parseChildren(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
502 buf.append(u'__')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
503
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
504 def parser_sup(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
505 # sup is mainly used for footnotes, so we check if we have an anchor inside
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
506 children = list([child for child in elt.children if unicode(child).strip() not in ('', '\n')])
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
507 if (len(children) == 1 and domish.IElement.providedBy(children[0])
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
508 and children[0].name == 'a' and '#' in children[0].getAttribute('href', '')):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
509 url = children[0]['href']
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
510 note_id = url[url.find('#')+1:]
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
511 if not note_id:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
512 log.warning("bad link found in footnote")
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
513 self.parserGeneric(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
514 return
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
515 # this looks like a footnote
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
516 buf.append(u'$$')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
517 buf.append(u' ') # placeholder
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
518 self.footnotes[note_id] = len(buf) - 1
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
519 buf.append(u'$$')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
520 else:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
521 self.parserGeneric(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
522
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
523 def parser_ul(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
524 self.parserList(elt, buf, FLAG_UL)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
525
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
526 def parserList(self, elt, buf, type_):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
527 self.flags.append(type_)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
528 self.parseChildren(elt, buf, block=True)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
529 idx = 0
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
530 for flag in reversed(self.flags):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
531 idx -= 1
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
532 if flag == type_:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
533 del self.flags[idx]
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
534 break
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
535
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
536 if idx == 0:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
537 raise exceptions.InternalError(u"flag has been removed by an other parser")
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
538
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
539 def parserHeading(self, elt, buf, level):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
540 buf.append((6-level) * u'!')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
541 for child in elt.children:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
542 # we ignore other elements for a Hx title
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
543 self.parserText(child, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
544 buf.append(u'\n')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
545
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
546 def parserFootnote(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
547 for elt in elt.elements():
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
548 # all children other than <p/> are ignored
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
549 if elt.name == 'p':
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
550 a_elt = elt.a
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
551 if a_elt is None:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
552 log.warning(u"<p/> element doesn't contain <a/> in footnote, ignoring it")
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
553 continue
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
554 try:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
555 note_idx = self.footnotes[a_elt['id']]
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
556 except KeyError:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
557 log.warning(u"Note id doesn't match any known note, ignoring it")
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
558 # we create a dummy element to parse all children after the <a/>
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
559 dummy_elt = domish.Element((None, 'note'))
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
560 a_idx = elt.children.index(a_elt)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
561 dummy_elt.children = elt.children[a_idx+1:]
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
562 note_buf = []
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
563 self.parseChildren(dummy_elt, note_buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
564 # now we can replace the placeholder
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
565 buf[note_idx] = u''.join(note_buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
566
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
567 def parserText(self, txt, buf, keep_whitespaces=False):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
568 txt = unicode(txt)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
569 if not keep_whitespaces:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
570 # we get text and only let one inter word space
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
571 txt = u' '.join(txt.split())
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
572 txt = re.sub(ESCAPE_CHARS, r'\\\1', txt)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
573 if txt:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
574 buf.append(txt)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
575 return txt
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
576
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
577 def parserGeneric(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
578 # as dotclear wiki syntax handle arbitrary XHTML code
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
579 # we use this feature to add elements that we don't know
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
580 buf.append(u"\n\n///html\n{}\n///\n\n".format(elt.toXml()))
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
581
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
582 def parseChildren(self, elt, buf, block=False):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
583 first_visible = True
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
584 for child in elt.children:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
585 if not block and not first_visible and buf and buf[-1][-1] not in (' ','\n'):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
586 # we add separation if it isn't already there
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
587 buf.append(u' ')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
588 if domish.IElement.providedBy(child):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
589 self._parse(child, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
590 first_visible = False
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
591 else:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
592 appended = self.parserText(child, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
593 if appended:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
594 first_visible = False
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
595
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
596 def _parse(self, elt, buf):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
597 elt_name = elt.name.lower()
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
598 style = elt.getAttribute('style')
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
599 if style and elt_name not in ELT_WITH_STYLE:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
600 # if we have style we use generic parser to put raw HTML
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
601 # to avoid losing it
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
602 parser = self.parserGeneric
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
603 else:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
604 try:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
605 parser = getattr(self, "parser_{}".format(elt_name))
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
606 except AttributeError:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
607 log.debug("Can't find parser for {} element, using generic one".format(elt.name))
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
608 parser = self.parserGeneric
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
609 parser(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
610
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
611 def parse(self, elt):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
612 self.flags = []
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
613 self.footnotes = {}
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
614 buf = []
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
615 self._parse(elt, buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
616 return u''.join(buf)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
617
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
618 def parseString(self, string):
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
619 wrapped_html = u"<div>{}</div>".format(string)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
620 try:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
621 div_elt = xml_tools.ElementParser()(wrapped_html)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
622 except domish.ParserError as e:
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
623 log.warning(u"Error while parsing HTML content: {}".format(e))
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
624 return
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
625 children = list(div_elt.elements())
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
626 if len(children) == 1 and children[0].name == 'div':
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
627 div_elt = children[0]
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
628 return self.parse(div_elt)
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
629
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
630
1806
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
631 class DCWikiSyntax(object):
1808
18561326a561 plugin syntax dotclear: renamed syntax to wiki_dotclear, and renamed plugin file to plugin_syntax_wiki_dotclear.py
Goffi <goffi@goffi.org>
parents: 1807
diff changeset
632 SYNTAX_NAME = "wiki_dotclear"
1806
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
633
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
634 def __init__(self, host):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
635 log.info(_(u"Dotclear wiki syntax plugin initialization"))
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
636 self.host = host
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
637 self._dc_parser = DCWikiParser()
1807
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
638 self._xhtml_parser = XHTMLParser()
1806
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
639 self._stx = self.host.plugins["TEXT-SYNTAXES"]
1808
18561326a561 plugin syntax dotclear: renamed syntax to wiki_dotclear, and renamed plugin file to plugin_syntax_wiki_dotclear.py
Goffi <goffi@goffi.org>
parents: 1807
diff changeset
640 self._stx.addSyntax(self.SYNTAX_NAME, self.parseWiki, self.parseXHTML, [self._stx.OPT_NO_THREAD])
1806
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
641
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
642 def parseWiki(self, wiki_stx):
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
643 div_elt = self._dc_parser.parse(wiki_stx)
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
644 return div_elt.toXml()
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
645
fd788d24277a plugin syntax dc_wiki: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
646 def parseXHTML(self, xhtml):
1807
0d3110341947 plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents: 1806
diff changeset
647 return self._xhtml_parser.parseString(xhtml)