wbzyl-codehighlighter-middleware 0.0.7 → 0.1.0
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.
- data/README.markdown +14 -3
- data/VERSION.yml +2 -2
- data/codehighlighter-middleware.gemspec +2 -2
- data/examples/app.rb +9 -3
- data/examples/config.ru +6 -7
- data/lib/codehighlighter-middleware.rb +64 -25
- metadata +2 -2
data/README.markdown
CHANGED
@@ -124,11 +124,22 @@ Supported languages:
|
|
124
124
|
|
125
125
|
### Ultraviolet
|
126
126
|
|
127
|
-
|
128
|
-
|
127
|
+
The *ultraviolet* gem needs oniguruma regexp library.
|
128
|
+
|
129
|
+
On Fedora install the library with:
|
130
|
+
|
131
|
+
sudo yum install oniguruma
|
132
|
+
|
133
|
+
For installation instruction from sources, see
|
129
134
|
[Carbonica](http://carboni.ca/projects/harsh/)
|
130
135
|
|
131
|
-
|
136
|
+
Now, install the gem:
|
137
|
+
|
138
|
+
sudo gem install ultraviolet
|
139
|
+
|
140
|
+
[Ultraviolet themes gallery](: http://ultraviolet.rubyforge.org/themes.xhtml)
|
141
|
+
|
142
|
+
Ultraviolet supports almost any language:
|
132
143
|
|
133
144
|
* actionscript
|
134
145
|
* active4d
|
data/VERSION.yml
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{codehighlighter-middleware}
|
5
|
-
s.version = "0.0
|
5
|
+
s.version = "0.1.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Wlodek Bzyl"]
|
9
|
-
s.date = %q{2009-05-
|
9
|
+
s.date = %q{2009-05-28}
|
10
10
|
s.description = %q{Rack Middleware for Code Highlighting.}
|
11
11
|
s.email = %q{matwb@univ.gda.pl}
|
12
12
|
s.extra_rdoc_files = [
|
data/examples/app.rb
CHANGED
@@ -1,14 +1,20 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
|
3
|
+
path = File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
|
4
|
+
$:.unshift(path) unless $:.include?(path)
|
5
|
+
|
3
6
|
require 'rubygems'
|
4
7
|
require 'sinatra'
|
5
8
|
require 'rdiscount'
|
9
|
+
|
10
|
+
gem 'wbzyl-sinatra-rdiscount'
|
6
11
|
require 'sinatra/rdiscount'
|
12
|
+
|
7
13
|
require 'codehighlighter-middleware'
|
8
14
|
|
9
|
-
require 'coderay'
|
10
|
-
|
11
|
-
|
15
|
+
require 'coderay' # Coderay
|
16
|
+
require 'syntax/convertors/html' # Syntax
|
17
|
+
require 'uv' # Ultraviolet
|
12
18
|
|
13
19
|
get "/" do
|
14
20
|
rdiscount :index
|
data/examples/config.ru
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
# run with: thin --rackup config.ru -p 4567 start
|
2
2
|
|
3
|
+
#use Rack::Static, :urls => ["/stylesheets"], :root => "public"
|
4
|
+
|
3
5
|
require 'app'
|
4
6
|
|
5
7
|
use Rack::Lint
|
6
8
|
|
7
|
-
#use Rack::Codehighlighter, :coderay
|
8
|
-
use Rack::Codehighlighter, :
|
9
|
-
|
10
|
-
#use Rack::Codehighlighter, :
|
11
|
-
|
12
|
-
use Rack::Static, :urls => ["/stylesheets"], :root => "public"
|
13
|
-
|
9
|
+
#use Rack::Codehighlighter, :coderay, :logging => true
|
10
|
+
#use Rack::Codehighlighter, :syntax, :logging => true
|
11
|
+
use Rack::Codehighlighter, :ultraviolet, :theme => 'dawn', :logging => true
|
12
|
+
#use Rack::Codehighlighter, :prettify, :logging => true
|
14
13
|
run Sinatra::Application
|
@@ -1,30 +1,46 @@
|
|
1
|
-
|
1
|
+
require 'rack/utils'
|
2
2
|
|
3
|
-
|
3
|
+
gem 'hpricot', '>=0.8.1'
|
4
4
|
require 'hpricot'
|
5
5
|
|
6
6
|
module Rack
|
7
7
|
class Codehighlighter
|
8
|
-
|
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 = {})
|
9
13
|
@app = app
|
10
14
|
@highlighter = highlighter
|
11
|
-
|
15
|
+
|
16
|
+
@opts = { :element => "//pre/code", :pattern => /\A:::(\w+)\s*\n/ }
|
17
|
+
|
18
|
+
@opts.merge! opts
|
12
19
|
end
|
20
|
+
|
13
21
|
def call(env)
|
22
|
+
began_at = Time.now
|
14
23
|
status, headers, response = @app.call(env)
|
15
|
-
|
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
|
+
|
16
31
|
content = ""
|
17
32
|
response.each { |part| content += part }
|
18
33
|
doc = Hpricot(content)
|
19
|
-
nodes = doc.search(
|
34
|
+
nodes = doc.search(@opts[:element])
|
20
35
|
nodes.each do |node|
|
21
36
|
s = node.inner_html || "[++where is the code?++]"
|
22
37
|
node.parent.swap(send(@highlighter, s))
|
23
38
|
end
|
24
|
-
|
39
|
+
|
25
40
|
body = doc.to_html
|
26
|
-
|
27
|
-
|
41
|
+
headers['content-length'] = body.bytesize.to_s
|
42
|
+
|
43
|
+
log(env, status, headers, began_at) if @opts[:logging]
|
28
44
|
[status, headers, [body]]
|
29
45
|
else
|
30
46
|
[status, headers, response]
|
@@ -32,6 +48,25 @@ module Rack
|
|
32
48
|
end
|
33
49
|
|
34
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
|
35
70
|
|
36
71
|
def syntax(string)
|
37
72
|
translate = {
|
@@ -40,9 +75,10 @@ module Rack
|
|
40
75
|
'css' => 'css21',
|
41
76
|
'sql' => 'sqlite'
|
42
77
|
}
|
43
|
-
lang =
|
44
|
-
|
45
|
-
|
78
|
+
lang = 'unknown'
|
79
|
+
refs = @opts[:pattern].match(string) # extract language name
|
80
|
+
if refs
|
81
|
+
lang = refs[1]
|
46
82
|
convertor = ::Syntax::Convertors::HTML.for_syntax translate[lang]
|
47
83
|
convertor.convert(unescape_html(string.sub(/\A.*\n/, "")) || "[=this can'n happen=]")
|
48
84
|
else
|
@@ -51,10 +87,11 @@ module Rack
|
|
51
87
|
end
|
52
88
|
|
53
89
|
def coderay(string)
|
54
|
-
lang =
|
55
|
-
|
56
|
-
|
57
|
-
|
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], ""))
|
58
95
|
"<pre class='CodeRay'>#{::CodeRay.encoder(:html).encode str, lang}</pre>"
|
59
96
|
else
|
60
97
|
"<pre class='CodeRay'>#{string}</pre>"
|
@@ -68,10 +105,11 @@ module Rack
|
|
68
105
|
'javascript' => 'js',
|
69
106
|
'python' => 'py'
|
70
107
|
}
|
71
|
-
lang =
|
72
|
-
|
73
|
-
|
74
|
-
|
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], "")
|
75
113
|
"<pre class='prettyprint lang-#{translate[lang] || lang}'>#{str}</pre>"
|
76
114
|
else
|
77
115
|
"<pre>#{string}</pre>"
|
@@ -79,12 +117,13 @@ module Rack
|
|
79
117
|
end
|
80
118
|
|
81
119
|
def ultraviolet(string)
|
82
|
-
opts = { :theme => '
|
120
|
+
opts = { :theme => 'dawn', :lines => false }
|
83
121
|
opts.merge! @opts
|
84
122
|
lang = 'text'
|
85
|
-
|
86
|
-
|
87
|
-
|
123
|
+
refs = @opts[:pattern].match(string) # extract language name
|
124
|
+
if refs
|
125
|
+
lang = refs[1]
|
126
|
+
str = unescape_html(string.sub(@opts[:pattern], ""))
|
88
127
|
"<pre class='#{opts[:theme]}'>#{::Uv.parse(str, 'xhtml', lang, opts[:lines], opts[:theme])}</pre>"
|
89
128
|
else
|
90
129
|
"<pre class='#{opts[:theme]}'>#{string}</pre>"
|
@@ -92,7 +131,7 @@ module Rack
|
|
92
131
|
end
|
93
132
|
|
94
133
|
def unescape_html(string)
|
95
|
-
string.gsub("<", '<').gsub(">", '>').gsub("&", '&')
|
134
|
+
string.to_s.gsub("<", '<').gsub(">", '>').gsub("&", '&')
|
96
135
|
end
|
97
136
|
|
98
137
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wbzyl-codehighlighter-middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
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-05-
|
12
|
+
date: 2009-05-28 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|