Skip to content

Instantly share code, notes, and snippets.

@raoulwegat
Last active November 18, 2022 02:34
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 raoulwegat/ae00889615df60a40caaa048b294eeb2 to your computer and use it in GitHub Desktop.
Save raoulwegat/ae00889615df60a40caaa048b294eeb2 to your computer and use it in GitHub Desktop.
Convert a directory of *.md to *.html. Based on https://gist.github.com/jiffyclub/5015986
#!/usr/bin/python3
import sys
import os
import re
import jinja2
import markdown
TEMPLATE = """<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: sans-serif;
}
code, pre {
font-family: monospace;
}
</style>
</head>
<body>
<div class="container">
{{content}}
</div>
</body>
</html>
"""
currentdir = os.getcwd()
filelist = os.listdir(currentdir)
fileext = '.md'
out_dir = '../html'
def process( filebase ):
f = open(filebase+fileext)
md = f.read()
extensions = ['extra', 'smarty']
html = markdown.markdown(md, extensions=extensions, output_format='html5')
outfile = jinja2.Template(TEMPLATE).render(content=html)
out_path = os.path.join(out_dir, filebase+".html")
file_write = open(out_path, "w")
file_write.write(outfile)
file_write.close()
def main():
for file in filelist:
filename = os.path.basename( file )
filebase = os.path.os.path.splitext( filename )[0]
if re.search(fileext, filename):
process( filebase )
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment