Skip to content

Instantly share code, notes, and snippets.

@dexafree
Created August 25, 2015 16:24
Show Gist options
  • Save dexafree/cdb8727cb389e47936fe to your computer and use it in GitHub Desktop.
Save dexafree/cdb8727cb389e47936fe to your computer and use it in GitHub Desktop.
Python script that allows me to use a custom org2md post-processor after pandoc's conversion
import sys
import re
lines = []
def fix_first_line(line):
is_jekyll_regex = re.compile("^---", re.IGNORECASE)
is_jekyll = re.search(is_jekyll_regex, line)
if is_jekyll:
lines.append('---\n')
lines.append('layout: post\n')
regex = re.compile("title: \"(.*)\"", re.IGNORECASE)
result = re.search(regex, line)
if result:
title = result.group(1)
lines.append('title: "' + title + '"\n')
regex_date = re.compile("date: ((\d+\-){2}\d+ (\d+:){2}\d+)", re.IGNORECASE)
date_result = re.search(regex_date, line)
if date_result:
date = date_result.group(1)
lines.append('date: ' + date + '\n')
regex_category = re.compile('category: (.*) ---', re.IGNORECASE)
category_result = re.search(regex_category, line)
if category_result:
category = category_result.group(1)
lines.append('category: ' + category + '\n---\n')
else:
lines.append(line)
with open(sys.argv[1]) as f:
content = f.readlines()
regex = re.compile("^``` ([\w].*)", re.IGNORECASE)
i = 0
for line in content:
if i > 0:
result = re.search(regex, line)
if result:
language = result.group(1)
starting = "```" + language + '\n'
lines.append(starting)
else:
replaced = re.sub(r"\\`(\w)", r'`\1', line)
replaced = re.sub(r"\\` ", r'` ', replaced)
lines.append(replaced)
else:
fix_first_line(line)
i += 1
f = open(sys.argv[1], 'w')
for line in lines:
f.write("%s" % line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment