hyperbolist-rack-codehighlighter 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.markdown +247 -0
- data/Rakefile +31 -0
- data/TODO +38 -0
- data/VERSION.yml +5 -0
- data/examples/app.rb +15 -0
- data/examples/check.rb +16 -0
- data/examples/config.ru +19 -0
- data/examples/public/javascripts/lang-css.js +2 -0
- data/examples/public/javascripts/lang-hs.js +2 -0
- data/examples/public/javascripts/lang-lisp.js +3 -0
- data/examples/public/javascripts/lang-lua.js +2 -0
- data/examples/public/javascripts/lang-ml.js +2 -0
- data/examples/public/javascripts/lang-proto.js +1 -0
- data/examples/public/javascripts/lang-sql.js +2 -0
- data/examples/public/javascripts/lang-vb.js +2 -0
- data/examples/public/javascripts/lang-wiki.js +1 -0
- data/examples/public/javascripts/prettify.js +31 -0
- data/examples/public/stylesheets/application.css +29 -0
- data/examples/public/stylesheets/censor.css +4 -0
- data/examples/public/stylesheets/coderay.css +126 -0
- data/examples/public/stylesheets/prettify.css +44 -0
- data/examples/public/stylesheets/syntax.css +79 -0
- data/examples/public/stylesheets/uv.css +1022 -0
- data/examples/public/stylesheets/uv/amy.css +147 -0
- data/examples/public/stylesheets/uv/blackboard.css +88 -0
- data/examples/public/stylesheets/uv/cobalt.css +149 -0
- data/examples/public/stylesheets/uv/dawn.css +121 -0
- data/examples/public/stylesheets/uv/espresso_libre.css +109 -0
- data/examples/public/stylesheets/uv/sunburst.css +180 -0
- data/examples/public/stylesheets/uv/twilight.css +137 -0
- data/examples/public/stylesheets/uv/zenburnesque.css +91 -0
- data/examples/views/index.erb +81 -0
- data/examples/views/layout.erb +38 -0
- data/lib/rack/codehighlighter.rb +152 -0
- metadata +121 -0
@@ -0,0 +1,81 @@
|
|
1
|
+
<h3>C</h3>
|
2
|
+
|
3
|
+
<pre>:::c
|
4
|
+
int a, b;
|
5
|
+
int main(int argc, char *argv[]) {
|
6
|
+
hello();
|
7
|
+
return 0;
|
8
|
+
}
|
9
|
+
void hello(void) {
|
10
|
+
printf("hello world\n");
|
11
|
+
}
|
12
|
+
</pre>
|
13
|
+
|
14
|
+
<h3>html</h3>
|
15
|
+
|
16
|
+
<pre>:::html
|
17
|
+
<html>
|
18
|
+
<head>
|
19
|
+
<title>hello world</title>
|
20
|
+
</head>
|
21
|
+
<body>
|
22
|
+
<h1>hello world</h1>
|
23
|
+
</body>
|
24
|
+
</html>
|
25
|
+
</pre>
|
26
|
+
|
27
|
+
<h3>ruby </h3>
|
28
|
+
|
29
|
+
<pre>:::ruby
|
30
|
+
def hello
|
31
|
+
puts 'hello world'
|
32
|
+
end
|
33
|
+
</pre>
|
34
|
+
|
35
|
+
<h3>css21</h3>
|
36
|
+
|
37
|
+
<pre>:::css
|
38
|
+
html {
|
39
|
+
margin: 0;
|
40
|
+
padding: 0;
|
41
|
+
background-color: #0C7D85;
|
42
|
+
line-height: 1.6;
|
43
|
+
}
|
44
|
+
body {
|
45
|
+
margin: 1em auto 1em auto;
|
46
|
+
padding: 1em 2em 2em 1em;
|
47
|
+
width: 760px;
|
48
|
+
border: 1px solid black;
|
49
|
+
background-color: #E8DDCB;
|
50
|
+
}
|
51
|
+
</pre>
|
52
|
+
|
53
|
+
<h3>sqlite</h3>
|
54
|
+
|
55
|
+
<pre>:::sql
|
56
|
+
drop table if exists exams;
|
57
|
+
create table exams (
|
58
|
+
id integer primary key autoincrement,
|
59
|
+
student_id integer not null, -- foreign key
|
60
|
+
result integer default 0 collate nocase,
|
61
|
+
when datetime
|
62
|
+
);
|
63
|
+
</pre>
|
64
|
+
|
65
|
+
<h3>javascript</h3>
|
66
|
+
|
67
|
+
<pre>:::javascript
|
68
|
+
for (var i = 0, length = properties.length; i < length; i++) {
|
69
|
+
var property = properties[i], value = source[property];
|
70
|
+
if (ancestor && Object.isFunction(value) &&
|
71
|
+
value.argumentNames().first() == "$super") {
|
72
|
+
var method = value, value = Object.extend((function(m) {
|
73
|
+
return function() { return ancestor[m].apply(this, arguments) };
|
74
|
+
})(property).wrap(method), {
|
75
|
+
valueOf: function() { return method },
|
76
|
+
toString: function() { return method.toString() }
|
77
|
+
});
|
78
|
+
}
|
79
|
+
this.prototype[property] = value;
|
80
|
+
}
|
81
|
+
</pre>
|
@@ -0,0 +1,38 @@
|
|
1
|
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" http://www.w3.org/TR/html4/strict.dtd">
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
5
|
+
<link rel="stylesheet" href="/stylesheets/application.css"
|
6
|
+
type="text/css" media="screen" charset="utf-8">
|
7
|
+
<link rel="stylesheet" href="/stylesheets/syntax.css"
|
8
|
+
type="text/css" media="screen" charset="utf-8">
|
9
|
+
<link rel="stylesheet" href="/stylesheets/coderay.css"
|
10
|
+
type="text/css" media="screen" charset="utf-8">
|
11
|
+
<link rel="stylesheet" href="/stylesheets/uv.css"
|
12
|
+
type="text/css" media="screen" charset="utf-8">
|
13
|
+
<link rel="stylesheet" href="/stylesheets/censor.css"
|
14
|
+
type="text/css" media="screen" charset="utf-8"/>
|
15
|
+
|
16
|
+
<link rel="stylesheet" href="/stylesheets/prettify.css"
|
17
|
+
type="text/css" media="screen" charset="utf-8"/>
|
18
|
+
|
19
|
+
<script src="/javascripts/prettify.js"
|
20
|
+
type="text/javascript" charset="utf-8"></script>
|
21
|
+
|
22
|
+
<script src="http://www.google.com/jsapi"></script>
|
23
|
+
<script>
|
24
|
+
// on page load complete, fire off a prettyPrint
|
25
|
+
google.setOnLoadCallback(function() {
|
26
|
+
prettyPrint();
|
27
|
+
});
|
28
|
+
</script>
|
29
|
+
|
30
|
+
<title><%= "Code Highlighting Middleware" %></title>
|
31
|
+
</head>
|
32
|
+
|
33
|
+
<body>
|
34
|
+
|
35
|
+
<%= yield %>
|
36
|
+
|
37
|
+
</body>
|
38
|
+
</html>
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'rack/utils'
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
class Codehighlighter
|
6
|
+
include Rack::Utils
|
7
|
+
|
8
|
+
# for logging use
|
9
|
+
FORMAT = %{%s - [%s] [%s] "%s %s%s %s" (%s) %d %d %0.4f\n}
|
10
|
+
|
11
|
+
def initialize(app, highlighter = :censor, opts = {})
|
12
|
+
@app = app
|
13
|
+
@highlighter = highlighter
|
14
|
+
@opts = {
|
15
|
+
:element => "pre",
|
16
|
+
:pattern => /\A:::(\w+)\s*(\n|
)/i, # 
 == line feed
|
17
|
+
:reason => "[[-- ugly code removed --]]", #8-)
|
18
|
+
:markdown => false
|
19
|
+
}
|
20
|
+
@opts.merge! opts
|
21
|
+
end
|
22
|
+
|
23
|
+
def call(env)
|
24
|
+
began_at = Time.now
|
25
|
+
status, headers, response = @app.call(env)
|
26
|
+
headers = HeaderHash.new(headers)
|
27
|
+
|
28
|
+
if !STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
|
29
|
+
!headers['transfer-encoding'] &&
|
30
|
+
headers['content-type'] &&
|
31
|
+
headers['content-type'].include?("text/html")
|
32
|
+
|
33
|
+
content = ""
|
34
|
+
response.each { |part| content += part }
|
35
|
+
doc = Nokogiri::HTML(content)
|
36
|
+
nodes = doc.search(@opts[:element])
|
37
|
+
nodes.each do |node|
|
38
|
+
s = node.inner_html || "[++where is the code?++]"
|
39
|
+
if @opts[:markdown]
|
40
|
+
node.parent.swap(send(@highlighter, s))
|
41
|
+
else
|
42
|
+
node.swap(send(@highlighter, s))
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
body = doc.to_html
|
47
|
+
headers['content-length'] = bytesize(body).to_s
|
48
|
+
|
49
|
+
log(env, status, headers, began_at) if @opts[:logging]
|
50
|
+
[status, headers, [body]]
|
51
|
+
else
|
52
|
+
[status, headers, response]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def log(env, status, headers, began_at)
|
59
|
+
# 127.0.0.1 - [ultraviolet] [10/Oct/2009 12:12:12] "GET /pastie HTTP/1.1" (text/html) 200 512 1.23
|
60
|
+
now = Time.now
|
61
|
+
logger = env['rack.errors']
|
62
|
+
logger.write FORMAT % [
|
63
|
+
env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
|
64
|
+
@highlighter,
|
65
|
+
now.strftime("%d/%b/%Y %H:%M:%S"),
|
66
|
+
env["REQUEST_METHOD"],
|
67
|
+
env["PATH_INFO"],
|
68
|
+
env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
|
69
|
+
env["HTTP_VERSION"],
|
70
|
+
headers["content-type"] || "unknown",
|
71
|
+
status.to_s[0..3],
|
72
|
+
headers['content-length'],
|
73
|
+
now - began_at
|
74
|
+
]
|
75
|
+
end
|
76
|
+
|
77
|
+
# simplifies testing
|
78
|
+
def censor(string)
|
79
|
+
"<pre class='censor'>#{@opts[:reason]}</pre>"
|
80
|
+
end
|
81
|
+
|
82
|
+
def syntax(string)
|
83
|
+
# allow use html instead of xml
|
84
|
+
translate = {
|
85
|
+
'html' => 'xml',
|
86
|
+
'c' => 'ansic',
|
87
|
+
'css' => 'css21',
|
88
|
+
'sql' => 'sqlite'
|
89
|
+
}
|
90
|
+
lang = 'unknown'
|
91
|
+
refs = @opts[:pattern].match(string) # extract language name
|
92
|
+
if refs
|
93
|
+
lang = refs[1]
|
94
|
+
convertor = ::Syntax::Convertors::HTML.for_syntax translate[lang]
|
95
|
+
convertor.convert(unescape_html(string.sub(@opts[:pattern], "")) || "[=this can'n happen=]")
|
96
|
+
else
|
97
|
+
"<pre>#{string}</pre>"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def coderay(string)
|
102
|
+
lang = 'unknown'
|
103
|
+
refs = @opts[:pattern].match(string) # extract language name
|
104
|
+
if refs
|
105
|
+
lang = refs[1]
|
106
|
+
str = unescape_html(string.sub(@opts[:pattern], ""))
|
107
|
+
"<pre class='CodeRay'>#{::CodeRay.encoder(:html).encode str, lang}</pre>"
|
108
|
+
else
|
109
|
+
"<pre class='CodeRay'>#{string}</pre>"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# experimental Javascript highlighter
|
114
|
+
def prettify(string)
|
115
|
+
# prettify uses short names; I want to use full names
|
116
|
+
translate = {
|
117
|
+
'ruby' => 'rb',
|
118
|
+
'bash' => 'bsh',
|
119
|
+
'javascript' => 'js',
|
120
|
+
'python' => 'py'
|
121
|
+
}
|
122
|
+
lang = 'unknown'
|
123
|
+
refs = @opts[:pattern].match(string) # extract language name
|
124
|
+
if refs
|
125
|
+
lang = refs[1]
|
126
|
+
str = string.sub(@opts[:pattern], "")
|
127
|
+
"<pre class='prettyprint lang-#{translate[lang] || lang}'>#{str}</pre>"
|
128
|
+
else
|
129
|
+
"<pre>#{string}</pre>"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def ultraviolet(string)
|
134
|
+
opts = { :theme => 'dawn', :lines => false }
|
135
|
+
opts.merge! @opts
|
136
|
+
lang = 'text'
|
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
|
+
"#{::Uv.parse(str, 'xhtml', lang, opts[:lines], opts[:theme])}"
|
142
|
+
else
|
143
|
+
"<pre class='#{opts[:theme]}'>#{string}</pre>"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def unescape_html(string)
|
148
|
+
string.to_s.gsub(/
/i, "\n").gsub("<", '<').gsub(">", '>').gsub("&", '&')
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
152
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hyperbolist-rack-codehighlighter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eric Sherman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-10 00:00:00 -05:00
|
13
|
+
default_executable:
|
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:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: nokogiri
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.4.1
|
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:
|
45
|
+
description: Rack Middleware for Code Highlighting.
|
46
|
+
email: hyperbolist@gmail.com
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- LICENSE
|
53
|
+
- README.markdown
|
54
|
+
- TODO
|
55
|
+
files:
|
56
|
+
- LICENSE
|
57
|
+
- Rakefile
|
58
|
+
- TODO
|
59
|
+
- VERSION.yml
|
60
|
+
- examples/app.rb
|
61
|
+
- examples/check.rb
|
62
|
+
- examples/config.ru
|
63
|
+
- examples/public/javascripts/lang-css.js
|
64
|
+
- examples/public/javascripts/lang-hs.js
|
65
|
+
- examples/public/javascripts/lang-lisp.js
|
66
|
+
- examples/public/javascripts/lang-lua.js
|
67
|
+
- examples/public/javascripts/lang-ml.js
|
68
|
+
- examples/public/javascripts/lang-proto.js
|
69
|
+
- examples/public/javascripts/lang-sql.js
|
70
|
+
- examples/public/javascripts/lang-vb.js
|
71
|
+
- examples/public/javascripts/lang-wiki.js
|
72
|
+
- examples/public/javascripts/prettify.js
|
73
|
+
- examples/public/stylesheets/application.css
|
74
|
+
- examples/public/stylesheets/censor.css
|
75
|
+
- examples/public/stylesheets/coderay.css
|
76
|
+
- examples/public/stylesheets/prettify.css
|
77
|
+
- examples/public/stylesheets/syntax.css
|
78
|
+
- examples/public/stylesheets/uv.css
|
79
|
+
- examples/public/stylesheets/uv/amy.css
|
80
|
+
- examples/public/stylesheets/uv/blackboard.css
|
81
|
+
- examples/public/stylesheets/uv/cobalt.css
|
82
|
+
- examples/public/stylesheets/uv/dawn.css
|
83
|
+
- examples/public/stylesheets/uv/espresso_libre.css
|
84
|
+
- examples/public/stylesheets/uv/sunburst.css
|
85
|
+
- examples/public/stylesheets/uv/twilight.css
|
86
|
+
- examples/public/stylesheets/uv/zenburnesque.css
|
87
|
+
- examples/views/index.erb
|
88
|
+
- examples/views/layout.erb
|
89
|
+
- lib/rack/codehighlighter.rb
|
90
|
+
- README.markdown
|
91
|
+
has_rdoc: true
|
92
|
+
homepage: http://github.com/hyperbolist/rack-codehighlighter
|
93
|
+
licenses: []
|
94
|
+
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options:
|
97
|
+
- --charset=UTF-8
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: "0"
|
105
|
+
version:
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: "0"
|
111
|
+
version:
|
112
|
+
requirements: []
|
113
|
+
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 1.3.5
|
116
|
+
signing_key:
|
117
|
+
specification_version: 3
|
118
|
+
summary: Rack Middleware for Code Highlighting.
|
119
|
+
test_files:
|
120
|
+
- examples/app.rb
|
121
|
+
- examples/check.rb
|