Created
May 27, 2023 13:01
-
-
Save myuanz/503283cbb13ac23aaefe2cbbd5b94a2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import re | |
import html | |
posts = list(Path('_posts').glob('*.md')) | |
for post_p in posts: | |
content = post_p.read_text(encoding='utf-8') | |
content = content.replace('<pre><code>', '<pre><code class="language-text">') | |
content = content.replace('"', '"') | |
res = re.findall(r'<pre><code class="language-(.*?)">(.*?)</code></pre>', content, re.DOTALL) | |
for lang_type, text in res: | |
target_text = html.unescape(text) | |
target_text = f'\n\n<!--程序替换头 <pre><code>-->\n```{lang_type}\n{target_text}\n```\n<!--程序替换尾 <pre><code>-->\n' | |
content = content.replace(f'<pre><code class="language-{lang_type}">{text}</code></pre>', target_text) | |
res = re.findall(r'<blockquote>(.+?)</blockquote>', content, re.DOTALL) | |
for text in res: | |
target_text = html.unescape(text) | |
target_text = f'\n\n<!--程序替换头 quote-->\n\n> {target_text}\n\n<!--程序替换尾 quote-->\n\n' | |
content = content.replace(f'<blockquote>{text}</blockquote>', target_text) | |
post_p.write_text(content, encoding='utf-8') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment