Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Instead of retrieving md files from their url, look for them recursiv…
Browse files Browse the repository at this point in the history
…ely given a root directory.
  • Loading branch information
iglesias committed Dec 3, 2013
1 parent 97fe45a commit a6a0bab
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions util/markdown.py
@@ -1,34 +1,51 @@
#!/usr/bin/env python

import os
import sys
import urllib2
import os, sys, getopt
from shogun.settings import SRC_DIR
from github import Github

# Dictionary markdown file identifier -> url with markdown content
MD_FILES = {'INSTALL': 'https://raw.github.com/shogun-toolbox/shogun/develop/INSTALL.md',
'README': 'https://raw.github.com/shogun-toolbox/shogun/develop/README.md',
'README_cmake': 'https://raw.github.com/shogun-toolbox/shogun/develop/README_cmake.md',
'README_developer': 'https://raw.github.com/shogun-toolbox/shogun/develop/README_developer.md'}
def print_help():
print 'markdown.py -d <root directory to recursively look for md files>'

def get_html_from_md(md_file='README'):
if md_file not in MD_FILES:
print 'Unknown markdown file %s.' % md_file
return

md_content = urllib2.urlopen(MD_FILES[md_file]).read()
def get_html_from_md(md_fname):
md_file = open(md_fname, 'r')
md_content = md_file.read()
md_file.close()
g = Github()
return g.render_markdown(md_content)

if __name__ == "__main__":
rootdir = SRC_DIR

try:
opts, args = getopt.getopt(sys.argv[1:], 'hd:')
except getopt.GetoptError:
print_help()
sys.exit(2)

for opt, arg in opts:
if opt == '-h':
print_help()
sys.exit(0)
elif opt == '-d':
rootdir = arg

print 'The root directory is %s.' % rootdir

if not os.path.exists('templates/md2html'):
os.mkdir('templates/md2html')
elif os.path.isfile('templates/md2html'):
print 'Cannot write output files. Please delete or rename the file templates/md2html.'
sys.exit(0)

for md_file in MD_FILES:
html_content = get_html_from_md(md_file)
html_file = open("templates/md2html/%s.html" % md_file, "w")
html_file.write(html_content)
html_file.close()
for root, dirs, files in os.walk(rootdir):
for fname in files:
if fname.endswith('.md'):
try:
html_content = get_html_from_md(os.path.join(root, fname))
html_file = open("templates/md2html/%s.html" % fname[:-3], "w")
html_file.write(html_content)
html_file.close()
print '%s rendered to %s.html.' % (fname, fname[:-3])
except UnicodeDecodeError:
print 'Decoding ERROR. Could not render to HTML %s.' % fname

0 comments on commit a6a0bab

Please sign in to comment.