rack-highlighter 0.1.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.
- data/.gitignore +17 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/Guardfile +5 -0
- data/LICENSE +22 -0
- data/README.md +183 -0
- data/Rakefile +12 -0
- data/lib/rack/highlighter.rb +97 -0
- data/lib/rack/highlighter/version.rb +5 -0
- data/rack-highlighter.gemspec +40 -0
- data/spec/highlighter_spec.rb +169 -0
- data/spec/spec_helper.rb +46 -0
- metadata +287 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012 Daniel Perez Alvarez
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# Rack::Highlighter [](http://travis-ci.org/unindented/rack-highlighter)
|
|
2
|
+
|
|
3
|
+
Rack Middleware that provides syntax highlighting of code blocks, using the [CodeRay](http://rubygems.org/gems/coderay), [Pygments](http://rubygems.org/gems/pygments.rb) or [Ultraviolet](http://rubygems.org/gems/ultraviolet) gems.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
Add this line to your application's `Gemfile`:
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
gem 'rack-highlighter'
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
And then execute:
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
$ bundle
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Or install it yourself as:
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
$ gem install rack-highlighter
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
### Adding highlighting to a Rails application
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
require 'rack/highlighter'
|
|
33
|
+
require 'pygments'
|
|
34
|
+
|
|
35
|
+
class Application < Rails::Application
|
|
36
|
+
config.middleware.use Rack::Highlighter, :pygments
|
|
37
|
+
end
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Adding highlighting to a Sinatra application
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
require 'sinatra'
|
|
44
|
+
require 'rack/highlighter'
|
|
45
|
+
require 'pygments'
|
|
46
|
+
|
|
47
|
+
use Rack::Highlighter, :pygments
|
|
48
|
+
|
|
49
|
+
get('/') do
|
|
50
|
+
'<pre><code class="ruby">puts "Hello world!"</code></pre>'
|
|
51
|
+
end
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Adding highlighting to a Rackup application
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
require 'rack'
|
|
58
|
+
require 'rack/highlighter'
|
|
59
|
+
require 'pygments'
|
|
60
|
+
|
|
61
|
+
use Rack::Highlighter, :pygments
|
|
62
|
+
|
|
63
|
+
run lambda { |env|
|
|
64
|
+
[200, {'Content-Type' => 'text/html'}, ['<pre><code class="ruby">puts "Hello world!"</code></pre>']]
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
## Highlighters
|
|
70
|
+
|
|
71
|
+
You can choose which highlighter to use. Just require the right gem, and specify it when invoking the middleware.
|
|
72
|
+
|
|
73
|
+
### CodeRay
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
require 'rack/highlighter'
|
|
77
|
+
require 'coderay'
|
|
78
|
+
|
|
79
|
+
use Rack::Highlighter, :coderay
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Pygments
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
require 'rack/highlighter'
|
|
86
|
+
require 'pygments'
|
|
87
|
+
|
|
88
|
+
use Rack::Highlighter, :pygments
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Ultraviolet
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
require 'rack/highlighter'
|
|
95
|
+
require 'uv'
|
|
96
|
+
|
|
97
|
+
use Rack::Highlighter, :ultraviolet
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
## Configuration
|
|
102
|
+
|
|
103
|
+
### Default
|
|
104
|
+
|
|
105
|
+
With the default options, `Rack::Highlighter` would recognize code blocks like the following:
|
|
106
|
+
|
|
107
|
+
```html
|
|
108
|
+
<pre>
|
|
109
|
+
<code class="ruby">puts "Hello world!"</code>
|
|
110
|
+
</pre>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Elements
|
|
114
|
+
|
|
115
|
+
If your code is wrapped in other tags, for example:
|
|
116
|
+
|
|
117
|
+
```html
|
|
118
|
+
<pre class="ruby">puts "Hello world!"</pre>
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
You can specify them in the `elements` option:
|
|
122
|
+
|
|
123
|
+
```ruby
|
|
124
|
+
use Rack::Highlighter, :pygments, { elements: ['pre'] }
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Attribute
|
|
128
|
+
|
|
129
|
+
If your block doesn't use the `class` attribute to declare the language of the code, for example:
|
|
130
|
+
|
|
131
|
+
```html
|
|
132
|
+
<pre>
|
|
133
|
+
<code data-lang="ruby">puts "Hello world!"</code>
|
|
134
|
+
</pre>
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
You can specify it in the `attribute` option:
|
|
138
|
+
|
|
139
|
+
```ruby
|
|
140
|
+
use Rack::Highlighter, :pygments, { attribute: 'data-lang' }
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Pattern
|
|
144
|
+
|
|
145
|
+
If your block uses a certain pattern to declare the language of the code, for example:
|
|
146
|
+
|
|
147
|
+
```html
|
|
148
|
+
<pre>
|
|
149
|
+
<code class="language-ruby">puts "Hello world!"</code>
|
|
150
|
+
</pre>
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
You can specify a regular expression to match it in the `pattern` option:
|
|
154
|
+
|
|
155
|
+
```ruby
|
|
156
|
+
use Rack::Highlighter, :pygments, { pattern: /language-(?<lang>\w+)/ }
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
The regular expression must have a capture group named `lang` for this to work.
|
|
160
|
+
|
|
161
|
+
### Additional parameters
|
|
162
|
+
|
|
163
|
+
You can pass additional parameters to the highlighter via the `misc` option:
|
|
164
|
+
|
|
165
|
+
```ruby
|
|
166
|
+
use Rack::Highlighter, :pygments, { misc: {linenos: 'inline'} }
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
## Meta
|
|
171
|
+
|
|
172
|
+
* Code: `git clone git://github.com/unindented/rack-highlighter.git`
|
|
173
|
+
* Home: <https://github.com/unindented/rack-highlighter/>
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
## Contributors
|
|
177
|
+
|
|
178
|
+
* Daniel Perez Alvarez ([unindented@gmail.com](mailto:unindented@gmail.com))
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
## License
|
|
182
|
+
|
|
183
|
+
Copyright (c) 2012 Daniel Perez Alvarez ([unindented.org](http://unindented.org/)). This is free software, and may be redistributed under the terms specified in the LICENSE file.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
require 'rack/highlighter/version'
|
|
2
|
+
|
|
3
|
+
require 'rack'
|
|
4
|
+
require 'nokogiri'
|
|
5
|
+
require 'htmlentities'
|
|
6
|
+
|
|
7
|
+
module Rack
|
|
8
|
+
class Highlighter
|
|
9
|
+
include Rack::Utils
|
|
10
|
+
|
|
11
|
+
DEFAULT_LANG = 'text'
|
|
12
|
+
|
|
13
|
+
DEFAULT_OPTS = {
|
|
14
|
+
elements: ['pre', 'code'],
|
|
15
|
+
attribute: 'class',
|
|
16
|
+
pattern: /(?<lang>\w+)/,
|
|
17
|
+
misc: {}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
def initialize(app, highlighter, opts = {})
|
|
21
|
+
@app = app
|
|
22
|
+
@highlighter = highlighter
|
|
23
|
+
@coder = HTMLEntities.new
|
|
24
|
+
@opts = DEFAULT_OPTS.merge(opts)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def call(env)
|
|
28
|
+
status, headers, response = @app.call(env)
|
|
29
|
+
headers = HeaderHash.new(headers)
|
|
30
|
+
|
|
31
|
+
if should_highlight?(status, headers)
|
|
32
|
+
content = extract_content(response)
|
|
33
|
+
|
|
34
|
+
doc = Nokogiri::HTML(content)
|
|
35
|
+
search_for_nodes(doc).each { |node| highlight_node(node) }
|
|
36
|
+
content = doc.to_html
|
|
37
|
+
|
|
38
|
+
headers['content-length'] = bytesize(content).to_s
|
|
39
|
+
response = [content]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
[status, headers, response]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def should_highlight?(status, headers)
|
|
48
|
+
!STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
|
|
49
|
+
!headers['transfer-encoding'] &&
|
|
50
|
+
headers['content-type'] &&
|
|
51
|
+
headers['content-type'].include?('text/html')
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def extract_content(response)
|
|
55
|
+
content = ''
|
|
56
|
+
response.each { |part| content += part }
|
|
57
|
+
content
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def search_for_nodes(doc)
|
|
61
|
+
doc.search(@opts[:elements].join('>'))
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def highlight_node(node)
|
|
65
|
+
lang = extract_lang(node)
|
|
66
|
+
code = @coder.decode(extract_html(node))
|
|
67
|
+
|
|
68
|
+
target = node
|
|
69
|
+
(@opts[:elements].length - 1).times { target = target.parent }
|
|
70
|
+
|
|
71
|
+
# Gotta clone the options, some misbehaving library could modify it.
|
|
72
|
+
target.swap(send(@highlighter, lang, code, @opts[:misc].clone))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def extract_lang(node)
|
|
76
|
+
attr = node[@opts[:attribute]]
|
|
77
|
+
((attr && @opts[:pattern].match(attr)) || { lang: DEFAULT_LANG })[:lang]
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def extract_html(node)
|
|
81
|
+
node.inner_html
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def coderay(lang, code, options)
|
|
85
|
+
CodeRay.scan(code, lang).div(options)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def pygments(lang, code, options)
|
|
89
|
+
Pygments.highlight(code, formatter: 'html', lexer: lang, options: options)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def ultraviolet(lang, code, options)
|
|
93
|
+
Uv.parse(code, 'xhtml', lang, options[:lines], options[:theme])
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
end
|
|
97
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
|
|
5
|
+
require 'rack/highlighter/version'
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |gem|
|
|
8
|
+
gem.name = 'rack-highlighter'
|
|
9
|
+
gem.version = Rack::Highlighter::VERSION
|
|
10
|
+
gem.authors = ['Daniel Perez Alvarez']
|
|
11
|
+
gem.email = ['unindented@gmail.com']
|
|
12
|
+
gem.description = %q{Rack Middleware for syntax highlighting.}
|
|
13
|
+
gem.summary = %q{Rack Middleware that provides syntax highlighting of code blocks, using CodeRay, Pygments or Ultraviolet.}
|
|
14
|
+
gem.homepage = 'http://github.com/unindented/rack-highlighter'
|
|
15
|
+
|
|
16
|
+
gem.files = `git ls-files`.split($/)
|
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
gem.require_paths = ['lib']
|
|
20
|
+
|
|
21
|
+
gem.required_ruby_version = '>= 1.9.0'
|
|
22
|
+
|
|
23
|
+
gem.add_runtime_dependency 'rack', '>= 1.0.0'
|
|
24
|
+
gem.add_runtime_dependency 'nokogiri', '>= 1.4.0'
|
|
25
|
+
gem.add_runtime_dependency 'htmlentities', '>= 4.0.0'
|
|
26
|
+
|
|
27
|
+
gem.add_development_dependency 'coderay', '~> 1.0'
|
|
28
|
+
gem.add_development_dependency 'pygments.rb', '~> 0.3'
|
|
29
|
+
gem.add_development_dependency 'ultraviolet', '~> 1.0'
|
|
30
|
+
|
|
31
|
+
gem.add_development_dependency 'rake'
|
|
32
|
+
gem.add_development_dependency 'minitest'
|
|
33
|
+
gem.add_development_dependency 'minitest-reporters'
|
|
34
|
+
gem.add_development_dependency 'rack-test'
|
|
35
|
+
|
|
36
|
+
gem.add_development_dependency 'guard-minitest'
|
|
37
|
+
gem.add_development_dependency 'rb-inotify'
|
|
38
|
+
gem.add_development_dependency 'rb-fsevent'
|
|
39
|
+
gem.add_development_dependency 'rb-fchange'
|
|
40
|
+
end
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
|
2
|
+
|
|
3
|
+
HIGHLIGHTERS = [
|
|
4
|
+
:coderay,
|
|
5
|
+
:pygments,
|
|
6
|
+
:ultraviolet
|
|
7
|
+
]
|
|
8
|
+
|
|
9
|
+
EXAMPLES = [
|
|
10
|
+
{
|
|
11
|
+
elements: ['pre', 'code'],
|
|
12
|
+
attribute: 'class',
|
|
13
|
+
value: 'ruby',
|
|
14
|
+
pattern: /(?<lang>\w+)/,
|
|
15
|
+
content: '(1..10).reduce(&:+)'
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
elements: ['pre'],
|
|
19
|
+
attribute: 'class',
|
|
20
|
+
value: 'ruby',
|
|
21
|
+
pattern: /(?<lang>\w+)/,
|
|
22
|
+
content: '(1..10).reduce(&:+)'
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
elements: ['pre', 'code'],
|
|
26
|
+
attribute: 'data-lang',
|
|
27
|
+
value: 'ruby',
|
|
28
|
+
pattern: /(?<lang>\w+)/,
|
|
29
|
+
content: '(1..10).reduce(&:+)'
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
elements: ['pre', 'code'],
|
|
33
|
+
attribute: 'class',
|
|
34
|
+
value: 'language-ruby',
|
|
35
|
+
pattern: /language-(?<lang>\w+)/,
|
|
36
|
+
content: '(1..10).reduce(&:+)'
|
|
37
|
+
}
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
describe Rack::Highlighter do
|
|
42
|
+
|
|
43
|
+
let(:app) do
|
|
44
|
+
create_app(status, headers, content, highlighter, options)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
before do
|
|
48
|
+
get '/'
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
describe 'with a content-type other than `text/html`' do
|
|
52
|
+
|
|
53
|
+
let(:status) { 200 }
|
|
54
|
+
let(:headers) do
|
|
55
|
+
{
|
|
56
|
+
'Content-Type' => 'text/plain',
|
|
57
|
+
'Content-Length' => content.length.to_s
|
|
58
|
+
}
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
let(:content) { 'foobar' }
|
|
62
|
+
let(:options) { {} }
|
|
63
|
+
|
|
64
|
+
HIGHLIGHTERS.each do |lib|
|
|
65
|
+
|
|
66
|
+
describe "when using #{lib}" do
|
|
67
|
+
|
|
68
|
+
let(:highlighter) { lib }
|
|
69
|
+
|
|
70
|
+
it 'leaves the status untouched' do
|
|
71
|
+
last_response.status.must_equal status
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it 'leaves the headers untouched' do
|
|
75
|
+
last_response.headers.must_equal headers
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it 'leaves the content untouched' do
|
|
79
|
+
last_response.body.must_equal content
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
describe 'with a content-type of `text/html`' do
|
|
89
|
+
|
|
90
|
+
let(:status) { 200 }
|
|
91
|
+
let(:headers) do
|
|
92
|
+
{
|
|
93
|
+
'Content-Type' => 'text/html',
|
|
94
|
+
'Content-Length' => content.length.to_s
|
|
95
|
+
}
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
describe 'with a code block in the default format' do
|
|
99
|
+
|
|
100
|
+
let(:content) do
|
|
101
|
+
generate_example({
|
|
102
|
+
elements: ['pre', 'code'],
|
|
103
|
+
attribute: 'class',
|
|
104
|
+
value: 'ruby',
|
|
105
|
+
content: '(1..10).reduce(&:+)'
|
|
106
|
+
})
|
|
107
|
+
end
|
|
108
|
+
let(:options) { {} }
|
|
109
|
+
|
|
110
|
+
HIGHLIGHTERS.each do |lib|
|
|
111
|
+
|
|
112
|
+
describe "when using #{lib}" do
|
|
113
|
+
|
|
114
|
+
let(:highlighter) { lib }
|
|
115
|
+
|
|
116
|
+
it 'leaves the status untouched' do
|
|
117
|
+
last_response.status.must_equal status
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
it 'updates the headers with the new `Content-Length`' do
|
|
121
|
+
last_response.headers['Content-Length'].wont_equal headers['Content-Length']
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
it 'modifies the content' do
|
|
125
|
+
last_response.body.wont_equal content
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
EXAMPLES.each do |example|
|
|
135
|
+
|
|
136
|
+
describe "with a code block wrapped in `#{example[:elements].join(' > ')} #{example[:attribute]}=\"#{example[:value]}\"`" do
|
|
137
|
+
|
|
138
|
+
let(:content) { generate_example(example) }
|
|
139
|
+
let(:options) { example }
|
|
140
|
+
|
|
141
|
+
HIGHLIGHTERS.each do |lib|
|
|
142
|
+
|
|
143
|
+
describe "when using #{lib}" do
|
|
144
|
+
|
|
145
|
+
let(:highlighter) { lib }
|
|
146
|
+
|
|
147
|
+
it 'leaves the status untouched' do
|
|
148
|
+
last_response.status.must_equal status
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
it 'updates the headers with the new `Content-Length`' do
|
|
152
|
+
last_response.headers['Content-Length'].wont_equal headers['Content-Length']
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
it 'modifies the content' do
|
|
156
|
+
last_response.body.wont_equal content
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'minitest/autorun'
|
|
2
|
+
require 'minitest/reporters'
|
|
3
|
+
require 'rack/test'
|
|
4
|
+
|
|
5
|
+
require 'coderay'
|
|
6
|
+
require 'pygments'
|
|
7
|
+
require 'uv'
|
|
8
|
+
|
|
9
|
+
require File.expand_path('../../lib/rack/highlighter', __FILE__)
|
|
10
|
+
|
|
11
|
+
include Rack::Test::Methods
|
|
12
|
+
|
|
13
|
+
MiniTest::Reporters.use! MiniTest::Reporters::SpecReporter.new
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def create_app(status, headers, content, highlighter, options)
|
|
17
|
+
app = lambda { |env| [status, headers, [content]] }
|
|
18
|
+
Rack::Highlighter.new(app, highlighter, options)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def generate_example(args)
|
|
22
|
+
elements = args[:elements].clone
|
|
23
|
+
attribute = args[:attribute]
|
|
24
|
+
value = args[:value]
|
|
25
|
+
content = args[:content]
|
|
26
|
+
|
|
27
|
+
doc = Nokogiri::HTML::DocumentFragment.parse('')
|
|
28
|
+
current = doc
|
|
29
|
+
|
|
30
|
+
until elements.empty?
|
|
31
|
+
tmp = Nokogiri::XML::Node.new(elements.shift, doc)
|
|
32
|
+
if elements.empty?
|
|
33
|
+
tmp[attribute] = value
|
|
34
|
+
tmp.content = content
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
current << tmp
|
|
38
|
+
current = tmp
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
doc.to_html
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def generate_example_with(klass, content, &block)
|
|
45
|
+
doc.to_html
|
|
46
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rack-highlighter
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Daniel Perez Alvarez
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-12-23 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rack
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 1.0.0
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ! '>='
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: 1.0.0
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: nokogiri
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ! '>='
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: 1.4.0
|
|
38
|
+
type: :runtime
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ! '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: 1.4.0
|
|
46
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: htmlentities
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
none: false
|
|
50
|
+
requirements:
|
|
51
|
+
- - ! '>='
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: 4.0.0
|
|
54
|
+
type: :runtime
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
none: false
|
|
58
|
+
requirements:
|
|
59
|
+
- - ! '>='
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 4.0.0
|
|
62
|
+
- !ruby/object:Gem::Dependency
|
|
63
|
+
name: coderay
|
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
|
65
|
+
none: false
|
|
66
|
+
requirements:
|
|
67
|
+
- - ~>
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '1.0'
|
|
70
|
+
type: :development
|
|
71
|
+
prerelease: false
|
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
73
|
+
none: false
|
|
74
|
+
requirements:
|
|
75
|
+
- - ~>
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: '1.0'
|
|
78
|
+
- !ruby/object:Gem::Dependency
|
|
79
|
+
name: pygments.rb
|
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
|
81
|
+
none: false
|
|
82
|
+
requirements:
|
|
83
|
+
- - ~>
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '0.3'
|
|
86
|
+
type: :development
|
|
87
|
+
prerelease: false
|
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
89
|
+
none: false
|
|
90
|
+
requirements:
|
|
91
|
+
- - ~>
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: '0.3'
|
|
94
|
+
- !ruby/object:Gem::Dependency
|
|
95
|
+
name: ultraviolet
|
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
|
97
|
+
none: false
|
|
98
|
+
requirements:
|
|
99
|
+
- - ~>
|
|
100
|
+
- !ruby/object:Gem::Version
|
|
101
|
+
version: '1.0'
|
|
102
|
+
type: :development
|
|
103
|
+
prerelease: false
|
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
105
|
+
none: false
|
|
106
|
+
requirements:
|
|
107
|
+
- - ~>
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '1.0'
|
|
110
|
+
- !ruby/object:Gem::Dependency
|
|
111
|
+
name: rake
|
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
|
113
|
+
none: false
|
|
114
|
+
requirements:
|
|
115
|
+
- - ! '>='
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
none: false
|
|
122
|
+
requirements:
|
|
123
|
+
- - ! '>='
|
|
124
|
+
- !ruby/object:Gem::Version
|
|
125
|
+
version: '0'
|
|
126
|
+
- !ruby/object:Gem::Dependency
|
|
127
|
+
name: minitest
|
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
|
129
|
+
none: false
|
|
130
|
+
requirements:
|
|
131
|
+
- - ! '>='
|
|
132
|
+
- !ruby/object:Gem::Version
|
|
133
|
+
version: '0'
|
|
134
|
+
type: :development
|
|
135
|
+
prerelease: false
|
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
137
|
+
none: false
|
|
138
|
+
requirements:
|
|
139
|
+
- - ! '>='
|
|
140
|
+
- !ruby/object:Gem::Version
|
|
141
|
+
version: '0'
|
|
142
|
+
- !ruby/object:Gem::Dependency
|
|
143
|
+
name: minitest-reporters
|
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
|
145
|
+
none: false
|
|
146
|
+
requirements:
|
|
147
|
+
- - ! '>='
|
|
148
|
+
- !ruby/object:Gem::Version
|
|
149
|
+
version: '0'
|
|
150
|
+
type: :development
|
|
151
|
+
prerelease: false
|
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
153
|
+
none: false
|
|
154
|
+
requirements:
|
|
155
|
+
- - ! '>='
|
|
156
|
+
- !ruby/object:Gem::Version
|
|
157
|
+
version: '0'
|
|
158
|
+
- !ruby/object:Gem::Dependency
|
|
159
|
+
name: rack-test
|
|
160
|
+
requirement: !ruby/object:Gem::Requirement
|
|
161
|
+
none: false
|
|
162
|
+
requirements:
|
|
163
|
+
- - ! '>='
|
|
164
|
+
- !ruby/object:Gem::Version
|
|
165
|
+
version: '0'
|
|
166
|
+
type: :development
|
|
167
|
+
prerelease: false
|
|
168
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
169
|
+
none: false
|
|
170
|
+
requirements:
|
|
171
|
+
- - ! '>='
|
|
172
|
+
- !ruby/object:Gem::Version
|
|
173
|
+
version: '0'
|
|
174
|
+
- !ruby/object:Gem::Dependency
|
|
175
|
+
name: guard-minitest
|
|
176
|
+
requirement: !ruby/object:Gem::Requirement
|
|
177
|
+
none: false
|
|
178
|
+
requirements:
|
|
179
|
+
- - ! '>='
|
|
180
|
+
- !ruby/object:Gem::Version
|
|
181
|
+
version: '0'
|
|
182
|
+
type: :development
|
|
183
|
+
prerelease: false
|
|
184
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
185
|
+
none: false
|
|
186
|
+
requirements:
|
|
187
|
+
- - ! '>='
|
|
188
|
+
- !ruby/object:Gem::Version
|
|
189
|
+
version: '0'
|
|
190
|
+
- !ruby/object:Gem::Dependency
|
|
191
|
+
name: rb-inotify
|
|
192
|
+
requirement: !ruby/object:Gem::Requirement
|
|
193
|
+
none: false
|
|
194
|
+
requirements:
|
|
195
|
+
- - ! '>='
|
|
196
|
+
- !ruby/object:Gem::Version
|
|
197
|
+
version: '0'
|
|
198
|
+
type: :development
|
|
199
|
+
prerelease: false
|
|
200
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
201
|
+
none: false
|
|
202
|
+
requirements:
|
|
203
|
+
- - ! '>='
|
|
204
|
+
- !ruby/object:Gem::Version
|
|
205
|
+
version: '0'
|
|
206
|
+
- !ruby/object:Gem::Dependency
|
|
207
|
+
name: rb-fsevent
|
|
208
|
+
requirement: !ruby/object:Gem::Requirement
|
|
209
|
+
none: false
|
|
210
|
+
requirements:
|
|
211
|
+
- - ! '>='
|
|
212
|
+
- !ruby/object:Gem::Version
|
|
213
|
+
version: '0'
|
|
214
|
+
type: :development
|
|
215
|
+
prerelease: false
|
|
216
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
217
|
+
none: false
|
|
218
|
+
requirements:
|
|
219
|
+
- - ! '>='
|
|
220
|
+
- !ruby/object:Gem::Version
|
|
221
|
+
version: '0'
|
|
222
|
+
- !ruby/object:Gem::Dependency
|
|
223
|
+
name: rb-fchange
|
|
224
|
+
requirement: !ruby/object:Gem::Requirement
|
|
225
|
+
none: false
|
|
226
|
+
requirements:
|
|
227
|
+
- - ! '>='
|
|
228
|
+
- !ruby/object:Gem::Version
|
|
229
|
+
version: '0'
|
|
230
|
+
type: :development
|
|
231
|
+
prerelease: false
|
|
232
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
233
|
+
none: false
|
|
234
|
+
requirements:
|
|
235
|
+
- - ! '>='
|
|
236
|
+
- !ruby/object:Gem::Version
|
|
237
|
+
version: '0'
|
|
238
|
+
description: Rack Middleware for syntax highlighting.
|
|
239
|
+
email:
|
|
240
|
+
- unindented@gmail.com
|
|
241
|
+
executables: []
|
|
242
|
+
extensions: []
|
|
243
|
+
extra_rdoc_files: []
|
|
244
|
+
files:
|
|
245
|
+
- .gitignore
|
|
246
|
+
- .travis.yml
|
|
247
|
+
- Gemfile
|
|
248
|
+
- Guardfile
|
|
249
|
+
- LICENSE
|
|
250
|
+
- README.md
|
|
251
|
+
- Rakefile
|
|
252
|
+
- lib/rack/highlighter.rb
|
|
253
|
+
- lib/rack/highlighter/version.rb
|
|
254
|
+
- rack-highlighter.gemspec
|
|
255
|
+
- spec/highlighter_spec.rb
|
|
256
|
+
- spec/spec_helper.rb
|
|
257
|
+
homepage: http://github.com/unindented/rack-highlighter
|
|
258
|
+
licenses: []
|
|
259
|
+
post_install_message:
|
|
260
|
+
rdoc_options: []
|
|
261
|
+
require_paths:
|
|
262
|
+
- lib
|
|
263
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
264
|
+
none: false
|
|
265
|
+
requirements:
|
|
266
|
+
- - ! '>='
|
|
267
|
+
- !ruby/object:Gem::Version
|
|
268
|
+
version: 1.9.0
|
|
269
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
270
|
+
none: false
|
|
271
|
+
requirements:
|
|
272
|
+
- - ! '>='
|
|
273
|
+
- !ruby/object:Gem::Version
|
|
274
|
+
version: '0'
|
|
275
|
+
segments:
|
|
276
|
+
- 0
|
|
277
|
+
hash: -1336533414819540878
|
|
278
|
+
requirements: []
|
|
279
|
+
rubyforge_project:
|
|
280
|
+
rubygems_version: 1.8.23
|
|
281
|
+
signing_key:
|
|
282
|
+
specification_version: 3
|
|
283
|
+
summary: Rack Middleware that provides syntax highlighting of code blocks, using CodeRay,
|
|
284
|
+
Pygments or Ultraviolet.
|
|
285
|
+
test_files:
|
|
286
|
+
- spec/highlighter_spec.rb
|
|
287
|
+
- spec/spec_helper.rb
|