Skip to content

Instantly share code, notes, and snippets.

@myuanz
Created May 27, 2023 13:01
Show Gist options
  • Save myuanz/503283cbb13ac23aaefe2cbbd5b94a2e to your computer and use it in GitHub Desktop.
Save myuanz/503283cbb13ac23aaefe2cbbd5b94a2e to your computer and use it in GitHub Desktop.
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('&quot;', '"')
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