Commit dfaba8db authored by pookey's avatar pookey

importing the programlisting xsl stuff that does the syntax highlighting

parent 5bc9d104
Making syntax highlighting work
-------------------------------
~/p/xsieve/opt/bin/xsieve -xinclude -o testdoc.html testdoc.xsl testdoc.xml
Development in this directory will be cancelled.
Something like $XSIEVE/examples/syntax_highlighting will be used.
colorer.scm: the main part, uniting trees
run-colorer.scm: low-level driver to execute a colorizing program
colorer.xsl: processing of "programlisting"
testdoc.* + test.xml: testing
run.sh: run "testdoc" conversion
testdata.xml, test.scm: testing
test-one.scm: testing
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:syn="http://ns.laxan.com/text-vimcolor/1" version="1.0">
<!-- $Id: colorer-html.xsl,v 1.2 2006/04/29 05:48:16 olpa Exp $ -->
<xsl:template match="syn:Comment">
<span style="color:#0000FF;">
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:template match="syn:Constant">
<span style="color:#FF00FF;">
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:template match="syn:Identifier">
<span style="color:#008B8B;">
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:template match="syn:Statement">
<span style="color:#A52A2A; font-weight:bold;">
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:template match="syn:PreProc">
<span style="color:#A020F0;">
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:template match="syn:Type">
<span style="color:#2E8B57; font-weight:bold;">
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:template match="syn:Special">
<span style="color:#6A5ACD;">
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:template match="syn:Underlined">
<span style="color:#000000; text-decoration:underline;">
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:template match="syn:Error">
<span style="color:#FFFFFF; background:#FF0000 none;">
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:template match="syn:Todo">
<span style="color:#0000FF; background: #FFFF00 none;">
<xsl:apply-templates />
</span>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
version = "1.0"
xmlns:s = "http://xsieve.sourceforge.net"
xmlns:syn = "http://ns.laxan.com/text-vimcolor/1"
extension-element-prefixes="s">
<!-- $Id: colorer-one.xsl,v 1.1 2006/05/22 04:23:51 olpa Exp $ -->
<xsl:import href="colorer.xsl" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="programlisting | screen[starts-with(@role,'colorer:')]">
<xsl:apply-imports />
</xsl:template>
</xsl:stylesheet>
; $Id: colorer.scm,v 1.8 2006/04/29 04:49:48 olpa Exp $
; construct a tree from the text and path
; "ignore" is a list of path element which shouldn't be added to the tree
; each path item is either symbol, which is the node name,
; either (symbol (@ ...)), which is the node name and the attribute node.
; It is supposed that elements with attributes aren't in the ignore list.
(define (colorer:path-to-tree text path ignore)
(let loop ((tree text) (path path))
(if (null? path)
tree
(let (
(cur (car path))
(nodeset (cons tree '())))
(loop
(if (pair? cur)
(append cur nodeset)
(if (memq cur ignore) tree (cons cur nodeset)))
(cdr path))))))
; A fragment of the text node handler
(define-macro (%colorer:on-text)
(quote (let loop ((cur cur))
(let* (
(len-buf (string-length buf))
(len-cur (string-length cur))
(len-min (min len-buf len-cur)))
(if (> len-cur 0) ; the text node in the h-tree isn't finished yet?
(let (
(result ; result is either a tree, eiter #f
(if (zero? len-buf) ; the text node in the main tree finished?
#f
(colorer:path-to-tree
(substring buf 0 len-min)
path
ignore))))
; Update buffer, switch to the main tree traversing,
; continue h-tree traversing on switching back
(set! buf (substring buf len-min len-buf))
(call-with-current-continuation (lambda (here)
(set! walker here)
(yield result)))
(loop (substring cur len-min len-cur))))))))
; A fragment of the node and attribute handler
(define-macro (%colorer:on-pair)
(quote (let ((elem (car cur)))
(if (eq? elem '@)
; attribute: attach to the path
(set-car! path (list (car path) cur))
; element: update path, continue traversing
(let ((path (cons (car cur) path)))
(for-each
(lambda (kid) (loop kid path))
(cdr cur)))))))
; generator of highlighted chunks.
; Creation:
; (define highlighter (colorer:join-markup-stepper highlight-tree ignore))
; Usage step:
; (highlighter more-buf)
; where more-buf either text, either #f. Each step returns either a
; subtree, either #f if buffer is over.
(define (colorer:join-markup-stepper highlight-tree ignore)
(letrec (
(buf #f)
(yield #f)
; The main loop
(walker-loop (lambda (cur path)
(let loop ((cur cur) (path path))
(if (pair? cur)
(%colorer:on-pair)
(%colorer:on-text)))
; The highlighting tree is over. Stop looping.
; If the main tree isn't over (impossible),
; just return the data from main tree.
(set! walker (lambda (dummy)
(if (and buf (> (string-length buf) 0))
(let ((old-buf buf))
(set! buf #f)
(yield old-buf))
(yield #f))))
(walker 'dummy)))
; Set buffer, continue looping
(walker-entry
(lambda (new-buf)
(if new-buf
(set! buf new-buf))
(call-with-current-continuation (lambda (here)
(set! yield here)
(walker 'resume)))))
; Use once, than re-set
(walker
(lambda (dummy)
(set! walker walker-loop)
(walker-loop highlight-tree '()))))
; create generator
walker-entry))
; add the colorer namespace to the tree
(define (colorer:wrap-by-ns tree)
`(syn:syntax (@ (@
(*NAMESPACES* (syn "http://ns.laxan.com/text-vimcolor/1"))))
,tree))
; join main markup with highlighting markup
(define colorer:id (lambda x x))
(define (colorer:join-markup main-tree highlight-tree ignore)
(let ((stepper (colorer:join-markup-stepper highlight-tree ignore)))
(colorer:wrap-by-ns
; Walk over the main tree
(pre-post-order main-tree `(
; Comments, entities etc are not possible, so only few special cases
(*PI* *preorder* . ,colorer:id)
(@ *preorder* . ,colorer:id)
(*default* . ,colorer:id)
; Text node: split on highlighted subtrees
(*text* . ,(lambda (trigger str)
(let loop (
(chunks '())
(tree (stepper str)))
(if tree
; Loop while trees are being generated
(loop (cons tree chunks) (stepper #f))
; The node is processed. If there is only one chunk, return
; it, otherwise wrap the nodeset of chunks by a dummy
; element. Handle also impossible case of absense of chunks.
(cond
((null? chunks) "")
((null? (cdr chunks)) (car chunks))
(else (cons 'syn:syntax (reverse chunks)))))))))))))
<xsl:stylesheet
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
version = "1.0"
xmlns:s = "http://xsieve.sourceforge.net"
xmlns:syn = "http://ns.laxan.com/text-vimcolor/1"
extension-element-prefixes="s">
<!-- $Id: colorer.xsl,v 1.6 2006/04/29 04:30:03 olpa Exp $ -->
<xsl:param name="colorer.bin">/home/clients/jhassine/doctrine/trunk/manual/docbook/programlisting/vimcolor/vimcolor-wrapper</xsl:param>
<xsl:param name="colorer.params">--format xml</xsl:param>
<xsl:param name="colorer.param.type">--filetype </xsl:param>
<xsl:param name="colorer.param.outfile">--output </xsl:param>
<s:init>
(load-from-path "sxml-utils.scm")
(load-from-path "colorer.scm")
(load-from-path "run-colorer.scm")
</s:init>
<!-- ProgramListing is colorized -->
<xsl:template match="programlisting[parent::syn:syntax] | screen[parent::syn:syntax]" priority="2">
<xsl:apply-imports/>
</xsl:template>
<!-- Colorize ProgramListing -->
<xsl:template match="programlisting | screen[starts-with(@role,'colorer:')]">
<xsl:variable name="type">
<xsl:choose>
<xsl:when test="self::screen"><xsl:value-of select="substring-after(@role,':')"/></xsl:when>
<xsl:otherwise><xsl:value-of select="@role"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<s:scheme>
(let* (
(highlighted-tree (run-colorer (x:eval "string(.)") (x:eval "string($type)")))
(current (x:current))
(united-tree
(if (not highlighted-tree)
#f
(colorer:join-markup current highlighted-tree '()))))
(x:apply-templates
'with-param 'colorized #t
(if united-tree
united-tree
(colorer:wrap-by-ns current))))
</s:scheme>
</xsl:template>
<xsl:template match="syn:syntax">
<xsl:apply-templates select="node()"/>
</xsl:template>
</xsl:stylesheet>
/Makefile/1.1/Mon May 22 04:24:28 2006//
/vim2xslt.pl/1.1/Mon May 22 04:24:28 2006//
D
xsieve/experiments/programlisting/colors
:pserver:anonymous@xsieve.cvs.sourceforge.net:/cvsroot/xsieve
vim_colors_dir = /usr/share/vim/colors
vim_files := $(wildcard $(vim_colors_dir)/*.vim)
xslt_files = $(patsubst %.vim,%.xsl,$(notdir $(vim_files)))
all: $(xslt_files)
%.xsl: $(vim_colors_dir)/%.vim
perl vim2xslt.pl $< >$@
#!/usr/bin/perl
# Take vim colors file and convert it to an XSLT stylesheet
# The only types vim-textcolor produces
my %syntax_data = (
'Comment' => undef,
'Constant' => undef,
'Identifier' => undef,
'Statement' => undef,
'PreProc' => undef,
'Type' => undef,
'Special' => undef,
'Underlined' => undef,
'Error' => undef,
'Todo' => undef
);
<xsl:stylesheet
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
version = "1.0">
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<html>
<head>
<title>Syntax highlighting for DocBook program listings</title>
</head>
<body style="background:#ffffff;">
<h1>Syntax highlighting for DocBook program listings</h1>
<p>Highlight content of <tt>ProgramListing</tt> using <a href="http://xsieve.sourceforge.net/">XSieve</a> and Vim. Example:</p>
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<th valign="top">DocBook</th>
<td>
<pre style="background:#f0f0f0;">&lt;programlisting role=&quot;xml&quot;&gt;
&amp;lt;para&gt;Hello, &lt;emphasis&gt;&amp;amp;who;&lt;/emphasis
&gt;!&amp;lt;/para&gt; &lt;co id=&quot;who-entity&quot;/&gt;
&lt;/programlisting&gt;</pre>
</td>
</tr>
<tr>
<th>HTML&nbsp;result&nbsp;&nbsp;&nbsp;</th>
<td>
<pre style="background:#f0f0f0;">
<span style="color:#008B8B;">&lt;para&gt;</span>Hello, <span class="emphasis"><em><span style="color:#2E8B57; font-weight:bold;">&amp;</span><span style="color:#A52A2A; font-weight:bold;">who</span><span style="color:#2E8B57; font-weight:bold;">;</span></em></span>!<span style="color:#008B8B;">&lt;/para&gt;</span> (1)</pre>
</td>
</tr>
</table>
<a href="xsieve-programlisting.zip">Download</a> xsieve-programlisting.zip.
<h2>Usage</h2>
<p>Set the correct path to the script <tt>vimcolor-wrapper</tt> in <tt>colorer.xsl</tt>.</p>
<p>Create an XSLT wrapper which imports DocBook and colorer stylesheets:</p>
<pre style="background:#f0f0f0;">
<font color="#008b8b">&lt;</font><font color="#6a5acd">xsl</font><font color="#0000ff">:</font><font color="#a52a2a"><b>stylesheet</b></font><font color="#008b8b"> ...&gt;</font>
...
<font color="#008b8b">&lt;</font><font color="#6a5acd">xsl</font><font color="#0000ff">:</font><font color="#a52a2a"><b>import</b></font><font color="#008b8b"> </font><font color="#2e8b57"><b>href</b></font>=<font color="#ff00ff">&quot;..../docbook.xsl&quot;</font><font color="#008b8b">&gt;</font> (1)
<font color="#008b8b">&lt;</font><font color="#6a5acd">xsl</font><font color="#0000ff">:</font><font color="#a52a2a"><b>import</b></font><font color="#008b8b"> </font><font color="#2e8b57"><b>href</b></font>=<font color="#ff00ff">&quot;..../colorer.xsl&quot;</font><font color="#008b8b">&gt;</font> (2)
<font color="#008b8b">&lt;</font><font color="#6a5acd">xsl</font><font color="#0000ff">:</font><font color="#a52a2a"><b>import</b></font><font color="#008b8b"> </font><font color="#2e8b57"><b>href</b></font>=<font color="#ff00ff">&quot;..../colorer-html.xsl&quot;</font><font color="#008b8b">&gt;</font> (3)
... Your DocBook customization layer ...
<font color="#008b8b">&lt;/</font><font color="#6a5acd">xsl</font><font color="#0000ff">:</font><font color="#a52a2a"><b>stylesheet</b></font><font color="#008b8b">&gt;</font>
</pre>
<p><b>(1)</b> The path to the DocBook XSLT stylesheet. For example, <tt>/usr/share/xml/docbook/xsl-stylesheets/html/docbook.xsl</tt><br />
<b>(2)</b> The path to the colorer XSLT stylesheet.<br />
<b>(3)</b> Or <tt>colorer-fo.xsl</tt> for FO output.</p>
<h2>Test data</h2>
<p>The package contains test files <tt>test.xml</tt>, <tt>testdoc.xsl</tt> and <tt>testdoc.html</tt>. Use them as the starting point:</p>
<pre>xsieve -o testdoc.html --param callout.graphics 0 testdoc.xsl test.xml</pre>
</body>
</html>
rm -rf xsieve-programlisting.zip xsieve-programlisting
mkdir xsieve-programlisting
rsync -av --exclude CVS vimcolor xsieve-programlisting/
cp colorer.xsl colorer-html.xsl testdoc.xsl index.html sxml-utils.scm colorer.scm run-colorer.scm test.xml testdoc.html xsieve-programlisting/
zip xsieve-programlisting.zip -r xsieve-programlisting
; Run colorer and return the result as SXML
; $Id: run-colorer.scm,v 1.6 2006/04/29 05:47:24 olpa Exp $
(define colorer-bin #f)
(define colorer-params #f)
(define colorer-param-type #f)
(define colorer-param-outfile #f)
; Initialize colorer variables (only once)
(define (init-colorer-variables)
(if (not colorer-bin) (begin
(set! colorer-bin (x:eval "string($colorer.bin)"))
(set! colorer-params (x:eval "string($colorer.params)"))
(set! colorer-param-type (x:eval "string($colorer.param.type)"))
(set! colorer-param-outfile (x:eval "string($colorer.param.outfile)")))))
(define-macro (no-errors . body)
`(catch #t (lambda () ,@body) (lambda (dummy . args) #f)))
(define (run-colorer program-text program-type)
; Some sanity check
(init-colorer-variables)
(if (not (and program-text (> (string-length program-text) 0)))
#f
(let* (
; Construct command line to run the colorer
(infile (tmpnam)) ; for the program text
(outfile (tmpnam)) ; for the colored tokens
(cmdline (string-append
colorer-bin " " colorer-params " "
(if (and program-type (> (string-length program-type) 0))
(string-append colorer-param-type program-type " ")
"")
colorer-param-outfile outfile " " infile)))
;(display "Command line: ")(display cmdline)(newline)
; Ignore errors
(no-errors
; Write the program text to the file and execute the colorer
(with-output-to-file infile
(lambda () (display program-text)))
;(system (string-append "cp " infile " lastin")) ; DEBUG
(system cmdline)
;(system (string-append "cp " outfile " last")) ; DEBUG
; Load the XML result, cleanup and return the result
(let* (
(eval-str (string-append "document('file://" outfile "')"))
(tree (x:eval eval-str)))
(no-errors (delete-file outfile))
(no-errors (delete-file infile))
; drop "*TOP*" and drop namespace declaration from "syn:syntax"
(cons 'syn:syntax (cdr (cdadar tree))))))))
~/p/xsieve/opt/bin/xsieve --xinclude -o testdoc.html testdoc.xsl testdoc.xml
#~/p/xsieve/opt/bin/xsieve --xinclude -o testdoc.html testdoc.xsl /home/olpa/p/xsieve/doc/book/xsieve.xml
~/p/xsieve/opt/bin/xsieve -o testdoc.html testdoc.xsl xsieve.xml
~/p/xsieve/opt/bin/xsieve --xinclude -o testdoc.html colorer-one.xsl /home/olpa/p/xsieve/example/hello/doc/listing2.xml
#~/p/xsieve/opt/bin/xsieve --xinclude -o testdoc.html testdoc.xsl /home/olpa/p/xsieve/doc/book/xsieve.xml
~/p/xsieve/opt/bin/xsieve -o testdoc.html --param callout.graphics 0 testdoc.xsl /home/olpa/p/xsieve/doc/project/xtech2006/programlisting/test.xml
; $Id: sxml-utils.scm,v 1.1 2006/03/02 04:32:58 olpa Exp $
; A copy-paste of a part of the SXML library
; from CVS: SSAX/lib/SXML-tree-trans.scm
(define (pre-post-order tree bindings)
(let* ((default-binding (assq '*default* bindings))
(text-binding (or (assq '*text* bindings) default-binding))
(text-handler ; Cache default and text bindings
(and text-binding
(if (procedure? (cdr text-binding))
(cdr text-binding) (cddr text-binding)))))
(let loop ((tree tree))
(cond
((null? tree) '())
((not (pair? tree))
(let ((trigger '*text*))
(if text-handler (text-handler trigger tree)
(error "Unknown binding for " trigger " and no default"))))
((not (symbol? (car tree))) (map loop tree)) ; tree is a nodelist
(else ; tree is an SXML node
(let* ((trigger (car tree))
(binding (or (assq trigger bindings) default-binding)))
(cond
((not binding)
(error "Unknown binding for " trigger " and no default"))
((not (pair? (cdr binding))) ; must be a procedure: handler
(apply (cdr binding) trigger (map loop (cdr tree))))
((eq? '*preorder* (cadr binding))
(apply (cddr binding) tree))
((eq? '*macro* (cadr binding))
(loop (apply (cddr binding) tree)))
(else ; (cadr binding) is a local binding
(apply (cddr binding) trigger
(pre-post-order (cdr tree) (append (cadr binding) bindings)))
))))))))
(load "sxml-utils.scm")
(load "colorer.scm")
(define main-tree '(programlisting (*PI* a "b") (@ (format "linespecific")) "<article id=\"hw\">
<title>Hello</title>
<para>Hello <object>World</object>!</para>
</article>"))
(define h-tree "")
(define result (colorer:join-markup main-tree h-tree '(h)))
(write result)
; $Id: test.scm,v 1.2 2006/03/02 06:01:06 olpa Exp $
(define (test-case main-tree h-tree expected-result)
(display "------- Running a test case...")
(let ((result (caddr (colorer:join-markup main-tree h-tree '(h)))))
(if (equal? result expected-result)
(begin
(display "Ok")(newline))
(begin
(display "Error")(newline)
(display "Expected: ")(write expected-result)(newline)
(display "Result: ")(write result)(newline)))))
(load "sxml-utils.scm")
(load "colorer.scm")
(load "testdata.scm")
<?xml version="1.0"?>
<article>
<title>Testing Syntax Highlighting</title>
<para>Testing syntax highlighting</para>
<programlisting role="xml">
&lt;para>Hello, <emphasis>&amp;who;</emphasis
>!&lt;/para> <co id="who-entity"/>
</programlisting>
</article>
; test cases for joining parallel markup
; $Id: testdata.scm,v 1.2 2006/03/02 05:58:55 olpa Exp $
; (test-case in-xml also-xml joined-xml)
; the simplest test, no highlighting at all
(test-case
'(i "012")
'(h "012")
'(i "012"))
; the simplest test, some highlighting
(test-case
'(i "012")
'(h "0" (a "1") "2")
'(i (colorer:dummy "0" (a "1") "2")))
; the size of text is different
(test-case
'(i "0123456789")
'(h (a "01") "234" (b "56") "7")
'(i (colorer:dummy (a "01") "234" (b "56") "7" "89")))
(test-case
'(i "01234567")
'(h "0" (a "12") "345" (b "5789"))
'(i (colorer:dummy "0" (a "12") "345" (b "67"))))
; the text of the main tree is not corrupted
(test-case
'(i "012345")
'(h "ab" (c "cd") "ef")
'(i (colorer:dummy "01" (c "23") "45")))
; attributes are saved
(test-case
'(i "012345")
'(h "01"
(a (@ (a1 "a1") (a2 "a2"))
(b (@ (b1 "b1") (b2 "b2"))
"23"))
"45")
'(i (colorer:dummy "01"
(a (@ (a1 "a1") (a2 "a2"))
(b (@ (b1 "b1") (b2 "b2"))
"23"))
"45")))
; ordering and nesting of empty tags
(test-case
'(i "012" (x (y)) (z) "34")
'(h "01" (a "23") "4")
'(i (colorer:dummy "01" (a "2")) (x (y)) (z) (colorer:dummy (a "3") "4")))
; intersecting at left
(test-case
'(i "01" (a "2345" (b "67")))
'(h "012" (x (y "3456")) "7")
'(i "01" (a (colorer:dummy "2" (x (y "345"))) (b (colorer:dummy (x (y "6")) "7")))))
; intersecting at right
(test-case
'(i "01" (a "23" (b "45") "6") "78")
'(h "01234" (x (y "56")) "78")
'(i "01" (a "23" (b (colorer:dummy "4" (x (y "5")))) (x (y "6"))) "78"))
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Article with code</title><meta name="generator" content="DocBook XSL Stylesheets V1.70.1" /></head><body><div class="article" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="id2434734"></a>Article with code</h2></div></div><hr /></div><p>A sample code:</p><div class="example"><a id="id2433082"></a><p class="title"><b>Example 1. </b></p><div class="example-contents"><div class="article" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="id2434675"></a>Testing Syntax Highlighting</h2></div></div><hr /></div><p>Testing syntax highlighting</p><pre class="programlisting">
<span xmlns="" xmlns:syn="http://ns.laxan.com/text-vimcolor/1" style="color:#008B8B;">&lt;para&gt;</span>Hello, <span class="emphasis"><em><span xmlns="" xmlns:syn="http://ns.laxan.com/text-vimcolor/1" style="color:#2E8B57; font-weight:bold;">&amp;</span><span xmlns="" xmlns:syn="http://ns.laxan.com/text-vimcolor/1" style="color:#A52A2A; font-weight:bold;">who</span><span xmlns="" xmlns:syn="http://ns.laxan.com/text-vimcolor/1" style="color:#2E8B57; font-weight:bold;">;</span></em></span>!<span xmlns="" xmlns:syn="http://ns.laxan.com/text-vimcolor/1" style="color:#008B8B;">&lt;/para&gt;</span> <a id="who-entity"></a><img src="images/callouts/1.png" alt="1" border="0" />
</pre></div></div></div><br class="example-break" /></div></body></html>
<article>
<title>Article with code</title>
<para>A sample code:</para>
<example>
<xi:include href="test.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
</example>
</article>
<xsl:stylesheet
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
version = "1.0">
<xsl:import href="/usr/share/sgml/docbook/xsl-stylesheets-1.70.1/xhtml/docbook.xsl"/>
<xsl:import href="colorer.xsl"/>
<xsl:import href="colorer-html.xsl"/>
</xsl:stylesheet>
/README/1.1/Fri Apr 28 07:09:09 2006//
/README-Path-Class/1.1/Fri Apr 28 07:09:09 2006//
/README-Text-VimColor/1.1/Fri Apr 28 07:09:09 2006//
/text-vimcolor/1.1/Fri Apr 28 07:09:09 2006//
/vimcolor-wrapper/1.1/Fri Apr 28 07:09:09 2006//
D/lib////
xsieve/experiments/programlisting/vimcolor
:pserver:anonymous@xsieve.cvs.sourceforge.net:/cvsroot/xsieve
Command-line syntax highlighting based on vim
It uses Perl modules Text::VimColor and Path::Class.
Perl libraries of these modules are copied to "lib".
The program "vimcolor-wrapper" sets the path to the
local copy of the libraries and runs the original
script "text-vimcolor".
NAME
Path::Class - Cross-platform path specification manipulation
SYNOPSIS
use Path::Class;
my $dir = dir('foo', 'bar'); # Path::Class::Dir object
my $file = file('bob', 'file.txt'); # Path::Class::File object
# Stringifies to 'foo/bar' on Unix, 'foo\bar' on Windows, etc.
print "dir: $dir\n";
# Stringifies to 'bob/file.txt' on Unix, 'bob\file.txt' on Windows
print "file: $file\n";
my $subdir = $dir->subdir('baz'); # foo/bar/baz
my $parent = $subdir->parent; # foo/bar
my $parent2 = $parent->parent; # foo
my $dir2 = $file->dir; # bob
# Work with foreign paths
use Path::Class qw(foreign_file foreign_dir);
my $file = foreign_file('Mac', ':foo:file.txt');
print $file->dir; # :foo:
print $file->as_foreign('Win32'); # foo\file.txt
# Interact with the underlying filesystem:
# $dir_handle is an IO::Dir object
my $dir_handle = $dir->open or die "Can't read $dir: $!";
# $file_handle is an IO::File object
my $file_handle = $file->open($mode) or die "Can't read $file: $!";
DESCRIPTION
`Path::Class' is a module for manipulation of file and directory
specifications (strings describing their locations, like
`'/home/ken/foo.txt'' or `'C:\Windows\Foo.txt'') in a cross-platform
manner. It supports pretty much every platform Perl runs on, including
Unix, Windows, Mac, VMS, Epoc, Cygwin, OS/2, and NetWare.
The well-known module `File::Spec' also provides this service, but it's
sort of awkward to use well, so people sometimes avoid it, or use it in
a way that won't actually work properly on platforms significantly
different than the ones they've tested their code on.
In fact, `Path::Class' uses `File::Spec' internally, wrapping all the
unsightly details so you can concentrate on your application code.
Whereas `File::Spec' provides functions for some common path
manipulations, `Path::Class' provides an object-oriented model of the
world of path specifications and their underlying semantics.
`File::Spec' doesn't create any objects, and its classes represent the
different ways in which paths must be manipulated on various platforms
(not a very intuitive concept). `Path::Class' creates objects
representing files and directories, and provides methods that relate
them to each other. For instance, the following `File::Spec' code:
my $absolute = File::Spec->file_name_is_absolute(
File::Spec->catfile( @dirs, $file )
);
can be written using `Path::Class' as
my $absolute = Path::Class::File->new( @dirs, $file )->is_absolute;
or even as
my $absolute = file( @dirs, $file )->is_absolute;
Similar readability improvements should happen all over the place when
using `Path::Class'.
Using `Path::Class' can help solve real problems in your code too - for
instance, how many people actually take the "volume" (like `C:' on
Windows) into account when writing `File::Spec'-using code? I thought
not. But if you use `Path::Class', your file and directory objects will
know what volumes they refer to and do the right thing.
The guts of the `Path::Class' code live in the `Path::Class::File' and
`Path::Class::Dir' modules, so please see those modules' documentation
for more details about how to use them.
EXPORT
The following functions are exported by default.
file
A synonym for `Path::Class::File->new'.
dir A synonym for `Path::Class::Dir->new'.
If you would like to prevent their export, you may explicitly pass an
empty list to perl's `use', i.e. `use Path::Class ()'.
The following are exported only on demand.
foreign_file
A synonym for `Path::Class::File->new_foreign'.
foreign_dir
A synonym for `Path::Class::Dir->new_foreign'.
Notes on Cross-Platform Compatibility
Although it is much easier to write cross-platform-friendly code with
this module than with `File::Spec', there are still some issues to be
aware of.
* Some platforms, notably VMS and some older versions of DOS (I
think), all filenames must have an extension. Thus if you create a
file called foo/bar and then ask for a list of files in the
directory foo, you may find a file called bar. instead of the bar
you were expecting. Thus it might be a good idea to use an extension
in the first place.
AUTHOR
Ken Williams, KWILLIAMS@cpan.org
COPYRIGHT
Copyright (c) Ken Williams. All rights reserved.
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
SEE ALSO
Path::Class::Dir, Path::Class::File, File::Spec
Text::VimColor
--------------
This module tries to markup text files according to their syntax. It can
be used to produce web pages with pretty-printed colourful source code
samples. It can produce output in the following formats:
The module comes with a command line program, text-vimcolor, which makes
it easy to do 'ad-hoc' syntax coloring jobs.
Geoff Richards <qef@laxan.com>
Release procedure
-----------------
* Update the version number in lib/Text/VimColor.pm and META.yml
* Update the changelog with a new section for a matching version number
and the correct date and time
* Copy the ChangeLog into place (from 'debian' directory in my CVS)
* Realclean, make and test
* Make the dist, take it to another machine and build and test there
* Commit everything, and set tag like 'Release_0_07-1'
* Upload to CPAN
xsieve/experiments/programlisting/vimcolor/lib
:pserver:anonymous@xsieve.cvs.sourceforge.net:/cvsroot/xsieve
/Class.pm/1.1/Fri Apr 28 07:17:33 2006//
D/Class////
xsieve/experiments/programlisting/vimcolor/lib/Path
:pserver:anonymous@xsieve.cvs.sourceforge.net:/cvsroot/xsieve
package Path::Class;
$VERSION = '0.15';
@ISA = qw(Exporter);
@EXPORT = qw(file dir);
@EXPORT_OK = qw(file dir foreign_file foreign_dir);
use strict;
use Exporter;
use Path::Class::File;
use Path::Class::Dir;
sub file { Path::Class::File->new(@_) }
sub dir { Path::Class::Dir ->new(@_) }
sub foreign_file { Path::Class::File->new_foreign(@_) }
sub foreign_dir { Path::Class::Dir ->new_foreign(@_) }
1;
__END__
=head1 NAME
Path::Class - Cross-platform path specification manipulation
=head1 SYNOPSIS
use Path::Class;
my $dir = dir('foo', 'bar'); # Path::Class::Dir object
my $file = file('bob', 'file.txt'); # Path::Class::File object
# Stringifies to 'foo/bar' on Unix, 'foo\bar' on Windows, etc.
print "dir: $dir\n";
# Stringifies to 'bob/file.txt' on Unix, 'bob\file.txt' on Windows
print "file: $file\n";
my $subdir = $dir->subdir('baz'); # foo/bar/baz
my $parent = $subdir->parent; # foo/bar
my $parent2 = $parent->parent; # foo
my $dir2 = $file->dir; # bob
# Work with foreign paths
use Path::Class qw(foreign_file foreign_dir);
my $file = foreign_file('Mac', ':foo:file.txt');
print $file->dir; # :foo:
print $file->as_foreign('Win32'); # foo\file.txt
# Interact with the underlying filesystem:
# $dir_handle is an IO::Dir object
my $dir_handle = $dir->open or die "Can't read $dir: $!";
# $file_handle is an IO::File object
my $file_handle = $file->open($mode) or die "Can't read $file: $!";
=head1 DESCRIPTION
C<Path::Class> is a module for manipulation of file and directory
specifications (strings describing their locations, like
C<'/home/ken/foo.txt'> or C<'C:\Windows\Foo.txt'>) in a cross-platform
manner. It supports pretty much every platform Perl runs on,
including Unix, Windows, Mac, VMS, Epoc, Cygwin, OS/2, and NetWare.
The well-known module C<File::Spec> also provides this service, but
it's sort of awkward to use well, so people sometimes avoid it, or use
it in a way that won't actually work properly on platforms
significantly different than the ones they've tested their code on.
In fact, C<Path::Class> uses C<File::Spec> internally, wrapping all
the unsightly details so you can concentrate on your application code.
Whereas C<File::Spec> provides functions for some common path
manipulations, C<Path::Class> provides an object-oriented model of the
world of path specifications and their underlying semantics.
C<File::Spec> doesn't create any objects, and its classes represent
the different ways in which paths must be manipulated on various
platforms (not a very intuitive concept). C<Path::Class> creates
objects representing files and directories, and provides methods that
relate them to each other. For instance, the following C<File::Spec>
code:
my $absolute = File::Spec->file_name_is_absolute(
File::Spec->catfile( @dirs, $file )
);
can be written using C<Path::Class> as
my $absolute = Path::Class::File->new( @dirs, $file )->is_absolute;
or even as
my $absolute = file( @dirs, $file )->is_absolute;
Similar readability improvements should happen all over the place when
using C<Path::Class>.
Using C<Path::Class> can help solve real problems in your code too -
for instance, how many people actually take the "volume" (like C<C:>
on Windows) into account when writing C<File::Spec>-using code? I
thought not. But if you use C<Path::Class>, your file and directory objects
will know what volumes they refer to and do the right thing.
The guts of the C<Path::Class> code live in the C<Path::Class::File>
and C<Path::Class::Dir> modules, so please see those
modules' documentation for more details about how to use them.
=head2 EXPORT
The following functions are exported by default.
=over 4
=item file
A synonym for C<< Path::Class::File->new >>.
=item dir
A synonym for C<< Path::Class::Dir->new >>.
=back
If you would like to prevent their export, you may explicitly pass an
empty list to perl's C<use>, i.e. C<use Path::Class ()>.
The following are exported only on demand.
=over 4
=item foreign_file
A synonym for C<< Path::Class::File->new_foreign >>.
=item foreign_dir
A synonym for C<< Path::Class::Dir->new_foreign >>.
=back
=head1 Notes on Cross-Platform Compatibility
Although it is much easier to write cross-platform-friendly code with
this module than with C<File::Spec>, there are still some issues to be
aware of.
=over 4
=item *
Some platforms, notably VMS and some older versions of DOS (I think),
all filenames must have an extension. Thus if you create a file
called F<foo/bar> and then ask for a list of files in the directory
F<foo>, you may find a file called F<bar.> instead of the F<bar> you
were expecting. Thus it might be a good idea to use an extension in
the first place.
=back
=head1 AUTHOR
Ken Williams, KWILLIAMS@cpan.org
=head1 COPYRIGHT
Copyright (c) Ken Williams. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 SEE ALSO
Path::Class::Dir, Path::Class::File, File::Spec
=cut
/Dir.pm/1.1/Fri Apr 28 07:17:33 2006//
/Entity.pm/1.1/Fri Apr 28 07:17:33 2006//
/File.pm/1.1/Fri Apr 28 07:17:33 2006//
D
xsieve/experiments/programlisting/vimcolor/lib/Path/Class
:pserver:anonymous@xsieve.cvs.sourceforge.net:/cvsroot/xsieve
This diff is collapsed.
package Path::Class::Entity;
use strict;
use File::Spec;
use File::stat ();
use overload
(
q[""] => 'stringify',
fallback => 1,
);
sub new {
my $from = shift;
my ($class, $fs_class) = (ref($from)
? (ref $from, $from->{file_spec_class})
: ($from, $Path::Class::Foreign));
return bless {file_spec_class => $fs_class}, $class;
}
sub is_dir { 0 }
sub _spec_class {
my ($class, $type) = @_;
die "Invalid system type '$type'" unless ($type) = $type =~ /^(\w+)$/; # Untaint
my $spec = "File::Spec::$type";
eval "require $spec; 1" or die $@;
return $spec;
}
sub new_foreign {
my ($class, $type) = (shift, shift);
local $Path::Class::Foreign = $class->_spec_class($type);
return $class->new(@_);
}
sub _spec { $_[0]->{file_spec_class} || 'File::Spec' }
sub is_absolute {
# 5.6.0 has a bug with regexes and stringification that's ticked by
# file_name_is_absolute(). Help it along.
$_[0]->_spec->file_name_is_absolute($_[0]->stringify)
}
sub cleanup {
my $self = shift;
my $cleaned = $self->new( $self->_spec->canonpath($self) );
%$self = %$cleaned;
return $self;
}
sub absolute {
my $self = shift;
return $self if $self->is_absolute;
return $self->new($self->_spec->rel2abs($self->stringify, @_));
}
sub relative {
my $self = shift;
return $self->new($self->_spec->abs2rel($self->stringify, @_));
}
sub stat { File::stat::stat("$_[0]") }
sub lstat { File::stat::lstat("$_[0]") }
1;
package Path::Class::File;
use strict;
use Path::Class::Dir;
use Path::Class::Entity;
use base qw(Path::Class::Entity);
use IO::File ();
sub new {
my $self = shift->SUPER::new;
my $file = pop();
my @dirs = @_;
my ($volume, $dirs, $base) = $self->_spec->splitpath($file);
if (length $dirs) {
push @dirs, $self->_spec->catpath($volume, $dirs, '');
}
$self->{dir} = @dirs ? Path::Class::Dir->new(@dirs) : undef;
$self->{file} = $base;
return $self;
}
sub as_foreign {
my ($self, $type) = @_;
local $Path::Class::Foreign = $self->_spec_class($type);
my $foreign = ref($self)->SUPER::new;
$foreign->{dir} = $self->{dir}->as_foreign($type) if defined $self->{dir};
$foreign->{file} = $self->{file};
return $foreign;
}
sub stringify {
my $self = shift;
return $self->{file} unless defined $self->{dir};
return $self->_spec->catfile($self->{dir}->stringify, $self->{file});
}
sub dir {
my $self = shift;
return $self->{dir} if defined $self->{dir};
return Path::Class::Dir->new($self->_spec->curdir);
}
BEGIN { *parent = \&dir; }
sub volume {
my $self = shift;
return '' unless defined $self->{dir};
return $self->{dir}->volume;
}
sub basename { shift->{file} }
sub open { IO::File->new(@_) }
sub openr { $_[0]->open('r') or die "Can't read $_[0]: $!" }
sub openw { $_[0]->open('w') or die "Can't write $_[0]: $!" }
sub touch {
my $self = shift;
if (-e $self) {
my $now = time();
utime $now, $now, $self;
} else {
$self->openw;
}
}
sub slurp {
my ($self, %args) = @_;
my $fh = $self->openr;
if ($args{chomped} or $args{chomp}) {
chomp( my @data = <$fh> );
return wantarray ? @data : join '', @data;
}
local $/ unless wantarray;
return <$fh>;
}
sub remove {
my $file = shift->stringify;
return unlink $file unless -e $file; # Sets $! correctly
1 while unlink $file;
return not -e $file;
}
1;
__END__
=head1 NAME
Path::Class::File - Objects representing files
=head1 SYNOPSIS
use Path::Class qw(file); # Export a short constructor
my $file = file('foo', 'bar.txt'); # Path::Class::File object
my $file = Path::Class::File->new('foo', 'bar.txt'); # Same thing
# Stringifies to 'foo/bar.txt' on Unix, 'foo\bar.txt' on Windows, etc.
print "file: $file\n";
if ($file->is_absolute) { ... }
my $v = $file->volume; # Could be 'C:' on Windows, empty string
# on Unix, 'Macintosh HD:' on Mac OS
$file->cleanup; # Perform logical cleanup of pathname
my $dir = $file->dir; # A Path::Class::Dir object
my $abs = $file->absolute; # Transform to absolute path
my $rel = $file->relative; # Transform to relative path
=head1 DESCRIPTION
The C<Path::Class::File> class contains functionality for manipulating
file names in a cross-platform way.
=head1 METHODS
=over 4
=item $file = Path::Class::File->new( <dir1>, <dir2>, ..., <file> )
=item $file = file( <dir1>, <dir2>, ..., <file> )
Creates a new C<Path::Class::File> object and returns it. The
arguments specify the path to the file. Any volume may also be
specified as the first argument, or as part of the first argument.
You can use platform-neutral syntax:
my $dir = file( 'foo', 'bar', 'baz.txt' );
or platform-native syntax:
my $dir = dir( 'foo/bar/baz.txt' );
or a mixture of the two:
my $dir = dir( 'foo/bar', 'baz.txt' );
All three of the above examples create relative paths. To create an
absolute path, either use the platform native syntax for doing so:
my $dir = dir( '/var/tmp/foo.txt' );
or use an empty string as the first argument:
my $dir = dir( '', 'var', 'tmp', 'foo.txt' );
If the second form seems awkward, that's somewhat intentional - paths
like C</var/tmp> or C<\Windows> aren't cross-platform concepts in the
first place, so they probably shouldn't appear in your code if you're
trying to be cross-platform. The first form is perfectly fine,
because paths like this may come from config files, user input, or
whatever.
=item $file->stringify
This method is called internally when a C<Path::Class::File> object is
used in a string context, so the following are equivalent:
$string = $file->stringify;
$string = "$file";
=item $file->volume
Returns the volume (e.g. C<C:> on Windows, C<Macintosh HD:> on Mac OS,
etc.) of the object, if any. Otherwise, returns the empty string.
=item $file->basename
Returns the name of the file as a string, without the directory
portion (if any).
=item $file->is_dir
Returns a boolean value indicating whether this object represents a
directory. Not surprisingly, C<Path::Class::File> objects always
return false, and C<Path::Class::Dir> objects always return true.
=item $file->is_absolute
Returns true or false depending on whether the file refers to an
absolute path specifier (like C</usr/local/foo.txt> or C<\Windows\Foo.txt>).
=item $file->cleanup
Performs a logical cleanup of the file path. For instance:
my $file = file('/foo//baz/./foo.txt')->cleanup;
# $file now represents '/foo/baz/foo.txt';
=item $dir = $file->dir
Returns a C<Path::Class::Dir> object representing the directory
containing this file.
=item $dir = $file->parent
A synonym for the C<dir()> method.
=item $abs = $file->absolute
Returns a C<Path::Class::File> object representing C<$file> as an
absolute path. An optional argument, given as either a string or a
C<Path::Class::Dir> object, specifies the directory to use as the base
of relativity - otherwise the current working directory will be used.
=item $rel = $file->relative
Returns a C<Path::Class::File> object representing C<$file> as a
relative path. An optional argument, given as either a string or a
C<Path::Class::Dir> object, specifies the directory to use as the base
of relativity - otherwise the current working directory will be used.
=item $foreign = $file->as_foreign($type)
Returns a C<Path::Class::File> object representing C<$file> as it would
be specified on a system of type C<$type>. Known types include
C<Unix>, C<Win32>, C<Mac>, C<VMS>, and C<OS2>, i.e. anything for which
there is a subclass of C<File::Spec>.
Any generated objects (subdirectories, files, parents, etc.) will also
retain this type.
=item $foreign = Path::Class::File->new_foreign($type, @args)
Returns a C<Path::Class::File> object representing a file as it would
be specified on a system of type C<$type>. Known types include
C<Unix>, C<Win32>, C<Mac>, C<VMS>, and C<OS2>, i.e. anything for which
there is a subclass of C<File::Spec>.
The arguments in C<@args> are the same as they would be specified in
C<new()>.
=item $fh = $file->open($mode, $permissions)
Passes the given arguments, including C<$file>, to C<< IO::File->new >>
(which in turn calls C<< IO::File->open >> and returns the result
as an C<IO::File> object. If the opening
fails, C<undef> is returned and C<$!> is set.
=item $fh = $file->openr()
A shortcut for
$fh = $file->open('r') or die "Can't read $file: $!";
=item $fh = $file->openw()
A shortcut for
$fh = $file->open('w') or die "Can't write $file: $!";
=item $file->touch
Sets the modification and access time of the given file to right now,
if the file exists. If it doesn't exist, C<touch()> will I<make> it
exist, and - YES! - set its modification and access time to now.
=item $file->slurp()
In a scalar context, returns the contents of C<$file> in a string. In
a list context, returns the lines of C<$file> (according to how C<$/>
is set) as a list. If the file can't be read, this method will throw
an exception.
If you want C<chomp()> run on each line of the file, pass a true value
for the C<chomp> or C<chomped> parameters:
my @lines = $file->slurp(chomp => 1);
=item $file->remove()
This method will remove the file in a way that works well on all
platforms, and returns a boolean value indicating whether or not the
file was successfully removed.
C<remove()> is better than simply calling Perl's C<unlink()> function,
because on some platforms (notably VMS) you actually may need to call
C<unlink()> several times before all versions of the file are gone -
the C<remove()> method handles this process for you.
=item $st = $file->stat()
Invokes C<< File::stat::stat() >> on this file and returns a
C<File::stat> object representing the result.
=item $st = $file->lstat()
Same as C<stat()>, but if C<$file> is a symbolic link, C<lstat()>
stats the link instead of the file the link points to.
=back
=head1 AUTHOR
Ken Williams, ken@mathforum.org
=head1 SEE ALSO
Path::Class, Path::Class::Dir, File::Spec
=cut
/VimColor.pm/1.1/Fri Apr 28 07:16:26 2006//
D/VimColor////
xsieve/experiments/programlisting/vimcolor/lib/Text
:pserver:anonymous@xsieve.cvs.sourceforge.net:/cvsroot/xsieve
This diff is collapsed.
/light.css/1.1/Fri Apr 28 07:16:26 2006//
/light.xsl/1.1/Fri Apr 28 07:16:26 2006//
/mark.vim/1.1/Fri Apr 28 07:16:26 2006//
D
xsieve/experiments/programlisting/vimcolor/lib/Text/VimColor
:pserver:anonymous@xsieve.cvs.sourceforge.net:/cvsroot/xsieve
/*
* A stylesheet designed to be used with the HTML output of the
* Perl module Text::Highlight::Vim.
*
* This is designed to make the highlighting look like the default gvim
* colour scheme, with 'background=light'.
*
* Geoff Richards (qef@laxan.com)
*
* This CSS file (light.css) is public domain. Do what you want with it.
* That doesn't mean that HTML with this CSS in is public domain.
*/
body { color: black; background: white none }
A:link { color: #00F; background: white none }
A:visited { color: #909; background: white none }
A:hover { color: #F00; background: white none }
A:active { color: #F00; background: white none }
.synComment { color: #0000FF }
.synConstant { color: #FF00FF }
.synIdentifier { color: #008B8B }
.synStatement { color: #A52A2A ; font-weight: bold }
.synPreProc { color: #A020F0 }
.synType { color: #2E8B57 ; font-weight: bold }
.synSpecial { color: #6A5ACD }
.synUnderlined { color: #000000 ; text-decoration: underline }
.synError { color: #FFFFFF ; background: #FF0000 none }
.synTodo { color: #0000FF ; background: #FFFF00 none }
<?xml version="1.0"?>
<!--
This is an XSLT/XSL-FO stylesheet designed to be used with the XML
output of the Perl module Text::VimColor.
This is designed to make the highlighting look like the default gvim
colour scheme, with 'background=light'.
Geoff Richards <qef@laxan.com>
This XSL file (light.xsl) is public domain. Do what you want with it.
Bugs: background colouring doesn't work in FOP.
-->
<xsl:stylesheet version="1.0"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:syn="http://ns.laxan.com/text-vimcolor/1">
<xsl:template match="syn:syntax">
<fo:root>
<fo:layout-master-set>
<!-- Master for odd (right hand) pages -->
<fo:simple-page-master master-name="recto"
page-height="297mm" page-width="210mm"
margin-top="10mm" margin-left="25mm"
margin-bottom="10mm" margin-right="15mm">
<fo:region-body margin-top="10mm" margin-bottom="10mm"/>
<fo:region-before extent="10mm"/>
<fo:region-after extent="10mm"/>
</fo:simple-page-master>
<!-- Master for even (left hand) pages -->
<fo:simple-page-master master-name="verso"
page-height="297mm" page-width="210mm"
margin-top="10mm" margin-left="15mm"
margin-bottom="10mm" margin-right="25mm">
<fo:region-body margin-top="10mm" margin-bottom="10mm"/>
<fo:region-before extent="10mm"/>
<fo:region-after extent="10mm"/>
</fo:simple-page-master>
<fo:page-sequence-master master-name="recto-verso">
<fo:repeatable-page-master-alternatives>
<fo:conditional-page-master-reference
master-name="recto" odd-or-even="odd"/>
<fo:conditional-page-master-reference
master-name="verso" odd-or-even="even"/>
</fo:repeatable-page-master-alternatives>
</fo:page-sequence-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="recto">
<!-- Header -->
<fo:static-content flow-name="xsl-region-before">
<fo:block text-align="end" font-size="10pt"
font-family="sans-serif" font-style="italic">
<xsl:value-of select="@filename"/>
</fo:block>
</fo:static-content>
<!-- Footer -->
<fo:static-content flow-name="xsl-region-after">
<fo:block text-align="end" font-size="10pt" font-family="sans-serif">
<fo:page-number/>
</fo:block>
</fo:static-content>
<!-- Body text -->
<fo:flow flow-name="xsl-region-body">
<fo:block font-family="monospace" font-size="10pt" line-height="12pt"
white-space-collapse="false">
<xsl:apply-templates/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="syn:Comment">
<fo:inline color="#0000FF"><xsl:apply-templates/></fo:inline>
</xsl:template>
<xsl:template match="syn:Constant">
<fo:inline color="#FF00FF"><xsl:apply-templates/></fo:inline>
</xsl:template>
<xsl:template match="syn:Identifier">
<fo:inline color="#008B8B"><xsl:apply-templates/></fo:inline>
</xsl:template>
<xsl:template match="syn:Statement">
<fo:inline color="#A52A2A" font-weight="bold"><xsl:apply-templates/></fo:inline>
</xsl:template>
<xsl:template match="syn:PreProc">
<fo:inline color="#A020F0"><xsl:apply-templates/></fo:inline>
</xsl:template>
<xsl:template match="syn:Type">
<fo:inline color="#2E8B57" font-weight="bold"><xsl:apply-templates/></fo:inline>
</xsl:template>
<xsl:template match="syn:Special">
<fo:inline color="#6A5ACD"><xsl:apply-templates/></fo:inline>
</xsl:template>
<xsl:template match="syn:Underlined">
<fo:inline text-decoration="underline"><xsl:apply-templates/></fo:inline>
</xsl:template>
<xsl:template match="syn:Error">
<fo:inline color="#FFFFFF" background-color="#FF0000"><xsl:apply-templates/></fo:inline>
</xsl:template>
<xsl:template match="syn:Todo">
<fo:inline color="#0000FF" background-color="#FFFF00"><xsl:apply-templates/></fo:inline>
</xsl:template>
</xsl:stylesheet>
" mark.vim - turn Vim syntax highlighting into an ad-hoc markup language that
" can be parsed by the Text::VimColor Perl module.
"
" Maintainer: Geoff Richards <qef@laxan.com>
" Based loosely on 2html.vim, by Bram Moolenaar <Bram@vim.org>,
" modified by David Ne\v{c}as (Yeti) <yeti@physics.muni.cz>.
set report=1000000
" For some reason (I'm sure it used to work) we now need to get Vim
" to make another attempt to detect the filetype if it wasn't set
" explicitly.
if !strlen(&filetype)
filetype detect
endif
syn on
" Set up the output buffer.
new
set modifiable
set paste
" Expand tabs. Without this they come out as '^I'.
set isprint+=9
wincmd p
" Loop over all lines in the original text
let s:end = line("$")
let s:lnum = 1
while s:lnum <= s:end
" Get the current line
let s:line = getline(s:lnum)
let s:len = strlen(s:line)
let s:new = ""
" Loop over each character in the line
let s:col = 1
while s:col <= s:len
let s:startcol = s:col " The start column for processing text
let s:id = synID(s:lnum, s:col, 1)
let s:col = s:col + 1
" Speed loop (it's small - that's the trick)
" Go along till we find a change in synID
while s:col <= s:len && s:id == synID(s:lnum, s:col, 1) | let s:col = s:col + 1 | endwhile
" Output the text with the same synID, with class set to c{s:id}
let s:id = synIDtrans(s:id)
let s:name = synIDattr(s:id, 'name')
let s:new = s:new . '>' . s:name . '>' . substitute(substitute(substitute(strpart(s:line, s:startcol - 1, s:col - s:startcol), '&', '\&a', 'g'), '<', '\&l', 'g'), '>', '\&g', 'g') . '<' . s:name . '<'
if s:col > s:len
break
endif
endwhile
exe "normal \<C-W>pa" . strtrans(s:new) . "\n\e\<C-W>p"
let s:lnum = s:lnum + 1
+
endwhile
" Strip whitespace from the ends of lines
%s:\s\+$::e
wincmd p
normal dd
#!/usr/bin/perl -w
use strict;
use Text::VimColor;
use Getopt::Long;
use File::Temp qw( tempfile );
use IO::File;
use Path::Class qw( file );
my $XSL_STYLESHEET = file($Text::VimColor::SHARED, 'light.xsl');
# Default values for options.
my $filetype;
my $format;
my $usage;
my $output_filename;
my $html_full_page;
my $html_no_inline_stylesheet;
my @let;
my @unlet;
my $option = GetOptions(
'debug' => \$Text::VimColor::DEBUG,
'filetype=s' => \$filetype,
'format=s' => \$format,
'help' => \$usage,
'output=s' => \$output_filename,
'full-page' => \$html_full_page,
'no-inline-stylesheet' => \$html_no_inline_stylesheet,
'let=s' => \@let,
'unlet=s' => \@unlet,
'usage' => \$usage,
);
if ($usage) {
print STDERR
"Usage: $0 --format html|xml [options] filename\n",
" $0 --format pdf --output foo.pdf [options] filename\n",
"(the output is written to standard output, except in PDF\n",
"mode, where you have to supply a filename for the output.)\n",
"\n",
"Options:\n",
" --debug turn on Text::VimColor debugging mode\n",
" --filetype set Vim filetype name, if it can't be guessed from\n",
" the file's name or contents\n",
" --format set format to use for output, can be xml,\n",
" html, or pdf\n",
" --help show this helpful message\n",
" --output set filename to write output to (required with\n",
" PDF format, otherwise defaults to standard output)\n",
" --full-page output a complete HTML page, not just a fragment\n",
" --no-inline-stylesheet\n",
" don't include the stylesheet in a complete HTML page\n",
" --let set a Vim variable with the Vim :let command\n",
" --unlet turn off default setting of a Vim variable\n";
}
defined $format
or die "$0: an output format must be specified (html, pdf or xml).\n";
$format = lc $format;
$format eq 'html' || $format eq 'pdf' || $format eq 'xml'
or die "$0: invalid output format '$format' (must be html, pdf or xml).\n";
my $output;
if (defined $output_filename) {
$output = IO::File->new($output_filename, 'w')
or die "$0: error opening output file '$output_filename': $!\n";
}
else {
$format ne 'pdf'
or die "$0: an output file must be specified with '--format pdf'.\n";
$output = \*STDOUT;
$output_filename = '<stdout>';
}
@ARGV <= 1
or die "$0: only one input filename should be specified.\n";
my $file = @ARGV ? shift : \*STDIN;
my $syntax = Text::VimColor->new(
filetype => $filetype,
html_full_page => $html_full_page,
html_inline_stylesheet => !$html_no_inline_stylesheet,
);
# Handle the --let and --unlet options.
foreach (@unlet) {
$syntax->vim_let($_ => undef);
}
foreach (@let) {
my ($name, $value) = /^(.*?)=(.*)\z/
or die "$0: bad --let option '$_'\n";
print STDERR "[$name] [$value]\n";
$syntax->vim_let($name => $value);
}
$syntax->syntax_mark_file($file);
if ($format eq 'xml') {
print $output $syntax->xml
or die "$0: error writing to output file '$output_filename': $!\n";
}
elsif ($format eq 'html') {
print $output $syntax->html
or die "$0: error writing to output file '$output_filename': $!\n";
}
else { # ($format eq 'pdf')
my ($fh, $tmp_filename) = tempfile();
print $fh $syntax->xml
or die "$0: error writing to temporary file '$tmp_filename': $!\n";
system('fop', '-xsl', $XSL_STYLESHEET,
'-xml', $tmp_filename,
'-pdf', $output_filename) == 0
or die "$0: error running 'fop' (exit code was $?).\n";
unlink $tmp_filename
or die "$0: error deleting temporary file '$tmp_filename': $!\n";
}
exit 0;
__END__
=head1 NAME
text-vimcolor - command-line program to syntax color a file in HTML, XML or PDF
=head1 SYNOPSIS
$ text-vimcolor --format html --full-page FILENAME > OUTPUT.html
$ text-vimcolor --format xml FILENAME > OUTPUT.xml
$ text-vimcolor --format pdf FILENAME --output OUTPUT.pdf
=head1 DESCRIPTION
This program uses the Vim text editor to highlight text according to its
syntax, and turn the highlighting into HTML, XML or PDF output. It works
with any file type which Vim itself can highlight. Usually Vim will be
able to autodetect the file format based on the filename (and sometimes the
contents of the file).
Exactly one filename should be given on the command line to name the input
file. If none is given input will instead be read from stdin (the standard
input).
If Vim can't guess the file type automatically, it can be specified explicitly
using the C<--filetype> option. For example:
$ text-vimcolor --format html --filetype prolog foo.pl > foo.html
This program is a command line interface to the Perl module Text::VimColor.
=head1 OPTIONS
The following options are understood:
=over 4
=item --help
Show a summary of the usage, including a list of options.
=item --debug
Turns on debugging in the underlying Perl module. This makes it print
the command used to run Vim.
=item --filetype I<file-type>
Set the type of the file explicitly. The I<file-type> argument should be
something which Vim will recognise when set with its C<filetype> option.
Examples are C<perl>, C<cpp> (for C++) and C<sh> (for Unix shell scripts).
These names are case sensitive, and should usually be all-lowercase.
=item --format I<output-format>
The output format to generate. Must be one of the following:
=over 4
=item html
Generate XHTML output, with text marked with C<E<lt>spanE<gt>> elements
with C<class> attributes. A CSS stylesheet should be used to define the
coloring, etc., for the output. See the C<--full-page> option below.
=item xml
Output is in a simple XML vocabulary. This can then be used by other
software to do further transformations (e.g., using XSLT).
=item pdf
XML output is generated and fed to the FOP XSL-FO processor, with an
appropriate XSL style sheet. The stylesheet uses XSLT to transform the
normal XML output into XSL-FO, which is then rendered to PDF. For this
to work, the command C<fop> must be available. An output file must be
specified with C<--output> with this format.
=back
Full details of the HTML and XML output formats can be found in the
documentation for Text::VimColor.
=item --output I<output-filename>
Specifies the name of the output file (which will end up containing either
HTML, XML or PDF). If this option is omitted, the output will be sent
to stdout (the standard output). This option is required when the output
format is PDF (because of limitations in FOP).
=item --full-page
When the output format is HTML, this option will make the output a complete
HTML page, rather than just a fragment of HTML. A CSS stylesheet will be
inserted inline into the output, so the output will be useable as it is.
=item --no-inline-stylesheet
When the output format is HTML and C<--fullpage> is given, a stylesheet
is normally inserted in-line in the output file. If this option is given it
will instead be referenced with a C<E<lt>linkE<gt>> element.
=item --let I<name>=I<value>
When Vim is run the value of I<name> will be set to I<value> using
Vim's C<let> command. More than one of these options can be set.
The value is not quoted or escaped in any way, so it can be an expression.
These settings take precedence over C<--unlet> options.
This option corresponds to the C<vim_let> setting and method in
the Perl module.
=item --unlet I<name>
Prevent the value of I<name> being set with Vim's C<let> command.
This can be used to turn off default settings.
This option corresponds to the C<vim_let> setting and method in
the Perl module, when used with a value of C<undef>.
=back
=head1 BUGS
=over 4
=item *
The PDF output option often doesn't work, because it is dependent on FOP,
which often doesn't work. This is also why it is mind numbingly slow.
=item *
FOP (0.20.3) seems to ignore the C<background-color> property on
C<E<lt>fo:inlineE<gt>>. If that's what it's meant to do, how do you set the
background color on part of a line?
=back
=head1 AUTHOR
Geoff Richards E<lt>qef@laxan.comE<gt>
=head1 COPYRIGHT
Copyright 2002-2006, Geoff Richards.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl.
=cut
# vi:ts=3 sw=3 expandtab
#!/bin/sh
DIR=`dirname $0`
PERL5LIB=$DIR/lib:$PERL5LIB
export PERL5LIB
$DIR/text-vimcolor "$@"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment