fix math formulas

This commit is contained in:
dousha 2025-01-06 14:15:49 +08:00
parent 00ba71b3e3
commit 238f7a3657
2 changed files with 18 additions and 2 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/venv/
/output/
*.xml
.DS_Store

19
main.py
View File

@ -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)