UncolorFilterによるコードハイライトの例
初回公開:2018/10/14
最終更新:未
サンプルコード
# ----------------------------------------------- UncolorFilterを定義 from pygments.util import get_bool_opt from pygments.token import Name from pygments.filter import Filter class UncolorFilter(Filter): def __init__(self, **options): Filter.__init__(self, **options) self.class_too = get_bool_opt(options, 'classtoo') def filter(self, lexer, stream): for ttype, value in stream: if ttype is Name.Function or (self.class_too and ttype is Name.Class): ttype = Name yield ttype, value # ----------------------------------------------- styleを定義 from pygments.styles.default import DefaultStyle from pygments.token import Name # トークンタイプNameを協調するために赤色,太字で表示 class MyStyle(DefaultStyle): styles = DefaultStyle.styles.copy() styles[Name]="bold #FF0000" # ----------------------------------------------- UncolorFilterでコードハイライト from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter code=u""" def add_func(a,b): \treturn a+b print add_func(2,3) class MyCls: pass my_clsMyCls() """ lexer = get_lexer_by_name("python") formatter = HtmlFormatter(style=MyStyle) with open("uncolor_none.html", 'w') as outfile: highlight(code, lexer, formatter,outfile) lexer = get_lexer_by_name("python") lexer.add_filter(UncolorFilter(classtoo=False)) with open("uncolor.html", 'w') as outfile: highlight(code, lexer, formatter,outfile) lexer = get_lexer_by_name("python") lexer.add_filter(UncolorFilter(classtoo=True)) with open("uncolor_classtoo.html", 'w') as outfile: highlight(code, lexer, formatter,outfile)
実行結果
htmlコード
uncolor_none.html
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">add_func</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">):</span> <span class="k">return</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span> <span class="k">print</span> <span class="n">add_func</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="k">class</span> <span class="nc">MyCls</span><span class="p">:</span> <span class="k">pass</span> <span class="n">my_clsMyCls</span><span class="p">()</span> </pre></div>
uncolor.html
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="n">add_func</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">):</span> <span class="k">return</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span> <span class="k">print</span> <span class="n">add_func</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="k">class</span> <span class="nc">MyCls</span><span class="p">:</span> <span class="k">pass</span> <span class="n">my_clsMyCls</span><span class="p">()</span> </pre></div>
uncolor_classtoo.html
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="n">add_func</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">):</span> <span class="k">return</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span> <span class="k">print</span> <span class="n">add_func</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="k">class</span> <span class="n">MyCls</span><span class="p">:</span> <span class="k">pass</span> <span class="n">my_clsMyCls</span><span class="p">()</span> </pre></div>
html表示イメージ
uncolor_none.html
def add_func(a,b):
return a+b
print add_func(2,3)
class MyCls:
pass
my_clsMyCls()
uncolor.html
def add_func(a,b):
return a+b
print add_func(2,3)
class MyCls:
pass
my_clsMyCls()
uncolor_classtoo.html
def add_func(a,b):
return a+b
print add_func(2,3)
class MyCls:
pass
my_clsMyCls()