Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
blom committed Mar 30, 2010
0 parents commit 5125857
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.yardoc
doc
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2010 Ørjan Blom <blom@blom.tv>

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
haml-coderay
============

[CodeRay][1] filter for [Haml][2]. Specify [language][3] to highlight
with an exclamation point followed by the language name:

:coderay
!ruby

if true
puts "hello"
end

:coderay
!xml

<foo>
<bar>...</bar>
</foo>

The default encoder and encoder options are `:div` and `{}`,
repectively, and are accessible through the following variables:

Haml::Filters::CodeRay.encoder
Haml::Filters::CodeRay.encoder_options

[1]: http://coderay.rubychan.de/
[2]: http://haml-lang.com/
[3]: http://coderay.rubychan.de/doc/classes/CodeRay/Scanners.html
17 changes: 17 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "rubygems"
require "rake"
require "yard"
require "spec/rake/spectask"
require "lib/haml-coderay"

task :default => :spec

Spec::Rake::SpecTask.new "spec" do |t|
t.spec_opts << "--color --format nested"
t.spec_files = FileList["spec/*_spec.rb"]
end

YARD::Rake::YardocTask.new do |t|
t.files = %w(lib/**/*.rb)
t.options = %w(-mmarkdown -rREADME.md -odoc)
end
13 changes: 13 additions & 0 deletions haml-coderay.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Gem::Specification.new do |spec|
spec.name = "haml-coderay"
spec.version = "0.0.0"
spec.summary = "CodeRay filter for Haml"
spec.description = "Adds a CodeRay syntax highlighting filter to Haml"
spec.author = "Ørjan Blom"
spec.email = "blom@blom.tv"
spec.homepage = "http://github.com/blom/haml-coderay"
spec.files = %w(LICENSE README.md lib/haml-coderay.rb)

spec.add_dependency "haml"
spec.add_dependency "coderay"
end
43 changes: 43 additions & 0 deletions lib/haml-coderay.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "haml"

# CodeRay filter for Haml. Specify language to highlight with an exclamation
# point followed by the language name.
#
# @example Ruby
#
# :coderay
# !ruby
#
# if true
# puts "hello"
# end
#
# @example XML
#
# :coderay
# !xml
#
# <foo>
# <bar>...</bar>
# </foo>
module Haml::Filters::CodeRay
include Haml::Filters::Base
lazy_require "coderay"

class << self
# CodeRay encoder (_default_: `:div`).
attr_accessor :encoder

# Coderay encoder options (_default_: `{}`).
attr_accessor :encoder_options
end
self.encoder = :div
self.encoder_options = {}

# @param [String] text text to render
def render(text)
text.sub!(/\A\S*!(\S+)\s+/, '')
::CodeRay.scan(text, $1.downcase.to_sym).
send(encoder, encoder_options)
end
end
31 changes: 31 additions & 0 deletions spec/haml-coderay_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "rubygems"
require "lib/haml-coderay"

describe Haml::Filters::CodeRay do
describe :encoder do
subject { Haml::Filters::CodeRay.encoder }
specify { should == :div }
end

describe :encoder_options do
subject { Haml::Filters::CodeRay.encoder_options }
specify { should == {} }
end
end

describe Haml::Filters.defined["coderay"], %<["coderay"]> do
it { should == Haml::Filters::CodeRay }
end

describe Haml::Engine do
it "should accept :coderay" do
Haml::Engine.new(":coderay\n !xml\n .").
render.should be_a String
end

it "should not accept :coderay without language specifier" do
lambda {
Haml::Engine.new(":coderay\n xml\n .").render
}.should raise_exception(NoMethodError, /downcase.*nil/)
end
end

0 comments on commit 5125857

Please sign in to comment.