Last active
August 29, 2015 14:00
-
-
Save jalperin/5dfe4f5bb0db092cf762 to your computer and use it in GitHub Desktop.
Convert MultiMarkdown (MMD) citations to Pandoc citations
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 | |
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