コード例 #1
0
def test_highlight_python():
    ret = highlight('```python\n' 'print("hello world")\n' '```\n', )
    assert ret == ('<div class="highlight python"><pre>'
                   '<span></span>'
                   '<span class="nb">print</span>'
                   '<span class="p">(</span>'
                   '<span class="s2">&quot;hello world&quot;</span>'
                   '<span class="p">)</span>\n'
                   '</pre></div>\n')
コード例 #2
0
def test_custom_renderer():
    class MyRenderer(CodeRenderer):
        def block_code(self, *args):
            return 'nope'
    ret = highlight(
        'hello\n'
        '```\n'
        'world\n'
        '```\n',
        Renderer=MyRenderer,
    )
    assert ret == '<p>hello</p>\nnope'
コード例 #3
0
def test_highlight_plain_text():
    ret = highlight(
        '```\n'
        'this is plain text, such class.\n'
        '```\n'
    )
    assert ret == (
        '<div class="highlight"><pre>'
        '<span></span>'
        'this is plain text, such class.\n'
        '</pre></div>\n'
    )
コード例 #4
0
ファイル: models.py プロジェクト: Touchfl0w/flasky
 def on_change_body(target, value, oldvalue, initiator):
     allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code','em', 'i', 'li',
                     'ol', 'pre', 'strong', 'ul','h1', 'h2', 'h3', 'p', 'img']
     #对于带样式/属性的tag,必须添加该字典
     allowed_attrs = {'*': ['class'],
                      'a': ['href', 'rel'],
                      'img': ['src', 'alt']}
     # k = markdown(value, output_format='html')
     #该markdown转换器更简单些,乱码少一些
     import markdown_code_blocks
     k = markdown_code_blocks.highlight(value)
     import re
     digest_list = [i for i in re.findall(r'>(.*?)<', k[:100]) if i]
     target.digest = ''.join(digest_list[:30])
     target.body_html = bleach.clean(k, tags=allowed_tags, attributes=allowed_attrs, strip=True)
コード例 #5
0
def md(s: str) -> str:
    html = markdown_code_blocks.highlight(s, Renderer=Renderer)
    # manually bless the highlighted output.
    return markupsafe.Markup(html)
コード例 #6
0
 def safe_highlight(data):
     return highlight(str(data))
コード例 #7
0
def compile_index(indexname, resname, backlink=None):
    indexpath = os.path.join("./docsgen/index", indexname)
    resfile = os.path.join("./docs/", resname)

    index = dict()
    additional_index_files = set()
    modules = list()

    print(f"Parsing index {indexname}...")
    with open(indexpath, "r") as f:
        for line in f:
            if len(line) < 2:
                continue
            cmd, *data = quotesplit(line)
            if cmd == "INCLUDE":
                path, file = data
                index[path] = file
            elif cmd == "INDEX":
                opath, dpath = data
                compile_index(
                    opath,
                    dpath,
                    backlink=indexname,
                )
                index[opath] = dpath
                additional_index_files.add(opath)
            elif cmd == "MODULE":
                path = data[0]
                print(f"Importing module {path}")
                module = importlib.import_module(path)
                modules.append(module)
                index[path] = module
            elif cmd == "HEADER":
                path = data[0]
                index[path] = DataHeader(path)
            else:
                print("!!! Unrecognized command:", cmd)

    resindex = list()

    print(f"Generating index for {indexpath}")
    if backlink is not None:
        resindex.append(f'<p><a href="{backlink}.html">🔗 Back</a></p>')

    for key in index:
        if key in additional_index_files:
            resindex.append(f'<p><a href="{index[key]}">🔗 {key}</a></p>')
        else:
            if isinstance(index[key], DataHeader):
                resindex.append(
                    f'<h1><a href="#{key.replace(" ", "-")}">{key}</a></h1>')
            else:
                resindex.append(
                    f'<p><a href="#{key.replace(" ", "-")}">{key}</a></p>')
                if inspect.ismodule(index[key]):
                    keys = pyclbr.readmodule_ex(key)
                    tmp = []
                    for sk, sv in keys.items():
                        tmp.append(
                            f'<li><a href=#{key+sk}>{sv.__class__.__name__} {sk}</a></li>'
                        )
                    resindex.append(
                        f'<ul class="modulelist">{"".join(tmp)}</ul>')

    resindex = "\n".join(resindex)

    resdata = list()

    print("Parsing files...")
    for key, value in index.items():
        if key in additional_index_files:
            continue

        elif isinstance(value, DataHeader):
            resdata.append(f'<h1 id={key.replace(" ", "-")}>{key}</h1><hr>')

        elif not isinstance(value, str):
            html = gen_docs_for_module(value)
            resdata.append(f'<h1 id={key.replace(" ", "-")}>{key}</h1>')
            resdata.append(html)

        elif value.endswith(".md"):
            path = os.path.join("./docsgen/data", value)
            print(f"Parsing {path}")
            with open(path) as f:
                html = highlight(f.read())
            resdata.append(f'<h1 id={key.replace(" ", "-")}>{key}</h1>')
            resdata.append(html)
        else:
            print(f"Unknown extension of {value}")

    resdata = "\n".join(resdata)

    print(f"Writing file {resfile}...")
    with open(resfile, "w") as f:
        data = patterndata % (resindex, resdata)
        f.write(data)

    print("Done!")
コード例 #8
0
def test_simple_markdown():
    ret = highlight('## ohai\n')
    assert ret == '<h2>ohai</h2>\n'
コード例 #9
0
def _to_slide(md: str) -> str:
    highlighted = highlight(md, Renderer=RawHTMLRenderer)
    return f'<section>{highlighted}</section>'