Skip to content

Instantly share code, notes, and snippets.

@wladston
Created August 24, 2016 22:34
Show Gist options
  • Save wladston/fff3ffadf54d89ba11cc6b64eef257c8 to your computer and use it in GitHub Desktop.
Save wladston/fff3ffadf54d89ba11cc6b64eef257c8 to your computer and use it in GitHub Desktop.
This converts <http://criticmarkup.com/> tags in a Markdown file to "equivalent" LaTex formatting.
#!/usr/bin/python
# This converts <http://criticmarkup.com/> tags in a Markdown file to
# "equivalent" LaTex formatting.
#
# By Wladston Filho <[email protected]>
import sys
import re
comm_pattern = re.compile('(?s)\{\>\>(?P<value>.*?)\<\<\}', re.DOTALL)
mark_pattern = re.compile('(?s)\{\=\=(?P<value>.*?)\=\=\}', re.DOTALL)
add_pattern = re.compile('(?s)\{\+\+(?P<value>.*?)\+\+[ \t]*'
'(\[(?P<meta>.*?)\])?[ \t]*\}', re.DOTALL)
del_pattern = re.compile('(?s)\{\-\-(?P<value>.*?)\-\-[ \t]*'
'(\[(?P<meta>.*?)\])?[ \t]*\}', re.DOTALL)
subs_pattern = re.compile('(?s)\{\~\~(?P<original>(?:[^\~\>]|(?:\~(?!\>)))+)'
'\~\>(?P<new>(?:[^\~\~]|(?:\~(?!\~\})))+)'
'\~\~\}', re.DOTALL)
hl_st = '\sethlcolor{yellow}\hl{'
del_st = ' \sethlcolor{red}\hl{'
ins_st = '\sethlcolor{green}\hl{'
comm_st = '\\pdfcomment[icon=Note,color=yellow]{'
def deletionProcess(group_object):
replaceString = del_st + group_object.group('value') + '}'
return replaceString
def subsProcess(group_object):
deleted = del_st + group_object.group('original') + '}'
inserted = ins_st + group_object.group('new') + '}'
return deleted + inserted
def additionProcess(group_object):
replaceString = ins_st + group_object.group('value') + '}'
return replaceString
def highlightProcess(group_object):
return hl_st + group_object.group('value') + '}'
def commProcess(group_object):
return comm_st + group_object.group('value') + '}'
if not sys.stdin.isatty():
h = sys.stdin.read()
else:
h = '''{>>comment<<}, {==mark==}, {++add++}, {--del--}, {~~sub~>subs~~}.'''
h = re.sub(del_pattern, deletionProcess, h)
h = re.sub(add_pattern, additionProcess, h)
h = re.sub(comm_pattern, commProcess, h)
h = re.sub(mark_pattern, highlightProcess, h)
h = re.sub(subs_pattern, subsProcess, h)
sys.stdout.write(h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment