rack-pygmentize 1.0.0

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.
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rack-pygmentize.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Lee Jarvis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ Rack::Pygmentize
2
+ ================
3
+
4
+ Get colorful with Rack::Pygmentize! Rack::Pygmentize uses the awesome
5
+ [Pygments](http://pygments.org/) library to make your code look pretty!
6
+ Thanks to [Albino](https://github.com/github/albino) the Pygmentizing
7
+ process is super awesome and made of win.
8
+
9
+ Installation
10
+ ------------
11
+
12
+ Ensure you have [Pygments](http://pygments.org/) installed and the
13
+ `pygmentize` executable is available in your `PATH`. Then run
14
+
15
+ gem install rack-pygmentize
16
+
17
+ Usage
18
+ -----
19
+
20
+ Just add `use Rack::Pygmentize` to your `config.ru`. See the example
21
+ in the `/examples` directory.
@@ -0,0 +1,6 @@
1
+ task :test do
2
+ require './lib/rack/pygmentize'
3
+ require 'minitest/autorun'
4
+ begin; require 'turn'; rescue LoadError; end
5
+ require_relative './test/rack_pygmentize_test'
6
+ end
@@ -0,0 +1,93 @@
1
+ require '../lib/rack/pygmentize'
2
+
3
+ str = DATA.read
4
+ app = proc {|e| [200, {'Content-Type' => 'text/html'}, [str]] }
5
+ builder = Rack::Builder.new
6
+ builder.use Rack::Pygmentize
7
+ builder.run app
8
+ Rack::Handler::WEBrick.run(builder, :Port => 3000)
9
+
10
+ __END__
11
+ <html>
12
+ <head>
13
+ <title>Hello</title>
14
+ <style type="text/css">
15
+ pre {line-height:150%;font-family:Monaco, monospace;font-size: 12px;padding:10px;border-top:1px solid #ccc;border-bottom:1px solid #ccc;}
16
+ pre .c{color:#998;font-style:italic;}
17
+ pre .err{color:#a61717;background-color:#e3d2d2;}
18
+ pre .k{font-weight:bold;}
19
+ pre .o{font-weight:bold;}
20
+ pre .cm{color:#998;font-style:italic;}
21
+ pre .cp{color:#999;font-weight:bold;}
22
+ pre .c1{color:#998;font-style:italic;}
23
+ pre .cs{color:#999;font-weight:bold;font-style:italic;}
24
+ pre .gd{color:#000;background-color:#fdd;}
25
+ pre .gd .x{color:#000;background-color:#faa;}
26
+ pre .ge{font-style:italic;}
27
+ pre .gr{color:#a00;}
28
+ pre .gh{color:#999;}
29
+ pre .gi{color:#000;background-color:#dfd;}
30
+ pre .gi .x{color:#000;background-color:#afa;}
31
+ pre .go{color:#888;}
32
+ pre .gp{color:#555;}
33
+ pre .gs{font-weight:bold;}
34
+ pre .gu{color:#800080;font-weight:bold;}
35
+ pre .gt{color:#a00;}
36
+ pre .kc{font-weight:bold;}
37
+ pre .kd{font-weight:bold;}
38
+ pre .kp{font-weight:bold;}
39
+ pre .kr{font-weight:bold;}
40
+ pre .kt{color:#458;font-weight:bold;}
41
+ pre .m{color:#099;}
42
+ pre .s{color:#d14;}
43
+ pre .na{color:#008080;}
44
+ pre .nb{color:#0086B3;}
45
+ pre .nc{color:#458;font-weight:bold;}
46
+ pre .no{color:#008080;}
47
+ pre .ni{color:#800080;}
48
+ pre .ne{color:#900;font-weight:bold;}
49
+ pre .nf{color:#900;font-weight:bold;}
50
+ pre .nn{color:#555;}
51
+ pre .nt{color:#000080;}
52
+ pre .nv{color:#008080;}
53
+ pre .ow{font-weight:bold;}
54
+ pre .w{color:#bbb;}
55
+ pre .mf{color:#099;}
56
+ pre .mh{color:#099;}
57
+ pre .mi{color:#099;}
58
+ pre .mo{color:#099;}
59
+ pre .sb{color:#d14;}
60
+ pre .sc{color:#d14;}
61
+ pre .sd{color:#d14;}
62
+ pre .s2{color:#d14;}
63
+ pre .se{color:#d14;}
64
+ pre .sh{color:#d14;}
65
+ pre .si{color:#d14;}
66
+ pre .sx{color:#d14;}
67
+ pre .sr{color:#009926;}
68
+ pre .s1{color:#d14;}
69
+ pre .ss{color:#990073;}
70
+ pre .bp{color:#999;}
71
+ pre .vc{color:#008080;}
72
+ pre .vg{color:#008080;}
73
+ pre .vi{color:#008080;}
74
+ pre .il{color:#099;}
75
+ </style>
76
+ </head>
77
+ <body>
78
+
79
+ <h1>Ruby</h1>
80
+ <pre lang="ruby">
81
+ def hello(item)
82
+ puts 'Hello ' + item + '!'
83
+ end
84
+ </pre>
85
+
86
+ <h1>Python</h1>
87
+ <pre lang="python">
88
+ def hello(item):
89
+ print 'Hello ' + item + '!'
90
+ </pre>
91
+
92
+ </body>
93
+ </html>
@@ -0,0 +1,39 @@
1
+ require 'rack'
2
+ require 'nokogiri'
3
+ require 'albino'
4
+
5
+ module Rack
6
+ class Pygmentize
7
+ def initialize(app, xpath="//pre[@lang]")
8
+ @app, @xpath = app, xpath
9
+ end
10
+
11
+ def call(env)
12
+ call!(env)
13
+ end
14
+
15
+ def call!(env)
16
+ @env = env.dup
17
+ status, headers, body = @app.call(@env)
18
+
19
+ if headers['Content-Type'] && headers['Content-Type'].include?('text/html')
20
+ headers.delete('Content-Length')
21
+ Rack::Response.new(parse(body), status, headers).finish
22
+ else
23
+ [status, headers, body]
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def parse(body)
30
+ doc = Nokogiri::HTML(body[0])
31
+ doc.search(@xpath).each do |pre|
32
+ code = Albino.colorize(pre.text.rstrip, pre[:lang])
33
+ pre.inner_html = code[28 .. -13]
34
+ end
35
+ doc.to_s
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,32 @@
1
+ class RackPygmentizeTest < MiniTest::Unit::TestCase
2
+ def response(body, xpath="//pre[@lang]")
3
+ builder = Rack::Builder.new
4
+ builder.use Rack::Pygmentize, xpath
5
+ builder.run proc { |e| [200, {'Content-Type' => 'text/html'}, [body]] }
6
+ Rack::MockRequest.new(builder).get '/'
7
+ end
8
+
9
+ def test_replace_markup
10
+ res = response(%Q(<pre lang="ruby">def foo; end</pre>))
11
+ assert res.body.include?('<span class="k">')
12
+ end
13
+
14
+ def test_alternative_xpath
15
+ res = response(%Q(<code lang="ruby">def foo; end</code>), '//code[@lang]')
16
+ assert res.body.include?('<span class="k">')
17
+ end
18
+
19
+ def test_negative_alternative_xpath
20
+ res = response(%Q(<pre lang="ruby">def foo; end</pre>), '//code[@lang]')
21
+ assert !res.body.include?('<span class="k">')
22
+ end
23
+
24
+ def test_multiple_code_blocks
25
+ res = response(%Q(
26
+ <pre lang="ruby">def foo; end</pre>
27
+ <pre lang="python">def foo:</pre>
28
+ ))
29
+ assert res.body.include?('python"><span')
30
+ assert res.body.include?('ruby"><span')
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-pygmentize
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Lee Jarvis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-14 00:00:00 +00:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rack
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 1.0.0
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 1.4.4
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: albino
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.3.2
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ description: Rack middleware used to automagically format your code blocks using pygmentize
50
+ email:
51
+ - lee@jarvis.co
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - lib/rack/pygmentize.rb
60
+ - Rakefile
61
+ - Gemfile
62
+ - LICENSE
63
+ - README.md
64
+ - .gemtest
65
+ - examples/rack-pygmentize.rb
66
+ - test/rack_pygmentize_test.rb
67
+ has_rdoc: true
68
+ homepage: http://github.com/injekt/rack-pygmentize
69
+ licenses: []
70
+
71
+ post_install_message:
72
+ rdoc_options: []
73
+
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.5.0
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Rack middleware to pygmentize your code blocks
95
+ test_files:
96
+ - test/rack_pygmentize_test.rb