simplified spip->md mapping

This commit is contained in:
Guilhem Fauré 2023-05-11 10:22:50 +02:00
parent 723a7ddeea
commit 3b36aeb776

View File

@ -1,101 +1,106 @@
import re import re
mappings = { mappings = (
"horizontal-rule": ( ( # horizontal-rule
re.compile(r"- ?- ?- ?- ?[\- ]*|<hr ?.*?>", re.S | re.I), re.compile(r"- ?- ?- ?- ?[\- ]*|<hr ?.*?>", re.S | re.I),
r"---", # r"---",
r"***",
), ),
"line-break": ( ( # line-break
re.compile(r"\r?\n_ *(?=\r?\n)|<br ?.*?>", re.S | re.I), re.compile(r"\r?\n_ *(?=\r?\n)|<br ?.*?>", re.S | re.I),
"\n", "\n",
), ),
"heading": ( ( # heading
re.compile(r"\{\{\{ *(.*?) *\}\}\}", re.S | re.I), re.compile(r"\{\{\{ *(.*?) *\}\}\}", re.S | re.I),
r"## \1", r"## \1",
), ),
"strong": ( ( # strong
re.compile(r"\{\{ *(.*?) *\}\}", re.S | re.I), re.compile(r"\{\{ *(.*?) *\}\}", re.S | re.I),
r"**\1**", r"**\1**",
), ),
"emphasis": ( ( # emphasis
re.compile(r"\{ *(.*?) *\}", re.S | re.I), re.compile(r"\{ *(.*?) *\}", re.S | re.I),
r"*\1*", r"*\1*",
), ),
"strikethrough": ( ( # strikethrough
re.compile( re.compile(
r"<del>\s*(.*?)\s*(?:(\r?\n){2,}|<\/del>)", r"<del>\s*(.*?)\s*(?:(\r?\n){2,}|<\/del>)",
re.S | re.I, re.S | re.I,
), ),
r"~\1~", r"~\1~",
), ),
"anchor": ( ( # anchor
re.compile(r"\[ *(.*?) *-> *(.*?) *\]", re.S | re.I), re.compile(r"\[ *(.*?) *-> *(.*?) *\]", re.S | re.I),
r"[\1](\2)", r"[\1](\2)",
), ),
"image": ( ( # image
re.compile(r"<(?:img|image)(.*?)(\|.*?)*>", re.S | re.I), re.compile(r"<(?:img|image)(.*?)(\|.*?)*>", re.S | re.I),
r"![image](\1)", r"![image](\1)",
), ),
"document-anchors": ( ( # document-anchors
re.compile(r"<(?:doc|emb)(.*?)(\|.*?)*>", re.S | re.I), re.compile(r"<(?:doc|emb)(.*?)(\|.*?)*>", re.S | re.I),
r"[document](\1)", r"[document](\1)",
), ),
"wikilink": ( ( # wikilink
re.compile(r"\[\? *(.*?) *\]", re.S | re.I), re.compile(r"\[\? *(.*?) *\]", re.S | re.I),
r"[\1](https://wikipedia.org/wiki/\1)", r"[\1](https://wikipedia.org/wiki/\1)",
), ),
"footnote": ( ( # footnote
re.compile(r"\[\[ *(.*?) *\]\]", re.S | re.I), re.compile(r"\[\[ *(.*?) *\]\]", re.S | re.I),
r"", r"",
), ),
"unordered-list": ( ( # unordered list
re.compile(r"(\r?\n)-(?!#|-)\*? *", re.S | re.I), re.compile(r"(\r?\n)-(?!#|-)\*? *", re.S | re.I),
r"\1- ", r"\1- ",
), ),
"wrong-unordered-list": ( ( # wrong unordered list
re.compile(r"(\r?\n)\* +", re.S | re.I), re.compile(r"(\r?\n)\* +", re.S | re.I),
r"\1- ", r"\1- ",
), ),
"ordered-list": ( ( # wrong unordered list WARNING suppresses preceding tag
re.compile(r"(\r?\n)<.*?>\* +", re.I),
r"\1- ",
),
( # ordered-list
re.compile(r"(\r?\n)-# *", re.S | re.I), re.compile(r"(\r?\n)-# *", re.S | re.I),
r"\g<1>1. ", r"\g<1>1. ",
), ),
"table-metadata": ( ( # table-metadata
re.compile(r"(\r?\n)\|\|(.*?)\|(.*?)\|\|", re.S | re.I), re.compile(r"(\r?\n)\|\|(.*?)\|(.*?)\|\|", re.S | re.I),
r"", r"",
), ),
"quote": ( ( # quote
re.compile( re.compile(
r"<(?:quote|poesie)>\s*(.*?)\s*(?:(\r?\n){2,}|<\/(?:quote|poesie)>)", r"<(?:quote|poesie)>\s*(.*?)\s*(?:(\r?\n){2,}|<\/(?:quote|poesie)>)",
re.S | re.I, re.S | re.I,
), ),
r"> \1\2\2", r"> \1\2\2",
), ),
"box": ( ( # box
re.compile( re.compile(
r"<code>\s*(.*?)\s*(?:(?:\r?\n){2,}|<\/code>)", r"<code>\s*(.*?)\s*(?:(?:\r?\n){2,}|<\/code>)",
re.S | re.I, re.S | re.I,
), ),
"`\\1`", "`\\1`",
), ),
"fence": ( ( # fence
re.compile( re.compile(
r"<cadre>\s*(.*?)\s*(?:(?:\r?\n){2,}|<\/cadre>)", r"<cadre>\s*(.*?)\s*(?:(?:\r?\n){2,}|<\/cadre>)",
re.S | re.I, re.S | re.I,
), ),
"```\n\\1\n\n```", "```\n\\1\n\n```",
), ),
"multi-language": ( # Keep only the first language ( # Keep only the first language in multi-language blocks
re.compile( re.compile(
r"<multi>\s*\[.{2,4}\]\s*(.*?)\s*(?:\s*\[.{2,4}\].*)*<\/multi>", r"<multi>\s*\[.{2,4}\]\s*(.*?)\s*(?:\s*\[.{2,4}\].*)*<\/multi>",
re.S | re.I, re.S | re.I,
), ),
r"\1", r"\1",
), ),
} )
def convert(markup): def convert(markup):
for spip, markdown in mappings.values(): for spip, markdown in mappings:
markup = spip.sub(markdown, markup) markup = spip.sub(markdown, markup)
return markup.encode("utf-8").decode("utf-8") return markup.encode("utf-8").decode("utf-8")