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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<?asciidoc-toc?>
<?asciidoc-numbered?>
<article lang="en">
<articleinfo>
<title>Source Code Highlight Filter</title>
</articleinfo>
<simpara>The AsciiDoc distribution includes a <emphasis>source</emphasis> filter for highlighting
code syntax.</simpara>
<section id="_docbook_outputs">
<title>DocBook Outputs</title>
<simpara>AsciiDoc encloses the source code in a DocBook <emphasis>programlisting</emphasis>
element and leaves source code highlighting to the DocBook toolchain
(dblatex has a particularly nice programlisting highlighter). The
DocBook programlisting element is assigned two attributes:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>
The <emphasis>language</emphasis> attribute is set to the AsciiDoc <emphasis>language</emphasis>
attribute.
</simpara>
</listitem>
<listitem>
<simpara>
The <emphasis>linenumbering</emphasis> attribute is set to the AsciiDoc <emphasis>src_numbered</emphasis>
attribute (<emphasis>numbered</emphasis> or <emphasis>unnumbered</emphasis>).
</simpara>
</listitem>
</orderedlist>
</section>
<section id="_html_outputs">
<title>HTML Outputs</title>
<simpara>You have the choice of three HTML source code highlighters, your
selection is determined by the <emphasis>source-highlighter</emphasis> attribute (defaults
to <emphasis>source-highlight</emphasis>):</simpara>
<note><simpara>Set the <emphasis>source-highlighter</emphasis> attribute from the <literal>asciidoc(1)</literal>
command-line or in the document header (not in the document body,
because the configuration file conditional macros are processed at
load time).</simpara></note>
<section id="_gnu_source_highlight">
<title>GNU Source Highlight</title>
<simpara>The default highlighter is the
<ulink url="https://www.gnu.org/software/src-highlite/">GNU source-highlight</ulink> which
can highlight <emphasis>html4</emphasis>, <emphasis>html5</emphasis> and <emphasis>xhtml11</emphasis> outputs. The GNU
source-highlight must be installed and the <emphasis>source-highlight</emphasis> command
must reside in the shell search <emphasis>PATH</emphasis>.</simpara>
</section>
<section id="_highlight">
<title>Highlight</title>
<simpara>You can use
<ulink url="http://www.andre-simon.de/doku/highlight/en/highlight.html">Highlight</ulink>
syntax highlighter for <emphasis>xhtml11</emphasis>, <emphasis>html5</emphasis> and <emphasis>html4</emphasis> outputs (set the
<emphasis>source-highlighter</emphasis> attribute to <emphasis>highlighter</emphasis>).</simpara>
<itemizedlist>
<listitem>
<simpara>
The <emphasis>highlight</emphasis> command must reside in the shell search <emphasis>PATH</emphasis>.
</simpara>
</listitem>
<listitem>
<simpara>
To make Highlighter your default highlighter put the following line
your <literal>~/.asciidoc/asciidoc.conf</literal> file:
</simpara>
<literallayout class="monospaced">source-highlighter=highlight</literallayout>
</listitem>
<listitem>
<simpara>
The AsciiDoc <emphasis>encoding</emphasis> attribute is passed to Highlighter using the
<literal>--encoding</literal> command-line option.
</simpara>
</listitem>
</itemizedlist>
</section>
<section id="_pygments">
<title>Pygments</title>
<simpara>The <ulink url="https://pygments.org/">Pygments</ulink> syntax highlighter can be used for
<emphasis>xhtml11</emphasis> and <emphasis>html5</emphasis> outputs (set the <emphasis>source-highlighter</emphasis> attribute
to <emphasis>pygments</emphasis>).</simpara>
<itemizedlist>
<listitem>
<simpara>
The <emphasis>pygmentize</emphasis> command must reside in the shell search <emphasis>PATH</emphasis>.
</simpara>
</listitem>
<listitem>
<simpara>
You can customize Pygments CSS styles by editing
<literal>./stylesheets/pygments.css</literal>. The <literal>pygments.css</literal> CSS file was
generated with:
</simpara>
<literallayout class="monospaced">from pygments.formatters import HtmlFormatter
print HtmlFormatter().get_style_defs('.highlight')</literallayout>
</listitem>
<listitem>
<simpara>
To make Pygments your default highlighter put the following line
your <literal>~/.asciidoc/asciidoc.conf</literal> file:
</simpara>
<literallayout class="monospaced">source-highlighter=pygments</literallayout>
</listitem>
<listitem>
<simpara>
The AsciiDoc <emphasis>encoding</emphasis> attribute is passed to Pygments using the
<literal>-O</literal> command-line option.
</simpara>
</listitem>
</itemizedlist>
</section>
</section>
<section id="_block_attributes">
<title>Block attributes</title>
<simpara>The following attributes can be included in source code block
attribute lists.</simpara>
<itemizedlist>
<listitem>
<simpara>
<emphasis>style</emphasis> and <emphasis>language</emphasis> are mandatory.
</simpara>
</listitem>
<listitem>
<simpara>
<emphasis>style</emphasis>, <emphasis>language</emphasis> and <emphasis>src_numbered</emphasis> are the first three
positional attributes in that order.
</simpara>
</listitem>
<listitem>
<simpara>
The <emphasis>args</emphasis> attribute allows the inclusion of arbitrary (highlighter
dependent) command options.
</simpara>
</listitem>
</itemizedlist>
<variablelist>
<varlistentry>
<term>
style
</term>
<listitem>
<simpara>
Set to <emphasis>source</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
language
</term>
<listitem>
<simpara>
The source code language name.
</simpara>
<note><simpara>The language names vary between highlighters — consult the
selected highlighter manual.</simpara></note>
</listitem>
</varlistentry>
<varlistentry>
<term>
src_numbered
</term>
<listitem>
<simpara>
Set to <emphasis>numbered</emphasis> to include line numbers.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
src_tab
</term>
<listitem>
<simpara>
Set tab size (GNU source-highlight only).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
args
</term>
<listitem>
<simpara>
Include this attribute value in the highlighter command-line (HTML
outputs) or in the <literal>programlisting</literal> element (DocBook).
</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section id="_testing">
<title>Testing</title>
<simpara>Test the filter by converting the test file to HTML with AsciiDoc:</simpara>
<literallayout class="monospaced">$ asciidoc -v ./filters/source/source-highlight-filter-test.txt
$ firefox ./filters/source/source-highlight-filter-test.html &</literallayout>
</section>
<section id="_examples">
<title>Examples</title>
<section id="_source_code_paragraphs">
<title>Source code paragraphs</title>
<simpara>The <literal>source</literal> paragraph style will highlight a paragraph of source
code. These three code paragraphs:</simpara>
<screen>[source,python]
if n < 0: print 'Hello World!'
:language: python
[source]
if n < 0: print 'Hello World!'
[source,ruby,numbered]
[true, false].cycle([0, 1, 2, 3, 4]) do |a, b|
puts "#{a.inspect} => #{b.inspect}"</screen>
<simpara>Render this highlighted source code:</simpara>
<programlisting language="python" linenumbering="unnumbered">if n < 0: print 'Hello World!'</programlisting>
<programlisting language="python" linenumbering="unnumbered">if n < 0: print 'Hello World!'</programlisting>
<programlisting language="ruby" linenumbering="numbered">[true, false].cycle([0, 1, 2, 3, 4]) do |a, b|
puts "#{a.inspect} => #{b.inspect}"</programlisting>
</section>
<section id="_unnumbered_source_code_listing">
<title>Unnumbered source code listing</title>
<simpara>This source-highlight filtered block:</simpara>
<screen> [source,python]
---------------------------------------------------------------------
''' A multi-line
comment.'''
def sub_word(mo):
''' Single line comment.'''
word = mo.group('word') # Inline comment
if word in keywords[language]:
return quote + word + quote
else:
return word
---------------------------------------------------------------------</screen>
<simpara>Renders this highlighted source code:</simpara>
<programlisting language="python" linenumbering="unnumbered">''' A multi-line
comment.'''
def sub_word(mo):
''' Single line comment.'''
word = mo.group('word') # Inline comment
if word in keywords[language]:
return quote + word + quote
else:
return word</programlisting>
</section>
<section id="_numbered_source_code_listing_with_callouts">
<title>Numbered source code listing with callouts</title>
<simpara>This source-highlight filtered block:</simpara>
<screen> [source,ruby,numbered]
---------------------------------------------------------------------
#
# Useful Ruby base class extensions.
#
class Array
# Execute a block passing it corresponding items in
# +self+ and +other_array+.
# If self has less items than other_array it is repeated.
def cycle(other_array) # :yields: item, other_item
other_array.each_with_index do |item, index|
yield(self[index % self.length], item)
end
end
end
if $0 == __FILE__ # <1>
# Array#cycle test
# true => 0
# false => 1
# true => 2
# false => 3
# true => 4
puts 'Array#cycle test' # <2>
[true, false].cycle([0, 1, 2, 3, 4]) do |a, b|
puts "#{a.inspect} => #{b.inspect}"
end
end
---------------------------------------------------------------------
<1> First callout.
<2> Second callout.</screen>
<simpara>Renders this highlighted source code:</simpara>
<programlisting language="ruby" linenumbering="numbered">#
# Useful Ruby base class extensions.
#
class Array
# Execute a block passing it corresponding items in
# +self+ and +other_array+.
# If self has less items than other_array it is repeated.
def cycle(other_array) # :yields: item, other_item
other_array.each_with_index do |item, index|
yield(self[index % self.length], item)
end
end
end
if $0 == __FILE__ # <co id="CO1-1"/>
# Array#cycle test
# true => 0
# false => 1
# true => 2
# false => 3
# true => 4
puts 'Array#cycle test' # <co id="CO1-2"/>
[true, false].cycle([0, 1, 2, 3, 4]) do |a, b|
puts "#{a.inspect} => #{b.inspect}"
end
end</programlisting>
<calloutlist>
<callout arearefs="CO1-1">
<para>
First callout.
</para>
</callout>
<callout arearefs="CO1-2">
<para>
Second callout.
</para>
</callout>
</calloutlist>
<tip>
<itemizedlist>
<listitem>
<simpara>
If the source <emphasis>language</emphasis> attribute has been set (using an
<emphasis>AttributeEntry</emphasis> or from the command-line) you don’t have to specify
it in each source code block.
</simpara>
</listitem>
<listitem>
<simpara>
You should place callout markers inside source code comments to
ensure they are not misinterpreted and mangled by the highlighter.
</simpara>
</listitem>
</itemizedlist>
</tip>
</section>
</section>
</article>
|