Ejemplo n.º 1
0
def search_and_replace(path, dark_mode):
    with open(path, 'r', encoding='utf8') as file:
        content = file.readlines()

    out = []

    for line in content:
        if line.strip().startswith('<header></header>'):
            space = whitespace(line)
            out.append(space + '<header>\n')
            for h in header:
                out.append(space + '  ' + h)
            out.append(space + '</header>\n')
        elif line.strip().startswith('<footer></footer>'):
            space = whitespace(line)
            out.append(space + '<footer>\n')
            for f in footer:
                out.append(space + '  ' + f)
            out.append(space + '</footer>\n')
        elif line.strip().startswith('<pre>'):
            space = whitespace(line)
            out.append(space + '<div class="code"><pre><code>')
            source = line.strip().replace('<pre>', '').replace('</pre>', '')
            out.append(code.highlight(source) + '</code></pre></div>\n')
        else:
            out.append(line)

    content = "".join(out)
    content = content.replace('(This)', path.replace('\\', '/'))

    if dark_mode:
        content = dark.dark_search_and_replace(content)

    with open(path, 'w', encoding='utf8') as file:
        file.write(content)
Ejemplo n.º 2
0
def showfile(root, fpath):
    ext = '.' + fpath.rsplit('.', 1)[1] if '.' in fpath else ''
    # attempt to load corresponding .yml file if .html
    if ext == '.html':
        yml, d = fpath.replace('.html', '.yml'), {}
        if os.path.isfile(yml):
            buf = unicode(open(yml).read(), encoding='utf-8')
            d = yaml.load(buf)
        return render_ext(fpath, root, **d)
    # attempt to render requested template in .yml file
    if ext == '.yml':
        d = yaml.load(unicode(open(fpath).read(), encoding='utf-8'))
        if 'template' in d:
            return render_ext(d['template'], root, **d)
    # check if file contains template tags (for templating non-html files)
    buf = open(fpath).read(1024)
    if '{%' in buf or '{{' in buf:
        return render_ext(fpath, root)
    # render file based on file ext
    if ext in EXT_MAP:
        title, body = EXT_MAP[ext](fpath)
        h1 = title if title else ''
        title = title if title else fpath
        return render_sys('page.html', root, title=title, body=body, h1=h1)
    try:
        lexer = pygments.lexers.get_lexer_for_filename(fpath)
    except:
        lexer = None
    if lexer and ext != '.txt':
        body = highlight(lexer, fpath)
        return render_sys('page.html', root, title=fpath, body=body)
    else:
        # just try for text as <pre> block
        buf = unicode(open(fpath).read(), encoding='utf-8')
        body = u'<pre>%s</pre>' % cgi.escape(buf)
        return render_sys('page.html', root, title=fpath, body=body)
Ejemplo n.º 3
0
Archivo: test.py Proyecto: jeberle/ws
def test_code():
    fpath = 'files/code.py'
    lexer = pygments.lexers.get_lexer_for_filename(fpath)
    body = highlight(lexer, fpath)
    open('files/last.html', 'w').write(body.encode('utf-8'))
    assert body == unicode(open('files/code.html').read(), encoding='utf-8')