Skip to content

Instantly share code, notes, and snippets.

@asimihsan
Created July 5, 2012 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asimihsan/3052477 to your computer and use it in GitHub Desktop.
Save asimihsan/3052477 to your computer and use it in GitHub Desktop.
markdown2 wrapper to get table of contents and pygments CSS
import os
import sys
import markdown2
from pygments.formatters import HtmlFormatter
def main():
# ------------------------------------------------------------------------
# Parse and validate inputs.
# ------------------------------------------------------------------------
input_filepath = sys.argv[1]
output_filepath = sys.argv[2]
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
# Read and validate file.
# ------------------------------------------------------------------------
with open(input_filepath) as f:
contents = f.read()
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
# Generate output, write to output file.
# ------------------------------------------------------------------------
m1 = markdown2.Markdown(extras = ["code-friendly", "fenced-code-blocks", "toc", "header-ids"])
output_body = m1.convert(contents)
output_pygments_css = """<style media="screen" type="text/css">\n%s\n</style>""" % HtmlFormatter().get_style_defs('.codehilite')
output_toc = """<div id="toc">\n<h2>Table of contents</h2>\n%s\n</div>""" % output_body.toc_html
all_output = "%s\n\n%s\n\n%s" % (output_pygments_css, output_toc, output_body)
with open(output_filepath, "w") as f:
f.write(all_output)
# ------------------------------------------------------------------------
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment