Example #1
0
def ignore_user(file_path):
    d = [
        (mdw.user_ignore_rules_fn, os.path.basename, 'ignored by filename'), 
        (mdw.user_ignore_rules_path, lambda x:os.path.relpath(x, mdw.src_dirname), 'ignored by path')
    ]
    
    for i in d:
        for m in i[0]:
            if fnmatch.fnmatch(i[1](file_path), m):
                mdw.skip_files[file_path] = i[2]
                mdw_report.warning('ignore', '\''+file_path+'\'  <->  \''+m+'\'')
                return False
        return True
Example #2
0
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)