wbzyl-rack-codehighlighter 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rack/codehighlighter.rb +1 -0
- metadata +1 -2
- data/lib/codehighlighter-middleware.rb +0 -138
data/lib/rack/codehighlighter.rb
CHANGED
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wlodek Bzyl
|
@@ -79,7 +79,6 @@ files:
|
|
79
79
|
- examples/public/stylesheets/uv/zenburnesque.css
|
80
80
|
- examples/views/index.rdiscount
|
81
81
|
- examples/views/layout.rdiscount
|
82
|
-
- lib/codehighlighter-middleware.rb
|
83
82
|
- lib/rack/codehighlighter.rb
|
84
83
|
- LICENSE
|
85
84
|
- README.markdown
|
@@ -1,138 +0,0 @@
|
|
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
|