Skip to content

Instantly share code, notes, and snippets.

@mcsf
Created March 24, 2018 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcsf/cbd5d646d7433f385b14d5cc0e8fec4e to your computer and use it in GitHub Desktop.
Save mcsf/cbd5d646d7433f385b14d5cc0e8fec4e to your computer and use it in GitHub Desktop.
Detect and wrap roman numerals with a special "small-caps" span tag
#!/usr/bin/env python
import sys
import re
re_full = r'(MMM|MM|M)?(CM|CD|DCCC|DCC|DC|D|CCC|CC|C)?(XC|XL|LXXX|LXX|LX|L|XXX|XX|X)?(IX|IV|VIII|VII|VI|V|III|II|I)?'
re_appr = r'\b[MDCLXVI]+\b'
def wrap_roman(matchobj):
s = matchobj.group(0)
m = re.match(re_full, s)
g = m.group(0) if m != None else ''
if len(s) > 0 and len(g) == len(s):
return '<span class="small-caps">' + g + '</span>'
return s
for line in sys.stdin:
print re.sub(re_appr, wrap_roman, line),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment