try with basic regex replacing

This commit is contained in:
Guilhem Fauré 2023-05-09 16:52:40 +02:00
parent a455c8e4a2
commit 8a6026d129
2 changed files with 28 additions and 11 deletions

View File

@ -1,5 +1,6 @@
pyyaml pyyaml
python-slugify[unidecode] python-slugify[unidecode]
peewee peewee
pyparsing
lark # pyparsing
# lark

View File

@ -1,17 +1,29 @@
import re
from os import path from os import path
from lark import Lark # from lark import Lark
from pyparsing import Word, alphas # from pyparsing import Word, alphas
larkParser = Lark(open(path.dirname(__file__) + "/spip.lark")) # larkParser = Lark(open(path.dirname(__file__) + "/spip.lark"))
class content: class content:
_mappings = (
(re.compile(r"\{\{\{(.*?)\}\}\}", re.S | re.I), r"## \1"),
(re.compile(r"\{\{ \{(.*?)\} \}\}", re.S | re.I), r"***\1***"),
(re.compile(r"\{ \{\{(.*?)\}\} \}", re.S | re.I), r"***\1***"),
(re.compile(r"\{\{(.*?)\}\}", re.S | re.I), r"**\1**"),
(re.compile(r"\{(.*?)\}", re.S | re.I), r"*\1*"),
)
def __init__(self, content): def __init__(self, content):
self.spip = content self.spip = content
def get_markdown(self): def get_markdown(self):
markdown = self.spip markdown = self.spip
for spip, md in self._mappings:
markdown = spip.sub(md, markdown)
return markdown
# Parses the body & display parse tree # Parses the body & display parse tree
try: try:
print(f" parse tree :\n") print(f" parse tree :\n")
@ -24,14 +36,18 @@ class content:
# Parses a file & display its parse tree # Parses a file & display its parse tree
def test(filename): def test(filename):
raw = open(path.dirname(__file__) + "/" + filename).read() raw = open(path.dirname(__file__) + "/" + filename).read()
print(f"--- Parse tree of {filename} ---\n\n")
print(larkParser.parse(raw)) print(f"--- Conversion of {filename} ---\n\n")
c = content(raw)
print(c.get_markdown())
# print(f"--- Parse tree of {filename} ---\n\n")
# print(larkParser.parse(raw))
if __name__ == "__main__": if __name__ == "__main__":
# Test # Test
test("../test/0.spip") test("../test/0.spip")
test("../test/1.spip") # test("../test/1.spip")
test("../test/2.spip") # test("../test/2.spip")
test("../test/3.spip") # test("../test/3.spip")
test("../test/4.spip") # test("../test/4.spip")