Apply Pygments
pyg
pygments
.*
.html
.tex
.svg
.txt
.png
.bmp
.gif
.jpg
.css
.sty
Setting | Description | Default |
---|---|---|
add-new-files | Boolean or list of extensions/patterns to match. | False |
added-in-version | Dexy version when this filter was first available. | |
additional-doc-filters | Filters to apply to additional documents created as side effects. | {} |
additional-doc-settings | Settings to apply to additional documents created as side effects. | {} |
allow-unknown-ext | Whether to allow unknown file extensions to be parsed with the TextLexer by default instead of raising an exception. | True |
allow-unprintable-input | Whether to allow unprintable input to be replaced with dummy text instead of raising an exception. | True |
data-type | Alias of custom data class to use to store filter output. | generic |
examples | Templates which should be used as examples for this filter. | ['pygments', 'pygments-image', 'pygments-stylesheets'] |
exclude-add-new-files | List of patterns to skip even if they match add-new-files. | [] |
exclude-new-files-from-dir | List of directories to skip when adding new files. | [] |
ext | File extension to output. | None |
extension-map | Dictionary mapping input extensions to default output extensions. | None |
formatter-settings | List of all settings which will be passed to the formatter constructor. | ['style', 'full', 'linenos', 'noclasses'] |
full | Pygments formatter option: output a 'full' document including header/footer tags. | None |
help | Helpstring for plugin. | Apply Pygments |
input-extensions | List of extensions which this filter can accept as input. | ['.*'] |
keep-originals | Whether, if additional-doc-filters are specified, the original unmodified docs should also be added. | False |
lexer | The name of the pygments lexer to use (will normally be determined automatically, only use this if you need to override the default setting or your filename isn't mapped to the lexer you want to use. | None |
lexer-args | Dictionary of custom arguments to be passed directly to the lexer. | {} |
lexer-settings | List of all settings which will be passed to the lexer constructor. | [] |
line-numbers | Alternative name for 'linenos'. | None |
linenos | Whether to include line numbers. May be set to 'table' or 'inline'. | None |
mkdir | A directory which should be created in working dir. | None |
mkdirs | A list of directories which should be created in working dir. | [] |
noclasses | If set to true, token tags will not use CSS classes, but inline styles. | None |
nodoc | Whether filter should be excluded from documentation. | False |
output | Whether to output results of this filter by default by reporters such as 'output' or 'website'. | False |
output-extensions | List of extensions which this filter can produce as output. | ['.html', '.tex', '.svg', '.txt', '.png', '.bmp', '.gif', '.jpg', '.css', '.sty'] |
override-workspace-exclude-filters | If True, document will be populated to other workspaces ignoring workspace-exclude-filters. | False |
preserve-prior-data-class | Whether output data class should be set to match the input data class. | False |
require-output | Should dexy raise an exception if no output is produced by this filter? | True |
style | Formatter style to output. | default |
tags | Tags which describe the filter. | [] |
unprintable-input-text | Dummy text to use instead of unprintable binary input. | not printable |
variables | A dictionary of variable names and values to make available to this filter. | {} |
vars | A dictionary of variable names and values to make available to this filter. | {} |
workspace-exclude-filters | Filters whose output should be excluded from workspace. | ['pyg'] |
workspace-includes | If set to a list of filenames or extensions, only these will be populated to working dir. | None |
The pygments filter applies syntax highlighting to source code.
Here is some raw source code:
print "Hello!"
And here is how we specify in dexy.yaml that the pyg filter should be applied to this file:
- hello.py|pyg
The default format is HTML, so the code will come out looking like this, with HTML markup applied:
<div class="highlight"><pre><span></span><a name="hello.py-pyg.html-1"></a><span class="nb">print</span> <span class="s2">"Hello!"</span> </pre></div>
If we follow the pyg filter with the l filter (which does nothing, but it only accepts input of documents with .tex extension):
- hello.py|pyg|l
then pygments will output LaTeX markup instead:
\begin{Verbatim}[commandchars=\\\{\}] \PY{n+nb}{print} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Hello!}\PY{l+s+s2}{\PYZdq{}} \end{Verbatim}
Pygments will automatically determine which lexer to use based on the file extension of the incoming file. In the above examples, the .py file extension tells pygments that it should use the python lexer. If you need a different lexer than the default for the file extension, then you can pass a custom lexer argument:
- hello.txt|pyg: - pyg: { 'lexer' : 'python' }
You can see a list of all the available lexers, the file extensions they correspond to, and the aliases you can use to specify them, as follows:
$ pygmentize -L lexers Pygments version 2.6.1, (c) 2006-2019 by Georg Brandl. Lexers: ~~~~~~~ * abap: ABAP (filenames *.abap, *.ABAP) * abnf: ABNF (filenames *.abnf) * ada, ada95, ada2005: Ada (filenames *.adb, *.ads, *.ada) * adl: ADL (filenames *.adl, *.adls, *.adlf, *.adlx) * agda: Agda (filenames *.agda)
Dexy adds some custom file extension -> lexer mappings of its own, such as .pycon and .rbcon for python console and ruby console transcripts.
Custom formatter options can also be passed. Note that these options may be different for the HTMLFormatter and, say, the LatexFormatter. For information consult the Pygments Documentation for the formatter you are using. Here is an example of setting the noclasses attribute to True:
- hello.py|pyg|-noclasses: - pyg: { 'noclasses' : True }
And here is the resulting HTML:
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><a name="hello.py-pyg.html-1"></a><span style="color: #008000">print</span> <span style="color: #BA2121">"Hello!"</span> </pre></div>
As a reminder here is the default HTML again:
<div class="highlight"><pre><span></span><a name="hello.py-pyg.html-1"></a><span class="nb">print</span> <span class="s2">"Hello!"</span> </pre></div>
It is possible to generate image files from source code. Here is how to tell pygments to output a jpg:
- hello.py|pyg|jn
And here's a png, this time without line numbers and using a custom font:
- hello.py|pyg|pn: - pyg: { 'line_numbers' : False, 'font_name' : 'Source Code Pro' }
Here's a gif with the bw (black and white) style:
- hello.py|pyg|gn: - pyg: { 'style' : 'bw' }
Consult the Pygments Documentation for details about other options you can pass to these formatters.
The pygments filter applies syntax highlighting to source code. For HTML and LaTeX, the default pygments output wraps the code with a class or macro, and a stylesheet needs to be applied to actually colorize the output.
You can use the pygmentize command line tool to list available styles:
$ pygmentize -L styles Pygments version 2.6.1, (c) 2006-2019 by Georg Brandl. Styles: ~~~~~~~ * default: The default style (inspired by Emacs 22). * emacs: The default style (inspired by Emacs 22). * friendly: A modern style based on the VIM pyte theme. * colorful: A colorful style, inspired by CodeRay. * autumn: A colorful style, inspired by the terminal highlighting style.
to generate a CSS stylesheet for use with HTML:
$ pygmentize -S manni -f html > manni.css $ head -n 5 manni.css .hll { background-color: #ffffcc } .c { color: #0099FF; font-style: italic } /* Comment */ .err { color: #AA0000; background-color: #FFAAAA } /* Error */ .k { color: #006699; font-weight: bold } /* Keyword */ .o { color: #555555 } /* Operator */
or to generate a .sty file for use with LaTeX:
$ pygmentize -S manni -f latex > manni.sty $ head -n 5 manni.sty \makeatletter \def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% \let\PY@ul=\relax \let\PY@tc=\relax% \let\PY@bc=\relax \let\PY@ff=\relax}
The pygments filter also has some built-in ways to get stylesheets in your project.
Any blank file ending in .css or .sty passed through the pyg filter will have stylesheet contents generated if the final output extension of the file is also set to .css or .sty:
- pastie.css|pyg: - pyg: { 'ext' : '.css' } - contents: ' '
Here is an excerpt from the generated stylesheet:
.hll { background-color: #ffffcc } .c { color: #408080; font-style: italic } /* Comment */ .err { border: 1px solid #FF0000 } /* Error */ .k { color: #008000; font-weight: bold } /* Keyword */ .o { color: #666666 } /* Operator */ .ch { color: #408080; font-style: italic } /* Comment.Hashbang */ .cm { color: #408080; font-style: italic } /* Comment.Multiline */ .cp { color: #BC7A00 } /* Comment.Preproc */ .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */ .c1 { color: #408080; font-style: italic } /* Comment.Single */
Here is a LaTeX version:
- pastie.sty|pyg: - pyg: { 'ext' : '.sty' } - contents: ' '
Here is an excerpt from the generated .sty file:
\makeatletter \def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% \let\PY@ul=\relax \let\PY@tc=\relax% \let\PY@bc=\relax \let\PY@ff=\relax} \def\PY@tok#1{\csname PY@tok@#1\endcsname} \def\PY@toks#1+{\ifx\relax#1\empty\else% \PY@tok{#1}\expandafter\PY@toks\fi} \def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% \PY@it{\PY@bf{\PY@ff{#1}}}}}}}
You can also insert style definitions directly into the header of a document. Jinja has a pygments object which contains entries for each of the avaliable pygments styles, for html or latex.
Here's an example of a HTML file which incorporates CSS and also lists the available styles:
<html> <head> <style type="text/css"> {{ pygments['pastie.css'] }} </style> </head> <body> <p>Here is a list of all the available stylesheets:</p> <ul> {% for k in sorted(pygments) -%} <li>{{ k }}</li> {% endfor -%} </ul> </body> </html>
This is run by passing the html through the jinja filter:
- example.html|jinja
Here is the resulting HTML:
<html> <head> <style type="text/css"> .hll { background-color: #ffffcc } .c { color: #888888 } /* Comment */ .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .k { color: #008800; font-weight: bold } /* Keyword */ .ch { color: #888888 } /* Comment.Hashbang */ .cm { color: #888888 } /* Comment.Multiline */ .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .cpf { color: #888888 } /* Comment.PreprocFile */ .c1 { color: #888888 } /* Comment.Single */ .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .ge { font-style: italic } /* Generic.Emph */ .gr { color: #aa0000 } /* Generic.Error */ .gh { color: #333333 } /* Generic.Heading */ .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .go { color: #888888 } /* Generic.Output */ .gp { color: #555555 } /* Generic.Prompt */ .gs { font-weight: bold } /* Generic.Strong */ .gu { color: #666666 } /* Generic.Subheading */ .gt { color: #aa0000 } /* Generic.Traceback */ .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .kp { color: #008800 } /* Keyword.Pseudo */ .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .na { color: #336699 } /* Name.Attribute */ .nb { color: #003388 } /* Name.Builtin */ .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .no { color: #003366; font-weight: bold } /* Name.Constant */ .nd { color: #555555 } /* Name.Decorator */ .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .nl { color: #336699; font-style: italic } /* Name.Label */ .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .py { color: #336699; font-weight: bold } /* Name.Property */ .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .nv { color: #336699 } /* Name.Variable */ .ow { color: #008800 } /* Operator.Word */ .w { color: #bbbbbb } /* Text.Whitespace */ .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .bp { color: #003388 } /* Name.Builtin.Pseudo */ .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .vc { color: #336699 } /* Name.Variable.Class */ .vg { color: #dd7700 } /* Name.Variable.Global */ .vi { color: #3333bb } /* Name.Variable.Instance */ .vm { color: #336699 } /* Name.Variable.Magic */ .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ </style> </head> <body> <p>Here is a list of all the available stylesheets:</p> <ul> <li>abap.css</li> <li>abap.html</li> <li>abap.tex</li> <li>algol.css</li> <li>algol.html</li> <li>algol.tex</li> <li>algol_nu.css</li> <li>algol_nu.html</li> <li>algol_nu.tex</li> <li>arduino.css</li> <li>arduino.html</li> <li>arduino.tex</li> <li>autumn.css</li> <li>autumn.html</li> <li>autumn.tex</li> <li>borland.css</li> <li>borland.html</li> <li>borland.tex</li> <li>bw.css</li> <li>bw.html</li> <li>bw.tex</li> <li>colorful.css</li> <li>colorful.html</li> <li>colorful.tex</li> <li>default.css</li> <li>default.html</li> <li>default.tex</li> <li>emacs.css</li> <li>emacs.html</li> <li>emacs.tex</li> <li>friendly.css</li> <li>friendly.html</li> <li>friendly.tex</li> <li>fruity.css</li> <li>fruity.html</li> <li>fruity.tex</li> <li>igor.css</li> <li>igor.html</li> <li>igor.tex</li> <li>inkpot.css</li> <li>inkpot.html</li> <li>inkpot.tex</li> <li>lovelace.css</li> <li>lovelace.html</li> <li>lovelace.tex</li> <li>manni.css</li> <li>manni.html</li> <li>manni.tex</li> <li>monokai.css</li> <li>monokai.html</li> <li>monokai.tex</li> <li>murphy.css</li> <li>murphy.html</li> <li>murphy.tex</li> <li>native.css</li> <li>native.html</li> <li>native.tex</li> <li>paraiso-dark.css</li> <li>paraiso-dark.html</li> <li>paraiso-dark.tex</li> <li>paraiso-light.css</li> <li>paraiso-light.html</li> <li>paraiso-light.tex</li> <li>pastie.css</li> <li>pastie.html</li> <li>pastie.tex</li> <li>perldoc.css</li> <li>perldoc.html</li> <li>perldoc.tex</li> <li>rainbow_dash.css</li> <li>rainbow_dash.html</li> <li>rainbow_dash.tex</li> <li>rrt.css</li> <li>rrt.html</li> <li>rrt.tex</li> <li>sas.css</li> <li>sas.html</li> <li>sas.tex</li> <li>solarized-dark.css</li> <li>solarized-dark.html</li> <li>solarized-dark.tex</li> <li>solarized-light.css</li> <li>solarized-light.html</li> <li>solarized-light.tex</li> <li>stata-dark.css</li> <li>stata-dark.html</li> <li>stata-dark.tex</li> <li>stata-light.css</li> <li>stata-light.html</li> <li>stata-light.tex</li> <li>stata.css</li> <li>stata.html</li> <li>stata.tex</li> <li>tango.css</li> <li>tango.html</li> <li>tango.tex</li> <li>trac.css</li> <li>trac.html</li> <li>trac.tex</li> <li>vim.css</li> <li>vim.html</li> <li>vim.tex</li> <li>vs.css</li> <li>vs.html</li> <li>vs.tex</li> <li>xcode.css</li> <li>xcode.html</li> <li>xcode.tex</li> </ul> </body> </html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | class PygmentsFilter(DexyFilter):
"""
Apply Pygments <http://pygments.org/> syntax highlighting.
Image output formats require PIL to be installed.
"""
aliases = ['pyg', 'pygments']
IMAGE_OUTPUT_EXTENSIONS = ['.png', '.bmp', '.gif', '.jpg']
MARKUP_OUTPUT_EXTENSIONS = [".html", ".tex", ".svg", ".txt"] # make sure .html is first so it is default output format
LEXER_ERR_MSG = """Pygments doesn't know how to syntax highlight files like '%s' (for '%s').\
You might need to specify the lexer manually."""
_settings = {
'examples' : ['pygments', 'pygments-image', 'pygments-stylesheets'],
'input-extensions' : [".*"],
'output-extensions' : MARKUP_OUTPUT_EXTENSIONS + IMAGE_OUTPUT_EXTENSIONS + ['.css', '.sty'],
'lexer' : ("""The name of the pygments lexer to use (will normally
be determined automatically, only use this if you need to override
the default setting or your filename isn't mapped to the lexer you
want to use.""", None),
'allow-unknown-ext' : ("""Whether to allow unknown file extensions
to be parsed with the TextLexer by default instead of raising
an exception.""", True),
'allow-unprintable-input' : ("""Whether to allow unprintable input
to be replaced with dummy text instead of raising an exception.""",
True),
'unprintable-input-text' : ("""Dummy text to use instead of
unprintable binary input.""", 'not printable'),
'lexer-args' : (
"Dictionary of custom arguments to be passed directly to the lexer.",
{}
),
'lexer-settings' : (
"List of all settings which will be passed to the lexer constructor.",
[]
),
'formatter-settings' : (
"""List of all settings which will be passed to the formatter
constructor.""", ['style', 'full', 'linenos', 'noclasses']
),
'style' : ( "Formatter style to output.", 'default'),
'noclasses' : ( "If set to true, token <span> tags will not use CSS classes, but inline styles.", None),
'full' : ("""Pygments formatter option: output a 'full' document
including header/footer tags.""", None),
'linenos' : ("""Whether to include line numbers. May be set to
'table' or 'inline'.""", None),
'line-numbers' : ("""Alternative name for 'linenos'.""", None),
}
lexer_cache = {}
def data_class_alias(klass, file_ext):
if file_ext in klass.MARKUP_OUTPUT_EXTENSIONS:
return 'sectioned'
else:
return 'generic'
def docmd_css(klass, style='default'):
"""
Prints out CSS for the specified style.
"""
print(klass.generate_css(style))
def docmd_sty(klass, style='default'):
"""
Prints out .sty file (latex) for the specified style.
"""
print(klass.generate_sty(style))
def generate_css(self, style='default'):
formatter = HtmlFormatter(style=style)
return formatter.get_style_defs()
def generate_sty(self, style='default'):
formatter = LatexFormatter(style=style)
return formatter.get_style_defs()
def calculate_canonical_name(self):
ext = self.prev_ext
if ext in [".css", ".sty"] and self.ext == ext:
return self.doc.name
elif self.alias == 'htmlsections':
name_without_ext = posixpath.splitext(self.doc.name)[0]
return "%s%s" % (name_without_ext, self.ext)
else:
return "%s%s" % (self.doc.name, self.ext)
def constructor_args(self, constructor_type, custom_args=None):
if custom_args:
args = custom_args
else:
args = {}
for argname in self.setting("%s-settings" % constructor_type):
if self.setting(argname):
args[argname] = self.setting(argname)
return args
def lexer_alias(self, ext):
if self.setting('lexer'):
self.log_debug("custom lexer %s specified" % self.setting('lexer'))
return self.setting('lexer')
is_json_file = ext in ('.json', '.dexy') or self.output_data.name.endswith(".dexy")
if is_json_file and (pygments.__version__ < '1.5'):
return "javascript"
elif is_json_file:
return "json"
if ext == '.Makefile' or (ext == '' and 'Makefile' in self.input_data.name):
return 'makefile'
try:
return file_ext_to_lexer_alias_cache[ext]
except KeyError:
pass
def create_lexer_instance(self):
ext = self.prev_ext
lexer_alias = self.lexer_alias(ext)
lexer_args = self.constructor_args('lexer')
lexer_args.update(self.setting('lexer-args'))
if not lexer_alias:
msg = self.LEXER_ERR_MSG
msgargs = (self.input_data.name, self.key)
if self.setting('allow-unknown-ext'):
self.log_warn(msg % msgargs)
lexer_alias = 'text'
else:
raise dexy.exceptions.UserFeedback(msg % msgargs)
if lexer_alias in pygments_lexer_cache and not lexer_args:
return pygments_lexer_cache[lexer_alias]
else:
lexer = get_lexer_by_name(lexer_alias, **lexer_args)
if not lexer_args:
pygments_lexer_cache[lexer_alias] = lexer
return lexer
return lexer
def create_formatter_instance(self):
if self.setting('line-numbers') and not self.setting('linenos'):
self.update_settings({'linenos' : self.setting('line-numbers')})
formatter_args = self.constructor_args('formatter', {
'lineanchors' : self.output_data.web_safe_document_key() })
if self.setting('style') and not 'style' in formatter_args:
formatter_args['style'] = self.setting('style')
self.log_debug("creating pygments formatter with args %s" % (formatter_args))
return get_formatter_for_filename(self.output_data.name, **formatter_args)
def process(self):
if self.ext in self.IMAGE_OUTPUT_EXTENSIONS:
try:
import PIL
PIL # because pyflakes
except ImportError:
print("python imaging library is required by pygments to create image output")
raise dexy.exceptions.InactivePlugin('pyg')
ext = self.prev_ext
if ext in [".css", ".sty"] and self.ext == ext:
# Special case if we get a virtual empty file, generate style file
self.log_debug("creating a style file in %s" % self.key)
if ext == '.css':
output = self.generate_css(self.setting('style'))
elif ext == '.sty':
output = self.generate_sty(self.setting('style'))
else:
msg = "pyg filter doesn't know how to generate a stylesheet for %s extension"
msgargs = (ext)
raise dexy.commands.UserFeedback(msg % msgargs)
self.output_data.set_data(output)
self.update_all_args({'override-workspace-exclude-filters' : True })
else:
lexer = self.create_lexer_instance()
if self.ext in self.IMAGE_OUTPUT_EXTENSIONS:
# Place each section into an image.
for k, v in self.input_data.items():
formatter = self.create_formatter_instance()
output_for_section = highlight(str(v), lexer, formatter)
new_doc_name = "%s--%s%s" % (self.doc.key.replace("|", "--"), k, self.ext)
self.add_doc(new_doc_name, output_for_section)
# Place entire contents into main file.
formatter = self.create_formatter_instance()
self.update_all_args({'override-workspace-exclude-filters' : True })
with open(self.output_filepath(), 'wb') as f:
f.write(highlight(str(self.input_data), lexer, formatter))
else:
formatter = self.create_formatter_instance()
for section_name, section_input in self.input_data.items():
try:
if isinstance(section_input, str):
section_input = section_input
else:
# If section_input is an instance of
# SectionValue then calling 'str' on this
# instance will call the __str__ method of
# SectionValue.
section_input = str(section_input)
section_output = highlight(section_input, lexer, formatter)
except UnicodeDecodeError:
if self.setting('allow-unprintable-input'):
section_input = self.setting('unprintable-input-text')
section_output = highlight(section_input, lexer, formatter)
else:
raise
self.output_data[section_name] = section_output
self.output_data.save()
|
Content © 2020 Dr. Ana Nelson | Site Design © Copyright 2011 Andre Gagnon | All Rights Reserved.