Skip to content

Instantly share code, notes, and snippets.

@metade
Created May 7, 2012 19:52
Show Gist options
  • Save metade/2629954 to your computer and use it in GitHub Desktop.
Save metade/2629954 to your computer and use it in GitHub Desktop.
coderay-prawn
# encoding: utf-8
module CodeRay
module Encoders
class Prawn < Encoder
NBSP = ' '
register_for :prawn
def text_token text, kind
text.gsub!('<', '&lt;')
if (kind == :keyword || kind == :method || kind == :directive || kind == :include || kind == :predefined_type)
text = "<b>#{text}</b>"
end
if (kind == :space)
@out << text.gsub(' ', NBSP).gsub("\t", NBSP*@tabs2spaces)
else
color = @styles[kind]
if color
@out << "<color rgb='#{color}'>#{text}</color>"
else
@out << text
end
end
end
protected
def setup options
super
@styles = {
:comment => '777777',
:content => 'DD2200',
:delimiter => '771100',
:directive => '008888',
:float => '6600EE',
:include => 'BB4444',
:instance_variable => '3333BB',
:integer => '0000DD',
:keyword => '008800',
:method => '0066BB',
:preprocessor => '557799',
:predefined_constant => '006699',
:predefined_type => '00AA55',
:symbol => 'AA6600',
}
tabs2spaces_map = {
'CodeRay::Scanners::Ruby' => 2
}
@tabs2spaces = tabs2spaces_map[self.scanner.class.name] || 4
@out = get_output(options)
end
end
end
end
# encoding: utf-8
require 'test/unit'
require 'coderay'
class PrawnTest < Test::Unit::TestCase
def test_hello_world
prawn = CodeRay.scan('puts "Hello, world!"', :ruby).prawn
expected = %[puts <color rgb='771100'>"</color><color rgb='DD2200'>Hello, world!</color><color rgb='771100'>"</color>]
assert_equal expected, prawn
end
def test_c
code = <<-C
#include <varargs.h>
#define ARY_DEFAULT_SIZE 16
register int size;
C
expected = <<-C
<color rgb='557799'>#include</color> <color rgb='BB4444'>&lt;varargs.h></color>
<color rgb='557799'>#define</color> ARY_DEFAULT_SIZE <color rgb='0000DD'>16</color>
  <color rgb='008888'><b>register</b></color> <color rgb='00AA55'>int</color> size;
C
prawn = CodeRay.scan(code, :c).prawn
assert_equal expected, prawn
end
def test_ruby_values
code = <<-RUBY
bool = (true || false)
fixed_num = 1234
float = 123.4
hash = { :water => 'wet', :fire => 'hot' }
RUBY
expected = <<-RUBY
bool = (<color rgb='006699'>true</color> || <color rgb='006699'>false</color>)
fixed_num = <color rgb='0000DD'>1234</color>
float = <color rgb='6600EE'>123.4</color>
hash = { <color rgb='AA6600'>:water</color> => <color rgb='771100'>'</color><color rgb='DD2200'>wet</color><color rgb='771100'>'</color>, <color rgb='AA6600'>:fire</color> => <color rgb='771100'>'</color><color rgb='DD2200'>hot</color><color rgb='771100'>'</color> }
RUBY
prawn = CodeRay.scan(code, :ruby).prawn
assert_equal expected, prawn
end
def test_ruby_method
code = <<-RUBY
# In an object instance variable (denoted with '@'), remember a block.
def remember(&a_block)
@block = a_block
end
RUBY
expected = <<-RUBY
<color rgb='777777'># In an object instance variable (denoted with '@'), remember a block.</color>
<color rgb='008800'><b>def</b></color> <color rgb='0066BB'><b>remember</b></color>(&a_block)
  <color rgb='3333BB'>@block</color> = a_block
<color rgb='008800'><b>end</b></color>
RUBY
prawn = CodeRay.scan(code, :ruby).prawn
assert_equal expected, prawn
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment