-
-
Save Fedik/674f4148439698a6681032b3bec370b3 to your computer and use it in GitHub Desktop.
Turn Markdown into HTML using Python markdown library and wraps the result in a complete HTML document with default Bootstrap styling. Requires the python libraries markdown.
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 | |
import argparse, sys, markdown | |
TEMPLATE = """<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<meta name="referrer" content="no-referrer" /> | |
<meta name="referrer" content="unsafe-url" /> | |
<meta name="referrer" content="origin" /> | |
<meta name="referrer" content="no-referrer-when-downgrade" /> | |
<meta name="referrer" content="origin-when-cross-origin" /> | |
<title>Page Title</title> | |
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> | |
<style> | |
body { | |
font-family: Helvetica,Arial,sans-serif; | |
} | |
code, pre { | |
font-family: monospace; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
{{content}} | |
</div> | |
</body> | |
</html> | |
""" | |
def parse_args(args=None): | |
d = 'Make a complete, styled HTML document from a Markdown file.' | |
parser = argparse.ArgumentParser(description=d) | |
parser.add_argument('mdfile', type=argparse.FileType('r'), nargs='?', | |
default=sys.stdin, | |
help='File to convert. Defaults to stdin.') | |
parser.add_argument('-o', '--out', type=argparse.FileType('w'), | |
default=sys.stdout, | |
help='Output file name. Defaults to stdout.') | |
return parser.parse_args(args) | |
def main(args=None): | |
args = parse_args(args) | |
md = args.mdfile.read() | |
extensions = ['extra', 'smarty'] | |
html = markdown.markdown(md, extensions=extensions, output_format='html5') | |
doc = TEMPLATE.replace('{{content}}', html); | |
args.out.write(doc) | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is simple yet useful, thank you! I've been migrating my personal website towards an event-driven serverless architecture today and considered deploying a blogging system where I could quickly author and store my blogs in Markdown and have them converted to HTML on the fly whenever a request is made to the corresponding URL, except that plain HTML looks way too bland and unprofessional even for a personal blog. Utilizing Bootstrap CSS for styling the blog was exactly the missing piece I was looking for ❤️