wbzyl-rack-codehighlighter 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +87 -87
- data/VERSION.yml +1 -1
- data/examples/config.ru +3 -3
- data/lib/rack/codehighlighter.rb +4 -3
- metadata +1 -1
data/README.markdown
CHANGED
@@ -7,27 +7,27 @@ a bunch of code highlighters to make their usage as generic possible.
|
|
7
7
|
* coderay
|
8
8
|
* syntax
|
9
9
|
* prettify
|
10
|
-
* censor
|
10
|
+
* censor (a fake highlighter used in example below)
|
11
11
|
|
12
12
|
Install the gem with:
|
13
13
|
|
14
14
|
sudo gem install wbzyl-rack-codehighlighter -s http://gems.github.com
|
15
15
|
|
16
16
|
|
17
|
-
The middleware
|
18
|
-
|
19
|
-
highlight all `pre` elements:
|
17
|
+
The middleware looks for code blocks to be highlighted in HTML produced by
|
18
|
+
application. For each block found it calls requested highlighter.
|
19
|
+
Below we ask *coderay* to highlight all `pre` elements:
|
20
20
|
|
21
21
|
use Rack::Codehighlighter, :coderay, :element => "pre", :pattern => /\A:::(\w+)\s*\n/
|
22
22
|
|
23
|
-
The middleware uses the pattern to learn what language the code block
|
23
|
+
The middleware uses the *pattern* to learn what language the code block
|
24
24
|
contains, for example
|
25
25
|
|
26
26
|
<pre>:::ruby
|
27
27
|
puts "hello world"
|
28
28
|
</pre>
|
29
29
|
|
30
|
-
Plain description what the pattern says:
|
30
|
+
Plain description what the *pattern* says:
|
31
31
|
If the element contents begins with three colons, the text following
|
32
32
|
the colons, up to the end of line, identifies the language. The text
|
33
33
|
matched by the pattern is removed from the code block before
|
@@ -37,85 +37,13 @@ The above example could be shortened to:
|
|
37
37
|
|
38
38
|
use Rack::Codehighlighter, :coderay
|
39
39
|
|
40
|
-
because the default values
|
40
|
+
because the default options values are used.
|
41
41
|
|
42
42
|
Normalization:
|
43
43
|
|
44
|
-
*
|
45
|
-
with attributes appropriate for codehighlighter used.
|
46
|
-
*
|
47
|
-
|
48
|
-
|
49
|
-
## A simple example with inline template
|
50
|
-
|
51
|
-
# example.rb
|
52
|
-
|
53
|
-
require 'rubygems'
|
54
|
-
|
55
|
-
gem 'sinatra', '>=0.9.0'
|
56
|
-
require 'sinatra'
|
57
|
-
|
58
|
-
gem 'wbzyl-rack-codehighlighter', '>=0.2.0'
|
59
|
-
require 'rack/codehighlighter'
|
60
|
-
|
61
|
-
use Rack::Codehighlighter, :censor, :reason => '[[--difficult code removed--]]'
|
62
|
-
|
63
|
-
get "/" do
|
64
|
-
erb :hello
|
65
|
-
end
|
66
|
-
|
67
|
-
__END__
|
68
|
-
|
69
|
-
@@ hello
|
70
|
-
<h3>Fibonacci numbers in Ruby</h3>
|
71
|
-
|
72
|
-
<pre>:::ruby
|
73
|
-
def fib(n)
|
74
|
-
if n < 2
|
75
|
-
1
|
76
|
-
else
|
77
|
-
fib(n-2) + fib(n-1)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
</pre>
|
81
|
-
|
82
|
-
Run the above example with:
|
83
|
-
|
84
|
-
ruby example.rb
|
85
|
-
|
86
|
-
The results are accessible from `http://localhost:4567`.
|
87
|
-
|
88
|
-
|
89
|
-
## Why using middleware for code highlighting is awesome?
|
90
|
-
|
91
|
-
In pre-Rack applications era possible approaches were:
|
92
|
-
|
93
|
-
* pure javascript; cons code must be html-escaped
|
94
|
-
* gems; conection to methods responsible for code highlighting
|
95
|
-
is obtrusive, i.e. via plugin + additional markup
|
96
|
-
|
97
|
-
Analyze packages mentioned at the *The Ruby Toolbox* page:
|
98
|
-
[Syntax Highlighting](http://ruby-toolbox.com/categories/syntax_highlighting.html)
|
99
|
-
|
100
|
-
Links:
|
101
|
-
|
102
|
-
http://carboni.ca/projects/harsh/
|
103
|
-
unless HAML is used
|
104
|
-
http://redclothcoderay.rubyforge.org/
|
105
|
-
http://github.com/augustl/redcloth-with-coderay
|
106
|
-
how to use with Rails
|
107
|
-
does't degrade to html: new source tag
|
108
|
-
http://github.com/arya/tm_syntax_highlighting/
|
109
|
-
how to connect to rails/sinatra?
|
110
|
-
|
111
|
-
[*Ruby tips from me, your idol*](http://www.binarylogic.com/2009/04/19/ruby-tips-from-me-your-idol):
|
112
|
-
I can not tell you how much time I’ve wasted trying to add in some
|
113
|
-
cool feature into rails. I would dig into the rails internals,
|
114
|
-
override methods, do all kinds of tricky stuff. I thought I was
|
115
|
-
awesome. A month later rails comes out with some cool new feature, I
|
116
|
-
update rails and everything explodes.
|
117
|
-
|
118
|
-
Conclusion: highlighting via plugins is doomed to explode sooner or later.
|
44
|
+
* Highlighted code is always wrapped with `pre` element
|
45
|
+
with attributes appropriate for codehighlighter used.
|
46
|
+
* Language names are taken from Ultraviolet.
|
119
47
|
|
120
48
|
|
121
49
|
## Using with Rack application
|
@@ -125,8 +53,7 @@ a **Sinatra** application. If your application includes a rackup file or
|
|
125
53
|
uses *Rack::Builder* to construct the application pipeline, simply
|
126
54
|
require and use as follows:
|
127
55
|
|
128
|
-
# get one of supported highlighters
|
129
|
-
gem 'coderay'
|
56
|
+
gem 'coderay' # get one of supported highlighters
|
130
57
|
require 'coderay'
|
131
58
|
|
132
59
|
gem 'wbzyl-rack-codehighlighter'
|
@@ -145,8 +72,7 @@ for sample stylesheets).
|
|
145
72
|
In order to use include the following in a Rails application
|
146
73
|
`config/environment.rb` file:
|
147
74
|
|
148
|
-
# get one of supported highlighters
|
149
|
-
require 'coderay'
|
75
|
+
require 'coderay' # get one of supported highlighters
|
150
76
|
|
151
77
|
require 'rack/codehighlighter'
|
152
78
|
|
@@ -199,9 +125,83 @@ All highlighters use `pre` element to wrap highlighted code.
|
|
199
125
|
In Markdown, Maruku and RDiscount templates code is wrapped with `pre>code`.
|
200
126
|
To remove an extra nesting the `:markdown` option should be used:
|
201
127
|
|
202
|
-
use Rack::Codehighlighter, :coderay, :markdown => true
|
128
|
+
use Rack::Codehighlighter, :coderay, :markdown => true,
|
203
129
|
:element => "pre>code", :pattern => /\A:::(\w+)\s*\n/, :logging => false
|
204
130
|
|
131
|
+
## A simple example with inline template
|
132
|
+
|
133
|
+
# example.rb
|
134
|
+
|
135
|
+
require 'rubygems'
|
136
|
+
|
137
|
+
gem 'sinatra', '>=0.9.0'
|
138
|
+
require 'sinatra'
|
139
|
+
|
140
|
+
gem 'wbzyl-rack-codehighlighter', '>=0.2.0'
|
141
|
+
require 'rack/codehighlighter'
|
142
|
+
|
143
|
+
use Rack::Codehighlighter, :censor, :reason => '[[--difficult code removed--]]'
|
144
|
+
|
145
|
+
get "/" do
|
146
|
+
erb :hello
|
147
|
+
end
|
148
|
+
|
149
|
+
__END__
|
150
|
+
|
151
|
+
@@ hello
|
152
|
+
<h3>Fibonacci numbers in Ruby</h3>
|
153
|
+
|
154
|
+
<pre>:::ruby
|
155
|
+
def fib(n)
|
156
|
+
if n < 2
|
157
|
+
1
|
158
|
+
else
|
159
|
+
fib(n-2) + fib(n-1)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
</pre>
|
163
|
+
|
164
|
+
Run the above example with:
|
165
|
+
|
166
|
+
ruby example.rb
|
167
|
+
|
168
|
+
The results are accessible from `http://localhost:4567`.
|
169
|
+
|
170
|
+
|
171
|
+
## Why using middleware for code highlighting is awesome?
|
172
|
+
|
173
|
+
In each piece of code inserted into html we must change:
|
174
|
+
`<` to `<`. This is annoying thing.
|
175
|
+
Each(? prettify, dp-) pure javascript highlighter has this defect.
|
176
|
+
|
177
|
+
In pre-Rack applications era possible approaches were:
|
178
|
+
|
179
|
+
* gems; conection to methods responsible for code highlighting
|
180
|
+
is obtrusive, i.e. via plugin + additional markup
|
181
|
+
|
182
|
+
Analyze packages mentioned at the *The Ruby Toolbox* page:
|
183
|
+
[Syntax Highlighting](http://ruby-toolbox.com/categories/syntax_highlighting.html)
|
184
|
+
|
185
|
+
Links:
|
186
|
+
|
187
|
+
http://carboni.ca/projects/harsh/
|
188
|
+
unless HAML is used
|
189
|
+
http://redclothcoderay.rubyforge.org/
|
190
|
+
http://github.com/augustl/redcloth-with-coderay
|
191
|
+
how to use with Rails
|
192
|
+
does't degrade to html: new source tag
|
193
|
+
http://github.com/arya/tm_syntax_highlighting/
|
194
|
+
how to connect to rails/sinatra?
|
195
|
+
|
196
|
+
[*Ruby tips from me, your idol*](http://www.binarylogic.com/2009/04/19/ruby-tips-from-me-your-idol):
|
197
|
+
I can not tell you how much time I’ve wasted trying to add in some
|
198
|
+
cool feature into rails. I would dig into the rails internals,
|
199
|
+
override methods, do all kinds of tricky stuff. I thought I was
|
200
|
+
awesome. A month later rails comes out with some cool new feature, I
|
201
|
+
update rails and everything explodes.
|
202
|
+
|
203
|
+
Conclusion: highlighting via plugins is doomed to explode sooner or later.
|
204
|
+
|
205
205
|
|
206
206
|
## Supported highlighters
|
207
207
|
|
data/VERSION.yml
CHANGED
data/examples/config.ru
CHANGED
@@ -7,12 +7,12 @@ use Rack::Lint
|
|
7
7
|
|
8
8
|
#use Rack::Codehighlighter, :prettify, :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => true
|
9
9
|
|
10
|
-
use Rack::Codehighlighter, :coderay, :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => true
|
10
|
+
#use Rack::Codehighlighter, :coderay, :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => true
|
11
11
|
|
12
12
|
#use Rack::Codehighlighter, :syntax, :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => true
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
use Rack::Codehighlighter, :ultraviolet, :theme => 'dawn',
|
15
|
+
:element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => true
|
16
16
|
|
17
17
|
#use Rack::Codehighlighter, :censor, :reason => '[[-- ugly code removed --]]'
|
18
18
|
|
data/lib/rack/codehighlighter.rb
CHANGED
@@ -10,13 +10,14 @@ module Rack
|
|
10
10
|
|
11
11
|
FORMAT = %{%s - [%s] [%s] "%s %s%s %s" (%s) %d %d %0.4f\n}
|
12
12
|
|
13
|
-
def initialize(app, highlighter = :
|
13
|
+
def initialize(app, highlighter = :censor, opts = {})
|
14
14
|
@app = app
|
15
15
|
@highlighter = highlighter
|
16
16
|
@opts = {
|
17
17
|
:element => "pre",
|
18
18
|
:pattern => /\A:::(\w+)\s*\n/,
|
19
|
-
:reason => "[[-- ugly code removed --]]"
|
19
|
+
:reason => "[[-- ugly code removed --]]",
|
20
|
+
:markdown => false
|
20
21
|
}
|
21
22
|
@opts.merge! opts
|
22
23
|
end
|
@@ -37,7 +38,7 @@ module Rack
|
|
37
38
|
nodes = doc.search(@opts[:element])
|
38
39
|
nodes.each do |node|
|
39
40
|
s = node.inner_html || "[++where is the code?++]"
|
40
|
-
if @opts[:
|
41
|
+
if @opts[:markdown]
|
41
42
|
node.parent.swap(send(@highlighter, s))
|
42
43
|
else
|
43
44
|
node.swap(send(@highlighter, s))
|