rack-pygments 0.1
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/LICENSE +22 -0
- data/README +89 -0
- data/examples/sinatra/app.rb +51 -0
- data/examples/sinatra/config.ru +8 -0
- data/examples/sinatra/public/style.css +62 -0
- data/lib/rack/pygments.rb +36 -0
- metadata +79 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009, Bryan Goines
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
Rack::Pygments
|
2
|
+
--------------
|
3
|
+
|
4
|
+
A rack middleware for Pygments, the syntax highlighter
|
5
|
+
|
6
|
+
It will work with any rack based app and Rails app.
|
7
|
+
|
8
|
+
|
9
|
+
Requirements:
|
10
|
+
-------------
|
11
|
+
|
12
|
+
- Rack (http://rack.rubyforge.org)
|
13
|
+
- Pygments (http://pygments.org/)
|
14
|
+
- Nokogiri (http://nokogiri.org/)
|
15
|
+
|
16
|
+
|
17
|
+
Install Nokogiri:
|
18
|
+
-----------------
|
19
|
+
|
20
|
+
sudo gem install nokogiri -s http://gemcutter.org/
|
21
|
+
|
22
|
+
Install Pygments:
|
23
|
+
-----------------
|
24
|
+
|
25
|
+
easy_install pygments
|
26
|
+
|
27
|
+
|
28
|
+
Install Rack::Pygments
|
29
|
+
----------------------
|
30
|
+
|
31
|
+
sudo gem install rack-pygments -s http://gemcutter.org/
|
32
|
+
|
33
|
+
|
34
|
+
Options (OPTIONAL):
|
35
|
+
--------
|
36
|
+
|
37
|
+
:html_tag - set html tag (default: highlight)
|
38
|
+
|
39
|
+
Example #1: :html_tag => "hl:code" #=> <hl:code lang="bash"></hl:code>
|
40
|
+
Example #2: :html_tag => "colorful" #=> <colorful lang="bash"></colorful>
|
41
|
+
|
42
|
+
:html_attr - set html tag's attribute (default: lang)
|
43
|
+
|
44
|
+
Example #1: :html_attr => "style" #=> <highlight style="bash"></highlight>
|
45
|
+
Example #2: :html_attr => "lexer" #=> <highlight lexer="python"></highlight>
|
46
|
+
|
47
|
+
|
48
|
+
Example:
|
49
|
+
---------
|
50
|
+
|
51
|
+
config.ru:
|
52
|
+
|
53
|
+
|
54
|
+
require "app"
|
55
|
+
require "rack/pygments"
|
56
|
+
|
57
|
+
use Rack::Pygments, :html_tag => "highlight",
|
58
|
+
:html_attr => "lang"
|
59
|
+
|
60
|
+
run Sinatra::Application
|
61
|
+
|
62
|
+
|
63
|
+
app.rb:
|
64
|
+
|
65
|
+
|
66
|
+
require "sinatra"
|
67
|
+
|
68
|
+
get "/" do
|
69
|
+
erb :colorme
|
70
|
+
end
|
71
|
+
|
72
|
+
__END__
|
73
|
+
|
74
|
+
@@colorme
|
75
|
+
|
76
|
+
<highlight lang="ruby">
|
77
|
+
|
78
|
+
def hello(name)
|
79
|
+
puts "Hello #{name.capitalize}!"
|
80
|
+
end
|
81
|
+
|
82
|
+
</highlight>
|
83
|
+
|
84
|
+
<highlight lang="python">
|
85
|
+
|
86
|
+
def hello(name):
|
87
|
+
print "Hello %s!" % name
|
88
|
+
|
89
|
+
</highlight>
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "sinatra"
|
2
|
+
|
3
|
+
set :public, File.dirname(__FILE__) + "/public"
|
4
|
+
set :static, true
|
5
|
+
|
6
|
+
get "/" do
|
7
|
+
erb :pygments
|
8
|
+
end
|
9
|
+
|
10
|
+
__END__
|
11
|
+
|
12
|
+
@@layout
|
13
|
+
<!DOCTYPE html>
|
14
|
+
<html>
|
15
|
+
<head>
|
16
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
17
|
+
|
18
|
+
<title>Rack::Pygments</title>
|
19
|
+
<link rel="stylesheet" href="/style.css" type="text/css" media="screen">
|
20
|
+
</head>
|
21
|
+
<body>
|
22
|
+
<%= yield %>
|
23
|
+
</body>
|
24
|
+
</html>
|
25
|
+
|
26
|
+
@@pygments
|
27
|
+
|
28
|
+
Bash
|
29
|
+
<highlight lang="bash">
|
30
|
+
|
31
|
+
sudo gem install rack-pygments -s http://gemcutter.org/
|
32
|
+
|
33
|
+
</highlight>
|
34
|
+
|
35
|
+
Ruby
|
36
|
+
<highlight lang="ruby">
|
37
|
+
|
38
|
+
def hello(arg)
|
39
|
+
puts "Hello #{arg.capitalize}!"
|
40
|
+
end
|
41
|
+
|
42
|
+
</highlight>
|
43
|
+
|
44
|
+
Python
|
45
|
+
|
46
|
+
<highlight lang="python">
|
47
|
+
|
48
|
+
def hello(arg):
|
49
|
+
print "Hello %s!" % arg
|
50
|
+
|
51
|
+
</highlight>
|
@@ -0,0 +1,62 @@
|
|
1
|
+
/* http://github.com/qrush/litanyagainstfear/blob/master/css/pygment_trac.css */
|
2
|
+
|
3
|
+
|
4
|
+
pre .c { color: #999988; font-style: italic } /* Comment */
|
5
|
+
pre .err { color: #a61717; background-color: #e3d2d2 } /* Error */
|
6
|
+
pre .k { font-weight: bold } /* Keyword */
|
7
|
+
pre .o { font-weight: bold } /* Operator */
|
8
|
+
pre .cm { color: #999988; font-style: italic } /* Comment.Multiline */
|
9
|
+
pre .cp { color: #999999; font-weight: bold } /* Comment.Preproc */
|
10
|
+
pre .c1 { color: #999988; font-style: italic } /* Comment.Single */
|
11
|
+
pre .cs { color: #999999; font-weight: bold; font-style: italic } /* Comment.Special */
|
12
|
+
pre .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
|
13
|
+
pre .gd .x { color: #000000; background-color: #ffaaaa } /* Generic.Deleted.Specific */
|
14
|
+
pre .ge { font-style: italic } /* Generic.Emph */
|
15
|
+
pre .gr { color: #aa0000 } /* Generic.Error */
|
16
|
+
pre .gh { color: #999999 } /* Generic.Heading */
|
17
|
+
pre .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
|
18
|
+
pre .gi .x { color: #000000; background-color: #aaffaa } /* Generic.Inserted.Specific */
|
19
|
+
pre .go { color: #888888 } /* Generic.Output */
|
20
|
+
pre .gp { color: #555555 } /* Generic.Prompt */
|
21
|
+
pre .gs { font-weight: bold } /* Generic.Strong */
|
22
|
+
pre .gu { color: #aaaaaa } /* Generic.Subheading */
|
23
|
+
pre .gt { color: #aa0000 } /* Generic.Traceback */
|
24
|
+
pre .kc { font-weight: bold } /* Keyword.Constant */
|
25
|
+
pre .kd { font-weight: bold } /* Keyword.Declaration */
|
26
|
+
pre .kp { font-weight: bold } /* Keyword.Pseudo */
|
27
|
+
pre .kr { font-weight: bold } /* Keyword.Reserved */
|
28
|
+
pre .kt { color: #445588; font-weight: bold } /* Keyword.Type */
|
29
|
+
pre .m { color: #009999 } /* Literal.Number */
|
30
|
+
pre .s { color: #d14 } /* Literal.String */
|
31
|
+
pre .na { color: #008080 } /* Name.Attribute */
|
32
|
+
pre .nb { color: #0086B3 } /* Name.Builtin */
|
33
|
+
pre .nc { color: #445588; font-weight: bold } /* Name.Class */
|
34
|
+
pre .no { color: #008080 } /* Name.Constant */
|
35
|
+
pre .ni { color: #800080 } /* Name.Entity */
|
36
|
+
pre .ne { color: #990000; font-weight: bold } /* Name.Exception */
|
37
|
+
pre .nf { color: #990000; font-weight: bold } /* Name.Function */
|
38
|
+
pre .nn { color: #555555 } /* Name.Namespace */
|
39
|
+
pre .nt { color: #000080 } /* Name.Tag */
|
40
|
+
pre .nv { color: #008080 } /* Name.Variable */
|
41
|
+
pre .ow { font-weight: bold } /* Operator.Word */
|
42
|
+
pre .w { color: #bbbbbb } /* Text.Whitespace */
|
43
|
+
pre .mf { color: #009999 } /* Literal.Number.Float */
|
44
|
+
pre .mh { color: #009999 } /* Literal.Number.Hex */
|
45
|
+
pre .mi { color: #009999 } /* Literal.Number.Integer */
|
46
|
+
pre .mo { color: #009999 } /* Literal.Number.Oct */
|
47
|
+
pre .sb { color: #d14 } /* Literal.String.Backtick */
|
48
|
+
pre .sc { color: #d14 } /* Literal.String.Char */
|
49
|
+
pre .sd { color: #d14 } /* Literal.String.Doc */
|
50
|
+
pre .s2 { color: #d14 } /* Literal.String.Double */
|
51
|
+
pre .se { color: #d14 } /* Literal.String.Escape */
|
52
|
+
pre .sh { color: #d14 } /* Literal.String.Heredoc */
|
53
|
+
pre .si { color: #d14 } /* Literal.String.Interpol */
|
54
|
+
pre .sx { color: #d14 } /* Literal.String.Other */
|
55
|
+
pre .sr { color: #009926 } /* Literal.String.Regex */
|
56
|
+
pre .s1 { color: #d14 } /* Literal.String.Single */
|
57
|
+
pre .ss { color: #990073 } /* Literal.String.Symbol */
|
58
|
+
pre .bp { color: #999999 } /* Name.Builtin.Pseudo */
|
59
|
+
pre .vc { color: #008080 } /* Name.Variable.Class */
|
60
|
+
pre .vg { color: #008080 } /* Name.Variable.Global */
|
61
|
+
pre .vi { color: #008080 } /* Name.Variable.Instance */
|
62
|
+
pre .il { color: #009999 } /* Literal.Number.Integer.Long */
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "nokogiri"
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class Pygments
|
5
|
+
|
6
|
+
def initialize(app, opts={})
|
7
|
+
@app = app
|
8
|
+
@tag = opts[:html_tag].nil? ? 'highlight' : opts[:html_tag]
|
9
|
+
@attr = opts[:html_attr].nil? ? 'lang' : opts[:html_attr]
|
10
|
+
if system("which pygmentize > /dev/null")
|
11
|
+
@bin = `which pygmentize`.chomp
|
12
|
+
else
|
13
|
+
raise Exception, "Pygmentize is missing"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
if env["PATH_INFO"] !~ /.*\.css$/
|
19
|
+
status, headers, response = @app.call(env)
|
20
|
+
content = response.instance_variable_get("@body").to_s
|
21
|
+
document = Nokogiri::HTML(content)
|
22
|
+
nodes = document.css(@tag)
|
23
|
+
nodes.each do |node|
|
24
|
+
lang = node.attribute(@attr).nil? ? 'bash' : node.attribute(@attr).value
|
25
|
+
pygmentize = `echo '#{node.content}' | #{@bin} -l #{lang} -f html`
|
26
|
+
node.replace(Nokogiri::HTML(pygmentize).css("div.highlight").first)
|
27
|
+
end
|
28
|
+
response = document.to_s
|
29
|
+
headers["Content-Length"] = response.length.to_s
|
30
|
+
[status,headers,[response]]
|
31
|
+
else
|
32
|
+
@app.call(env)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-pygments
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bryan Goines
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-12 00:00:00 -06: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: "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: "0"
|
34
|
+
version:
|
35
|
+
description: rack-pygments - Rack Middleware for Pygments, the syntax highlighter
|
36
|
+
email: bryan@ffiirree.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README
|
43
|
+
files:
|
44
|
+
- lib/rack/pygments.rb
|
45
|
+
- examples/sinatra/app.rb
|
46
|
+
- examples/sinatra/config.ru
|
47
|
+
- examples/sinatra/public/style.css
|
48
|
+
- LICENSE
|
49
|
+
- README
|
50
|
+
has_rdoc: false
|
51
|
+
homepage: http://github.com/bry4n/rack-pygments
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project: rack-pygments
|
74
|
+
rubygems_version: 1.3.5
|
75
|
+
signing_key:
|
76
|
+
specification_version: 1
|
77
|
+
summary: rack-pygments
|
78
|
+
test_files: []
|
79
|
+
|