annotate sat/plugins/plugin_syntax_wiki_dotclear.py @ 3981:acc9dfc8ba8d

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