Skip to content

Instantly share code, notes, and snippets.

@yuanchuan
Created December 10, 2011 06:09
Show Gist options
  • Save yuanchuan/1454682 to your computer and use it in GitHub Desktop.
Save yuanchuan/1454682 to your computer and use it in GitHub Desktop.
document generator
#!/usr/bin/env python
# Get markdown module at: http://pypi.python.org/pypi/Markdown
from markdown import markdownFromFile
from string import Template
from datetime import datetime
import os
doc_path = 'document'
doc_names = ['intro','general','html','css','javascript']
doc_dict = {}
output = 'document.html'
source = 'tmpl/template.html'
temp = '__tmp__'
def get_file_content( file ):
if os.path.exists( file ):
return open( file, 'r' ).read()
return ''
def markdown_to_html( file ):
if os.path.exists( file ):
markdownFromFile( file, temp )
return get_file_content( temp )
return ''
def write_to_file( file, content ):
f = open( file, 'w')
f.write( content )
f.close()
doc_dict['time'] = str( datetime.now() )
for n in doc_names:
name = doc_path +'/'+ n + '.md'
doc_dict[n] = markdown_to_html( name )
try:
write_to_file(
output,
Template(
get_file_content( source )
).substitute( doc_dict )
)
print('build success.')
except KeyError, err:
print( 'missing dict value for: ', str( err ) )
os.system( 'rm ' + temp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment