Created
March 24, 2018 21:59
-
-
Save mcsf/cbd5d646d7433f385b14d5cc0e8fec4e to your computer and use it in GitHub Desktop.
Detect and wrap roman numerals with a special "small-caps" span tag
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
#!/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