summaryrefslogtreecommitdiff
path: root/source-highlight-filter.html
blob: 07149627550909a6f5991e769cf61a0c297664db (plain)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="generator" content="AsciiDoc 9.0.4" />
<title>Source Code Highlight Filter</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css" />
<link rel="stylesheet" href="./layout2.css" type="text/css" />
<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
</head>
<body style="max-width:70em">
<div id="layout-menu-box">
<div id="layout-menu">
  <div>&#187;<a href="index.html">Home</a></div>
  <div>&#187;<a href="userguide.html">User&nbsp;Guide</a></div>
  <div>&#187;<a href="INSTALL.html">Installation</a></div>
  <div>&#187;<a href="faq.html">FAQ</a></div>
  <div>&#187;<a href="manpage.html">asciidoc(1)</a></div>
  <div>&#187;<a href="a2x.1.html">a2x(1)</a></div>
  <div>&#187;<a href="asciidocapi.html">API</a></div>
  <div>&#187;<a href="plugins.html">Plugins</a></div>
  <div>&#187;<a href="http://powerman.name/doc/asciidoc">Cheatsheet</a></div>
  <div>&#187;<a href="testasciidoc.html">Tests</a></div>
  <div>&#187;<a href="CHANGELOG.html">ChangeLog</a></div>
  <div>&#187;<a href="support.html">Support</a></div>
  <div id="page-source">&#187;<a href="source-highlight-filter.txt">Page&nbsp;Source</a></div>
</div>
</div>
<div id="layout-content-box">
<div id="layout-banner">
  <div id="layout-title">AsciiDoc</div>
  <div id="layout-description">Text based document generation</div>
</div>
<div id="layout-content">
<div id="header">
<h1>Source Code Highlight Filter</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>The AsciiDoc distribution includes a <em>source</em> filter for highlighting
code syntax.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_docbook_outputs">DocBook Outputs</h2>
<div class="sectionbody">
<div class="paragraph"><p>AsciiDoc encloses the source code in a DocBook <em>programlisting</em>
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:</p></div>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
The <em>language</em> attribute is set to the AsciiDoc <em>language</em>
  attribute.
</p>
</li>
<li>
<p>
The <em>linenumbering</em> attribute is set to the AsciiDoc <em>src_numbered</em>
  attribute (<em>numbered</em> or <em>unnumbered</em>).
</p>
</li>
</ol></div>
</div>
</div>
<div class="sect1">
<h2 id="_html_outputs">HTML Outputs</h2>
<div class="sectionbody">
<div class="paragraph"><p>You have the choice of three HTML source code highlighters, your
selection is determined by the <em>source-highlighter</em> attribute (defaults
to <em>source-highlight</em>):</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">Set the <em>source-highlighter</em> attribute from the <code>asciidoc(1)</code>
command-line or in the document header (not in the document body,
because the configuration file conditional macros are processed at
load time).</td>
</tr></table>
</div>
<div class="sect2">
<h3 id="_gnu_source_highlight">GNU Source Highlight</h3>
<div class="paragraph"><p>The default highlighter is the
<a href="http://www.gnu.org/software/src-highlite/">GNU source-highlight</a> which
can highlight <em>html4</em>, <em>html5</em> and <em>xhtml11</em> outputs.  The GNU
source-highlight must be installed and the <em>source-highlight</em> command
must reside in the shell search <em>PATH</em>.</p></div>
</div>
<div class="sect2">
<h3 id="_highlight">Highlight</h3>
<div class="paragraph"><p>You can use
<a href="http://www.andre-simon.de/doku/highlight/en/highlight.html">Highlight</a>
syntax highlighter for <em>xhtml11</em>, <em>html5</em> and <em>html4</em> outputs (set the
<em>source-highlighter</em> attribute to <em>highlighter</em>).</p></div>
<div class="ulist"><ul>
<li>
<p>
The <em>highlight</em> command must reside in the shell search <em>PATH</em>.
</p>
</li>
<li>
<p>
To make Highlighter your default highlighter put the following line
  your <code>~/.asciidoc/asciidoc.conf</code> file:
</p>
<div class="literalblock">
<div class="content">
<pre><code>source-highlighter=highlight</code></pre>
</div></div>
</li>
<li>
<p>
The AsciiDoc <em>encoding</em> attribute is passed to Highlighter using the
  <code>--encoding</code> command-line option.
</p>
</li>
</ul></div>
</div>
<div class="sect2">
<h3 id="_pygments">Pygments</h3>
<div class="paragraph"><p>The <a href="http://pygments.org/">Pygments</a> syntax highlighter can be used for
<em>xhtml11</em> and <em>html5</em> outputs (set the <em>source-highlighter</em> attribute
to <em>pygments</em>).</p></div>
<div class="ulist"><ul>
<li>
<p>
The <em>pygmentize</em> command must reside in the shell search <em>PATH</em>.
</p>
</li>
<li>
<p>
You can customize Pygments CSS styles by editing
  <code>./stylesheets/pygments.css</code>. The <code>pygments.css</code> CSS file was
  generated with:
</p>
<div class="literalblock">
<div class="content">
<pre><code>from pygments.formatters import HtmlFormatter
print HtmlFormatter().get_style_defs('.highlight')</code></pre>
</div></div>
</li>
<li>
<p>
To make Pygments your default highlighter put the following line
  your <code>~/.asciidoc/asciidoc.conf</code> file:
</p>
<div class="literalblock">
<div class="content">
<pre><code>source-highlighter=pygments</code></pre>
</div></div>
</li>
<li>
<p>
The AsciiDoc <em>encoding</em> attribute is passed to Pygments using the
  <code>-O</code> command-line option.
</p>
</li>
</ul></div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_block_attributes">Block attributes</h2>
<div class="sectionbody">
<div class="paragraph"><p>The following attributes can be included in source code block
attribute lists.</p></div>
<div class="ulist"><ul>
<li>
<p>
<em>style</em> and <em>language</em> are mandatory.
</p>
</li>
<li>
<p>
<em>style</em>, <em>language</em> and <em>src_numbered</em> are the first three
  positional attributes in that order.
</p>
</li>
<li>
<p>
The <em>args</em> attribute allows the inclusion of arbitrary (highlighter
  dependent) command options.
</p>
</li>
</ul></div>
<div class="dlist"><dl>
<dt class="hdlist1">
style
</dt>
<dd>
<p>
  Set to <em>source</em>.
</p>
</dd>
<dt class="hdlist1">
language
</dt>
<dd>
<p>
  The source code language name.
</p>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">The language names vary between highlighters&#8201;&#8212;&#8201;consult the
selected highlighter manual.</td>
</tr></table>
</div>
</dd>
<dt class="hdlist1">
src_numbered
</dt>
<dd>
<p>
  Set to <em>numbered</em> to include line numbers.
</p>
</dd>
<dt class="hdlist1">
src_tab
</dt>
<dd>
<p>
  Set tab size (GNU source-highlight only).
</p>
</dd>
<dt class="hdlist1">
args
</dt>
<dd>
<p>
  Include this attribute value in the highlighter command-line (HTML
  outputs) or in the <code>programlisting</code> element (DocBook).
</p>
</dd>
</dl></div>
</div>
</div>
<div class="sect1">
<h2 id="_testing">Testing</h2>
<div class="sectionbody">
<div class="paragraph"><p>Test the filter by converting the test file to HTML with AsciiDoc:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>$ asciidoc -v ./filters/source/source-highlight-filter-test.txt
$ firefox ./filters/source/source-highlight-filter-test.html &amp;</code></pre>
</div></div>
</div>
</div>
<div class="sect1">
<h2 id="_examples">Examples</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_source_code_paragraphs">Source code paragraphs</h3>
<div class="paragraph"><p>The <code>source</code> paragraph style will highlight a paragraph of source
code. These three code paragraphs:</p></div>
<div class="listingblock">
<div class="content">
<pre><code>[source,python]
if n &lt; 0: print 'Hello World!'

:language: python

[source]
if n &lt; 0: print 'Hello World!'

[source,ruby,numbered]
[true, false].cycle([0, 1, 2, 3, 4]) do |a, b|
    puts "#{a.inspect} =&gt; #{b.inspect}"</code></pre>
</div></div>
<div class="paragraph"><p>Render this highlighted source code:</p></div>
<div class="listingblock">
<div class="content">
<pre><code><span style="color:#000000; font-weight:bold">if</span> n <span style="color:#000000">&lt;</span> <span style="color:#b07e00">0</span><span style="color:#000000">:</span> <span style="color:#000000; font-weight:bold">print</span> <span style="color:#bf0303">&apos;Hello World!&apos;</span></code></pre>
</div></div>
<div class="listingblock">
<div class="content">
<pre><code><span style="color:#000000; font-weight:bold">if</span> n <span style="color:#000000">&lt;</span> <span style="color:#b07e00">0</span><span style="color:#000000">:</span> <span style="color:#000000; font-weight:bold">print</span> <span style="color:#bf0303">&apos;Hello World!&apos;</span></code></pre>
</div></div>
<div class="listingblock">
<div class="content">
<pre><code><span style="color:#555555">    1 </span><span style="color:#000000">[</span><span style="color:#000000; font-weight:bold">true</span><span style="color:#000000">,</span> <span style="color:#000000; font-weight:bold">false</span><span style="color:#000000">].</span><span style="color:#010181">cycle</span><span style="color:#000000">([</span><span style="color:#b07e00">0</span><span style="color:#000000">,</span> <span style="color:#b07e00">1</span><span style="color:#000000">,</span> <span style="color:#b07e00">2</span><span style="color:#000000">,</span> <span style="color:#b07e00">3</span><span style="color:#000000">,</span> <span style="color:#b07e00">4</span><span style="color:#000000">])</span> <span style="color:#000000; font-weight:bold">do</span> <span style="color:#000000">|</span>a<span style="color:#000000">,</span> b<span style="color:#000000">|</span>
<span style="color:#555555">    2 </span>    puts <span style="color:#bf0303">&quot;#{a.inspect} =&gt; #{b.inspect}&quot;</span></code></pre>
</div></div>
</div>
<div class="sect2">
<h3 id="_unnumbered_source_code_listing">Unnumbered source code listing</h3>
<div class="paragraph"><p>This source-highlight filtered block:</p></div>
<div class="listingblock">
<div class="content">
<pre><code> [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
 ---------------------------------------------------------------------</code></pre>
</div></div>
<div class="paragraph"><p>Renders this highlighted source code:</p></div>
<div class="listingblock">
<div class="content">
<pre><code><span style="color:#bf0303">&apos;&apos;&apos; A multi-line</span>
<span style="color:#bf0303">    comment.&apos;&apos;&apos;</span>
<span style="color:#000000; font-weight:bold">def</span> <span style="color:#010181">sub_word</span><span style="color:#000000">(</span>mo<span style="color:#000000">):</span>
    <span style="color:#bf0303">&apos;&apos;&apos; Single line comment.&apos;&apos;&apos;</span>
    word <span style="color:#000000">=</span> mo<span style="color:#000000">.</span><span style="color:#010181">group</span><span style="color:#000000">(</span><span style="color:#bf0303">&apos;word&apos;</span><span style="color:#000000">)</span>     <span style="color:#838183; font-style:italic"># Inline comment</span>
    <span style="color:#000000; font-weight:bold">if</span> word <span style="color:#000000; font-weight:bold">in</span> keywords<span style="color:#000000">[</span>language<span style="color:#000000">]:</span>
        <span style="color:#000000; font-weight:bold">return</span> quote <span style="color:#000000">+</span> word <span style="color:#000000">+</span> quote
    <span style="color:#000000; font-weight:bold">else</span><span style="color:#000000">:</span>
        <span style="color:#000000; font-weight:bold">return</span> word</code></pre>
</div></div>
</div>
<div class="sect2">
<h3 id="_numbered_source_code_listing_with_callouts">Numbered source code listing with callouts</h3>
<div class="paragraph"><p>This source-highlight filtered block:</p></div>
<div class="listingblock">
<div class="content">
<pre><code> [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__                                 # &lt;1&gt;
   # Array#cycle test
   # true =&gt; 0
   # false =&gt; 1
   # true =&gt; 2
   # false =&gt; 3
   # true =&gt; 4
   puts 'Array#cycle test'                         # &lt;2&gt;
   [true, false].cycle([0, 1, 2, 3, 4]) do |a, b|
     puts "#{a.inspect} =&gt; #{b.inspect}"
   end
 end
 ---------------------------------------------------------------------

 &lt;1&gt; First callout.
 &lt;2&gt; Second callout.</code></pre>
</div></div>
<div class="paragraph"><p>Renders this highlighted source code:</p></div>
<div class="listingblock">
<div class="content">
<pre><code><span style="color:#555555">    1 </span><span style="color:#838183; font-style:italic">#</span>
<span style="color:#555555">    2 </span><span style="color:#838183; font-style:italic"># Useful Ruby base class extensions.</span>
<span style="color:#555555">    3 </span><span style="color:#838183; font-style:italic">#</span>
<span style="color:#555555">    4 </span>
<span style="color:#555555">    5 </span><span style="color:#000000; font-weight:bold">class</span> Array
<span style="color:#555555">    6 </span>
<span style="color:#555555">    7 </span>  <span style="color:#838183; font-style:italic"># Execute a block passing it corresponding items in</span>
<span style="color:#555555">    8 </span>  <span style="color:#838183; font-style:italic"># +self+ and +other_array+.</span>
<span style="color:#555555">    9 </span>  <span style="color:#838183; font-style:italic"># If self has less items than other_array it is repeated.</span>
<span style="color:#555555">   10 </span>
<span style="color:#555555">   11 </span>  <span style="color:#000000; font-weight:bold">def</span> <span style="color:#010181">cycle</span><span style="color:#000000">(</span>other_array<span style="color:#000000">)</span>  <span style="color:#838183; font-style:italic"># :yields: item, other_item</span>
<span style="color:#555555">   12 </span>    other_array<span style="color:#000000">.</span>each_with_index <span style="color:#000000; font-weight:bold">do</span> <span style="color:#000000">|</span>item<span style="color:#000000">,</span> index<span style="color:#000000">|</span>
<span style="color:#555555">   13 </span>      <span style="color:#000000; font-weight:bold">yield</span><span style="color:#000000">(</span><span style="color:#000000; font-weight:bold">self</span><span style="color:#000000">[</span>index <span style="color:#000000">%</span> <span style="color:#000000; font-weight:bold">self</span><span style="color:#000000">.</span>length<span style="color:#000000">],</span> item<span style="color:#000000">)</span>
<span style="color:#555555">   14 </span>    <span style="color:#000000; font-weight:bold">end</span>
<span style="color:#555555">   15 </span>  <span style="color:#000000; font-weight:bold">end</span>
<span style="color:#555555">   16 </span>
<span style="color:#555555">   17 </span><span style="color:#000000; font-weight:bold">end</span>
<span style="color:#555555">   18 </span>
<span style="color:#555555">   19 </span><span style="color:#000000; font-weight:bold">if</span> <span style="color:#0057ae">$0</span> <span style="color:#000000">==</span> __FILE__                                 <span style="color:#838183; font-style:italic"># <img src="./images/icons/callouts/1.png" alt="1" /></span>
<span style="color:#555555">   20 </span>  <span style="color:#838183; font-style:italic"># Array#cycle test</span>
<span style="color:#555555">   21 </span>  <span style="color:#838183; font-style:italic"># true =&gt; 0</span>
<span style="color:#555555">   22 </span>  <span style="color:#838183; font-style:italic"># false =&gt; 1</span>
<span style="color:#555555">   23 </span>  <span style="color:#838183; font-style:italic"># true =&gt; 2</span>
<span style="color:#555555">   24 </span>  <span style="color:#838183; font-style:italic"># false =&gt; 3</span>
<span style="color:#555555">   25 </span>  <span style="color:#838183; font-style:italic"># true =&gt; 4</span>
<span style="color:#555555">   26 </span>  puts <span style="color:#bf0303">&apos;Array#cycle test&apos;</span>                         <span style="color:#838183; font-style:italic"># <img src="./images/icons/callouts/2.png" alt="2" /></span>
<span style="color:#555555">   27 </span>  <span style="color:#000000">[</span><span style="color:#000000; font-weight:bold">true</span><span style="color:#000000">,</span> <span style="color:#000000; font-weight:bold">false</span><span style="color:#000000">].</span><span style="color:#010181">cycle</span><span style="color:#000000">([</span><span style="color:#b07e00">0</span><span style="color:#000000">,</span> <span style="color:#b07e00">1</span><span style="color:#000000">,</span> <span style="color:#b07e00">2</span><span style="color:#000000">,</span> <span style="color:#b07e00">3</span><span style="color:#000000">,</span> <span style="color:#b07e00">4</span><span style="color:#000000">])</span> <span style="color:#000000; font-weight:bold">do</span> <span style="color:#000000">|</span>a<span style="color:#000000">,</span> b<span style="color:#000000">|</span>
<span style="color:#555555">   28 </span>    puts <span style="color:#bf0303">&quot;#{a.inspect} =&gt; #{b.inspect}&quot;</span>
<span style="color:#555555">   29 </span>  <span style="color:#000000; font-weight:bold">end</span>
<span style="color:#555555">   30 </span><span style="color:#000000; font-weight:bold">end</span></code></pre>
</div></div>
<div class="colist arabic"><table>
<tr><td><img src="./images/icons/callouts/1.png" alt="1" /></td><td>
First callout.
</td></tr>
<tr><td><img src="./images/icons/callouts/2.png" alt="2" /></td><td>
Second callout.
</td></tr>
</table></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
<td class="content">
<div class="ulist"><ul>
<li>
<p>
If the source <em>language</em> attribute has been set (using an
  <em>AttributeEntry</em> or from the command-line) you don&#8217;t have to specify
  it in each source code block.
</p>
</li>
<li>
<p>
You should place callout markers inside source code comments to
  ensure they are not misinterpreted and mangled by the highlighter.
</p>
</li>
</ul></div>
</td>
</tr></table>
</div>
</div>
</div>
</div>
</div>
<div id="footnotes"><hr /></div>
<div id="footer">
<div id="footer-text">
Version 9.0.4<br />
Last updated 2020-12-05 17:40:55 UTC<br />
AsciiDoc is a trademark of the Eclipse Foundation, Inc.
</div>
<div id="footer-badges">
<a href="https://validator.w3.org/check?uri=referer">
  <img style="border:0;width:88px;height:31px"
    src="https://www.w3.org/Icons/valid-xhtml11-blue"
    alt="Valid XHTML 1.1" height="31" width="88" />
</a>
<a href="https://jigsaw.w3.org/css-validator/">
  <img style="border:0;width:88px;height:31px"
    src="https://jigsaw.w3.org/css-validator/images/vcss-blue"
    alt="Valid CSS!" />
</a>
</div>
</div>
</div>
</div>
</body>
</html>