rack-codehighlighter 0.4.1 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -12,6 +12,10 @@ It supports the most popular Ruby code highlighters of today:
12
12
  * coderay
13
13
  * syntax
14
14
 
15
+ As well as
16
+
17
+ * [Unofficial Pygments API](http://pygments.appspot.com/)
18
+
15
19
  To ease testing it implements *censor* highlighter.
16
20
 
17
21
 
@@ -99,29 +103,48 @@ In examples displayed below, the default value of each option is used.
99
103
  Coderay:
100
104
 
101
105
  use Rack::Codehighlighter, :coderay,
102
- :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => false
106
+ :element => "pre", :pattern => /\A:::(\w+)\s*(\n|
)/i, :logging => false
103
107
 
104
108
  Ultraviolet:
105
109
 
106
110
  use Rack::Codehighlighter, :ultraviolet, :theme => "dawn", :lines => false,
107
- :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => false
111
+ :element => "pre", :pattern => /\A:::(\w+)\s*(\n|
)/i, :logging => false
112
+
113
+ or
114
+
115
+ use Rack::Codehighlighter, :ultraviolet, :markdown => true,
116
+ :theme => "minimal_theme", :lines => false, :element => "pre>code",
117
+ :pattern => /\A:::([-_+\w]+)\s*(\n|
)/, :logging => false,
118
+ :themes => {"vibrant_ink" => ["ruby"], "upstream_sunburst" => ["objective-c", "java"]}
119
+
120
+ Unofficial Pygments API:
121
+
122
+ use Rack::Codehighlighter, :pygments_api, :element => "pre",
123
+ :pattern => /\A:::([-_+\w]+)\s*(\n|
)/, :logging => false
108
124
 
109
125
  Syntax:
110
126
 
111
127
  use Rack::Codehighlighter, :syntax,
112
- :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => false
128
+ :element => "pre", :pattern => /\A:::(\w+)\s*(\n|
)/i, :logging => false
113
129
 
114
130
  Censor:
115
131
 
116
132
  use Rack::Codehighlighter, :censor, :reason => "[[-- ugly code removed --]]",
117
- :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :logging => false
133
+ :element => "pre", :pattern => /\A:::(\w+)\s*(\n|
)/i, :logging => false
118
134
 
119
135
 
120
136
  Markdown, Maruku and RDiscount processors, the code is wrapped with `pre>code`.
121
137
  To remove this extra one level of nesting the `:markdown` option should be used:
122
138
 
123
139
  use Rack::Codehighlighter, :coderay, :markdown => true,
124
- :element => "pre>code", :pattern => /\A:::(\w+)\s*\n/, :logging => false
140
+ :element => "pre>code", :pattern => /\A:::(\w+)\s*(\n|
)/i, :logging => false
141
+
142
+ **Remark:** Within `pre` tag, HAML replaces each new line characters
143
+ with the `
` entity (line feed) so that it can ensure that it
144
+ doesn't adversely affect indentation.
145
+ This change confuses both rack-codehighlighter and the highlighters
146
+ themselves (e.g. Ultraviolet and Coderay). So, to support `pre` tags
147
+ as rendered by HAML, `
` was added to the default pattern.
125
148
 
126
149
  Check the `examples` directory for working examples.
127
150
 
data/Rakefile CHANGED
@@ -13,19 +13,12 @@ begin
13
13
  gemspec.files = %w[LICENSE TODO Rakefile VERSION.yml] + FileList['lib/**/*.rb', "examples/**/*"]
14
14
 
15
15
  gemspec.add_runtime_dependency 'rack', '>=1.0.0'
16
- gemspec.add_runtime_dependency 'hpricot', '>=0.8.1'
16
+ gemspec.add_runtime_dependency 'nokogiri', '>=1.4.1'
17
17
 
18
18
  gemspec.add_development_dependency 'rack-test', '>=0.3.0'
19
19
  end
20
20
  Jeweler::GemcutterTasks.new
21
21
  rescue LoadError
22
- puts "Jeweler not available."
23
- puts "Install it with:"
24
- puts " sudo gem install technicalpickles-jeweler -s http://gems.github.com"
22
+ puts "Jeweler not available. Install it with:"
23
+ puts " sudo gem install jeweler"
25
24
  end
26
-
27
- #Rake::TestTask.new(:test) do |t|
28
- # t.libs << 'lib' << 'test'
29
- # t.pattern = 'test/**/*_test.rb'
30
- # t.verbose = false
31
- #end
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
+ :build:
3
+ :patch: 4
2
4
  :major: 0
3
5
  :minor: 4
4
- :patch: 1
5
- :build:
@@ -1,5 +1,5 @@
1
1
  require 'rack/utils'
2
- require 'hpricot'
2
+ require 'nokogiri'
3
3
 
4
4
  module Rack
5
5
  class Codehighlighter
@@ -32,7 +32,7 @@ module Rack
32
32
 
33
33
  content = ""
34
34
  response.each { |part| content += part }
35
- doc = Hpricot(content)
35
+ doc = Nokogiri::HTML(content)
36
36
  nodes = doc.search(@opts[:element])
37
37
  nodes.each do |node|
38
38
  s = node.inner_html || "[++where is the code?++]"
@@ -130,15 +130,33 @@ module Rack
130
130
  end
131
131
  end
132
132
 
133
+ def pygments_api(string)
134
+ require 'net/http'
135
+ require 'uri'
136
+ lang = 'unknown'
137
+ refs = @opts[:pattern].match(string) # extract language name
138
+ if refs
139
+ lang = refs[1]
140
+ str = unescape_html(string.sub(@opts[:pattern], ""))
141
+ req = Net::HTTP.post_form(URI.parse('http://pygments.appspot.com/'),
142
+ {'lang' => lang, 'code' => str})
143
+ "#{req.body}"
144
+ else
145
+ "<pre>#{string}</pre>"
146
+ end
147
+ end
148
+
133
149
  def ultraviolet(string)
134
- opts = { :theme => 'dawn', :lines => false }
150
+ opts = { :theme => 'dawn', :lines => false, :themes => {} }
135
151
  opts.merge! @opts
136
152
  lang = 'text'
137
153
  refs = @opts[:pattern].match(string) # extract language name
138
154
  if refs
139
155
  lang = refs[1]
156
+ theme = opts[:themes].collect do |k,v|
157
+ k if v.include? lang end.compact.first || opts[:theme]
140
158
  str = unescape_html(string.sub(@opts[:pattern], ""))
141
- "#{::Uv.parse(str, 'xhtml', lang, opts[:lines], opts[:theme])}"
159
+ "#{::Uv.parse(str, 'xhtml', lang, opts[:lines], theme)}"
142
160
  else
143
161
  "<pre class='#{opts[:theme]}'>#{string}</pre>"
144
162
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-codehighlighter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.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: 2010-01-05 00:00:00 +01:00
12
+ date: 2010-02-14 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -23,14 +23,14 @@ dependencies:
23
23
  version: 1.0.0
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
- name: hpricot
26
+ name: nokogiri
27
27
  type: :runtime
28
28
  version_requirement:
29
29
  version_requirements: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.8.1
33
+ version: 1.4.1
34
34
  version:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: rack-test