Unix - Python script - print file with color scheme

From NoskeWiki
Jump to: navigation, search

About

NOTE: This page is a daughter page of: Unix and Python


This page contains code a Python script for colorizing the output of a file, such as a HTML file. It's not yet complete, but I thought I'd record where I'm up to.

To use the script, copy it to any folder, create a "test.html" file in the same folder then run this from a bash Terminal:

$ python print_file_with_color_scheme.py -f test.html

Since your program path fairly long, you'll definitely want to alias this to something shorter like "catc" (think: colored version of cat). To do that, add this to your ~./bashrc file:

alias catc='python ~/path_to_your_program/print_file_with_color_scheme.py -f'

... once you reload bash you can now just enter 'catc test.html' from anywhere.


Useful Application: outputting colored cheatsheets with the help of bash alias

Something useful you can do with this application is create a bunch of "cheatsheet" files, and add each of these to your ~./bashrc below catc like this:

alias cheatsheat_mecurial='catc cheatsheat_mecurial.html'

Now you can now just enter 'cheatsheat_mecurial' (using tab to autocomplete after first few chars) and it will print a nicely formatted cheatsheet for you to remember commonly used commands.


print_file_with_color_scheme.py

print_file_with_color_scheme.py

#!/usr/bin/env python
# print_file_with_color_scheme.py - command line program which inputs a file
# path '-f' and prints the file contents to screen after applying color
# substitution.
#
# Usage:
#   print_file_with_color_scheme.py -f <file.html>
#
# Usage examples:
#   $ python print_file_with_color_scheme.py -f example_page.html
#
# Currently this is hardwired to recognize a small set of HTML tags
# ('<h1>', '<b>', etc) and replace these with set ANSI colors.
#
# TODO(anoske): A more advanced version would let you add a
# '-c color_replace_scheme.txt' where you could specify a set of
# of strings and colors to replace them with in the form:
#   '//'      GREEN
#   'Warning' RED   UNDERLINE
# Or else something a little more like sed and use regex.
 
import optparse
 
# A dictionary of common names assigned to various ASNI SGR styles
# (format & color).
# See: http://www.andrewnoske.com/Bash_-_adding_color
style = {
  'NORMAL': '\033[0m',
  'BOLD': '\033[1m',
  'FAINT': '\033[2m',
  'ITALICS': '\033[3m',
  'UNDERLINE': '\033[4m',
  'RED': '\033[94m',
  'BLUE': '\033[94m',
  'GREEN': '\033[92m',
  'GREY': '\033[37;2m',
  'YELLOW': '\033[33m',
  'YELLOW_BRIGHT_BOLD': '\033[33;1m',
  'YELLOW_BRIGHT_BOLD_UNDERLINED': '\033[33;1;4m',
  'YELLOW_BRIGHT_BOLD_INVERTED': '\033[33;7m',
  'BLUE_UNDERLINED': '\033[94;4m',
}
STYLE_RESET = '\033[0m'
 
# A mapping of 'find' and 'replace' strings. No regex/wildcards are
# used. In this default configuration, common HTML tags
# (eg: '<b>') are replaced with ANSI style characters to
# add color (and remove the tag).
replace_arr = {
  '<h1>': style['YELLOW_BRIGHT_BOLD'], '</h1>': STYLE_RESET,
  '<h2>': style['YELLOW'], '</h2>': STYLE_RESET, 
  '<b>': style['BOLD'], '</b>': STYLE_RESET,
  '<i>': style['GREY'], '</i>': STYLE_RESET,  # 'ITALICS' doesn't show.
  '<u>': style['UNDERLINE'], '</u>': STYLE_RESET,
  '<a>': style['BLUE_UNDERLINED'], '</a>': STYLE_RESET,
  '<red>': style['RED'], '</red>': STYLE_RESET,
  '<green>': style['GREEN'], '</green>': STYLE_RESET,
  '<blue>': style['BLUE'], '</blue>': STYLE_RESET,
  '<grey>': style['GREY'], '</grey>': STYLE_RESET,
 
  '<title>': style['YELLOW_BRIGHT_BOLD_INVERTED'] + '   ',
  '</title>': '   ' + STYLE_RESET,
  '<hr>': '----------------------------------------',
}
 
usage_str = """
<title>HOW TO USE THIS PROGRAM</title>
 
Usage: <b>python print_file_with_color_scheme.py -f file_to_print.html</b>
See: <a>http://andrewnoske.com/wiki/Unix_-_Python_script_-_print_file_with_color_scheme</a>
"""
 
def main():
  p = optparse.OptionParser()
  p.add_option('--file', '-f', default='')
  options, arguments = p.parse_args()
 
  contents = usage_str;
  if len(options.file) > 0:
    file = open(options.file, 'r')
    contents = file.read()
 
  for key in replace_arr:
    contents = contents.replace(key, replace_arr[key])
  print contents + '\n'
 
 
if __name__ == '__main__':
  main()



To test it out create this html page in the same dir:

example_page.html

<title>Big Title</title>
<h1>Heading</h1>
Some <b>bold text</b> and a <a>http://hyperlink</a>.

And then run:

$ python print_file_with_color_scheme.py -f example_page.html


See Also


Code license
For all of the code on my site... if there are specific instruction or licence comments please leave them in. If you copy my code with minimum modifications to another webpage, or into any code other people will see I would love an acknowledgment to my site.... otherwise, the license for this code is more-or-less WTFPL (do what you want)! If only copying <20 lines, then don't bother. That said - if you'd like to add a web-link to my site www.andrewnoske.com or (better yet) the specific page with code, that's a really sweet gestures! Links to the page may be useful to yourself or your users and helps increase traffic to my site. Hope my code is useful!  :)