Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_syntax_wiki_dotclear.py @ 4226:3f7ca590a5da
cli (pubsub): use `PEP` when service is not specified in error message.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 05 Mar 2024 17:31:56 +0100 |
parents | 4b842c1fb686 |
children | 0d7bb4df2343 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
1806 | 3 |
4 # SàT plugin for Dotclear Wiki Syntax | |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
1806 | 6 |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 # XXX: ref used: http://dotclear.org/documentation/2.0/usage/syntaxes#wiki-syntax-and-xhtml-equivalent | |
21 | |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
22 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
23 from libervia.backend.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
24 |
1806 | 25 log = getLogger(__name__) |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
26 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
27 from libervia.backend.core import exceptions |
1806 | 28 from twisted.words.xish import domish |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
29 from libervia.backend.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 | 31 import re |
32 | |
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 | 41 } |
42 | |
3028 | 43 NOTE_TPL = "[{}]" # Note template |
44 NOTE_A_REV_TPL = "rev_note_{}" | |
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 | 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 | 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 | 88 |
89 | |
90 class DCWikiParser(object): | |
91 def __init__(self): | |
92 self._footnotes = None | |
3028 | 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 | 101 |
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 | 104 self._parse(string, p_elt) |
105 | |
106 def parser_html(self, string, parent): | |
107 wrapped_html = "<div>{}</div>".format(string) | |
108 try: | |
109 div_elt = xml_tools.ElementParser()(wrapped_html) | |
110 except domish.ParserError as e: | |
3028 | 111 log.warning("Error while parsing HTML content, ignoring it: {}".format(e)) |
1806 | 112 return |
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 | 115 div_elt = children[0] |
116 parent.addChild(div_elt) | |
117 | |
118 def parser_escape_char(self, string, parent): | |
119 parent.addContent(string) | |
120 | |
121 def _parser_title(self, string, parent, name): | |
122 elt = parent.addElement(name) | |
123 elt.addContent(string) | |
124 | |
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 | 127 |
128 def _parser_list(self, string, parent, list_type): | |
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 | 132 |
133 string = string[depth:].lstrip() | |
134 | |
3028 | 135 for i in range(depth + 1): |
1806 | 136 list_elt = getattr(parent, list_type) |
137 if not list_elt: | |
138 parent = parent.addElement(list_type) | |
139 else: | |
140 parent = list_elt | |
141 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
142 li_elt = parent.addElement("li") |
1806 | 143 self._parse(string, li_elt) |
144 | |
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 | 147 |
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 | 150 |
151 def parser_preformated(self, string, parent): | |
152 pre_elt = parent.pre | |
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 | 155 else: |
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 | 158 pre_elt.addContent(string) |
159 | |
160 def parser_quote(self, string, parent): | |
161 blockquote_elt = parent.blockquote | |
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 | 164 p_elt = blockquote_elt.p |
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 | 167 else: |
3028 | 168 string = "\n" + string |
1806 | 169 |
170 self._parse(string, p_elt) | |
171 | |
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 | 174 self._parse(string, em_elt) |
175 | |
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 | 178 self._parse(string, strong_elt) |
179 | |
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 | 182 |
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 | 185 self._parse(string, ins_elt) |
186 | |
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 | 189 self._parse(string, del_elt) |
190 | |
191 def parser_link(self, string, parent): | |
3028 | 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 | 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 | 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 | 198 a_elt.addContent(url) |
199 else: | |
200 name = url_data[0] | |
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 | 203 a_elt.addContent(name) |
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 | 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 | 208 if length > 4: |
3028 | 209 log.warning("too much data for url, ignoring extra data") |
1806 | 210 |
211 def parser_image(self, string, parent): | |
3028 | 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 | 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 | 216 try: |
217 data = image_data[idx] | |
218 except IndexError: | |
219 break | |
220 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
221 if attribute != "position": |
1806 | 222 img_elt[attribute] = data |
223 else: | |
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 | 233 else: |
3028 | 234 log.warning("bad position argument for image, ignoring it") |
1806 | 235 |
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 | 239 |
240 def parser_acronym(self, string, parent): | |
3028 | 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 | 244 |
245 def parser_inline_quote(self, string, parent): | |
3028 | 246 quote_data = string.split("|") |
1806 | 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 | 250 try: |
251 data = quote_data[idx] | |
252 except IndexError: | |
253 break | |
254 q_elt[attribute] = data | |
255 | |
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 | 258 |
259 def parser_footnote(self, string, parent): | |
260 idx = len(self._footnotes) + 1 | |
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 | 266 a_elt["href"] = "#{}".format(NOTE_A_TPL.format(idx)) |
1806 | 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 | 271 a_elt["href"] = "#{}".format(NOTE_A_REV_TPL.format(idx)) |
1806 | 272 self._parse(string, p_elt) |
273 # footnotes are actually added at the end of the parsing | |
274 self._footnotes.append(p_elt) | |
275 | |
276 def parser_text(self, string, parent): | |
277 parent.addContent(string) | |
278 | |
279 def _parse(self, string, parent, block_level=False): | |
280 regex = wiki_block_level_re if block_level else wiki_re | |
281 | |
282 for match in regex.finditer(string): | |
283 if match.lastgroup is None: | |
284 parent.addContent(string) | |
285 return | |
286 matched = match.group(match.lastgroup) | |
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 | 289 except AttributeError: |
3028 | 290 log.warning("No parser found for {}".format(match.lastgroup)) |
1806 | 291 # parent.addContent(string) |
292 continue | |
293 parser(matched, parent) | |
294 | |
295 def parse(self, string): | |
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 | 298 self._parse(string, parent=div_elt, block_level=True) |
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 | 302 # we add a simple horizontal rule which can be customized |
303 # with footnotes class, instead of a text which would need | |
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 | 306 for elt in self._footnotes: |
307 foot_div_elt.addChild(elt) | |
308 return div_elt | |
309 | |
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 | 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), |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
320 lambda elt, buf, level=i: self.parser_heading(elt, buf, level), |
2624
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: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
334 self.parser_generic(elt, buf) |
1807
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 | 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 | 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 | 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 | 352 buf.append("[") |
353 buf.append("|".join(link_data)) | |
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 | 360 log.debug("Acronyme without title, using generic parser") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
361 self.parser_generic(elt, buf) |
1807
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 | 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 | 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 = [] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
373 self.parse_children(elt, tmp_buf) |
3028 | 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 | 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 | 381 buf.append("@@") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
382 self.parse_children(elt, buf) |
3028 | 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 | 386 buf.append("--") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
387 self.parse_children(elt, buf) |
3028 | 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": |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
392 self.parser_footnote(elt, buf) |
1807
0d3110341947
plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents:
1806
diff
changeset
|
393 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
394 self.parse_children(elt, buf, block=True) |
1807
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 | 397 buf.append("''") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
398 self.parse_children(elt, buf) |
3028 | 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 | 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 | 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 | 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 | 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 | 446 buf.append("((") |
447 buf.append("|".join(image_data)) | |
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 | 451 buf.append("++") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
452 self.parse_children(elt, buf) |
3028 | 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 | 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 | 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 | 476 buf.append(" ") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
477 self.parse_children(elt, buf) |
3028 | 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): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
481 self.parser_list(elt, buf, FLAG_OL) |
1807
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): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
484 self.parse_children(elt, buf) |
3028 | 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 | 488 pre = "".join( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
489 [ |
3028 | 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 | 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 | 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 | 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 | 511 buf.append("{{") |
512 buf.append("|".join(quote_data)) | |
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): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
516 self.parse_children(elt, buf, block=True) |
1807
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 | 519 buf.append("__") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
520 self.parse_children(elt, buf) |
3028 | 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 | 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") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
538 self.parser_generic(elt, buf) |
1807
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 | 541 buf.append("$$") |
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 | 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: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
546 self.parser_generic(elt, buf) |
1807
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): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
549 self.parser_list(elt, buf, FLAG_UL) |
1807
0d3110341947
plugin syntax dc_wiki: added XHTML => dc_wiki converter, plus some bug fixes:
Goffi <goffi@goffi.org>
parents:
1806
diff
changeset
|
550 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
551 def parser_list(self, elt, buf, type_): |
1807
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_) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
553 self.parse_children(elt, buf, block=True) |
1807
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 | 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 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
564 def parser_heading(self, elt, buf, level): |
3028 | 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 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
568 self.parser_text(child, buf) |
3028 | 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 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
571 def parser_footnote(self, elt, buf): |
1807
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 | 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 | 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 = [] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
590 self.parse_children(dummy_elt, 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
|
591 # now we can replace the placeholder |
3028 | 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 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
594 def parser_text(self, txt, buf, keep_whitespaces=False): |
3028 | 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 | 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 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
604 def parser_generic(self, elt, buf): |
1807
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 | 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 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
609 def parse_children(self, elt, buf, block=False): |
1807
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 | 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: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
619 appended = self.parser_text(child, buf) |
1807
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 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
629 parser = self.parser_generic |
1807
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 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
637 parser = self.parser_generic |
1807
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 | 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 | 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 | 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 | 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 | 662 |
663 def __init__(self, host): | |
3028 | 664 log.info(_("Dotclear wiki syntax plugin initialization")) |
1806 | 665 self.host = host |
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"] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
669 self._stx.add_syntax( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
670 self.SYNTAX_NAME, self.parse_wiki, self.parse_xhtml, [self._stx.OPT_NO_THREAD] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
671 ) |
1806 | 672 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
673 def parse_wiki(self, wiki_stx): |
1806 | 674 div_elt = self._dc_parser.parse(wiki_stx) |
675 return div_elt.toXml() | |
676 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
677 def parse_xhtml(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) |