Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a60ee88
Showing
5 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2010 Chris Wanstrath | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
Software), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
Mustache Syntax Highlighter | ||
=========================== | ||
|
||
A plugin for [mustache.rb][mr] that lets you syntax highlight blocks | ||
of code using sections. | ||
|
||
For example, this template: | ||
|
||
I wrote some great code! | ||
|
||
{{# highlight_ruby}} | ||
def hello | ||
puts "Hi world!" | ||
end | ||
{{/ highlight_ruby}} | ||
|
||
Sweet. It works. | ||
|
||
Will produce this text: | ||
|
||
I wrote some great code! | ||
|
||
<div class="highlight"><pre><span class="k">def</span> <span class="nf">hello</span> | ||
<span class="nb">puts</span> <span class="s2">"Hi world!"</span> | ||
<span class="k">end</span> | ||
</pre></div> | ||
|
||
Sweet. It works. | ||
|
||
Just include the [Pygments css file][pc] and you're ready to roll. | ||
|
||
If you want to use this in a `Mustache` view you can include it in | ||
your subclass: | ||
|
||
class BlogPost < Mustache | ||
include Mustache::Plugins::SyntaxHighlighter | ||
|
||
def name | ||
"Bobs" | ||
end | ||
end | ||
|
||
The code you want to highlight will be rendered before being passed to | ||
the highlighter. | ||
|
||
So this: | ||
|
||
{{# highlight_ruby}} | ||
def hello | ||
puts "Hi {{name}}!" | ||
end | ||
{{/ highlight_ruby}} | ||
|
||
Becomes this: | ||
|
||
<div class="highlight"><pre><span class="k">def</span> <span class="nf">hello</span> | ||
<span class="nb">puts</span> <span class="s2">"Hi Bobs!"</span> | ||
<span class="k">end</span> | ||
</pre></div> | ||
|
||
Note the `Hi Bobs!` | ||
|
||
## Dependencies | ||
|
||
* Pygments | ||
* Mustache 0.11.0 | ||
|
||
[mr]: http://github.com/defunkt/mustache | ||
[pc]: http://pygments.org/media/pygments_style.css |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'mustache' | ||
require 'mustache/plugins/syntax_highlighter' | ||
|
||
class Dude < Mustache | ||
include Mustache::Plugins::SyntaxHighlighter | ||
|
||
def name | ||
"Chris" | ||
end | ||
end | ||
|
||
Dude.template = <<template | ||
{{# highlight_ruby}} | ||
def hello | ||
puts "Hi {{name}}!" | ||
end | ||
{{/ highlight_ruby}} | ||
template | ||
|
||
puts Dude.render |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
## | ||
# Wrapper for the Pygments command line tool, pygmentize. | ||
# | ||
# Pygments: http://pygments.org/ | ||
# | ||
# Assumes pygmentize is in the path. If not, set its location | ||
# with Albino.bin = '/path/to/pygmentize' | ||
# | ||
# Use like so: | ||
# | ||
# @syntaxer = Albino.new('/some/file.rb', :ruby) | ||
# puts @syntaxer.colorize | ||
# | ||
# This'll print out an HTMLized, Ruby-highlighted version | ||
# of '/some/file.rb'. | ||
# | ||
# To use another formatter, pass it as the third argument: | ||
# | ||
# @syntaxer = Albino.new('/some/file.rb', :ruby, :bbcode) | ||
# puts @syntaxer.colorize | ||
# | ||
# You can also use the #colorize class method: | ||
# | ||
# puts Albino.colorize('/some/file.rb', :ruby) | ||
# | ||
# Another also: you get a #to_s, for somewhat nicer use in Rails views. | ||
# | ||
# ... helper file ... | ||
# def highlight(text) | ||
# Albino.new(text, :ruby) | ||
# end | ||
# | ||
# ... view file ... | ||
# <%= highlight text %> | ||
# | ||
# The default lexer is 'text'. You need to specify a lexer yourself; | ||
# because we are using STDIN there is no auto-detect. | ||
# | ||
# To see all lexers and formatters available, run `pygmentize -L`. | ||
# | ||
# Chris Wanstrath // chris@ozmm.org | ||
# GitHub // http://github.com | ||
# | ||
require 'open4' | ||
|
||
class Albino | ||
@@bin = 'pygmentize' | ||
|
||
def self.bin=(path) | ||
@@bin = path | ||
end | ||
|
||
def self.colorize(*args) | ||
new(*args).colorize | ||
end | ||
|
||
def initialize(target, lexer = :text, format = :html) | ||
@target = File.exists?(target) ? File.read(target) : target rescue target | ||
@options = { :l => lexer, :f => format } | ||
end | ||
|
||
def execute(command) | ||
pid, stdin, stdout, stderr = Open4.popen4(command) | ||
stdin.puts @target | ||
stdin.close | ||
stdout.read.strip | ||
end | ||
|
||
def colorize(options = {}) | ||
execute @@bin + convert_options(options) | ||
end | ||
alias_method :to_s, :colorize | ||
|
||
def convert_options(options = {}) | ||
@options.merge(options).inject('') do |string, (flag, value)| | ||
string + " -#{flag} #{value}" | ||
end | ||
end | ||
end | ||
|
||
if $0 == __FILE__ | ||
require 'rubygems' | ||
require 'test/spec' | ||
require 'mocha' | ||
begin require 'redgreen'; rescue LoadError; end | ||
|
||
context "Albino" do | ||
setup do | ||
@syntaxer = Albino.new(__FILE__, :ruby) | ||
end | ||
|
||
specify "defaults to text" do | ||
syntaxer = Albino.new(__FILE__) | ||
syntaxer.expects(:execute).with('pygmentize -f html -l text').returns(true) | ||
syntaxer.colorize | ||
end | ||
|
||
specify "accepts options" do | ||
@syntaxer.expects(:execute).with('pygmentize -f html -l ruby').returns(true) | ||
@syntaxer.colorize | ||
end | ||
|
||
specify "works with strings" do | ||
syntaxer = Albino.new('class New; end', :ruby) | ||
assert_match %r(highlight), syntaxer.colorize | ||
end | ||
|
||
specify "aliases to_s" do | ||
assert_equal @syntaxer.colorize, @syntaxer.to_s | ||
end | ||
|
||
specify "class method colorize" do | ||
assert_equal @syntaxer.colorize, Albino.colorize(__FILE__, :ruby) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'albino' | ||
|
||
class Mustache | ||
module Plugins | ||
module SyntaxHighlighter | ||
LEXERS = %w( ruby js python scheme ) | ||
|
||
LEXERS.each do |lexer| | ||
define_method "highlight_#{lexer}" do | ||
lambda { |text| Albino.new(text, lexer) } | ||
end | ||
end | ||
end | ||
end | ||
end |