rack-pygments 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/rack/pygments/version.rb +9 -0
- data/lib/rack/pygments.rb +19 -15
- data/spec/lib/rack/pygments_spec.rb +75 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +2 -0
- metadata +23 -7
data/lib/rack/pygments.rb
CHANGED
@@ -5,7 +5,7 @@ module Rack
|
|
5
5
|
|
6
6
|
def initialize(app, opts={})
|
7
7
|
@app = app
|
8
|
-
@tag = opts[:html_tag].nil? ? 'highlight' : opts[:html_tag]
|
8
|
+
@tag = opts[:html_tag].nil? ? 'highlight' : opts[:html_tag]
|
9
9
|
@attr = opts[:html_attr].nil? ? 'lang' : opts[:html_attr]
|
10
10
|
if system("which pygmentize > /dev/null")
|
11
11
|
@bin = `which pygmentize`.chomp
|
@@ -15,22 +15,26 @@ module Rack
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def call(env)
|
18
|
-
if env["PATH_INFO"] !~ /.*\.css$/
|
19
18
|
status, headers, response = @app.call(env)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
19
|
+
return [status, headers, response] unless pygmentize?(status, headers)
|
20
|
+
response_body = pygmentize(response.join)
|
21
|
+
headers["Content-Length"] = response_body.length.to_s
|
22
|
+
[status,headers,[response_body]]
|
23
|
+
end
|
24
|
+
|
25
|
+
def pygmentize?(status, headers)
|
26
|
+
status == 200 && headers['Content-Type'] =~ /html/
|
27
|
+
end
|
28
|
+
|
29
|
+
def pygmentize(content)
|
30
|
+
document = Nokogiri::HTML(content)
|
31
|
+
nodes = document.css(@tag)
|
32
|
+
nodes.each do |node|
|
33
|
+
lang = node.attribute(@attr).nil? ? 'bash' : node.attribute(@attr).value
|
34
|
+
pygmentized = `echo '#{node.content}' | #{@bin} -l #{lang} -f html`
|
35
|
+
node.replace(Nokogiri::HTML(pygmentized).css("div.highlight").first)
|
33
36
|
end
|
37
|
+
document.to_s
|
34
38
|
end
|
35
39
|
end
|
36
40
|
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
class App
|
5
|
+
def initialize(env={})
|
6
|
+
@env = env
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env={})
|
10
|
+
[@env[:status], @env[:headers], @env[:response]]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
shared_examples_for "a pass-through" do
|
15
|
+
it "passes the status, headers, and response through unchanged" do
|
16
|
+
pygments.call({}).should == [status, headers, response]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe Rack::Pygments do
|
21
|
+
describe "#call" do
|
22
|
+
let(:app) do
|
23
|
+
App.new(:status => status,
|
24
|
+
:headers => headers,
|
25
|
+
:response => response)
|
26
|
+
end
|
27
|
+
let(:status) { 200 }
|
28
|
+
let(:headers) { {'Content-Length' => 0, 'Content-Type' => 'text/html'} }
|
29
|
+
let(:response) { [] }
|
30
|
+
let(:pygments) { Rack::Pygments.new(app)}
|
31
|
+
context "when the response content is HTML" do
|
32
|
+
context "and the response status is 200" do
|
33
|
+
context "and there is no highlight element in the HTML" do
|
34
|
+
let(:response) do
|
35
|
+
doc = <<-DOC
|
36
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
|
37
|
+
<html>
|
38
|
+
<head><title>Page</title></head>
|
39
|
+
<body><h1>Header</h1></body>
|
40
|
+
</html>
|
41
|
+
DOC
|
42
|
+
[doc]
|
43
|
+
end
|
44
|
+
it_should_behave_like "a pass-through"
|
45
|
+
end
|
46
|
+
context "and there is a highlight element in the HTML" do
|
47
|
+
let(:response) do
|
48
|
+
doc = <<-DOC
|
49
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
|
50
|
+
<html>
|
51
|
+
<head><title>Page</title></head>
|
52
|
+
<body><h1>Header</h1><highlight lang="ruby">@var='foo'</highlight></body>
|
53
|
+
</html>
|
54
|
+
DOC
|
55
|
+
[doc]
|
56
|
+
end
|
57
|
+
it "replaces the highlight element with a div containing marked-up code" do
|
58
|
+
response = pygments.call({})[2]
|
59
|
+
document = Nokogiri::HTML(response.join)
|
60
|
+
document.css('highlight').should be_empty
|
61
|
+
document.css('div.highlight').should have(1).element
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
context "and the response status is not 200" do
|
66
|
+
let(:status) { 304 }
|
67
|
+
it_should_behave_like "a pass-through"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
context "when the response content is not HTML" do
|
71
|
+
before { headers['Content-Type'] = 'text/css' }
|
72
|
+
it_should_behave_like "a pass-through"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -4,16 +4,17 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
version: "0.
|
7
|
+
- 3
|
8
|
+
version: "0.3"
|
9
9
|
platform: ruby
|
10
10
|
authors:
|
11
11
|
- Bryan Goines
|
12
|
+
- Matt Yoho
|
12
13
|
autorequire:
|
13
14
|
bindir: bin
|
14
15
|
cert_chain: []
|
15
16
|
|
16
|
-
date: 2010-
|
17
|
+
date: 2010-07-19 00:00:00 -05:00
|
17
18
|
default_executable:
|
18
19
|
dependencies:
|
19
20
|
- !ruby/object:Gem::Dependency
|
@@ -40,6 +41,18 @@ dependencies:
|
|
40
41
|
version: "0"
|
41
42
|
type: :runtime
|
42
43
|
version_requirements: *id002
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: rspec
|
46
|
+
prerelease: false
|
47
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
type: :development
|
55
|
+
version_requirements: *id003
|
43
56
|
description: rack-pygments - Rack Middleware for Pygments, the syntax highlighter
|
44
57
|
email: bryan@ffiirree.com
|
45
58
|
executables: []
|
@@ -49,13 +62,14 @@ extensions: []
|
|
49
62
|
extra_rdoc_files:
|
50
63
|
- README
|
51
64
|
files:
|
65
|
+
- lib/rack/pygments/version.rb
|
52
66
|
- lib/rack/pygments.rb
|
67
|
+
- examples/sinatra/public/style.css
|
53
68
|
- examples/sinatra/app.rb
|
54
69
|
- examples/sinatra/config.ru
|
55
|
-
- examples/sinatra/public/style.css
|
56
70
|
- LICENSE
|
57
71
|
- README
|
58
|
-
has_rdoc:
|
72
|
+
has_rdoc: true
|
59
73
|
homepage: http://github.com/bry4n/rack-pygments
|
60
74
|
licenses: []
|
61
75
|
|
@@ -85,5 +99,7 @@ rubygems_version: 1.3.6
|
|
85
99
|
signing_key:
|
86
100
|
specification_version: 1
|
87
101
|
summary: rack-pygments
|
88
|
-
test_files:
|
89
|
-
|
102
|
+
test_files:
|
103
|
+
- spec/lib/rack/pygments_spec.rb
|
104
|
+
- spec/spec.opts
|
105
|
+
- spec/spec_helper.rb
|