wbzyl-rack-codehighlighter 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +7 -2
- data/lib/codehighlighter-middleware.rb +138 -0
- data/lib/rack/codehighlighter.rb +138 -0
- metadata +25 -7
- data/.gitignore +0 -4
- data/Rakefile +0 -39
- data/TODO +0 -22
- data/VERSION.yml +0 -4
data/README.markdown
CHANGED
@@ -5,7 +5,7 @@ of exisitng code highlighters to make their usage as generic possible.
|
|
5
5
|
|
6
6
|
To install it, run:
|
7
7
|
|
8
|
-
sudo gem install wbzyl-codehighlighter
|
8
|
+
sudo gem install wbzyl-rack-codehighlighter -s http://gems.github.com
|
9
9
|
|
10
10
|
Markup your code with:
|
11
11
|
|
@@ -19,10 +19,15 @@ Example (incomplete html, needs a layout file with link to css):
|
|
19
19
|
|
20
20
|
require 'rubygems'
|
21
21
|
require 'sinatra'
|
22
|
+
|
23
|
+
gem 'coderay'
|
22
24
|
require 'coderay' # use the Coderay highlighter
|
23
25
|
|
24
26
|
gem 'wbzyl-sinatra-rdiscount'
|
25
|
-
require '
|
27
|
+
require 'sinatra/rdiscount'
|
28
|
+
|
29
|
+
gem 'wbzyl-rack-codehighlighter'
|
30
|
+
require 'rack/codehighlighter'
|
26
31
|
|
27
32
|
use Rack::Codehighlighter, :coderay
|
28
33
|
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'rack/utils'
|
2
|
+
|
3
|
+
gem 'hpricot', '>=0.8.1'
|
4
|
+
require 'hpricot'
|
5
|
+
|
6
|
+
module Rack
|
7
|
+
class Codehighlighter
|
8
|
+
include Rack::Utils
|
9
|
+
|
10
|
+
FORMAT = %{%s - [%s] [%s] "%s %s%s %s" (%s) %d %d %0.4f\n}
|
11
|
+
|
12
|
+
def initialize(app, highlighter = :coderay, opts = {})
|
13
|
+
@app = app
|
14
|
+
@highlighter = highlighter
|
15
|
+
|
16
|
+
@opts = { :element => "//pre/code", :pattern => /\A:::(\w+)\s*\n/ }
|
17
|
+
|
18
|
+
@opts.merge! opts
|
19
|
+
end
|
20
|
+
|
21
|
+
def call(env)
|
22
|
+
began_at = Time.now
|
23
|
+
status, headers, response = @app.call(env)
|
24
|
+
headers = HeaderHash.new(headers)
|
25
|
+
|
26
|
+
if !STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
|
27
|
+
!headers['transfer-encoding'] &&
|
28
|
+
headers['content-type'] &&
|
29
|
+
headers['content-type'].include?("text/html")
|
30
|
+
|
31
|
+
content = ""
|
32
|
+
response.each { |part| content += part }
|
33
|
+
doc = Hpricot(content)
|
34
|
+
nodes = doc.search(@opts[:element])
|
35
|
+
nodes.each do |node|
|
36
|
+
s = node.inner_html || "[++where is the code?++]"
|
37
|
+
node.parent.swap(send(@highlighter, s))
|
38
|
+
end
|
39
|
+
|
40
|
+
body = doc.to_html
|
41
|
+
headers['content-length'] = body.bytesize.to_s
|
42
|
+
|
43
|
+
log(env, status, headers, began_at) if @opts[:logging]
|
44
|
+
[status, headers, [body]]
|
45
|
+
else
|
46
|
+
[status, headers, response]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def log(env, status, headers, began_at)
|
53
|
+
# lilith.local [coderay] text/html [26/may/2009 12:00:00] "GET / HTTP/1.1" 200 ? ?\n
|
54
|
+
now = Time.now
|
55
|
+
logger = env['rack.errors']
|
56
|
+
logger.write FORMAT % [
|
57
|
+
env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
|
58
|
+
@highlighter,
|
59
|
+
now.strftime("%d/%b/%Y %H:%M:%S"),
|
60
|
+
env["REQUEST_METHOD"],
|
61
|
+
env["PATH_INFO"],
|
62
|
+
env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
|
63
|
+
env["HTTP_VERSION"],
|
64
|
+
headers["content-type"] || "unknown",
|
65
|
+
status.to_s[0..3],
|
66
|
+
headers['content-length'],
|
67
|
+
now - began_at
|
68
|
+
]
|
69
|
+
end
|
70
|
+
|
71
|
+
def syntax(string)
|
72
|
+
translate = {
|
73
|
+
'html' => 'xml',
|
74
|
+
'c' => 'ansic',
|
75
|
+
'css' => 'css21',
|
76
|
+
'sql' => 'sqlite'
|
77
|
+
}
|
78
|
+
lang = 'unknown'
|
79
|
+
refs = @opts[:pattern].match(string) # extract language name
|
80
|
+
if refs
|
81
|
+
lang = refs[1]
|
82
|
+
convertor = ::Syntax::Convertors::HTML.for_syntax translate[lang]
|
83
|
+
convertor.convert(unescape_html(string.sub(/\A.*\n/, "")) || "[=this can'n happen=]")
|
84
|
+
else
|
85
|
+
"<pre>#{string}</pre>"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def coderay(string)
|
90
|
+
lang = 'unknown'
|
91
|
+
refs = @opts[:pattern].match(string) # extract language name
|
92
|
+
if refs
|
93
|
+
lang = refs[1]
|
94
|
+
str = unescape_html(string.sub(@opts[:pattern], ""))
|
95
|
+
"<pre class='CodeRay'>#{::CodeRay.encoder(:html).encode str, lang}</pre>"
|
96
|
+
else
|
97
|
+
"<pre class='CodeRay'>#{string}</pre>"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def prettify(string)
|
102
|
+
translate = {
|
103
|
+
'ruby' => 'rb',
|
104
|
+
'bash' => 'bsh',
|
105
|
+
'javascript' => 'js',
|
106
|
+
'python' => 'py'
|
107
|
+
}
|
108
|
+
lang = 'unknown'
|
109
|
+
refs = @opts[:pattern].match(string) # extract language name
|
110
|
+
if refs
|
111
|
+
lang = refs[1]
|
112
|
+
str = string.sub(@opts[:pattern], "")
|
113
|
+
"<pre class='prettyprint lang-#{translate[lang] || lang}'>#{str}</pre>"
|
114
|
+
else
|
115
|
+
"<pre>#{string}</pre>"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def ultraviolet(string)
|
120
|
+
opts = { :theme => 'dawn', :lines => false }
|
121
|
+
opts.merge! @opts
|
122
|
+
lang = 'text'
|
123
|
+
refs = @opts[:pattern].match(string) # extract language name
|
124
|
+
if refs
|
125
|
+
lang = refs[1]
|
126
|
+
str = unescape_html(string.sub(@opts[:pattern], ""))
|
127
|
+
"#{::Uv.parse(str, 'xhtml', lang, opts[:lines], opts[:theme])}"
|
128
|
+
else
|
129
|
+
"<pre class='#{opts[:theme]}'>#{string}</pre>"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def unescape_html(string)
|
134
|
+
string.to_s.gsub("<", '<').gsub(">", '>').gsub("&", '&')
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'rack/utils'
|
2
|
+
|
3
|
+
gem 'hpricot', '>=0.8.1'
|
4
|
+
require 'hpricot'
|
5
|
+
|
6
|
+
module Rack
|
7
|
+
class Codehighlighter
|
8
|
+
include Rack::Utils
|
9
|
+
|
10
|
+
FORMAT = %{%s - [%s] [%s] "%s %s%s %s" (%s) %d %d %0.4f\n}
|
11
|
+
|
12
|
+
def initialize(app, highlighter = :coderay, opts = {})
|
13
|
+
@app = app
|
14
|
+
@highlighter = highlighter
|
15
|
+
|
16
|
+
@opts = { :element => "//pre/code", :pattern => /\A:::(\w+)\s*\n/ }
|
17
|
+
|
18
|
+
@opts.merge! opts
|
19
|
+
end
|
20
|
+
|
21
|
+
def call(env)
|
22
|
+
began_at = Time.now
|
23
|
+
status, headers, response = @app.call(env)
|
24
|
+
headers = HeaderHash.new(headers)
|
25
|
+
|
26
|
+
if !STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
|
27
|
+
!headers['transfer-encoding'] &&
|
28
|
+
headers['content-type'] &&
|
29
|
+
headers['content-type'].include?("text/html")
|
30
|
+
|
31
|
+
content = ""
|
32
|
+
response.each { |part| content += part }
|
33
|
+
doc = Hpricot(content)
|
34
|
+
nodes = doc.search(@opts[:element])
|
35
|
+
nodes.each do |node|
|
36
|
+
s = node.inner_html || "[++where is the code?++]"
|
37
|
+
node.parent.swap(send(@highlighter, s))
|
38
|
+
end
|
39
|
+
|
40
|
+
body = doc.to_html
|
41
|
+
headers['content-length'] = body.bytesize.to_s
|
42
|
+
|
43
|
+
log(env, status, headers, began_at) if @opts[:logging]
|
44
|
+
[status, headers, [body]]
|
45
|
+
else
|
46
|
+
[status, headers, response]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def log(env, status, headers, began_at)
|
53
|
+
# lilith.local [coderay] text/html [26/may/2009 12:00:00] "GET / HTTP/1.1" 200 ? ?\n
|
54
|
+
now = Time.now
|
55
|
+
logger = env['rack.errors']
|
56
|
+
logger.write FORMAT % [
|
57
|
+
env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
|
58
|
+
@highlighter,
|
59
|
+
now.strftime("%d/%b/%Y %H:%M:%S"),
|
60
|
+
env["REQUEST_METHOD"],
|
61
|
+
env["PATH_INFO"],
|
62
|
+
env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
|
63
|
+
env["HTTP_VERSION"],
|
64
|
+
headers["content-type"] || "unknown",
|
65
|
+
status.to_s[0..3],
|
66
|
+
headers['content-length'],
|
67
|
+
now - began_at
|
68
|
+
]
|
69
|
+
end
|
70
|
+
|
71
|
+
def syntax(string)
|
72
|
+
translate = {
|
73
|
+
'html' => 'xml',
|
74
|
+
'c' => 'ansic',
|
75
|
+
'css' => 'css21',
|
76
|
+
'sql' => 'sqlite'
|
77
|
+
}
|
78
|
+
lang = 'unknown'
|
79
|
+
refs = @opts[:pattern].match(string) # extract language name
|
80
|
+
if refs
|
81
|
+
lang = refs[1]
|
82
|
+
convertor = ::Syntax::Convertors::HTML.for_syntax translate[lang]
|
83
|
+
convertor.convert(unescape_html(string.sub(/\A.*\n/, "")) || "[=this can'n happen=]")
|
84
|
+
else
|
85
|
+
"<pre>#{string}</pre>"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def coderay(string)
|
90
|
+
lang = 'unknown'
|
91
|
+
refs = @opts[:pattern].match(string) # extract language name
|
92
|
+
if refs
|
93
|
+
lang = refs[1]
|
94
|
+
str = unescape_html(string.sub(@opts[:pattern], ""))
|
95
|
+
"<pre class='CodeRay'>#{::CodeRay.encoder(:html).encode str, lang}</pre>"
|
96
|
+
else
|
97
|
+
"<pre class='CodeRay'>#{string}</pre>"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def prettify(string)
|
102
|
+
translate = {
|
103
|
+
'ruby' => 'rb',
|
104
|
+
'bash' => 'bsh',
|
105
|
+
'javascript' => 'js',
|
106
|
+
'python' => 'py'
|
107
|
+
}
|
108
|
+
lang = 'unknown'
|
109
|
+
refs = @opts[:pattern].match(string) # extract language name
|
110
|
+
if refs
|
111
|
+
lang = refs[1]
|
112
|
+
str = string.sub(@opts[:pattern], "")
|
113
|
+
"<pre class='prettyprint lang-#{translate[lang] || lang}'>#{str}</pre>"
|
114
|
+
else
|
115
|
+
"<pre>#{string}</pre>"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def ultraviolet(string)
|
120
|
+
opts = { :theme => 'dawn', :lines => false }
|
121
|
+
opts.merge! @opts
|
122
|
+
lang = 'text'
|
123
|
+
refs = @opts[:pattern].match(string) # extract language name
|
124
|
+
if refs
|
125
|
+
lang = refs[1]
|
126
|
+
str = unescape_html(string.sub(@opts[:pattern], ""))
|
127
|
+
"#{::Uv.parse(str, 'xhtml', lang, opts[:lines], opts[:theme])}"
|
128
|
+
else
|
129
|
+
"<pre class='#{opts[:theme]}'>#{string}</pre>"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def unescape_html(string)
|
134
|
+
string.to_s.gsub("<", '<').gsub(">", '>').gsub("&", '&')
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
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.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wlodek Bzyl
|
@@ -12,6 +12,16 @@ cert_chain: []
|
|
12
12
|
date: 2009-05-31 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rack
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.0
|
24
|
+
version:
|
15
25
|
- !ruby/object:Gem::Dependency
|
16
26
|
name: hpricot
|
17
27
|
type: :runtime
|
@@ -22,6 +32,16 @@ dependencies:
|
|
22
32
|
- !ruby/object:Gem::Version
|
23
33
|
version: 0.8.1
|
24
34
|
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rack-test
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.3.0
|
44
|
+
version:
|
25
45
|
description: Rack Middleware for Code Highlighting.
|
26
46
|
email: matwb@univ.gda.pl
|
27
47
|
executables: []
|
@@ -32,12 +52,6 @@ extra_rdoc_files:
|
|
32
52
|
- LICENSE
|
33
53
|
- README.markdown
|
34
54
|
files:
|
35
|
-
- .gitignore
|
36
|
-
- LICENSE
|
37
|
-
- README.markdown
|
38
|
-
- Rakefile
|
39
|
-
- TODO
|
40
|
-
- VERSION.yml
|
41
55
|
- examples/app.rb
|
42
56
|
- examples/config.ru
|
43
57
|
- examples/public/javascripts/lang-css.js
|
@@ -65,6 +79,10 @@ files:
|
|
65
79
|
- examples/public/stylesheets/uv/zenburnesque.css
|
66
80
|
- examples/views/index.rdiscount
|
67
81
|
- examples/views/layout.rdiscount
|
82
|
+
- lib/codehighlighter-middleware.rb
|
83
|
+
- lib/rack/codehighlighter.rb
|
84
|
+
- LICENSE
|
85
|
+
- README.markdown
|
68
86
|
has_rdoc: false
|
69
87
|
homepage: http://github.com/wbzyl/rack-codehighlighter
|
70
88
|
post_install_message:
|
data/.gitignore
DELETED
data/Rakefile
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
require "rubygems"
|
2
|
-
require "rake/gempackagetask"
|
3
|
-
require "rake/clean"
|
4
|
-
#require "spec/rake/spectask"
|
5
|
-
|
6
|
-
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '/lib')
|
7
|
-
|
8
|
-
begin
|
9
|
-
require 'jeweler'
|
10
|
-
Jeweler::Tasks.new do |s|
|
11
|
-
s.name = "rack-codehighlighter"
|
12
|
-
s.summary = "Rack Middleware for Code Highlighting."
|
13
|
-
s.email = "matwb@univ.gda.pl"
|
14
|
-
s.homepage = "http://github.com/wbzyl/rack-codehighlighter"
|
15
|
-
s.authors = ["Wlodek Bzyl"]
|
16
|
-
s.description = s.summary
|
17
|
-
|
18
|
-
# s.add_dependency 'coderay', '>=0.8.312'
|
19
|
-
s.add_dependency 'hpricot', '>=0.8.1'
|
20
|
-
end
|
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"
|
25
|
-
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
|
32
|
-
|
33
|
-
desc 'Install the package as a gem.'
|
34
|
-
task :install => [:clean, :build] do
|
35
|
-
gem = Dir['pkg/*.gem'].last
|
36
|
-
sh "sudo gem install --no-rdoc --no-ri --local #{gem}"
|
37
|
-
end
|
38
|
-
|
39
|
-
task :default => :test
|
data/TODO
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
|
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)
|
7
|
-
|
8
|
-
|
9
|
-
* see http://tomayko.com/writings/javascript-prettification
|
10
|
-
(support for jQuery, new pallette)
|
11
|
-
|
12
|
-
* example:
|
13
|
-
<pre class="prettyprint lang-html">
|
14
|
-
The lang-* class specifies the language file extensions.
|
15
|
-
File extensions supported by default include
|
16
|
-
"bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html",
|
17
|
-
"java", "js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh",
|
18
|
-
"xhtml", "xml", "xsl".
|
19
|
-
</pre>
|
20
|
-
|
21
|
-
[1.05.2009]
|
22
|
-
Add support for unobtrustive Code Highlighter By Dan Webb 11/2005
|
data/VERSION.yml
DELETED