Skip to content

Instantly share code, notes, and snippets.

@jalperin
Last active August 29, 2015 14:00
Show Gist options
  • Save jalperin/5dfe4f5bb0db092cf762 to your computer and use it in GitHub Desktop.
Save jalperin/5dfe4f5bb0db092cf762 to your computer and use it in GitHub Desktop.
Convert MultiMarkdown (MMD) citations to Pandoc citations
import re
import fileinput
def change_references(s):
# inline citations are always single author, take them out of []
s = re.sub(r'\[#([^\]]+);\]', '@\\1', s)
n = 1
# replace all the "," in [#xxx,xxx,xxx,xxx] with ,@
while ( n > 0 ):
(s, n) = re.subn(r'(\[#[^\]]+),([^\]]+\])', '\\1;@\\2', s)
# replace [# with [@
s = re.sub(r'\[#', '[@', s)
# move suffix from [p. xx][#format] to [#format, p. xx]
# but remember we already replaced # with @
s = re.sub(r'\[([^#\]]+)\](\[@([^\]]+))\]', '\\2, \\1]', s)
return s
content = ""
for line in fileinput.input():
content += line
print change_references(content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment