HtmlFormatterのnobackgroundオプションの例
【 目次 】
初回公開:2018/10/14
最終更新:未
サンプルコード
from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter code = 'print("Hello World")' lexer = get_lexer_by_name("python") # nobackgroundオプションだけを単独で指定 with open("default.html", 'w') as outfile: highlight(code, lexer, HtmlFormatter(),outfile) with open("nobackground.html", 'w') as outfile: highlight(code, lexer, HtmlFormatter(nobackground=True),outfile) # noclassesオプションを指定すると外側のdivタグのstyle属性が無くなる。 with open("default_noclasses.html", 'w') as outfile: highlight(code, lexer, HtmlFormatter(nobackground=True,noclasses=True),outfile) with open("nobackground_noclasses.html", 'w') as outfile: highlight(code, lexer, HtmlFormatter(nobackground=True,noclasses=True),outfile) # fullオプションを追加。 with open("default_full.html", 'w') as outfile: highlight(code, lexer, HtmlFormatter(full=True),outfile) with open("nobackground_full.html", 'w') as outfile: highlight(code, lexer, HtmlFormatter(nobackground=True,full=True),outfile) # fullオプションに更にcssfileオプションを追加。 with open("default_cssfile.html", 'w') as outfile: highlight(code, lexer, HtmlFormatter(cssfile="default_cssfile.css",full=True),outfile) with open("nobackground_cssfile.html", 'w') as outfile: highlight(code, lexer, HtmlFormatter(nobackground=True,cssfile="nobackground_cssfile.css",full=True),outfile)
実行結果
nobackgroundオプションだけを単独で指定
default.htmlとnobackground.htmlの内容は全く同じものが出力される。
nobackgroundオプションだけを単独で指定してもnobackgroundオプションを指定しない場合と出力に違いは見られない。(htmlタグのclass属性に変化はない。)
default.htmlとnobackground.html
<div class="highlight"><pre><span></span><span class="k">print</span><span class="p">(</span><span class="s2">"Hello World"</span><span class="p">)</span> </pre></div>
noclassesオプションを追加
noclassesオプションを指定して直接インラインでstyle属性を出力
default_noclasses.html
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Hello World"</span>) </pre></div>
html表示イメージ
print("Hello World")
nobackground_noclasses.html
<div class="highlight"><pre><span></span><span class="k">print</span><span class="p">(</span><span class="s2">"Hello World"</span><span class="p">)</span> </pre></div>
html表示イメージ
print("Hello World")
fullオプションを追加
default_full.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <meta http-equiv="content-type" content="text/html; charset=None"> <style type="text/css"> td.linenos { background-color: #f0f0f0; padding-right: 10px; } span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; } pre { line-height: 125%; } body .hll { background-color: #ffffcc } body { background: #f8f8f8; } body .c { color: #408080; font-style: italic } /* Comment */ body .err { border: 1px solid #FF0000 } /* Error */ body .k { color: #008000; font-weight: bold } /* Keyword */ body .o { color: #666666 } /* Operator */ body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */ body .cm { color: #408080; font-style: italic } /* Comment.Multiline */ body .cp { color: #BC7A00 } /* Comment.Preproc */ body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */ body .c1 { color: #408080; font-style: italic } /* Comment.Single */ body .cs { color: #408080; font-style: italic } /* Comment.Special */ body .gd { color: #A00000 } /* Generic.Deleted */ body .ge { font-style: italic } /* Generic.Emph */ body .gr { color: #FF0000 } /* Generic.Error */ body .gh { color: #000080; font-weight: bold } /* Generic.Heading */ body .gi { color: #00A000 } /* Generic.Inserted */ body .go { color: #888888 } /* Generic.Output */ body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ body .gs { font-weight: bold } /* Generic.Strong */ body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ body .gt { color: #0044DD } /* Generic.Traceback */ body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ body .kp { color: #008000 } /* Keyword.Pseudo */ body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ body .kt { color: #B00040 } /* Keyword.Type */ body .m { color: #666666 } /* Literal.Number */ body .s { color: #BA2121 } /* Literal.String */ body .na { color: #7D9029 } /* Name.Attribute */ body .nb { color: #008000 } /* Name.Builtin */ body .nc { color: #0000FF; font-weight: bold } /* Name.Class */ body .no { color: #880000 } /* Name.Constant */ body .nd { color: #AA22FF } /* Name.Decorator */ body .ni { color: #999999; font-weight: bold } /* Name.Entity */ body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ body .nf { color: #0000FF } /* Name.Function */ body .nl { color: #A0A000 } /* Name.Label */ body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ body .nt { color: #008000; font-weight: bold } /* Name.Tag */ body .nv { color: #19177C } /* Name.Variable */ body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ body .w { color: #bbbbbb } /* Text.Whitespace */ body .mb { color: #666666 } /* Literal.Number.Bin */ body .mf { color: #666666 } /* Literal.Number.Float */ body .mh { color: #666666 } /* Literal.Number.Hex */ body .mi { color: #666666 } /* Literal.Number.Integer */ body .mo { color: #666666 } /* Literal.Number.Oct */ body .sa { color: #BA2121 } /* Literal.String.Affix */ body .sb { color: #BA2121 } /* Literal.String.Backtick */ body .sc { color: #BA2121 } /* Literal.String.Char */ body .dl { color: #BA2121 } /* Literal.String.Delimiter */ body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ body .s2 { color: #BA2121 } /* Literal.String.Double */ body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ body .sh { color: #BA2121 } /* Literal.String.Heredoc */ body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ body .sx { color: #008000 } /* Literal.String.Other */ body .sr { color: #BB6688 } /* Literal.String.Regex */ body .s1 { color: #BA2121 } /* Literal.String.Single */ body .ss { color: #19177C } /* Literal.String.Symbol */ body .bp { color: #008000 } /* Name.Builtin.Pseudo */ body .fm { color: #0000FF } /* Name.Function.Magic */ body .vc { color: #19177C } /* Name.Variable.Class */ body .vg { color: #19177C } /* Name.Variable.Global */ body .vi { color: #19177C } /* Name.Variable.Instance */ body .vm { color: #19177C } /* Name.Variable.Magic */ body .il { color: #666666 } /* Literal.Number.Integer.Long */ </style> </head> <body> <h2></h2> <div class="highlight"><pre><span></span><span class="k">print</span><span class="p">(</span><span class="s2">"Hello World"</span><span class="p">)</span> </pre></div> </body> </html>
nobackground_full.html
default_full.htmlにあったhtmlヘッダのstlyタグ内のbody { background: #f8f8f8; }
の行が削除されている。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <meta http-equiv="content-type" content="text/html; charset=None"> <style type="text/css"> td.linenos { background-color: #f0f0f0; padding-right: 10px; } span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; } pre { line-height: 125%; } body .hll { background-color: #ffffcc } body .c { color: #408080; font-style: italic } /* Comment */ body .err { border: 1px solid #FF0000 } /* Error */ body .k { color: #008000; font-weight: bold } /* Keyword */ body .o { color: #666666 } /* Operator */ body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */ body .cm { color: #408080; font-style: italic } /* Comment.Multiline */ body .cp { color: #BC7A00 } /* Comment.Preproc */ body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */ body .c1 { color: #408080; font-style: italic } /* Comment.Single */ body .cs { color: #408080; font-style: italic } /* Comment.Special */ body .gd { color: #A00000 } /* Generic.Deleted */ body .ge { font-style: italic } /* Generic.Emph */ body .gr { color: #FF0000 } /* Generic.Error */ body .gh { color: #000080; font-weight: bold } /* Generic.Heading */ body .gi { color: #00A000 } /* Generic.Inserted */ body .go { color: #888888 } /* Generic.Output */ body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ body .gs { font-weight: bold } /* Generic.Strong */ body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ body .gt { color: #0044DD } /* Generic.Traceback */ body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ body .kp { color: #008000 } /* Keyword.Pseudo */ body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ body .kt { color: #B00040 } /* Keyword.Type */ body .m { color: #666666 } /* Literal.Number */ body .s { color: #BA2121 } /* Literal.String */ body .na { color: #7D9029 } /* Name.Attribute */ body .nb { color: #008000 } /* Name.Builtin */ body .nc { color: #0000FF; font-weight: bold } /* Name.Class */ body .no { color: #880000 } /* Name.Constant */ body .nd { color: #AA22FF } /* Name.Decorator */ body .ni { color: #999999; font-weight: bold } /* Name.Entity */ body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ body .nf { color: #0000FF } /* Name.Function */ body .nl { color: #A0A000 } /* Name.Label */ body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ body .nt { color: #008000; font-weight: bold } /* Name.Tag */ body .nv { color: #19177C } /* Name.Variable */ body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ body .w { color: #bbbbbb } /* Text.Whitespace */ body .mb { color: #666666 } /* Literal.Number.Bin */ body .mf { color: #666666 } /* Literal.Number.Float */ body .mh { color: #666666 } /* Literal.Number.Hex */ body .mi { color: #666666 } /* Literal.Number.Integer */ body .mo { color: #666666 } /* Literal.Number.Oct */ body .sa { color: #BA2121 } /* Literal.String.Affix */ body .sb { color: #BA2121 } /* Literal.String.Backtick */ body .sc { color: #BA2121 } /* Literal.String.Char */ body .dl { color: #BA2121 } /* Literal.String.Delimiter */ body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ body .s2 { color: #BA2121 } /* Literal.String.Double */ body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ body .sh { color: #BA2121 } /* Literal.String.Heredoc */ body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ body .sx { color: #008000 } /* Literal.String.Other */ body .sr { color: #BB6688 } /* Literal.String.Regex */ body .s1 { color: #BA2121 } /* Literal.String.Single */ body .ss { color: #19177C } /* Literal.String.Symbol */ body .bp { color: #008000 } /* Name.Builtin.Pseudo */ body .fm { color: #0000FF } /* Name.Function.Magic */ body .vc { color: #19177C } /* Name.Variable.Class */ body .vg { color: #19177C } /* Name.Variable.Global */ body .vi { color: #19177C } /* Name.Variable.Instance */ body .vm { color: #19177C } /* Name.Variable.Magic */ body .il { color: #666666 } /* Literal.Number.Integer.Long */ </style> </head> <body> <h2></h2> <div class="highlight"><pre><span></span><span class="k">print</span><span class="p">(</span><span class="s2">"Hello World"</span><span class="p">)</span> </pre></div> </body> </html>
fullオプションにさらにcssfileを追加してcssの指定を外部ファイルに出力
htmlファイルうを比較
cssファイル名の違い以外にdefault_cssfile.htmlとnobackground_cssfile.htmlの内容に違いは見られない。
default_cssfile.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <meta http-equiv="content-type" content="text/html; charset=None"> <link rel="stylesheet" href="default_cssfile.css" type="text/css"> </head> <body> <h2></h2> <div class="highlight"><pre><span></span><span class="k">print</span><span class="p">(</span><span class="s2">"Hello World"</span><span class="p">)</span> </pre></div> </body> </html>
nobackground_cssfile.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <meta http-equiv="content-type" content="text/html; charset=None"> <link rel="stylesheet" href="nobackground_cssfile.css" type="text/css"> </head> <body> <h2></h2> <div class="highlight"><pre><span></span><span class="k">print</span><span class="p">(</span><span class="s2">"Hello World"</span><span class="p">)</span> </pre></div> </body> </html>
cssファイルうを比較
default_cssfile.css
td.linenos { background-color: #f0f0f0; padding-right: 10px; } span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; } pre { line-height: 125%; } body .hll { background-color: #ffffcc } body .c { color: #408080; font-style: italic } /* Comment */ body .err { border: 1px solid #FF0000 } /* Error */ body .k { color: #008000; font-weight: bold } /* Keyword */ body .o { color: #666666 } /* Operator */ body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */ body .cm { color: #408080; font-style: italic } /* Comment.Multiline */ body .cp { color: #BC7A00 } /* Comment.Preproc */ body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */ body .c1 { color: #408080; font-style: italic } /* Comment.Single */ body .cs { color: #408080; font-style: italic } /* Comment.Special */ body .gd { color: #A00000 } /* Generic.Deleted */ body .ge { font-style: italic } /* Generic.Emph */ body .gr { color: #FF0000 } /* Generic.Error */ body .gh { color: #000080; font-weight: bold } /* Generic.Heading */ body .gi { color: #00A000 } /* Generic.Inserted */ body .go { color: #888888 } /* Generic.Output */ body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ body .gs { font-weight: bold } /* Generic.Strong */ body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ body .gt { color: #0044DD } /* Generic.Traceback */ body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ body .kp { color: #008000 } /* Keyword.Pseudo */ body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ body .kt { color: #B00040 } /* Keyword.Type */ body .m { color: #666666 } /* Literal.Number */ body .s { color: #BA2121 } /* Literal.String */ body .na { color: #7D9029 } /* Name.Attribute */ body .nb { color: #008000 } /* Name.Builtin */ body .nc { color: #0000FF; font-weight: bold } /* Name.Class */ body .no { color: #880000 } /* Name.Constant */ body .nd { color: #AA22FF } /* Name.Decorator */ body .ni { color: #999999; font-weight: bold } /* Name.Entity */ body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ body .nf { color: #0000FF } /* Name.Function */ body .nl { color: #A0A000 } /* Name.Label */ body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ body .nt { color: #008000; font-weight: bold } /* Name.Tag */ body .nv { color: #19177C } /* Name.Variable */ body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ body .w { color: #bbbbbb } /* Text.Whitespace */ body .mb { color: #666666 } /* Literal.Number.Bin */ body .mf { color: #666666 } /* Literal.Number.Float */ body .mh { color: #666666 } /* Literal.Number.Hex */ body .mi { color: #666666 } /* Literal.Number.Integer */ body .mo { color: #666666 } /* Literal.Number.Oct */ body .sa { color: #BA2121 } /* Literal.String.Affix */ body .sb { color: #BA2121 } /* Literal.String.Backtick */ body .sc { color: #BA2121 } /* Literal.String.Char */ body .dl { color: #BA2121 } /* Literal.String.Delimiter */ body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ body .s2 { color: #BA2121 } /* Literal.String.Double */ body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ body .sh { color: #BA2121 } /* Literal.String.Heredoc */ body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ body .sx { color: #008000 } /* Literal.String.Other */ body .sr { color: #BB6688 } /* Literal.String.Regex */ body .s1 { color: #BA2121 } /* Literal.String.Single */ body .ss { color: #19177C } /* Literal.String.Symbol */ body .bp { color: #008000 } /* Name.Builtin.Pseudo */ body .fm { color: #0000FF } /* Name.Function.Magic */ body .vc { color: #19177C } /* Name.Variable.Class */ body .vg { color: #19177C } /* Name.Variable.Global */ body .vi { color: #19177C } /* Name.Variable.Instance */ body .vm { color: #19177C } /* Name.Variable.Magic */ body .il { color: #666666 } /* Literal.Number.Integer.Long */
nobackground_cssfile.css
default_cssfile.cssにあったhtmlヘッダのstlyタグ内のbody { background: #f8f8f8; }
の行が削除されている。
td.linenos { background-color: #f0f0f0; padding-right: 10px; } span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; } pre { line-height: 125%; } body .hll { background-color: #ffffcc } body .c { color: #408080; font-style: italic } /* Comment */ body .err { border: 1px solid #FF0000 } /* Error */ body .k { color: #008000; font-weight: bold } /* Keyword */ body .o { color: #666666 } /* Operator */ body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */ body .cm { color: #408080; font-style: italic } /* Comment.Multiline */ body .cp { color: #BC7A00 } /* Comment.Preproc */ body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */ body .c1 { color: #408080; font-style: italic } /* Comment.Single */ body .cs { color: #408080; font-style: italic } /* Comment.Special */ body .gd { color: #A00000 } /* Generic.Deleted */ body .ge { font-style: italic } /* Generic.Emph */ body .gr { color: #FF0000 } /* Generic.Error */ body .gh { color: #000080; font-weight: bold } /* Generic.Heading */ body .gi { color: #00A000 } /* Generic.Inserted */ body .go { color: #888888 } /* Generic.Output */ body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ body .gs { font-weight: bold } /* Generic.Strong */ body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ body .gt { color: #0044DD } /* Generic.Traceback */ body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ body .kp { color: #008000 } /* Keyword.Pseudo */ body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ body .kt { color: #B00040 } /* Keyword.Type */ body .m { color: #666666 } /* Literal.Number */ body .s { color: #BA2121 } /* Literal.String */ body .na { color: #7D9029 } /* Name.Attribute */ body .nb { color: #008000 } /* Name.Builtin */ body .nc { color: #0000FF; font-weight: bold } /* Name.Class */ body .no { color: #880000 } /* Name.Constant */ body .nd { color: #AA22FF } /* Name.Decorator */ body .ni { color: #999999; font-weight: bold } /* Name.Entity */ body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ body .nf { color: #0000FF } /* Name.Function */ body .nl { color: #A0A000 } /* Name.Label */ body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ body .nt { color: #008000; font-weight: bold } /* Name.Tag */ body .nv { color: #19177C } /* Name.Variable */ body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ body .w { color: #bbbbbb } /* Text.Whitespace */ body .mb { color: #666666 } /* Literal.Number.Bin */ body .mf { color: #666666 } /* Literal.Number.Float */ body .mh { color: #666666 } /* Literal.Number.Hex */ body .mi { color: #666666 } /* Literal.Number.Integer */ body .mo { color: #666666 } /* Literal.Number.Oct */ body .sa { color: #BA2121 } /* Literal.String.Affix */ body .sb { color: #BA2121 } /* Literal.String.Backtick */ body .sc { color: #BA2121 } /* Literal.String.Char */ body .dl { color: #BA2121 } /* Literal.String.Delimiter */ body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ body .s2 { color: #BA2121 } /* Literal.String.Double */ body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ body .sh { color: #BA2121 } /* Literal.String.Heredoc */ body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ body .sx { color: #008000 } /* Literal.String.Other */ body .sr { color: #BB6688 } /* Literal.String.Regex */ body .s1 { color: #BA2121 } /* Literal.String.Single */ body .ss { color: #19177C } /* Literal.String.Symbol */ body .bp { color: #008000 } /* Name.Builtin.Pseudo */ body .fm { color: #0000FF } /* Name.Function.Magic */ body .vc { color: #19177C } /* Name.Variable.Class */ body .vg { color: #19177C } /* Name.Variable.Global */ body .vi { color: #19177C } /* Name.Variable.Instance */ body .vm { color: #19177C } /* Name.Variable.Magic */ body .il { color: #666666 } /* Literal.Number.Integer.Long */