HtmlFormatterのlinespansオプションの例

初回公開:2018/10/14
最終更新:未

サンプルコード

from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter

code=u"""
class SuperCls(object):
    def method(self, arg):
        print("スーパクラスのメソッドを実行, arg=",arg)

class SubCls(SuperCls):
    def method(self):
        print ("サブクラスのメソッドを実行")
        super().method("arg")

sub_inst=SubCls()
sub_inst.method()
"""
lexer = get_lexer_by_name("python")

with open("linespans.html", 'w') as outfile:
    highlight(code, lexer, HtmlFormatter(linespans="foo"),outfile)

with open("linespans_,linenos.html", 'w') as outfile:
    highlight(code, lexer, HtmlFormatter(linespans="foo",linenos=True),outfile)

実行結果

linenos=Falseの場合- linespans.html

<div class="highlight"><pre><span></span><span id="foo-1"><span class="k">class</span> <span class="nc">SuperCls</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span id="foo-2">    <span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
</span><span id="foo-3">        <span class="k">print</span><span class="p">(</span><span class="s2">&quot;スーパクラスのメソッドを実行, arg=&quot;</span><span class="p">,</span><span class="n">arg</span><span class="p">)</span>
</span><span id="foo-4">
</span><span id="foo-5"><span class="k">class</span> <span class="nc">SubCls</span><span class="p">(</span><span class="n">SuperCls</span><span class="p">):</span>
</span><span id="foo-6">    <span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span id="foo-7">        <span class="k">print</span> <span class="p">(</span><span class="s2">&quot;サブクラスのメソッドを実行&quot;</span><span class="p">)</span>
</span><span id="foo-8">        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">method</span><span class="p">(</span><span class="s2">&quot;arg&quot;</span><span class="p">)</span>
</span><span id="foo-9">
</span><span id="foo-10"><span class="n">sub_inst</span><span class="o">=</span><span class="n">SubCls</span><span class="p">()</span>
</span><span id="foo-11"><span class="n">sub_inst</span><span class="o">.</span><span class="n">method</span><span class="p">()</span>
</span></pre></div>

linenos=Trueの場合- anchorlinenos_noclasses.html

<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="foo-1"><span class="k">class</span> <span class="nc">SuperCls</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span id="foo-2">    <span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
</span><span id="foo-3">        <span class="k">print</span><span class="p">(</span><span class="s2">&quot;スーパクラスのメソッドを実行, arg=&quot;</span><span class="p">,</span><span class="n">arg</span><span class="p">)</span>
</span><span id="foo-4">
</span><span id="foo-5"><span class="k">class</span> <span class="nc">SubCls</span><span class="p">(</span><span class="n">SuperCls</span><span class="p">):</span>
</span><span id="foo-6">    <span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span id="foo-7">        <span class="k">print</span> <span class="p">(</span><span class="s2">&quot;サブクラスのメソッドを実行&quot;</span><span class="p">)</span>
</span><span id="foo-8">        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">method</span><span class="p">(</span><span class="s2">&quot;arg&quot;</span><span class="p">)</span>
</span><span id="foo-9">
</span><span id="foo-10"><span class="n">sub_inst</span><span class="o">=</span><span class="n">SubCls</span><span class="p">()</span>
</span><span id="foo-11"><span class="n">sub_inst</span><span class="o">.</span><span class="n">method</span><span class="p">()</span>
</span></pre></div>
</td></tr></table>
ページのトップへ戻る