def convert(src_fp, tpl_c): # read source file f = open(src_fp) c = f.read() f.close() # decode from utf-8 try: c = c.decode(mdw.encoding) except: mdw_report.warning('skip', 'file \''+src_fp+'\' not encoding '+mdw.encoding) mdw.skip_files[src_fp] = 'encoding not '+mdw.encoding # encoding error mdw.rel_build_files.remove(src_fp) return # get title t = mdw.title_re.search(c) if t : title = t.group(1).strip() c = c[:t.start()] + c[t.end():] else: title = 'Untitled' # html_root html_root = os.path.relpath(mdw.src_dirname, os.path.normpath(os.path.dirname(src_fp))) # markdown2 convert c = markdown2.markdown(c, extras = ['fenced-code-blocks', 'toc', 'wiki-tables']) # replace tpl_c tpl_c = tpl_c.replace(u'%title%', title) tpl_c = tpl_c.replace(u'%html_root%',html_root+u'/') c = c.replace(u'[TOC]', '<div class="toc">'+c.toc_html+'</div>', 1) if c.toc_html else c c = tpl_c.replace(u'%content%', c) #output out_p = mdw.html_re.sub(r'\1.html', os.path.relpath(src_fp, mdw.src_dirname)) out_dirname = os.path.dirname(out_p) if out_dirname and os.path.exists(out_dirname) == False: os.makedirs(out_dirname) #write f = open(out_p, 'w') f.write(c.encode(mdw.encoding)) f.close() mdw_report.success(src_fp+' -> '+out_p)
def init(): if os.path.exists(mdw.mark_fn): mdw_report.error('This directory has been initialized already!') try: os.mkdir(mdw.src_dirname) except: mdw_report.error('failed to mkdir \''+mdw.src_dirname+'\'') import shutil shutil.copytree(os.path.join(mdw.mdw_dir_path, 'static'), 'static') shutil.copyfile(os.path.join(mdw.mdw_dir_path, 'tpl.html'), os.path.join(mdw.src_dirname, 'tpl.html')) open(mdw.mark_fn, 'w').write('DO NOT Delete this file.') mdw_report.success(''' source file directory : src output html directory : . template file : src/tpl.html enjoy!''')
def clean(): mdw_core.check_cwd() write_cache({}) mdw_report.success('cache clean')