pygments filter

128 views
Skip to first unread message

david

unread,
Jul 21, 2010, 7:32:50 AM7/21/10
to asciidoc
Hello,

I am working on a patch to add a pygments filter to asciidoc
(www.pygments.org/).

The patch is based on GNU source-highlight filter, and use pygments
command line interface ('pygmentize'). Pygmentize only generate xhtml
output, so the filter is inactive with html4 backend. I have also
modified xhtml stylesheets.

There is an issue with callouts:

asciidoc -v pygmentize-filter.txt
asciidoc: reading: /home/david/bin/Asciidoc/asciidoc.conf
asciidoc: reading: /home/david/bin/Asciidoc/xhtml11.conf
asciidoc: reading: /home/david/bin/Asciidoc/xhtml11-quirks.conf
asciidoc: reading: /home/david/bin/Asciidoc/filters/code/code-
filter.conf
asciidoc: reading: /home/david/bin/Asciidoc/filters/pigmentize/
pygmentize-filter.conf
asciidoc: reading: /home/david/bin/Asciidoc/filters/music/music-
filter.conf
asciidoc: reading: /home/david/bin/Asciidoc/filters/graphviz/graphviz-
filter.conf
asciidoc: reading: /home/david/bin/Asciidoc/filters/latex/latex-
filter.conf
asciidoc: reading: /home/david/bin/Asciidoc/filters/source/source-
highlight-filter.conf
asciidoc: reading: /home/david/bin/pygmentize-filter.txt
asciidoc: writing: /home/david/bin/pygmentize-filter.html
asciidoc: reading: /home/david/bin/Asciidoc/lang-en.conf
asciidoc: pygmentize-filter.txt: line 39: filtering: pygmentize -f
html -l python
asciidoc: pygmentize-filter.txt: line 44: filtering: pygmentize -f
html -l python
asciidoc: pygmentize-filter.txt: line 48: filtering: pygmentize -f
html -l ruby -O linenos=table
asciidoc: pygmentize-filter.txt: line 82: filtering: pygmentize -f
html -l python
asciidoc: pygmentize-filter.txt: line 162: filtering: pygmentize -f
html -l ruby -O linenos=table
asciidoc: WARNING: pygmentize-filter.txt: line 165: no callouts refer
to list item 1
asciidoc: WARNING: pygmentize-filter.txt: line 166: no callouts refer
to list item 2

I don't know what is going on here, so the patch could not be included
as it is. Does anyone have an advice? Unfortunately, I'm a very poor
programmer...

I am hoping you will accept this into the next version of asciidoc.

Thanks,

David Hajage

the patch :

exporting patch:
# HG changeset patch
# User David Hajage <dha...@gmail.net>
# Date 1279655270 -7200
# Node ID ea9faedb3b4f091a1162c4c30ecf059d3a02929f
# Parent 1ba8886d0ee4369e94bea8b5e9b7dea5799a582a
pygments filter

diff -r 1ba8886d0ee4 -r ea9faedb3b4f doc/pygmentize-filter.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/pygmentize-filter.txt Tue Jul 20 21:47:50 2010 +0200
@@ -0,0 +1,195 @@
+Source Code Pygments Filter
+===========================
+
+The AsciiDoc distribution includes another source code syntax
+highlight filter (`pygmentize-filter.conf`). It uses
+http://www.pygments.org[Pygments] to highlight HTML outputs; DocBook
+outputs are highlighted by toolchains that have `programlisting`
+element highlight support, for example 'dblatex'.
+
+TIP: If the source 'language' attribute has been set (using an
+'AttributeEntry' or from the command-line) you don't have to specify
+it in each source code block.
+
+
+Examples
+--------
+Source code paragraphs
+~~~~~~~~~~~~~~~~~~~~~~
+The `pygmentize` paragraph style will highlight a paragraph of source
+code. These three code paragraphs:
+
+---------------------------------------------------------------------
+[pygmentize,python]
+if n < 0: print 'Hello World!'
+
+:language: python
+
+[pygmentize]
+if n < 0: print 'Hello World!'
+
+[pygmentize,ruby,numbered]
+[true, false].cycle([0, 1, 2, 3, 4]) do |a, b|
+ puts "#{a.inspect} => #{b.inspect}"
+---------------------------------------------------------------------
+
+Render this highlighted source code:
+
+[pygmentize,python]
+if n < 0: print 'Hello World!'
+
+:language: python
+
+[pygmentize]
+if n < 0: print 'Hello World!'
+
+[pygmentize,ruby,numbered]
+[true, false].cycle([0, 1, 2, 3, 4]) do |a, b|
+ puts "#{a.inspect} => #{b.inspect}"
+
+
+Unnumbered source code listing
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+This pygmentize filtered block:
+
+---------------------------------------------------------------------
+ [pygmentize,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
+
---------------------------------------------------------------------
+---------------------------------------------------------------------
+
+Renders this highlighted source code:
+
+[pygmentize,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
+---------------------------------------------------------------------
+
+Numbered source code listing with callouts
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+This pygmentize filtered block:
+
+---------------------------------------------------------------------
+ [pygmentize,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.
+
+---------------------------------------------------------------------
+
+Renders this highlighted source code:
+
+[pygmentize,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.
+
+
+Installation
+------------
+HTML
+~~~~
+If you want to syntax highlight AsciiDoc HTML outputs with pygments
+(`xhtml11` backend, `html4` *doesn't work*) you need to install
+http://www.pygments.org[Pygments] (most distributions have this
+package).
+
+DocBook
+~~~~~~~
+AsciiDoc encloses the source code in a DocBook 'programlisting'
+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:
+
+. The 'language' attribute is set to the AsciiDoc 'language'
+ attribute.
+. The 'linenumbering' attribute is set to the AsciiDoc 'src_numbered'
+ attribute ('numbered' or 'unnumbered').
+
+Testing
+~~~~~~~
+Test the filter by converting the test file to HTML with AsciiDoc:
+
+ $ asciidoc -v ./filters/source/pygmentize-filter-test.txt
+ $ firefox ./filters/source/pygmentize-filter-test.html &
diff -r 1ba8886d0ee4 -r ea9faedb3b4f filters/pigmentize/pygmentize-
filter-test.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/filters/pigmentize/pygmentize-filter-test.txt Tue Jul 20
21:47:50 2010 +0200
@@ -0,0 +1,19 @@
+Pygments Filter Test
+====================
+
+Details of the filter can be found in
+`./doc/pygmentize-filter.txt`.
+
+[pygmentize,python,numbered]
+---------------------------------------------------------------------
+''' 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
+---------------------------------------------------------------------
+
diff -r 1ba8886d0ee4 -r ea9faedb3b4f filters/pigmentize/pygmentize-
filter.conf
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/filters/pigmentize/pygmentize-filter.conf Tue Jul 20 21:47:50
2010 +0200
@@ -0,0 +1,68 @@
+#
+# AsciiDoc source code pygments filter configuration file.
+#
+# Documented in pygmentize-filter.txt in AsciiDoc distribution
+# ./examples/website/ directory.
+#
+# HTML outputs require pygments
+# http://www.pygments.org
+#
+
+########################
+# Source block templates
+########################
+[pygmentize-block]
+template::[listingblock]
+
+# Customized listingblock block for xhtml11 to ensure valid XHTML1.1.
+ifdef::backend-xhtml11[]
+[pygmentize-block]
+<div class="listingblock">
+<a id="{id}"></a>
+<div class="title">{caption=}{title}</div>
+<div class="content">
+|
+</div></div>
+endif::backend-xhtml11[]
+
+# Use DocBook programlisting element.
+ifdef::backend-docbook[]
+[pygmentize-block]
+<formalpara{id? id="{id}"}{role? role="{role}"}{reftext?
xreflabel="{reftext}"}><title>{title}</title><para>
+<programlisting language="{language}"
linenumbering="{src_numbered=unnumbered}">
+|
+</programlisting>
+{title#}</para></formalpara>
+endif::backend-docbook[]
+
+#########################
+# Source paragraph styles
+#########################
+[paradef-default]
+ifdef::basebackend-html[]
+pygmentize-style=template="listingblock"
+endif::basebackend-html[]
+
+ifdef::backend-xhtml11[]
+pygmentize-style=template="pygmentize-
block",presubs=(),postsubs=("callouts",),posattrs=("style","language","src_numbered"),filter="pygmentize
-f html -l {language} {src_numbered?-O linenos=table}"
+endif::backend-xhtml11[]
+
+ifdef::backend-docbook[]
+pygmentize-style=template="pygmentize-
block",presubs=(),postsubs=("specialcharacters","callouts"),posattrs=("style","language","src_numbered"),filter=""
+endif::backend-docbook[]
+
+#########################
+# Source block styles
+#########################
+[blockdef-listing]
+ifdef::basebackend-html[]
+pygmentize-style=template="listingblock"
+endif::basebackend-html[]
+
+ifdef::backend-xhtml11[]
+pygmentize-style=template="pygmentize-
block",presubs=(),postsubs=("callouts",),posattrs=("style","language","src_numbered"),filter="pygmentize
-f html -l {language} {src_numbered?-O linenos=table}"
+endif::backend-xhtml11[]
+
+ifdef::backend-docbook[]
+pygmentize-style=template="pygmentize-
block",presubs=(),postsubs=("specialcharacters","callouts"),posattrs=("style","language","src_numbered")
+endif::backend-docbook[]
diff -r 1ba8886d0ee4 -r ea9faedb3b4f stylesheets/volnitsky.css
--- a/stylesheets/volnitsky.css Thu May 13 09:12:42 2010 +1200
+++ b/stylesheets/volnitsky.css Tue Jul 20 21:47:50 2010 +0200
@@ -380,3 +380,70 @@
a:hover, a:focus {
text-decoration: underline !important;
}
+
+/*
+ pygmentize filter
+*/
+.highlight .hll { background-color: #ffffcc }
+.highlight { background: #f4f4f4; }
+.highlight .c { color: #008800; font-style: italic } /* Comment */
+.highlight .err { border: 1px solid #FF0000 } /* Error */
+.highlight .k { color: #AA22FF; font-weight: bold } /* Keyword */
+.highlight .o { color: #666666 } /* Operator */
+.highlight .cm { color: #008800; font-style: italic } /*
Comment.Multiline */
+.highlight .cp { color: #008800 } /* Comment.Preproc */
+.highlight .c1 { color: #008800; font-style: italic } /*
Comment.Single */
+.highlight .cs { color: #008800; font-weight: bold } /*
Comment.Special */
+.highlight .gd { color: #A00000 } /* Generic.Deleted */
+.highlight .ge { font-style: italic } /* Generic.Emph */
+.highlight .gr { color: #FF0000 } /* Generic.Error */
+.highlight .gh { color: #000080; font-weight: bold } /*
Generic.Heading */
+.highlight .gi { color: #00A000 } /* Generic.Inserted */
+.highlight .go { color: #808080 } /* Generic.Output */
+.highlight .gp { color: #000080; font-weight: bold } /*
Generic.Prompt */
+.highlight .gs { font-weight: bold } /* Generic.Strong */
+.highlight .gu { color: #800080; font-weight: bold } /*
Generic.Subheading */
+.highlight .gt { color: #0040D0 } /* Generic.Traceback */
+.highlight .kc { color: #AA22FF; font-weight: bold } /*
Keyword.Constant */
+.highlight .kd { color: #AA22FF; font-weight: bold } /*
Keyword.Declaration */
+.highlight .kn { color: #AA22FF; font-weight: bold } /*
Keyword.Namespace */
+.highlight .kp { color: #AA22FF } /* Keyword.Pseudo */
+.highlight .kr { color: #AA22FF; font-weight: bold } /*
Keyword.Reserved */
+.highlight .kt { color: #00BB00; font-weight: bold } /* Keyword.Type
*/
+.highlight .m { color: #666666 } /* Literal.Number */
+.highlight .s { color: #BB4444 } /* Literal.String */
+.highlight .na { color: #BB4444 } /* Name.Attribute */
+.highlight .nb { color: #AA22FF } /* Name.Builtin */
+.highlight .nc { color: #0000FF } /* Name.Class */
+.highlight .no { color: #880000 } /* Name.Constant */
+.highlight .nd { color: #AA22FF } /* Name.Decorator */
+.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity
*/
+.highlight .ne { color: #D2413A; font-weight: bold } /*
Name.Exception */
+.highlight .nf { color: #00A000 } /* Name.Function */
+.highlight .nl { color: #A0A000 } /* Name.Label */
+.highlight .nn { color: #0000FF; font-weight: bold } /*
Name.Namespace */
+.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */
+.highlight .nv { color: #B8860B } /* Name.Variable */
+.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word
*/
+.highlight .w { color: #bbbbbb } /* Text.Whitespace */
+.highlight .mf { color: #666666 } /* Literal.Number.Float */
+.highlight .mh { color: #666666 } /* Literal.Number.Hex */
+.highlight .mi { color: #666666 } /* Literal.Number.Integer */
+.highlight .mo { color: #666666 } /* Literal.Number.Oct */
+.highlight .sb { color: #BB4444 } /* Literal.String.Backtick */
+.highlight .sc { color: #BB4444 } /* Literal.String.Char */
+.highlight .sd { color: #BB4444; font-style: italic } /*
Literal.String.Doc */
+.highlight .s2 { color: #BB4444 } /* Literal.String.Double */
+.highlight .se { color: #BB6622; font-weight: bold } /*
Literal.String.Escape */
+.highlight .sh { color: #BB4444 } /* Literal.String.Heredoc */
+.highlight .si { color: #BB6688; font-weight: bold } /*
Literal.String.Interpol */
+.highlight .sx { color: #008000 } /* Literal.String.Other */
+.highlight .sr { color: #BB6688 } /* Literal.String.Regex */
+.highlight .s1 { color: #BB4444 } /* Literal.String.Single */
+.highlight .ss { color: #B8860B } /* Literal.String.Symbol */
+.highlight .bp { color: #AA22FF } /* Name.Builtin.Pseudo */
+.highlight .vc { color: #B8860B } /* Name.Variable.Class */
+.highlight .vg { color: #B8860B } /* Name.Variable.Global */
+.highlight .vi { color: #B8860B } /* Name.Variable.Instance */
+.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */
+
diff -r 1ba8886d0ee4 -r ea9faedb3b4f stylesheets/xhtml11.css
--- a/stylesheets/xhtml11.css Thu May 13 09:12:42 2010 +1200
+++ b/stylesheets/xhtml11.css Tue Jul 20 21:47:50 2010 +0200
@@ -374,3 +374,70 @@
margin-left: 6em;
font-size: 0.9em;
}
+
+/*
+ pygmentize filter
+*/
+.highlight .hll { background-color: #ffffcc }
+.highlight { background: #f4f4f4; }
+.highlight .c { color: #008800; font-style: italic } /* Comment */
+.highlight .err { border: 1px solid #FF0000 } /* Error */
+.highlight .k { color: #AA22FF; font-weight: bold } /* Keyword */
+.highlight .o { color: #666666 } /* Operator */
+.highlight .cm { color: #008800; font-style: italic } /*
Comment.Multiline */
+.highlight .cp { color: #008800 } /* Comment.Preproc */
+.highlight .c1 { color: #008800; font-style: italic } /*
Comment.Single */
+.highlight .cs { color: #008800; font-weight: bold } /*
Comment.Special */
+.highlight .gd { color: #A00000 } /* Generic.Deleted */
+.highlight .ge { font-style: italic } /* Generic.Emph */
+.highlight .gr { color: #FF0000 } /* Generic.Error */
+.highlight .gh { color: #000080; font-weight: bold } /*
Generic.Heading */
+.highlight .gi { color: #00A000 } /* Generic.Inserted */
+.highlight .go { color: #808080 } /* Generic.Output */
+.highlight .gp { color: #000080; font-weight: bold } /*
Generic.Prompt */
+.highlight .gs { font-weight: bold } /* Generic.Strong */
+.highlight .gu { color: #800080; font-weight: bold } /*
Generic.Subheading */
+.highlight .gt { color: #0040D0 } /* Generic.Traceback */
+.highlight .kc { color: #AA22FF; font-weight: bold } /*
Keyword.Constant */
+.highlight .kd { color: #AA22FF; font-weight: bold } /*
Keyword.Declaration */
+.highlight .kn { color: #AA22FF; font-weight: bold } /*
Keyword.Namespace */
+.highlight .kp { color: #AA22FF } /* Keyword.Pseudo */
+.highlight .kr { color: #AA22FF; font-weight: bold } /*
Keyword.Reserved */
+.highlight .kt { color: #00BB00; font-weight: bold } /* Keyword.Type
*/
+.highlight .m { color: #666666 } /* Literal.Number */
+.highlight .s { color: #BB4444 } /* Literal.String */
+.highlight .na { color: #BB4444 } /* Name.Attribute */
+.highlight .nb { color: #AA22FF } /* Name.Builtin */
+.highlight .nc { color: #0000FF } /* Name.Class */
+.highlight .no { color: #880000 } /* Name.Constant */
+.highlight .nd { color: #AA22FF } /* Name.Decorator */
+.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity
*/
+.highlight .ne { color: #D2413A; font-weight: bold } /*
Name.Exception */
+.highlight .nf { color: #00A000 } /* Name.Function */
+.highlight .nl { color: #A0A000 } /* Name.Label */
+.highlight .nn { color: #0000FF; font-weight: bold } /*
Name.Namespace */
+.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */
+.highlight .nv { color: #B8860B } /* Name.Variable */
+.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word
*/
+.highlight .w { color: #bbbbbb } /* Text.Whitespace */
+.highlight .mf { color: #666666 } /* Literal.Number.Float */
+.highlight .mh { color: #666666 } /* Literal.Number.Hex */
+.highlight .mi { color: #666666 } /* Literal.Number.Integer */
+.highlight .mo { color: #666666 } /* Literal.Number.Oct */
+.highlight .sb { color: #BB4444 } /* Literal.String.Backtick */
+.highlight .sc { color: #BB4444 } /* Literal.String.Char */
+.highlight .sd { color: #BB4444; font-style: italic } /*
Literal.String.Doc */
+.highlight .s2 { color: #BB4444 } /* Literal.String.Double */
+.highlight .se { color: #BB6622; font-weight: bold } /*
Literal.String.Escape */
+.highlight .sh { color: #BB4444 } /* Literal.String.Heredoc */
+.highlight .si { color: #BB6688; font-weight: bold } /*
Literal.String.Interpol */
+.highlight .sx { color: #008000 } /* Literal.String.Other */
+.highlight .sr { color: #BB6688 } /* Literal.String.Regex */
+.highlight .s1 { color: #BB4444 } /* Literal.String.Single */
+.highlight .ss { color: #B8860B } /* Literal.String.Symbol */
+.highlight .bp { color: #AA22FF } /* Name.Builtin.Pseudo */
+.highlight .vc { color: #B8860B } /* Name.Variable.Class */
+.highlight .vg { color: #B8860B } /* Name.Variable.Global */
+.highlight .vi { color: #B8860B } /* Name.Variable.Instance */
+.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */
+
Reply all
Reply to author
Forward
0 new messages