wbzyl-rack-codehighlighter 0.2.2 → 0.2.4

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.
@@ -1,98 +1,96 @@
1
1
  # Rack::Codehighlighter middleware
2
2
 
3
- *Rack::Codehighlighter* provides a thin wrapper over
4
- a bunch of code highlighters to make their usage as generic possible.
3
+
4
+ ## What?
5
+
6
+ The *rack-codehighlighter* gem provides a thin wrapper over
7
+ a bunch of code highlighters to make their usage as generic possible:
5
8
 
6
9
  * ultraviolet
7
10
  * coderay
8
11
  * syntax
9
12
  * prettify
10
- * censor (a fake highlighter used in example below)
11
-
12
- Install the gem with:
13
-
14
- sudo gem install wbzyl-rack-codehighlighter -s http://gems.github.com
15
-
13
+ * censor (a fake highlighter used in the example below)
16
14
 
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
-
21
- use Rack::Codehighlighter, :coderay, :element => "pre", :pattern => /\A:::(\w+)\s*\n/
22
15
 
23
- The middleware uses the *pattern* to learn what language the code block
24
- contains, for example
16
+ ## How it works?
25
17
 
26
- <pre>:::ruby
27
- puts "hello world"
28
- </pre>
18
+ *Rack::Codehighlighter* looks for code blocks to be highlighted in the HTML
19
+ produced by your application. For each block found it calls requested
20
+ highlighter.
29
21
 
30
- Plain description what the *pattern* says:
31
- If the element contents begins with three colons, the text following
32
- the colons, up to the end of line, identifies the language. The text
33
- matched by the pattern is removed from the code block before
34
- processing.
35
22
 
36
- The above example could be shortened to:
23
+ ## Installing and Usage
37
24
 
38
- use Rack::Codehighlighter, :coderay
25
+ Install the gem with:
39
26
 
40
- because the default options values are used.
27
+ sudo gem install wbzyl-rack-codehighlighter -s http://gems.github.com
41
28
 
42
- Normalization:
29
+ ### Using *rack-codehighlighter* with a Rails application
43
30
 
44
- * Highlighted code is always wrapped with `pre` element
45
- with attributes appropriate for codehighlighter used.
46
- * Language names are taken from Ultraviolet.
31
+ In order to use, include the following code in a Rails application
32
+ `config/environment.rb` file:
47
33
 
34
+ require 'coderay' # get one of supported highlighters
35
+ require 'rack/codehighlighter'
36
+
37
+ Rails::Initializer.run do |config|
38
+ config.gem 'coderay'
39
+ config.gem 'wbzyl-rack-codehighlighter'
40
+
41
+ config.middleware.use Rack::Codehighlighter, :coderay, :element => "pre", :pattern => /\A:::(\w+)\s*\n/
42
+ end
48
43
 
49
- ## Using with Rack application
44
+ ### Using *rack-codehighlighter* with a Rack application
50
45
 
51
- *Rack::Codehighlighter* can be used with any Rack application, for example with
52
- a **Sinatra** application. If your application includes a rackup file or
53
- uses *Rack::Builder* to construct the application pipeline, simply
54
- require and use as follows:
46
+ The *rack-codehighlighter* gem can be used with any Rack application,
47
+ for example with a **Sinatra** application. If your application
48
+ includes a rackup file or uses *Rack::Builder* to construct the
49
+ application pipeline, simply require and use as follows:
55
50
 
56
- gem 'coderay' # get one of supported highlighters
57
- require 'coderay'
58
-
51
+ gem 'coderay' # get one of supported highlighters
52
+ require 'coderay'
53
+
59
54
  gem 'wbzyl-rack-codehighlighter'
60
55
  require 'rack/codehighlighter'
61
56
 
62
- use Rack::Codehighlighter, :coderay
57
+ use Rack::Codehighlighter, :coderay, :element => "pre", :pattern => /\A:::(\w+)\s*\n/
63
58
  run app
64
59
 
65
- Remember to include in the layout an appropriate stylesheet
66
- (look into `examples/public/stylesheets` directory
67
- for sample stylesheets).
68
60
 
61
+ ## *Rack::Codehighlighter* by an example
69
62
 
70
- ## Using with Rails
63
+ Below we ask *Rack::Codehighlighter* to look for code blocks into all
64
+ `pre` *element*s:
71
65
 
72
- In order to use include the following in a Rails application
73
- `config/environment.rb` file:
66
+ use Rack::Codehighlighter, :coderay, :element => "pre", :pattern => /\A:::(\w+)\s*\n/
74
67
 
75
- require 'coderay' # get one of supported highlighters
76
-
77
- require 'rack/codehighlighter'
78
-
79
- Rails::Initializer.run do |config|
80
- config.gem 'coderay'
81
- config.gem 'wbzyl-rack-codehighlighter'
82
-
83
- config.middleware.use Rack::Codehighlighter, :coderay
84
- end
68
+ and use the `/\A:::(\w+)\s*\n/` *pattern* to learn what language
69
+ the code block contains.
85
70
 
86
- Check the Rack configuration:
71
+ For example:
87
72
 
88
- rake middleware
73
+ <pre>:::ruby
74
+ puts "hello world"
75
+ </pre>
76
+
77
+ Informal description of the *pattern*: if the element contents begins
78
+ with three colons, word characters following these colons up to
79
+ optional spaces followed by end of line, name the language.
89
80
 
90
- Remember to include in the layout an appropriate stylesheet
91
- (look into `examples/public/stylesheets` directory
92
- for sample stylesheets).
81
+ Language names are taken from the *ultraviolet* gem, see below for
82
+ the list.
93
83
 
84
+ The matched element is removed, and the code block is passed to *coderay*
85
+ highlighter for processing.
94
86
 
95
- ## Configuration examples
87
+ The highlighted code returned by the *coderay* highlighter is
88
+ wrapped with `pre` element with attributes appropriate for the
89
+ codehighlighter used.
90
+
91
+ ### More examples
92
+
93
+ In examples displayed below, the default value of each option is used.
96
94
 
97
95
  Coderay:
98
96
 
@@ -119,24 +117,25 @@ Censor:
119
117
  use Rack::Codehighlighter, :censor, :reason => "[[-- ugly code removed --]]",
120
118
  :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => false
121
119
 
122
- In the above examples, the default value of each option is used.
123
120
 
124
- All highlighters use `pre` element to wrap highlighted code.
125
- In Markdown, Maruku and RDiscount templates code is wrapped with `pre>code`.
126
- To remove an extra nesting the `:markdown` option should be used:
121
+ In Markdown, Maruku and RDiscount templates, after converting to HTML,
122
+ code is wrapped with `pre>code`. To remove this extra one level of
123
+ nesting the `:markdown` option should be used:
127
124
 
128
125
  use Rack::Codehighlighter, :coderay, :markdown => true,
129
126
  :element => "pre>code", :pattern => /\A:::(\w+)\s*\n/, :logging => false
130
127
 
131
- ## A simple example with inline template
128
+ Check the `examples` directory for working examples.
129
+
130
+ ## Try it!
131
+
132
+ Simple Copy & Paste example. Implemented in Sinatra and uses inline template.
132
133
 
133
134
  # example.rb
134
135
 
135
136
  require 'rubygems'
136
-
137
- gem 'sinatra', '>=0.9.0'
137
+ gem 'sinatra', '>=0.9.0'
138
138
  require 'sinatra'
139
-
140
139
  gem 'wbzyl-rack-codehighlighter', '>=0.2.0'
141
140
  require 'rack/codehighlighter'
142
141
 
@@ -147,10 +146,8 @@ To remove an extra nesting the `:markdown` option should be used:
147
146
  end
148
147
 
149
148
  __END__
150
-
151
149
  @@ hello
152
150
  <h3>Fibonacci numbers in Ruby</h3>
153
-
154
151
  <pre>:::ruby
155
152
  def fib(n)
156
153
  if n < 2
@@ -161,54 +158,18 @@ To remove an extra nesting the `:markdown` option should be used:
161
158
  end
162
159
  </pre>
163
160
 
164
- Run the above example with:
161
+ Run the example with:
165
162
 
166
163
  ruby example.rb
167
164
 
168
165
  The results are accessible from `http://localhost:4567`.
169
166
 
170
167
 
171
- ## Why using middleware for code highlighting is awesome?
172
-
173
- In each piece of code inserted into html we must change:
174
- `<` to `&lt;`. 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
-
206
168
  ## Supported highlighters
207
169
 
208
170
  These currently include: *Syntax* (fast), *Coderay* (very fast),
209
171
  *Ultraviolet* (slow, but highlights almost any language).
210
172
 
211
-
212
173
  ### [Syntax](http://syntax.rubyforge.org/)
213
174
 
214
175
  Languages supported by *Syntax*:
@@ -216,7 +177,6 @@ Languages supported by *Syntax*:
216
177
  * xml
217
178
  * ruby
218
179
 
219
-
220
180
  ### [Coderay](http://coderay.rubychan.de/)
221
181
 
222
182
  Languages supported by *Coderay*:
@@ -243,10 +203,10 @@ The *ultraviolet* gem needs oniguruma regexp library.
243
203
 
244
204
  On Fedora install the library with:
245
205
 
246
- sudo yum install oniguruma
206
+ sudo yum install oniguruma oniguruma-devel
247
207
 
248
- For installation instruction from sources, see
249
- [Carbonica](http://carboni.ca/projects/harsh/)
208
+ For installation instruction of the *oniguruma* library from sources,
209
+ see [Carbonica](http://carboni.ca/projects/harsh/)
250
210
 
251
211
  Now, install the gem:
252
212
 
@@ -288,3 +248,66 @@ Ultraviolet supports almost any language:
288
248
  * vectorscript
289
249
  * xhtml\_1.0, xml, xml\_strict, xsl
290
250
  * yaml, yui\_javascript
251
+
252
+
253
+ ## Why? TODO
254
+
255
+ Currently, almost everything what I write is rendered by Ruby on Rails
256
+ and Sinatra applications. To improve the readability of the text, I
257
+ want the code fragments to be colored. So extending Rails and Sinatra
258
+ frameworks with syntax highlighting is a must.
259
+
260
+ The problem is to how syntax highlighting features are hooked into
261
+ these frameworks.
262
+
263
+ Look at the current practice. To this end, analyze the top three tools
264
+ listed on *The Ruby Toolbox* in category
265
+ [Syntax Highlighting](http://ruby-toolbox.com/categories/syntax_highlighting.html).
266
+
267
+ [tm_syntax_highlighting](http://github.com/arya/tm_syntax_highlighting/) (plugin):
268
+
269
+ code(some_ruby_code, :theme => "twilight", :lang => "ruby", :line_numbers => true)
270
+
271
+ [harsh](http://carboni.ca/projects/harsh/) (plugin):
272
+
273
+ <% harsh :theme => :dawn do %> | <% harsh %Q{ some_ruby_code }, :theme => :dawn %>
274
+ some_ruby_code |
275
+ <% end %> |
276
+
277
+ [Redcloth with CodeRay](http://github.com/augustl/redcloth-with-coderay) (gem):
278
+
279
+ <source:ruby> some_ruby_code </source>
280
+
281
+ Overdone: highlighting engine/library/framework.
282
+ Different solutions for each one framework are needed.
283
+ Different output: include.
284
+
285
+ With Rack based application we can simplifiy thingst by adding to the
286
+ application pipeline..
287
+
288
+ [*Ruby tips from me, your idol*](http://www.binarylogic.com/2009/04/19/ruby-tips-from-me-your-idol):
289
+ I can not tell you how much time I’ve wasted trying to add in some
290
+ cool feature into rails. I would dig into the rails internals,
291
+ override methods, do all kinds of tricky stuff. I thought I was
292
+ awesome. A month later rails comes out with some cool new feature, I
293
+ update rails and everything explodes.
294
+
295
+ Conclusion: highlighting via plugins is doomed to explode sooner or later.
296
+
297
+
298
+ *Rack::Codehighlighter* provides a thin wrapper over
299
+ a bunch of code highlighters to make their usage as generic possible.
300
+
301
+ Uniform/define own..
302
+
303
+ In each piece of code inserted into html we must change:
304
+ `<` to `&lt;`. This is annoying thing.
305
+ Each(? prettify, dp-) pure javascript highlighter has this defect.
306
+
307
+ In pre-Rack applications era possible approaches were:
308
+
309
+ * gems; connection to methods responsible for code highlighting
310
+ is obtrusive, i.e. via plugin + additional markup
311
+
312
+ Analyze packages mentioned at the *The Ruby Toolbox* page:
313
+ [Syntax Highlighting](http://ruby-toolbox.com/categories/syntax_highlighting.html)
data/TODO CHANGED
@@ -1,13 +1,9 @@
1
+ [15.06.2009]
1
2
 
2
- [1.05.2009, done]
3
- Add support for code-prettify:
4
-
5
- * [source code](http://code.google.com/p/google-code-prettify/)
6
- [example](http://google-code-prettify.googlecode.com/svn/trunk/README.html)
3
+ Compare: http://freelancing-god.github.com/ts/en/
4
+ Description of thinking-sphinx gem.
7
5
 
8
-
9
- * see http://tomayko.com/writings/javascript-prettification
10
- (support for jQuery, new pallette)
6
+ [1.05.2009, done]
11
7
 
12
8
  * example:
13
9
  <pre class="prettyprint lang-html">
@@ -17,6 +13,3 @@
17
13
  "java", "js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh",
18
14
  "xhtml", "xml", "xsl".
19
15
  </pre>
20
-
21
- [1.05.2009]
22
- Add support for unobtrustive Code Highlighter By Dan Webb 11/2005
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 2
3
- :major: 0
4
2
  :minor: 2
3
+ :patch: 4
4
+ :major: 0
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+
3
+ require 'coderay'
4
+ require 'uv'
5
+ require 'syntax/convertors/html'
6
+
7
+ str = "def hitch; end"
8
+
9
+ puts "---- Syntax ----"
10
+ puts Syntax::Convertors::HTML.for_syntax('ruby').convert(str)
11
+
12
+ puts "---- Coderay ----"
13
+ puts CodeRay.encoder(:html).encode(str, 'ruby')
14
+
15
+ puts "---- Ultraviolet ----"
16
+ puts Uv.parse(str, 'xhtml', 'ruby_experimental', false, 'dawn')
@@ -1,7 +1,4 @@
1
- gem 'rack'
2
1
  require 'rack/utils'
3
-
4
- gem 'hpricot', '>=0.8.1'
5
2
  require 'hpricot'
6
3
 
7
4
  module Rack
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wbzyl-rack-codehighlighter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wlodek Bzyl
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-07 00:00:00 -07:00
12
+ date: 2009-07-26 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -57,6 +57,7 @@ files:
57
57
  - TODO
58
58
  - VERSION.yml
59
59
  - examples/app.rb
60
+ - examples/check.rb
60
61
  - examples/config.ru
61
62
  - examples/public/javascripts/lang-css.js
62
63
  - examples/public/javascripts/lang-hs.js
@@ -113,4 +114,5 @@ signing_key:
113
114
  specification_version: 3
114
115
  summary: Rack Middleware for Code Highlighting.
115
116
  test_files:
117
+ - examples/check.rb
116
118
  - examples/app.rb