Browse files

Add syntax highlighting for PDFs

* Requires "verbatim" XSL to be included
* Use saxon instead of xsltproc so xslthl can be included
* Add some color to the syntax highlighting
* Use homebrew specific location for highlighting configuration.
  It was very unhappy with relative paths and wouldn't take the option
  on the java command line :(
* Vendor saxon 6.5 (jar in docbook-xsl doesn't work for some reason)
  • Loading branch information...
1 parent cb9428c commit cddb437d27e01c4fda7ae5a51bb47cdd57595acb @nickrivadeneira nickrivadeneira committed Sep 16, 2011
View
1 .gitignore
@@ -1,2 +1,3 @@
example/output
output
+.DS_Store
View
4 docbook-xsl/fo.xsl
@@ -16,6 +16,10 @@
<xsl:import href="http://docbook.sourceforge.net/release/xsl/current/fo/docbook.xsl"/>
<xsl:import href="common.xsl"/>
+<xsl:import href="fo/verbatim.xsl"/>
+<xsl:param name="highlight.source" select="1"></xsl:param>
+<xsl:param name="highlight.xslthl.config">file:///usr/local/Cellar/docbook/5.0/docbook/xsl/1.76.1/highlighting/xslthl-config.xml</xsl:param>
+
<xsl:param name="fop1.extensions" select="1" />
<xsl:param name="variablelist.as.blocks" select="1" />
View
16 docbook-xsl/fo/highlight.xsl
@@ -15,18 +15,10 @@
******************************************************************** -->
-<xsl:template match='xslthl:keyword' mode="xslthl">
- <fo:inline font-weight="bold"><xsl:apply-templates mode="xslthl"/></fo:inline>
-</xsl:template>
-
<xsl:template match='xslthl:string' mode="xslthl">
<fo:inline font-weight="bold" font-style="italic"><xsl:apply-templates mode="xslthl"/></fo:inline>
</xsl:template>
-<xsl:template match='xslthl:comment' mode="xslthl">
- <fo:inline font-style="italic"><xsl:apply-templates mode="xslthl"/></fo:inline>
-</xsl:template>
-
<xsl:template match='xslthl:tag' mode="xslthl">
<fo:inline font-weight="bold"><xsl:apply-templates mode="xslthl"/></fo:inline>
</xsl:template>
@@ -39,6 +31,14 @@
<fo:inline font-weight="bold"><xsl:apply-templates mode="xslthl"/></fo:inline>
</xsl:template>
+<xsl:template match='xslthl:keyword' mode="xslthl">
+ <fo:inline font-weight="bold" color="blue"><xsl:apply-templates mode="xslthl"/></fo:inline>
+</xsl:template>
+
+<xsl:template match='xslthl:comment' mode="xslthl">
+ <fo:inline font-style="italic" color="grey"><xsl:apply-templates mode="xslthl"/></fo:inline>
+</xsl:template>
+
<!--
<xsl:template match='xslthl:html'>
<span style='background:#AFF'><font color='blue'><xsl:apply-templates/></font></span>
View
28 lib/git-scribe/generate.rb
@@ -57,16 +57,23 @@ def do_docbook
def do_pdf
info "GENERATING PDF"
do_docbook
- # TODO: syntax highlighting (fop?)
- strparams = {'callout.graphics' => 0,
- 'navig.graphics' => 0,
- 'admon.textlabel' => 1,
- 'admon.graphics' => 0}
- param = strparams.map { |k, v| "--stringparam #{k} #{v}" }.join(' ')
- cmd = "xsltproc --nonet #{param} --output #{local('book.fo')} #{base('docbook-xsl/fo.xsl')} #{local('book.xml')}"
- ex(cmd)
- cmd = "fop -fo #{local('book.fo')} -pdf #{local('book.pdf')}"
- ex(cmd)
+
+ params = {
+ 'callout.graphics' => 0,
+ 'navig.graphics' => 0,
+ 'admon.textlabel' => 1,
+ 'admon.graphics' => 0,
+ }.map { |k, v| "-D#{k}=#{v}" }.join(' ')
+
+ ex <<-SH
+ java -cp "#{base('vendor/saxon.jar')}:#{base('vendor/xslthl-2.0.2.jar')}" \
+ #{params} \
+ com.icl.saxon.StyleSheet \
+ -o #{local('book.fo')} \
+ #{local('book.xml')} #{base('docbook-xsl/fo.xsl')}
+ SH
+ ex "fop -fo #{local('book.fo')} -pdf #{local('book.pdf')}"
+
if $?.exitstatus == 0
'book.pdf'
end
@@ -338,6 +345,5 @@ def ex(command)
info out
$?.exitstatus == 0
end
-
end
end
View
12 test/gen_test.rb
@@ -52,6 +52,18 @@
end
end
+ test "scribe can generate a pdf with syntax highlighting" do
+ in_temp_dir do
+ @scribe.init('t')
+ Dir.chdir('t') do
+ data = @scribe.gen('pdf')
+ fo = Nokogiri::XML(File.read('output/book.fo'))
+ fo.remove_namespaces!
+ assert fo.at_css "inline[font-weight=bold][color=blue]:contains('char')"
+ end
+ end
+ end
+
test "scribe can generate a epub" do
in_temp_dir do
@scribe.init('t')
View
BIN vendor/saxon.jar
Binary file not shown.
View
BIN vendor/xslthl-2.0.2.jar
Binary file not shown.

0 comments on commit cddb437

Please sign in to comment.