Created
November 17, 2022 14:25
-
-
Save samg7b5/2ebfc9e18cab74f5000919b9b76ae604 to your computer and use it in GitHub Desktop.
Convert WordPress to Dev.to markup
This file contains 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
import re | |
INFILE = 'from_wp.txt' | |
OUTFILE = 'for_devto.txt' | |
patterns = [ | |
(r"<!--[a-zA-Z0-9:\/\s\-\"\{\}\,]*-->",""), | |
(r"<li>","- "), | |
(r"</li>",""), | |
(r"<strong>","**"), | |
(r"</strong>","**"), | |
(r"<em>","_"), | |
(r"</em>","_"), | |
(r"<ul>","<u>"), | |
(r"</ul>","</u>"), | |
(r"<a href=\"([A-Za-z0-9\:\/\.\-\?\=\#\_]*)\">([A-Za-z0-9\s\'\:\.\-\(\)\/\?\=\#\_]*)</a>","[\\2](\\1)"), | |
(r"<code>","`"), | |
(r"</code>","`"), | |
(r"<p>",""), | |
(r"</p>",""), | |
(r"<h1>","##"), | |
(r"</h1>",""), | |
(r"<h2>","###"), | |
(r"</h2>",""), | |
(r"<h3>","####"), | |
(r"</h3>",""), | |
(r"</figure>",""), | |
] | |
with open(INFILE,encoding='utf-8') as f: | |
text = f.read().replace("\n\n","\n") | |
for patt, repl in patterns: | |
text = re.sub(pattern=patt, repl=repl, string=text) | |
with open(OUTFILE, 'w', encoding='utf-8') as f: | |
f.write(text.replace("\n\n","\n")) | |
print(f"Results are in {OUTFILE}. Remember to upload images manually to Dev.to (if you don't want WordPress bandwidth to be used), but retain <figcaption>...</figcaption> blocks") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment