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
Lee Jarvis
committed
Mar 14, 2011
0 parents
commit 07aa17c
Showing
12 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
*.gem | |||
.bundle | |||
Gemfile.lock | |||
pkg/* |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
source "http://rubygems.org" | |||
|
|||
# Specify your gem's dependencies in rack-pygmentize.gemspec | |||
gemspec |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
Copyright (c) 2010 Lee Jarvis | |||
|
|||
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,21 @@ | |||
Rack::Pygmentize | |||
================ | |||
|
|||
Get colorful with Rack::Pygmentize! Rack::Pygmentize uses the awesome | |||
[Pygments](http://pygments.org/) library to make your code look pretty! | |||
Thanks to [Albino](https://github.com/github/albino) the Pygmentizing | |||
process is super awesome and made of win. | |||
|
|||
Installation | |||
------------ | |||
|
|||
Ensure you have [Pygments](http://pygments.org/) installed and the | |||
`pygmentize` executable is available in your `PATH`. Then run | |||
|
|||
gem install rack-pygmentize | |||
|
|||
Usage | |||
----- | |||
|
|||
Just add `use Rack::Pygmentize` to your `config.ru`. See the example | |||
in the `/examples` directory. |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,6 @@ | |||
task :test do | |||
require './lib/rack/pygmentize' | |||
require 'minitest/autorun' | |||
begin; require 'turn'; rescue LoadError; end | |||
require_relative './test/rack_pygmentize_test' | |||
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,93 @@ | |||
require '../lib/rack/pygmentize' | |||
|
|||
str = DATA.read | |||
app = proc {|e| [200, {'Content-Type' => 'text/html'}, [str]] } | |||
builder = Rack::Builder.new | |||
builder.use Rack::Pygmentize | |||
builder.run app | |||
Rack::Handler::WEBrick.run(builder, :Port => 3000) | |||
|
|||
__END__ | |||
<html> | |||
<head> | |||
<title>Hello</title> | |||
<style type="text/css"> | |||
pre {line-height:150%;font-family:Monaco, monospace;font-size: 12px;padding:10px;border-top:1px solid #ccc;border-bottom:1px solid #ccc;} | |||
pre .c{color:#998;font-style:italic;} | |||
pre .err{color:#a61717;background-color:#e3d2d2;} | |||
pre .k{font-weight:bold;} | |||
pre .o{font-weight:bold;} | |||
pre .cm{color:#998;font-style:italic;} | |||
pre .cp{color:#999;font-weight:bold;} | |||
pre .c1{color:#998;font-style:italic;} | |||
pre .cs{color:#999;font-weight:bold;font-style:italic;} | |||
pre .gd{color:#000;background-color:#fdd;} | |||
pre .gd .x{color:#000;background-color:#faa;} | |||
pre .ge{font-style:italic;} | |||
pre .gr{color:#a00;} | |||
pre .gh{color:#999;} | |||
pre .gi{color:#000;background-color:#dfd;} | |||
pre .gi .x{color:#000;background-color:#afa;} | |||
pre .go{color:#888;} | |||
pre .gp{color:#555;} | |||
pre .gs{font-weight:bold;} | |||
pre .gu{color:#800080;font-weight:bold;} | |||
pre .gt{color:#a00;} | |||
pre .kc{font-weight:bold;} | |||
pre .kd{font-weight:bold;} | |||
pre .kp{font-weight:bold;} | |||
pre .kr{font-weight:bold;} | |||
pre .kt{color:#458;font-weight:bold;} | |||
pre .m{color:#099;} | |||
pre .s{color:#d14;} | |||
pre .na{color:#008080;} | |||
pre .nb{color:#0086B3;} | |||
pre .nc{color:#458;font-weight:bold;} | |||
pre .no{color:#008080;} | |||
pre .ni{color:#800080;} | |||
pre .ne{color:#900;font-weight:bold;} | |||
pre .nf{color:#900;font-weight:bold;} | |||
pre .nn{color:#555;} | |||
pre .nt{color:#000080;} | |||
pre .nv{color:#008080;} | |||
pre .ow{font-weight:bold;} | |||
pre .w{color:#bbb;} | |||
pre .mf{color:#099;} | |||
pre .mh{color:#099;} | |||
pre .mi{color:#099;} | |||
pre .mo{color:#099;} | |||
pre .sb{color:#d14;} | |||
pre .sc{color:#d14;} | |||
pre .sd{color:#d14;} | |||
pre .s2{color:#d14;} | |||
pre .se{color:#d14;} | |||
pre .sh{color:#d14;} | |||
pre .si{color:#d14;} | |||
pre .sx{color:#d14;} | |||
pre .sr{color:#009926;} | |||
pre .s1{color:#d14;} | |||
pre .ss{color:#990073;} | |||
pre .bp{color:#999;} | |||
pre .vc{color:#008080;} | |||
pre .vg{color:#008080;} | |||
pre .vi{color:#008080;} | |||
pre .il{color:#099;} | |||
</style> | |||
</head> | |||
<body> | |||
|
|||
<h1>Ruby</h1> | |||
<pre lang="ruby"> | |||
def hello(item) | |||
puts 'Hello ' + item + '!' | |||
end | |||
</pre> | |||
|
|||
<h1>Python</h1> | |||
<pre lang="python"> | |||
def hello(item): | |||
print 'Hello ' + item + '!' | |||
</pre> | |||
|
|||
</body> | |||
</html> |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
module Rack | |||
module Pygmentize | |||
# Your code goes here... | |||
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
module Rack | |||
module Pygmentize | |||
VERSION = "0.0.1" | |||
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
require 'rack' | |||
require 'nokogiri' | |||
require 'albino' | |||
|
|||
module Rack | |||
class Pygmentize | |||
def initialize(app, xpath="//pre[@lang]") | |||
@app, @xpath = app, xpath | |||
end | |||
|
|||
def call(env) | |||
call!(env) | |||
end | |||
|
|||
def call!(env) | |||
@env = env.dup | |||
status, headers, body = @app.call(@env) | |||
|
|||
if headers['Content-Type'] && headers['Content-Type'].include?('text/html') | |||
headers.delete('Content-Length') | |||
Rack::Response.new(parse(body), status, headers).finish | |||
else | |||
[status, headers, body] | |||
end | |||
end | |||
|
|||
private | |||
|
|||
def parse(body) | |||
doc = Nokogiri::HTML(body[0]) | |||
doc.search(@xpath).each do |pre| | |||
code = Albino.colorize(pre.text.rstrip, pre[:lang]) | |||
pre.inner_html = code[28 .. -13] | |||
end | |||
doc.to_s | |||
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,18 @@ | |||
Gem::Specification.new do |s| | |||
s.name = "rack-pygmentize" | |||
s.version = "1.0.0" | |||
s.platform = Gem::Platform::RUBY | |||
s.authors = ["Lee Jarvis"] | |||
s.email = ["lee@jarvis.co"] | |||
s.homepage = "http://github.com/injekt/rack-pygmentize" | |||
s.summary = %q{Rack middleware to pygmentize your code blocks} | |||
s.description = %q{Rack middleware used to automagically format your code blocks using pygmentize} | |||
|
|||
s.files = %w(lib/rack/pygmentize.rb Rakefile Gemfile LICENSE README.md .gemtest examples/rack-pygmentize.rb) | |||
s.test_files = %w(test/rack_pygmentize_test.rb) | |||
s.require_paths = ["lib"] | |||
|
|||
s.add_runtime_dependency(%q<rack>, [">= 1.0.0"]) | |||
s.add_runtime_dependency(%q<nokogiri>, [">= 1.4.4"]) | |||
s.add_runtime_dependency(%q<albino>, [">= 1.3.2"]) | |||
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,32 @@ | |||
class RackPygmentizeTest < MiniTest::Unit::TestCase | |||
def response(body, xpath="//pre[@lang]") | |||
builder = Rack::Builder.new | |||
builder.use Rack::Pygmentize, xpath | |||
builder.run proc { |e| [200, {'Content-Type' => 'text/html'}, [body]] } | |||
Rack::MockRequest.new(builder).get '/' | |||
end | |||
|
|||
def test_replace_markup | |||
res = response(%Q(<pre lang="ruby">def foo; end</pre>)) | |||
assert res.body.include?('<span class="k">') | |||
end | |||
|
|||
def test_alternative_xpath | |||
res = response(%Q(<code lang="ruby">def foo; end</code>), '//code[@lang]') | |||
assert res.body.include?('<span class="k">') | |||
end | |||
|
|||
def test_negative_alternative_xpath | |||
res = response(%Q(<pre lang="ruby">def foo; end</pre>), '//code[@lang]') | |||
assert !res.body.include?('<span class="k">') | |||
end | |||
|
|||
def test_multiple_code_blocks | |||
res = response(%Q( | |||
<pre lang="ruby">def foo; end</pre> | |||
<pre lang="python">def foo:</pre> | |||
)) | |||
assert res.body.include?('python"><span') | |||
assert res.body.include?('ruby"><span') | |||
end | |||
end |