Created
July 5, 2012 09:04
-
-
Save asimihsan/3052477 to your computer and use it in GitHub Desktop.
markdown2 wrapper to get table of contents and pygments CSS
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
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