Skip to content

Instantly share code, notes, and snippets.

@samg7b5
Created November 17, 2022 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samg7b5/2ebfc9e18cab74f5000919b9b76ae604 to your computer and use it in GitHub Desktop.
Save samg7b5/2ebfc9e18cab74f5000919b9b76ae604 to your computer and use it in GitHub Desktop.
Convert WordPress to Dev.to markup
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