From 238f7a3657541a636eb6a4d2ddfc685a82f29d65 Mon Sep 17 00:00:00 2001 From: dousha Date: Mon, 6 Jan 2025 14:15:49 +0800 Subject: [PATCH] fix math formulas --- .gitignore | 1 + main.py | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index b643e99..828ee55 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /venv/ /output/ *.xml +.DS_Store diff --git a/main.py b/main.py index 8a6676e..9b6afae 100644 --- a/main.py +++ b/main.py @@ -54,6 +54,20 @@ def find_all_matches(pattern, string, group=0): out.append(m[group]) return out +def fix_math_content(content): + return content.replace(r'\_', '_').replace(r'\*', '*') + + +def fix_math(content): + math_regex = re.compile(r'(\\[\[\(])(.+?)(\\[\)\]])') + pos = 0 + out = '' + while m := math_regex.search(content, pos): + out = out + content[pos:m.start()] + out = out + m.group(1) + fix_math_content(m.group(2)) + m.group(3) + pos = m.end() + return out + content[pos:] + mkdirp('output') @@ -107,8 +121,7 @@ for item in items: lines[i + 1] = nextline content = '\n'.join(lines) - content = md(content, code_language_callback=code_parser, - escape_misc=True).strip() + '\n' + content = md(content, code_language_callback=code_parser).strip() + '\n' ref_regex = re.compile(r'\[ref](.+?)\[/ref]') refs = [] stuff = find_all_matches(ref_regex, content, 1) @@ -136,6 +149,8 @@ for item in items: content = content + '\n' + '\n\n'.join(ref_output) + '\n' + content = fix_math(content) + frontmatter = f"---\nlayout: {post_type}\nid: {post_id}\ntitle: \"{title}\"\ncreator: \"{creator}\"\ndate: {date}\ncategories:\n{categories}\ntags:\n{tags}\ndraft: {'true' if status == 'draft' else 'false'}\npublished: {'true' if status == 'publish' else 'false'}\n---\n\n" title_slug = slugify(title)