#!/usr/bin/python import pygments.formatters import pygments.styles import sys,os formatters = { 'latex': ('tex', pygments.formatters.latex.LatexFormatter), 'html': ('css', pygments.formatters.html.HtmlFormatter) } styles = pygments.styles.get_all_styles() def usage(): print >>sys.stderr, """Usage: %s [-a|style] formatter [output] Output all style definitions for the given style and formatter to the specified output file. Use - to output to stdout. Default outpufile is stylename.extension (extension being the extension of the formatter) Use -a to output style definitions for all styles. Each style is written to its own file. Specifying an output file is not needed (or possible). Supported formatters: html, latex Supported styles: %s """ % (sys.argv[0], ' '.join(styles)) sys.exit(1) def write_style(formatter, style, output): if output == '-': outfd = sys.stdout else: if os.path.exists(output): answer = '' while answer.lower() not in ('a','s','o'): sys.stdout.write("%s exists. [S]kip [A]bort [O]verwrite? " % output) answer = raw_input() if answer.lower() == 's': return if answer.lower() == 'a': sys.exit(1) outfd = open(output, 'w') outfd.write(formatters[formatter][1](style=style).get_style_defs()) # Main program if len(sys.argv) == 3 and sys.argv[1] == '-a': formatter = sys.argv[2] if formatter not in formatters: usage() for style in styles: write_style(formatter, style, style + '.' + formatters[formatter][0]) elif len(sys.argv) == 3: style, formatter = sys.argv[1:] if style not in styles or formatter not in formatters: usage() write_style(formatter, style, style + '.' + formatters[formatter][0]) elif len(sys.argv) == 4: style, formatter, output = sys.argv[1:] if style not in styles or formatter not in formatters: usage() write_style(formatter, style, output) else: usage()