Created
February 2, 2012 15:18
-
-
Save sbp/1723940 to your computer and use it in GitHub Desktop.
Convert between XMP and PRE sections using pygments
This file contains hidden or 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 os | |
import re | |
import subprocess | |
import shutil | |
r_tag = re.compile(r"(?s)<[^>]+>") | |
r_pre = re.compile(r"(?s)<pre([^>]+)>(.*?)</pre>") | |
r_xmp = re.compile(r"(?s)<xmp([^>]+)>(.*?)</xmp>") | |
r_pyg = re.compile(r"\bpyg-([a-z]+)\b") | |
prefix = len("<div class=\"highlight\"><pre>") | |
suffix = len("</pre></div>\n") | |
def xmp_to_pre(match): | |
attributes, content = match.groups() | |
syntax = r_pyg.search(attributes) | |
if not syntax: | |
return match.group(0) | |
syntax = syntax.group(1) | |
command = ["pygmentize", "-l", syntax, "-f", "html"] | |
p = subprocess.Popen(command, | |
stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
stdout, stderr = p.communicate(content) | |
result = stdout[prefix:-suffix] | |
result = result.replace("\t", "<span class=\"tab\"> </span>") | |
return "<pre%s>\n%s</pre>" % (attributes, result) | |
def pre_to_xmp(match): | |
attributes, content = match.groups() | |
syntax = r_pyg.search(attributes) | |
if not syntax: | |
return match.group(0) | |
result = content.replace("<span class=\"tab\"> </span>", "\t") | |
result = r_tag.sub("", result) | |
result = result.replace("<", "<") | |
result = result.replace(">", ">") | |
result = result.replace(""", "\"") | |
result = result.replace("&", "&") | |
return "<xmp%s>%s</xmp>" % (attributes, result) | |
def pre(filename): | |
with open(filename) as f: | |
text = f.read() | |
text = r_xmp.sub(xmp_to_pre, text) | |
with open(filename, "w") as o: | |
o.write(text) | |
def xmp(filename): | |
with open(filename) as f: | |
text = f.read() | |
text = r_pre.sub(pre_to_xmp, text) | |
with open(filename, "w") as o: | |
o.write(text) | |
def main(): | |
command = sys.argv[1] | |
filename = sys.argv[2] | |
backup = os.path.expanduser("~/.pyg-backup") | |
shutil.copy2(filename, backup) | |
if command == "pre": | |
pre(filename) | |
elif command == "xmp": | |
xmp(filename) | |
else: print "?" | |
if __name__ == "__main__": | |
main() |
Ancient long-forgotten magics that let you write markup embedded in HTML without having to use all that silly escaped entity nonsense. Don't tell anybody, they might try to ban it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's xmp? :D