Skip to content

Instantly share code, notes, and snippets.

@jonforums
Created January 12, 2012 19:35
Show Gist options
  • Save jonforums/1602575 to your computer and use it in GitHub Desktop.
Save jonforums/1602575 to your computer and use it in GitHub Desktop.
Nanoc code highlighting experiment
# run via: ruby [-Ilib] hilite_bug.rb
# ruby [-Ilib] hilite_bug.rb > nanoc_hl.html 2> nokogiri_out.txt
begin
require 'nanoc'
require 'nanoc/filters'
require 'nanoc/helpers'
include Nanoc::Helpers::HTMLEscape
rescue LoadError
require 'nanoc3'
require 'nanoc3/filters'
require 'nanoc3/helpers'
include Nanoc3::Helpers::HTMLEscape
end
require 'nokogiri'
require 'redcarpet'
STDERR.puts "Nanoc v#{Nanoc3::VERSION}"
STDERR.puts "Nokogiri v#{Nokogiri::VERSION}"
STDERR.puts "Redcarpet v#{Redcarpet::VERSION}"
raw = DATA.read
# emulate `filter :erubis`
if Nanoc3::VERSION < '3.3.0'
klass = Nanoc3::Filters::ERB
else
klass = Nanoc3::Filters::Erubis
end
erb = klass.new.run(raw)
STDERR.puts "\n=== #{klass.to_s.split(':')[-1]} filtered content ===\n\n"
STDERR.puts erb
# emulate `filter :redcarpet`
content = Nanoc3::Filters::Redcarpet.new.run(erb)
# emulate `filter :colorize_syntax, :coderay => { :line_numbers => :table }`
cs = Nanoc3::Filters::ColorizeSyntax.new
filtered = cs.run(content,
:syntax => :html,
:coderay => { :line_numbers => :table })
# what's the post-Redcarpet, post-coderay content look like?
puts filtered
# what's Nokogiri's HTML and XML post-RDiscount fragments look like?
STDERR.puts "\n\n=== Nokogiri::HTML.fragment(content) ===\n\n"
STDERR.puts Nokogiri::HTML.fragment(content).to_html
STDERR.puts "\n\n=== Nokogiri::XML.fragment(content) ===\n\n"
STDERR.puts Nokogiri::XML.fragment(content).to_html
__END__
My markdown paragraph describing the following syntax highlighted code
<pre><code class="language-c">
<% h do %>
/* build with:
*
* cl /W2 /Oy- /Zi slowhello.c user32.lib
* -or-
* [AQtime] gcc -O2 -gstabs+ -o slowhello.exe slowhello.c -luser32
* [verysleepy] gcc -O2 -fno-omit-frame-pointer -g3 -o slowhello.exe slowhello.c -luser32
*/
#include <sys/stat.h>
#include <windows.h>
#define STAT_COUNT 50000
void
rb_mongo_stat(const char *filename)
{
int i;
struct stat buf;
for (i = 0; i < STAT_COUNT; i++) {
stat(filename, &buf);
}
}
void
hello(void)
{
rb_mongo_stat("slowhello.exe");
MessageBox(NULL, "Hi Speedy G!", "Pokey", MB_SETFOREGROUND);
}
int
main(int argc, char *argv[])
{
hello();
return 0;
}
<% end %>
</code></pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment