Skip to content

Commit

Permalink
Merge pull request #34 from thoughtbot/master
Browse files Browse the repository at this point in the history
Add syntax highlighting for PDFs
  • Loading branch information
schacon committed Sep 16, 2011
2 parents 6641024 + 187875f commit 048a3c1
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
example/output
output
.DS_Store
2 changes: 2 additions & 0 deletions Gemfile
@@ -0,0 +1,2 @@
source :rubygems
gemspec
22 changes: 22 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,22 @@
PATH
remote: .
specs:
git-scribe (0.0.9)
liquid
nokogiri

GEM
remote: http://rubygems.org/
specs:
liquid (2.2.2)
nokogiri (1.5.0)
rake (0.9.2)
test-unit (2.3.2)

PLATFORMS
ruby

DEPENDENCIES
git-scribe!
rake
test-unit
9 changes: 5 additions & 4 deletions Rakefile
Expand Up @@ -8,12 +8,13 @@ end

task :default => :test

desc "Run the test suite"
task :test do
sh('turn test/**/*_test.rb')
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/*_test.rb']
t.verbose = true
end


#
# Publishing
#
Expand Down
4 changes: 4 additions & 0 deletions docbook-xsl/fo.xsl
Expand Up @@ -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" />

Expand Down
16 changes: 8 additions & 8 deletions docbook-xsl/fo/highlight.xsl
Expand Up @@ -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>
Expand All @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions git-scribe.gemspec
Expand Up @@ -22,6 +22,8 @@ Gem::Specification.new do |s|

s.add_dependency('nokogiri')
s.add_dependency('liquid')
s.add_development_dependency("rake")
s.add_development_dependency("test-unit")

s.executables = %w( git-scribe )

Expand Down
2 changes: 1 addition & 1 deletion lib/git-scribe/check.rb
Expand Up @@ -65,7 +65,7 @@ def check(args = [])

def check_can_run(command)
`#{command} 2>&1`
$?.exitstatus == 0
$?.success?
end
end
end
36 changes: 21 additions & 15 deletions lib/git-scribe/generate.rb
Expand Up @@ -42,7 +42,7 @@ def a2x(type)
end

def a2x_wss(type)
a2x(type) + " --stylesheet=stylesheets/handbookish.css"
a2x(type) + " --stylesheet=stylesheets/scribe.css"
end

def do_docbook
Expand All @@ -57,17 +57,24 @@ 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)
if $?.exitstatus == 0

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 $?.success?
'book.pdf'
end
end
Expand Down Expand Up @@ -109,7 +116,7 @@ def do_site
# TODO: check if html was already done
ex("asciidoc -b docbook #{BOOK_FILE}")
xsldir = base('docbook-xsl/xhtml')
ex("xsltproc --stringparam html.stylesheet stylesheets/handbookish.css --nonet #{xsldir}/chunk.xsl book.xml")
ex("xsltproc --stringparam html.stylesheet stylesheets/scribe.css --nonet #{xsldir}/chunk.xsl book.xml")

source = File.read('index.html')
html = Nokogiri::HTML.parse(source, nil, 'utf-8')
Expand Down Expand Up @@ -336,8 +343,7 @@ def gather_and_process
def ex(command)
out = `#{command} 2>&1`
info out
$?.exitstatus == 0
$?.success?
end

end
end
4 changes: 2 additions & 2 deletions test/check_test.rb
Expand Up @@ -10,14 +10,14 @@
assert_equal status.size, 6
end

# there no option '-version' for apache fop cli
# there no option '-failboat' for apache fop cli
# it accepts only '-v' option but doesn't exit immediately.
# it should be additional no-op flag provided (like '-out list')
#
# see http://svn.apache.org/repos/asf/xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java
test "scribe should correctly check fop availability" do
assert_equal @scribe.check_can_run('fop -v -out list'), true
assert_equal @scribe.check_can_run('fop -v'), false
assert_equal @scribe.check_can_run('fop -version'), false
assert_equal @scribe.check_can_run('fop -failboat'), false
end
end
18 changes: 15 additions & 3 deletions test/gen_test.rb
Expand Up @@ -20,7 +20,7 @@
out = Dir.glob('output/**/*')
assert out.include? 'output/book.html'
assert out.include? 'output/image'
assert out.include? 'output/stylesheets/handbookish.css'
assert out.include? 'output/stylesheets/scribe.css'
end
end
end
Expand All @@ -35,7 +35,7 @@
assert out.include? 'output/the_first_chapter.html'
assert out.include? 'output/the_second_chapter.html'
assert out.include? 'output/image'
assert out.include? 'output/stylesheets/handbookish.css'
assert out.include? 'output/stylesheets/scribe.css'
end
end
end
Expand All @@ -52,12 +52,24 @@
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')
Dir.chdir('t') do
data = @scribe.gen('epub')
assert_equal data, 'book.epub'
assert_equal 'book.epub', data
out = Dir.glob('output/**/*')
assert out.include? 'output/book.epub'
end
Expand Down
10 changes: 4 additions & 6 deletions test/test_helper.rb
Expand Up @@ -2,17 +2,15 @@
$LOAD_PATH.unshift dir + '/../lib'
$TESTING = true

require 'test/unit'
# Necessary to override stdlib: http://www.ruby-forum.com/topic/212974
require 'rubygems'
gem 'test-unit'
require 'test/unit'

require 'git-scribe'
require 'pp'
require 'tempfile'

begin
require 'leftright'
rescue LoadError
end

##
# test/spec/mini 3
# http://gist.github.com/25455
Expand Down
Binary file added vendor/saxon.jar
Binary file not shown.
Binary file added vendor/xslthl-2.0.2.jar
Binary file not shown.

0 comments on commit 048a3c1

Please sign in to comment.