How I Add Code to My Presentations
This is a post from Luke's old blog; it is saved here statically for historical purposes, as of October 2008
Many of my presentations include source code (including yesterday's), and I always struggle a bit to find a way to get attractive, syntax highlighted code into my presentations. This is further complicated by the fact that I often include Puppet code, and there are currently only syntax highlighters for Vim and Emacs, so the apparently-normal Enscript highlighting won't work for me.
So, I hacked up a Vim script that can create marked up HTML, then I threw that into a Rake file that converts my code samples into HTML. Here's the code:
outputs = []
FileList['examples/*'].each do |input|
output = File.join("output", File.basename(input)).sub(/\.\w+/, ".html")
outputs << output
file output => ["Rakefile", input] do
vimsyn(input, output)
end
end
def vimsyn(file, outfile = nil)
filetype = case file
when /\.pp/: "puppet"
when /\.rb/: "ruby"
when /\.mk/: "make"
when /\.sh/: "bash"
when /\.txt/: "text"
else
raise "Unknown File type for %s" % file
end
name = File.basename(file).sub(/\..+/, '')
outfile ||= "/tmp/outfile"
synfile = outfile + ".tmp"
codefile = file
filetype="puppet"
puts "Creating %s" % outfile
expr = %Q%/usr/bin/vim -f -n -X -e -s -c 'set filetype=#{filetype}' -c 'syntax on' -c 'let html_use_css=1' -c 'run syntax/2html.vim' -c 'wq! #{synfile}' -c 'q' #{codefile}%
`#{expr}`
html = File.read(synfile)
html.sub!(/<title>.+<\/title>/, "<title>#{name}</title>")
html.sub!(/^(pre.+) \}/) { $1 + "font-size: 22pt; }" }
html.gsub!(/text-decoration: underline;/, '')
File.open(outfile, "w") { |f| f.print html }
File.unlink(synfile)
end
task :clean do
outputs.each do |file|
if FileTest.exists?(file)
File.unlink(file)
end
end
end
task :default => outputs
I usually (but not always) use Apple's Keynote. So, for this presentation, I used Keynote's "Web Views" to include the HTML directly in the presentations. There were some significant hitches to doing so: First and most stupidly, Keynote won't include local file:/// URLs, only http:///, which means that you can't embed local files. This is apparently by design (someone mumbled something about security, which is, um, inane).
The second hitch is that there does not appear to be any way to change the text size within the web view, so you have to generate the html with the correct size. This is pretty stupid, considering that we otherwise make sure we use scalar graphics instead of PNGs or whatever so they're easy to resize. This makes it pretty annoying to use lots of code samples, because they're invairably different lengths and thus should be different sizes for best readability.
The last problem is that the web views update automatically be default, which means that if you go off the 'net, they lose all of their content. Thus, you have to go through and mark them all to be updating manually.
I also use OmniGraffle to build all of my graphics, which works easily and makes pretty attractive pictures.
This isn't an awesome solution, but it seems to be working for now, anyway, and hopefully it will make it a bit easier for others to get code into their presentations.