This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead of retrieving md files from their url, look for them recursiv…
…ely given a root directory.
- Loading branch information
Showing
1 changed file
with
36 additions
and
19 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |