Created
August 20, 2012 13:28
-
-
Save ncornette/3404041 to your computer and use it in GitHub Desktop.
converts color codes to html for colors to be displayed in a browser
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 | |
# | |
# Converts color codes from text to html | |
# so that colors can be visible in a browser | |
# | |
import re | |
import sys | |
HTML_START="<html><head /><body><pre>" | |
HTML_END="</pre></body></html>" | |
COLOR_REPLACE='<b><span style="color:%s;background:#000000">%s</span></b>' | |
def to_html_color(color_string_orig, color_string): | |
# generate html code for color | |
return COLOR_REPLACE % (color_string,color_string_orig) | |
def replace_color(color_match): | |
# function for color substitution | |
color_code=color_match.group(0) | |
color_code_noalpha=color_code[-6:] | |
if color_code_noalpha[0]!="#": | |
color_code_noalpha="#"+color_code_noalpha | |
return to_html_color(color_code, color_code_noalpha) | |
if __name__=="__main__": | |
# read from file or stdin | |
if len(sys.argv)>1: | |
f_in = file(sys.argv[1],'r') | |
content = f_in.read() | |
f_in.close() | |
else: | |
content = sys.stdin.read() | |
# convert from xml | |
content = content.replace("<","<") | |
content = content.replace(">",">") | |
print HTML_START | |
print re.sub("#[0-9a-zA-Z]{2,8}",replace_color,content) | |
print HTML_END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment