wbzyl-rack-codehighlighter 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,33 +1,103 @@
1
- # Rack Middleware for Code Highlighting
1
+ # Rack::Codehighlighter middleware
2
2
 
3
- Why one should use middleware (filter) for code highlighting?
4
- The short answer is: because it is unobtrusive.
3
+ *Rack::Codehighlighter* provides a thin wrapper over
4
+ a bunch of code highlighters to make their usage as generic possible.
5
5
 
6
- In pre-Rack applications era the possible approaches were:
6
+ * ultraviolet
7
+ * coderay
8
+ * syntax
9
+ * prettify
10
+ * censor
7
11
 
8
- 1. pure javascript
12
+ Install the gem with:
9
13
 
10
- [*Ruby tips from me, your idol*](http://www.binarylogic.com/2009/04/19/ruby-tips-from-me-your-idol):
11
- I can not tell you how much time I’ve wasted trying to add in some
12
- cool feature into rails. I would dig into the rails internals,
13
- override methods, do all kinds of tricky stuff. I thought I was
14
- awesome. A month later rails comes out with some cool new feature, I
15
- update rails and everything explodes.
14
+ sudo gem install wbzyl-rack-codehighlighter -s http://gems.github.com
16
15
 
17
- Code must be html-escaped.
18
16
 
19
- 2. gems (ok) + conection (obtrusive)
17
+ The middleware reads HTML produced by an application, where it looks
18
+ for code blocks to highlight. Below we ask *coderay* to
19
+ highlight all `pre` elements:
20
20
 
21
+ use Rack::Codehighlighter, :coderay, :element => "pre", :pattern => /\A:::(\w+)\s*\n/
21
22
 
22
- To install it, run:
23
+ The middleware uses the pattern to learn what language the code block
24
+ contains, for example
25
+
26
+ <pre>:::ruby
27
+ puts "hello world"
28
+ </pre>
29
+
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
+
36
+ The above example could be shortened to:
37
+
38
+ use Rack::Codehighlighter, :coderay
39
+
40
+ because the default values for options are used.
41
+
42
+ Normalization:
43
+
44
+ * highlighted code is always wrapped with `pre` element
45
+ with attributes appropriate for codehighlighter used. (2
46
+ * language names are taken from Ultraviolet
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`.
23
87
 
24
- sudo gem install wbzyl-rack-codehighlighter -s http://gems.github.com
25
88
 
26
- Analyze
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:
27
98
  [Syntax Highlighting](http://ruby-toolbox.com/categories/syntax_highlighting.html)
28
- packages from the *The Ruby Toolbox* page.
29
99
 
30
- Exisitng practice is obtrusive:
100
+ Links:
31
101
 
32
102
  http://carboni.ca/projects/harsh/
33
103
  unless HAML is used
@@ -37,14 +107,15 @@ Exisitng practice is obtrusive:
37
107
  does't degrade to html: new source tag
38
108
  http://github.com/arya/tm_syntax_highlighting/
39
109
  how to connect to rails/sinatra?
40
-
41
- Pure Javascript highlighters:
42
-
43
- In Ruby on Rails (redcloth)
44
-
45
- Add censored method/example.
46
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.
47
117
 
118
+ Conclusion: highlighting via plugins is doomed to explode sooner or later.
48
119
 
49
120
 
50
121
  ## Using with Rack application
@@ -54,128 +125,89 @@ a **Sinatra** application. If your application includes a rackup file or
54
125
  uses *Rack::Builder* to construct the application pipeline, simply
55
126
  require and use as follows:
56
127
 
128
+ # get one of supported highlighters
129
+ gem 'coderay'
130
+ require 'coderay'
131
+
57
132
  gem 'wbzyl-rack-codehighlighter'
58
133
  require 'rack/codehighlighter'
59
-
60
- gem 'ultraviolet'
61
- require 'uv'
62
-
63
- use Rack::Codehighlighter, :ultraviolet
134
+
135
+ use Rack::Codehighlighter, :coderay
64
136
  run app
65
137
 
66
- Instead of *ultraviolet* you can use other supported highlighters:
67
- *syntax*, *coderay*, *prettify*.
138
+ Remember to include in the layout an appropriate stylesheet
139
+ (look into `examples/public/stylesheets` directory
140
+ for sample stylesheets).
68
141
 
69
- Include in the layout one of provided stylesheets.
70
142
 
71
143
  ## Using with Rails
72
144
 
73
145
  In order to use include the following in a Rails application
74
146
  `config/environment.rb` file:
75
147
 
148
+ # get one of supported highlighters
149
+ require 'coderay'
150
+
76
151
  require 'rack/codehighlighter'
77
152
 
78
153
  Rails::Initializer.run do |config|
154
+ config.gem 'coderay'
79
155
  config.gem 'wbzyl-rack-codehighlighter'
80
- config.middleware.use Rack::Codehighlighter, :ultraviolet
156
+
157
+ config.middleware.use Rack::Codehighlighter, :coderay
81
158
  end
82
159
 
83
160
  Check the Rack configuration:
84
161
 
85
162
  rake middleware
86
163
 
87
- More configuration options: see below.
88
-
164
+ Remember to include in the layout an appropriate stylesheet
165
+ (look into `examples/public/stylesheets` directory
166
+ for sample stylesheets).
89
167
 
90
- [*Ruby tips from me, your idol*](http://www.binarylogic.com/2009/04/19/ruby-tips-from-me-your-idol):
91
- Think about what you are doing, try to understand it, come up with a
92
- better solution, etc.
93
- *Is it Rack a cool feature?*
94
-
95
-
96
- ## Configuration options
97
-
98
- Markup your code with:
99
-
100
- <pre><code>:::ruby
101
- ...
102
- </code></pre>
103
-
104
- ## Quick Sinatra example
105
-
106
- Example (incomplete html, needs a layout file with link to css):
107
-
108
- # file example.rb
109
-
110
- require 'rubygems'
111
- require 'sinatra'
112
-
113
- gem 'coderay'
114
- require 'coderay'
115
-
116
- gem 'wbzyl-rack-codehighlighter'
117
- require 'rack/codehighlighter'
118
-
119
- use Rack::Codehighlighter, :coderay
120
-
121
- get "/" do
122
- erb :hello
123
- end
124
-
125
- __END__
126
-
127
- @@ hello
128
- ### Fibonacci numbers in Ruby
129
-
130
- <pre><code>:::ruby
131
- def fib(n)
132
- if n < 2
133
- 1
134
- else
135
- fib(n-2) + fib(n-1)
136
- end
137
- end
138
- </code></pre>
139
168
 
140
- Run the above example with:
169
+ ## Configuration examples
141
170
 
142
- ruby example.rb
171
+ Coderay:
143
172
 
173
+ use Rack::Codehighlighter, :coderay,
174
+ :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => false
144
175
 
145
- ## More Sinatra examples
176
+ Ultraviolet:
146
177
 
147
- The default markup:
178
+ use Rack::Codehighlighter, :ultraviolet, :theme => "dawn", :lines => false,
179
+ :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => false
148
180
 
149
- If the first line begins with three colons, the text following
150
- the colons identifies the language (ruby in the example).
151
- The first line is removed from the code block before processing.
181
+ Prettify:
152
182
 
153
- The directory *examples* contains ready to run, simple Sinatra app. Try
183
+ use Rack::Codehighlighter, :prettify,
184
+ :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => false
154
185
 
155
- rackup -p 4567 config.ru
186
+ Syntax:
156
187
 
157
- or better yet (requires the *thin* gem to be installed):
188
+ use Rack::Codehighlighter, :syntax,
189
+ :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => false
158
190
 
159
- thin --rackup config.ru -p 4567 start
191
+ Censor:
160
192
 
193
+ use Rack::Codehighlighter, :censor, :reason => "[[-- ugly code removed --]]",
194
+ :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => false
161
195
 
162
- Finally, visit the following url:
196
+ In the above examples, the default value of each option is used.
163
197
 
164
- http://localhost:4567
198
+ All highlighters use `pre` element to wrap highlighted code.
199
+ In Markdown, Maruku and RDiscount templates code is wrapped with `pre>code`.
200
+ To remove an extra nesting the `:markdown` option should be used:
165
201
 
166
- and contemplate the sheer beauty of the rendered code.
202
+ use Rack::Codehighlighter, :coderay, :markdown => true
203
+ :element => "pre>code", :pattern => /\A:::(\w+)\s*\n/, :logging => false
167
204
 
168
205
 
169
- ## Supported Highlighters
206
+ ## Supported highlighters
170
207
 
171
208
  These currently include: *Syntax* (fast), *Coderay* (very fast),
172
209
  *Ultraviolet* (slow, but highlights almost any language).
173
210
 
174
- The *Codehighlighter* gem provides a thin interface over a bunch of
175
- exisitng code highlighters to make their usage as generic possible.
176
-
177
- What does it mean? Explain.
178
-
179
211
 
180
212
  ### [Syntax](http://syntax.rubyforge.org/)
181
213
 
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 0
2
+ :patch: 1
3
3
  :major: 0
4
4
  :minor: 2
@@ -5,14 +5,14 @@ use Rack::Lint
5
5
 
6
6
  # use default options
7
7
 
8
- #use Rack::Codehighlighter, :prettify, :element => "//pre", :pattern => /\A:::(\w+)\s*\n/, :logging => true
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
- #use Rack::Codehighlighter, :syntax, :element => "//pre", :pattern => /\A:::(\w+)\s*\n/, :logging => true
12
+ #use Rack::Codehighlighter, :syntax, :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => true
13
13
 
14
14
  #use Rack::Codehighlighter, :ultraviolet, :theme => 'dawn',
15
- # :element => "//pre", :pattern => /\A:::(\w+)\s*\n/, :logging => true
15
+ # :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => true
16
16
 
17
17
  #use Rack::Codehighlighter, :censor, :reason => '[[-- ugly code removed --]]'
18
18
 
@@ -14,7 +14,7 @@ module Rack
14
14
  @app = app
15
15
  @highlighter = highlighter
16
16
  @opts = {
17
- :element => "//pre",
17
+ :element => "pre",
18
18
  :pattern => /\A:::(\w+)\s*\n/,
19
19
  :reason => "[[-- ugly code removed --]]"
20
20
  }
@@ -37,7 +37,7 @@ module Rack
37
37
  nodes = doc.search(@opts[:element])
38
38
  nodes.each do |node|
39
39
  s = node.inner_html || "[++where is the code?++]"
40
- if @opts[:remove_parent_element]
40
+ if @opts[:maruku]
41
41
  node.parent.swap(send(@highlighter, s))
42
42
  else
43
43
  node.swap(send(@highlighter, s))
@@ -91,7 +91,7 @@ module Rack
91
91
  if refs
92
92
  lang = refs[1]
93
93
  convertor = ::Syntax::Convertors::HTML.for_syntax translate[lang]
94
- convertor.convert(unescape_html(string.sub(/\A.*\n/, "")) || "[=this can'n happen=]")
94
+ convertor.convert(unescape_html(string.sub(@opts[:pattern], "")) || "[=this can'n happen=]")
95
95
  else
96
96
  "<pre>#{string}</pre>"
97
97
  end
@@ -100,6 +100,7 @@ module Rack
100
100
  def coderay(string)
101
101
  lang = 'unknown'
102
102
  refs = @opts[:pattern].match(string) # extract language name
103
+ # instead of pre the original/matched tag should be used
103
104
  if refs
104
105
  lang = refs[1]
105
106
  str = unescape_html(string.sub(@opts[:pattern], ""))
@@ -118,6 +119,7 @@ module Rack
118
119
  }
119
120
  lang = 'unknown'
120
121
  refs = @opts[:pattern].match(string) # extract language name
122
+ # instead of pre the original/matched tag should be used
121
123
  if refs
122
124
  lang = refs[1]
123
125
  str = string.sub(@opts[:pattern], "")
@@ -137,6 +139,7 @@ module Rack
137
139
  str = unescape_html(string.sub(@opts[:pattern], ""))
138
140
  "#{::Uv.parse(str, 'xhtml', lang, opts[:lines], opts[:theme])}"
139
141
  else
142
+ # instead of pre the original/matched tag should be used
140
143
  "<pre class='#{opts[:theme]}'>#{string}</pre>"
141
144
  end
142
145
  end
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.0
4
+ version: 0.2.1
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-06 00:00:00 -07:00
12
+ date: 2009-06-07 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency