Skip to content

Instantly share code, notes, and snippets.

@pillyshi
Last active June 4, 2019 04:17
Show Gist options
  • Save pillyshi/8a8e60e0c7624e67b0cf3e8b3a94f6cb to your computer and use it in GitHub Desktop.
Save pillyshi/8a8e60e0c7624e67b0cf3e8b3a94f6cb to your computer and use it in GitHub Desktop.
Convert markdown to the text for wordpress web editor.
import argparse
import re
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--in-path', type=str)
return parser.parse_args()
def convert(text):
text = re.sub(r'\\', r'\\\\', text)
text = re.sub(r'_', r'\\_', text)
text = re.sub(r'\$(.+?)\$', r'[latex]\1[/latex]', text)
text = re.sub(r'\$\$([\s\S]+?)\$\$', r'[latex display="true"]\1[/latex]', text)
text = text.replace('\n', '')
return text
def main(args):
with open(args.in_path) as f:
text = f.read()
text = '\n\n'.join(convert(t) for t in text.split('\n\n'))
print(text)
if __name__ == "__main__":
args = get_args()
main(args)
@pillyshi
Copy link
Author

pillyshi commented Jun 4, 2019

Usage

python markdown_to_wordpress.py --in-path /path/to/markdown | pbcopy

then, paste to markdown web editor.

@pillyshi
Copy link
Author

pillyshi commented Jun 4, 2019

This script supports wp-katex plugin. Equations written in latex will be converted as follows:

  • $ ~ $ => [latex] ~ [/latex]
  • $$ ~ $$ => [latex display="true"] ~ [/latex]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment