highlight 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +9 -0
- data/MIT-LICENSE +20 -0
- data/README.md +132 -0
- data/Rakefile +30 -0
- data/generators/highlight_styles_generator/highlight_styles_generator.rb +32 -0
- data/generators/highlight_styles_generator/templates/NOTES +3 -0
- data/generators/highlight_styles_generator/templates/highlight.css +59 -0
- data/lib/simplabs/highlight.rb +187 -0
- data/lib/simplabs/highlight/pygments_wrapper.rb +49 -0
- data/lib/simplabs/highlight/railtie.rb +24 -0
- data/rails/init.rb +16 -0
- data/spec/boot.rb +14 -0
- data/spec/lib/highlight_spec.rb +90 -0
- data/spec/lib/pygments_wrapper_spec.rb +23 -0
- data/spec/lib/view_methods_spec.rb +53 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +9 -0
- metadata +96 -0
data/HISTORY.md
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008-2010 Marco Otte-Witte
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
Highlight
|
2
|
+
=========
|
3
|
+
|
4
|
+
Highlight is a simple syntax highlighting gem for Ruby and Rails. It's basically a
|
5
|
+
wrapper around the popular [http://pygments.org](http://pygments.org) highlighter that's
|
6
|
+
written in Python and supports an impressive number of languages.
|
7
|
+
|
8
|
+
If pygments is installed on the machine and in the `PATH`, that binary is used, otherwise
|
9
|
+
the plugin falls back to the web API at [http://pygments.appspot.com/](http://pygments.appspot.com/),
|
10
|
+
created by Trevor Turk.
|
11
|
+
|
12
|
+
See the API docs at [http://rdoc.info/projects/simplabs/highlight](http://rdoc.info/projects/simplabs/highlight).
|
13
|
+
|
14
|
+
Usage
|
15
|
+
-----
|
16
|
+
|
17
|
+
Highlight can either be used standalone via
|
18
|
+
|
19
|
+
require 'simplabs/highlight'
|
20
|
+
Simplabs::Highlight.highlight(:ruby, 'class Test; end')
|
21
|
+
|
22
|
+
or in Rails where it adds the `highlight_code` helper:
|
23
|
+
|
24
|
+
highlight_code(language, code = nil, &block)
|
25
|
+
|
26
|
+
`language` may be either a Symbol or a String (see supported languages
|
27
|
+
below). The code can be passed either as a string or inside a block, e.g.:
|
28
|
+
|
29
|
+
highlight_code(:ruby, 'class Test; end')
|
30
|
+
|
31
|
+
or
|
32
|
+
|
33
|
+
highlight_code(:ruby) do
|
34
|
+
klass = 'class'
|
35
|
+
name = 'Test'
|
36
|
+
_end = 'end'
|
37
|
+
"#{klass} #{name}; #{_end}"
|
38
|
+
end
|
39
|
+
|
40
|
+
Since highlighting the code takes a while, all highlighted source code
|
41
|
+
should be cached, e.g.:
|
42
|
+
|
43
|
+
<%- code = 'class Test; end' -%>
|
44
|
+
<%- cache Digest::SHA1.hexdigest(code) do -%>
|
45
|
+
<%= highlight_code(:ruby, code) -%>
|
46
|
+
<%- end -%>
|
47
|
+
|
48
|
+
|
49
|
+
Supported Languages
|
50
|
+
-------------------
|
51
|
+
|
52
|
+
The following languages are supported (there are probably more that are supported by `pygments`).
|
53
|
+
All of the paranthesized identifiers may be used as parameters for highlight to denote the
|
54
|
+
language the source code to highlight is written in (use either Symbols or Strings).
|
55
|
+
|
56
|
+
* Actionscript (`as`, `as3`, `actionscript`)
|
57
|
+
* Applescript (`applescript`)
|
58
|
+
* bash (`bash`, `sh`)
|
59
|
+
* C (`c`, `h`)
|
60
|
+
* Clojure (`clojure`)
|
61
|
+
* C++ (`c++`, `cpp`, `hpp`)
|
62
|
+
* C# (`c#`, `csharp`, `cs`)
|
63
|
+
* CSS (`css`)
|
64
|
+
* diff (`diff`)
|
65
|
+
* Dylan (`dylan`)
|
66
|
+
* Erlang (`erlang`, `erl`, `er`)
|
67
|
+
* HTML (`html`, `htm`)
|
68
|
+
* Java (`java`)
|
69
|
+
* JavaScript (`javascript`, `js`, `jscript`)
|
70
|
+
* JSP (`jsp`)
|
71
|
+
* Make (`make`, `basemake`, `makefile`)
|
72
|
+
* Objective-C (`objective-c`)
|
73
|
+
* OCaml (`ocaml`)
|
74
|
+
* Perl (`perl`, `pl`)
|
75
|
+
* PHP (`php`)
|
76
|
+
* Python (`python`, `py`)
|
77
|
+
* RHTML (`erb`, `rhtml`)
|
78
|
+
* Ruby (`ruby`, `rb`)
|
79
|
+
* Scala (`scala`)
|
80
|
+
* Scheme (`scheme`)
|
81
|
+
* Smalltalk (`smalltalk`)
|
82
|
+
* Smarty (`smarty`)
|
83
|
+
* SQL (`sql`)
|
84
|
+
* XML (`xml`, `xsd`)
|
85
|
+
* XSLT (`xslt`)
|
86
|
+
* YAML (`yaml`, `yml`)
|
87
|
+
|
88
|
+
|
89
|
+
Installation
|
90
|
+
------------
|
91
|
+
|
92
|
+
Installation is as easy as
|
93
|
+
|
94
|
+
gem install highlight
|
95
|
+
|
96
|
+
To use highlight in Rails apps, you have to define the dependency. For Rails 2.x this is done in the `environment.rb`:
|
97
|
+
|
98
|
+
config.gem 'highlight', :lib => 'simplabs/highlight'
|
99
|
+
|
100
|
+
while for Rails 3 the dependency is defined in the application's Gemfile:
|
101
|
+
|
102
|
+
gem 'highlight', :require => 'simplabs/highlight'
|
103
|
+
|
104
|
+
Highlight also comes with a default CSS file that defines styles for the highlighted code. This CSS file can be copied to
|
105
|
+
your application's `public/stylesheets` directory via
|
106
|
+
|
107
|
+
./script/generate highlight_styles
|
108
|
+
|
109
|
+
for Rails 2.x or via
|
110
|
+
|
111
|
+
rails generate highlight_styles
|
112
|
+
|
113
|
+
for Rails 3.
|
114
|
+
|
115
|
+
If you don't have python and pygments installed, you will need that too.
|
116
|
+
For instructions on installing pygments, refer to
|
117
|
+
[http://pygments.org/docs/installation/](http://pygments.org/docs/installation/).
|
118
|
+
|
119
|
+
|
120
|
+
Author
|
121
|
+
------
|
122
|
+
|
123
|
+
Copyright (c) 2008-2010 Marco Otte-Witte ([http://simplabs.com](http://simplabs.com)),
|
124
|
+
released under the MIT license
|
125
|
+
|
126
|
+
|
127
|
+
Acknowledgements
|
128
|
+
----------------
|
129
|
+
|
130
|
+
The actual highlighting is done by Pygments ([http://pygments.org](http://pygments.org)).
|
131
|
+
|
132
|
+
The pygments web API at [http://pygments.appspot.com/](http://pygments.appspot.com/) was created by Trevor Turk.
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
Bundler.setup
|
5
|
+
Bundler.require
|
6
|
+
|
7
|
+
require 'spec/rake/spectask'
|
8
|
+
require 'simplabs/excellent/rake'
|
9
|
+
|
10
|
+
desc 'Default: run specs.'
|
11
|
+
task :default => :spec
|
12
|
+
|
13
|
+
desc 'Run the specs'
|
14
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
15
|
+
t.rcov_opts << '--exclude "gems/*,spec/*,init.rb"'
|
16
|
+
t.rcov = true
|
17
|
+
t.rcov_dir = 'doc/coverage'
|
18
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Generate The Highlight documentation'
|
22
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
23
|
+
t.files = ['lib/**/*.rb']
|
24
|
+
t.options = ['--no-private', '--title', 'Highlight Documentation']
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Analyse the Highlight source with Excellent.'
|
28
|
+
Simplabs::Excellent::Rake::ExcellentTask.new(:excellent) do |t|
|
29
|
+
t.paths = ['lib']
|
30
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
if Rails::VERSION::MAJOR >= 3
|
2
|
+
|
3
|
+
class HighlightStylesGenerator < Rails::Generators::Base
|
4
|
+
|
5
|
+
include Rails::Generators::Actions
|
6
|
+
|
7
|
+
def create_stylesheet_file
|
8
|
+
empty_directory('public/stylesheets')
|
9
|
+
copy_file(
|
10
|
+
File.join(File.dirname(__FILE__), 'templates', 'highlight.css'),
|
11
|
+
'public/stylesheets/highlight.css'
|
12
|
+
)
|
13
|
+
readme(File.join(File.dirname(__FILE__), 'templates', 'NOTES'))
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
else
|
19
|
+
|
20
|
+
class HighlightStylesGenerator < Rails::Generator::Base
|
21
|
+
|
22
|
+
def manifest
|
23
|
+
record do |m|
|
24
|
+
m.directory('public/stylesheets')
|
25
|
+
m.file('highlight.css', 'public/stylesheets/highlight.css')
|
26
|
+
m.readme('NOTES')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
.c { color: #408080; font-style: italic } /* Comment */
|
2
|
+
.err { border: 1px solid #FF0000 } /* Error */
|
3
|
+
.k { color: #008000; font-weight: bold } /* Keyword */
|
4
|
+
.o { color: #666666 } /* Operator */
|
5
|
+
.cm { color: #408080; font-style: italic } /* Comment.Multiline */
|
6
|
+
.cp { color: #BC7A00 } /* Comment.Preproc */
|
7
|
+
.c1 { color: #408080; font-style: italic } /* Comment.Single */
|
8
|
+
.cs { color: #408080; font-style: italic } /* Comment.Special */
|
9
|
+
.gd { color: #A00000 } /* Generic.Deleted */
|
10
|
+
.ge { font-style: italic } /* Generic.Emph */
|
11
|
+
.gr { color: #FF0000 } /* Generic.Error */
|
12
|
+
.gh { color: #000080; font-weight: bold } /* Generic.Heading */
|
13
|
+
.gi { color: #00A000 } /* Generic.Inserted */
|
14
|
+
.go { color: #808080 } /* Generic.Output */
|
15
|
+
.gp { color: #000080; font-weight: bold } /* Generic.Prompt */
|
16
|
+
.gs { font-weight: bold } /* Generic.Strong */
|
17
|
+
.gu { color: #800080; font-weight: bold } /* Generic.Subheading */
|
18
|
+
.gt { color: #0040D0 } /* Generic.Traceback */
|
19
|
+
.kc { color: #008000; font-weight: bold } /* Keyword.Constant */
|
20
|
+
.kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
|
21
|
+
.kp { color: #008000 } /* Keyword.Pseudo */
|
22
|
+
.kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
|
23
|
+
.kt { color: #B00040 } /* Keyword.Type */
|
24
|
+
.m { color: #666666 } /* Literal.Number */
|
25
|
+
.s { color: #BA2121 } /* Literal.String */
|
26
|
+
.na { color: #7D9029 } /* Name.Attribute */
|
27
|
+
.nb { color: #008000 } /* Name.Builtin */
|
28
|
+
.nc { color: #0000FF; font-weight: bold } /* Name.Class */
|
29
|
+
.no { color: #880000 } /* Name.Constant */
|
30
|
+
.nd { color: #AA22FF } /* Name.Decorator */
|
31
|
+
.ni { color: #999999; font-weight: bold } /* Name.Entity */
|
32
|
+
.ne { color: #D2413A; font-weight: bold } /* Name.Exception */
|
33
|
+
.nf { color: #0000FF } /* Name.Function */
|
34
|
+
.nl { color: #A0A000 } /* Name.Label */
|
35
|
+
.nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
|
36
|
+
.nt { color: #008000; font-weight: bold } /* Name.Tag */
|
37
|
+
.nv { color: #19177C } /* Name.Variable */
|
38
|
+
.ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
|
39
|
+
.w { color: #bbbbbb } /* Text.Whitespace */
|
40
|
+
.mf { color: #666666 } /* Literal.Number.Float */
|
41
|
+
.mh { color: #666666 } /* Literal.Number.Hex */
|
42
|
+
.mi { color: #666666 } /* Literal.Number.Integer */
|
43
|
+
.mo { color: #666666 } /* Literal.Number.Oct */
|
44
|
+
.sb { color: #BA2121 } /* Literal.String.Backtick */
|
45
|
+
.sc { color: #BA2121 } /* Literal.String.Char */
|
46
|
+
.sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
|
47
|
+
.s2 { color: #BA2121 } /* Literal.String.Double */
|
48
|
+
.se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
|
49
|
+
.sh { color: #BA2121 } /* Literal.String.Heredoc */
|
50
|
+
.si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
|
51
|
+
.sx { color: #008000 } /* Literal.String.Other */
|
52
|
+
.sr { color: #BB6688 } /* Literal.String.Regex */
|
53
|
+
.s1 { color: #BA2121 } /* Literal.String.Single */
|
54
|
+
.ss { color: #19177C } /* Literal.String.Symbol */
|
55
|
+
.bp { color: #008000 } /* Name.Builtin.Pseudo */
|
56
|
+
.vc { color: #19177C } /* Name.Variable.Class */
|
57
|
+
.vg { color: #19177C } /* Name.Variable.Global */
|
58
|
+
.vi { color: #19177C } /* Name.Variable.Instance */
|
59
|
+
.il { color: #666666 } /* Literal.Number.Integer.Long */
|
@@ -0,0 +1,187 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
5
|
+
require 'simplabs/highlight/pygments_wrapper'
|
6
|
+
|
7
|
+
module Simplabs
|
8
|
+
|
9
|
+
require 'simplabs/highlight/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
|
10
|
+
|
11
|
+
# Highlight is a simple syntax highlighting plugin for Ruby on Rails.
|
12
|
+
# It's basically a wrapper around the popular http://pygments.org
|
13
|
+
# highlighter that's written in Python and supports an impressive
|
14
|
+
# number of languages. If pygments is installed on the machine and in
|
15
|
+
# the +PATH+, that binary is used, otherwise the plugin falls back
|
16
|
+
# to the web API at (http://pygments.appspot.com/), created by Trevor
|
17
|
+
# Turk.
|
18
|
+
#
|
19
|
+
# <b>Supported Languages</b>
|
20
|
+
#
|
21
|
+
# The following languages are supported. All of the paranthesized
|
22
|
+
# identifiers may be used as parameters for the +highlight+ method to
|
23
|
+
# denote the language the source code to highlight is written in (use
|
24
|
+
# either Symbols or Strings).
|
25
|
+
#
|
26
|
+
# * Actionscript (+as+, +as3+, +actionscript+)
|
27
|
+
# * Applescript (+applescript+)
|
28
|
+
# * bash (+bash+, +sh+)
|
29
|
+
# * C (+c+, +h+)
|
30
|
+
# * Clojure (+clojure+)
|
31
|
+
# * C++ (+c+++, +cpp+, +hpp+)
|
32
|
+
# * C# (+c#+, +csharp+, +cs+)
|
33
|
+
# * CSS (+css+)
|
34
|
+
# * diff (+diff+)
|
35
|
+
# * Dylan (+dylan+)
|
36
|
+
# * Erlang (+erlang+, +erl+, +er+)
|
37
|
+
# * HTML (+html+, +htm+)
|
38
|
+
# * Java (+java+)
|
39
|
+
# * JavaScript (+javascript+, +js+, +jscript+)
|
40
|
+
# * JSP (+jsp+)
|
41
|
+
# * Make (+make+, +basemake+, +makefile+)
|
42
|
+
# * NASM (+nasm+, +asm+)
|
43
|
+
# * Objective-C (+objective-c+)
|
44
|
+
# * OCaml (+ocaml+)
|
45
|
+
# * Perl (+perl+, +pl+)
|
46
|
+
# * PHP (+php+)
|
47
|
+
# * Python (+python+, +py+)
|
48
|
+
# * RHTML (+erb+, +rhtml+)
|
49
|
+
# * Ruby (+ruby+, +rb+)
|
50
|
+
# * Scala (+scala+)
|
51
|
+
# * Scheme (+scheme+)
|
52
|
+
# * Smalltalk (+smalltalk+)
|
53
|
+
# * Smarty (+smarty+)
|
54
|
+
# * SQL (+sql+)
|
55
|
+
# * XML (+xml+, +xsd+)
|
56
|
+
# * XSLT (+xslt+)
|
57
|
+
# * YAML (+yaml+, +yml+)
|
58
|
+
#
|
59
|
+
module Highlight
|
60
|
+
|
61
|
+
mattr_accessor :use_web_api
|
62
|
+
|
63
|
+
SUPPORTED_LANGUAGES = {
|
64
|
+
:as => ['as', 'as3', 'actionscript'],
|
65
|
+
:applescript => ['applescript'],
|
66
|
+
:bash => ['bash', 'sh'],
|
67
|
+
:c => ['c', 'h'],
|
68
|
+
:clojure => ['clojure'],
|
69
|
+
:cpp => ['c++', 'cpp', 'hpp'],
|
70
|
+
:csharp => ['c#', 'csharp', 'cs'],
|
71
|
+
:css => ['css'],
|
72
|
+
:diff => ['diff'],
|
73
|
+
:dylan => ['dylan'],
|
74
|
+
:erlang => ['erlang'],
|
75
|
+
:html => ['html', 'htm'],
|
76
|
+
:java => ['java'],
|
77
|
+
:js => ['javascript', 'js', 'jscript'],
|
78
|
+
:jsp => ['jsp'],
|
79
|
+
:make => ['make', 'basemake', 'makefile'],
|
80
|
+
:nasm => ['nasm', 'asm'],
|
81
|
+
:'objective-c' => ['objective-c'],
|
82
|
+
:ocaml => ['ocaml'],
|
83
|
+
:perl => ['perl', 'pl'],
|
84
|
+
:php => ['php'],
|
85
|
+
:python => ['python', 'py'],
|
86
|
+
:rhtml => ['erb', 'rhtml'],
|
87
|
+
:ruby => ['ruby', 'rb'],
|
88
|
+
:scala => ['scala'],
|
89
|
+
:scheme => ['scheme'],
|
90
|
+
:smallralk => ['smalltalk'],
|
91
|
+
:smarty => ['smarty'],
|
92
|
+
:sql => ['sql', 'mysql'],
|
93
|
+
:xml => ['xml', 'xsd'],
|
94
|
+
:xslt => ['xslt'],
|
95
|
+
:yaml => ['yaml', 'yml']
|
96
|
+
}
|
97
|
+
|
98
|
+
WEB_API_URL = 'http://pygments.appspot.com/'
|
99
|
+
|
100
|
+
# Highlights the passed +code+ with the appropriate rules
|
101
|
+
# according to the specified +language+.
|
102
|
+
#
|
103
|
+
# @param [Symbol, String] language
|
104
|
+
# the language the +code+ is in
|
105
|
+
# @param [String] code
|
106
|
+
# the actual code to highlight
|
107
|
+
#
|
108
|
+
# @return [String]
|
109
|
+
# the highlighted +code+ or simply the HTML-escaped code if +language+
|
110
|
+
# is not supported.
|
111
|
+
#
|
112
|
+
def self.highlight(language, code)
|
113
|
+
language = get_language_sym(language)
|
114
|
+
return CGI.escapeHTML(code) unless language
|
115
|
+
if Simplabs::Highlight.use_web_api
|
116
|
+
highlight_with_web_api(language, code)
|
117
|
+
else
|
118
|
+
Simplabs::Highlight::PygmentsWrapper.new(code, language).highlight
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# View Helpers for using {Highlight} in Ruby on Rails templates.
|
123
|
+
#
|
124
|
+
module ViewMethods
|
125
|
+
|
126
|
+
# Highlights the passed +code+ with the appropriate rules
|
127
|
+
# according to the specified +language+. The code can be specified
|
128
|
+
# either as a string or provided in a block.
|
129
|
+
#
|
130
|
+
# @param [Symbol, String] language
|
131
|
+
# the language the +code+ is in
|
132
|
+
# @param [String] code
|
133
|
+
# the actual code to highlight
|
134
|
+
# @yield
|
135
|
+
# the +code+ can also be specified as result of a given block.
|
136
|
+
#
|
137
|
+
# @return [String]
|
138
|
+
# the highlighted +code+ or simply the HTML-escaped code if +language+
|
139
|
+
# is not supported.
|
140
|
+
#
|
141
|
+
# @example Specifying the code to highlight as a String
|
142
|
+
#
|
143
|
+
# highlight_code(:ruby, 'class Test; end')
|
144
|
+
#
|
145
|
+
# @example Specifying the code to highlight in a block
|
146
|
+
#
|
147
|
+
# highlight_code(:ruby) do
|
148
|
+
# klass = 'class'
|
149
|
+
# name = 'Test'
|
150
|
+
# _end = 'end'
|
151
|
+
# "#{klass} #{name}; #{_end}"
|
152
|
+
# end
|
153
|
+
#
|
154
|
+
# @raise [ArgumentError] if both the +code+ parameter and a block are given
|
155
|
+
# @raise [ArgumentError] if neither the +code+ parameter or a block are given
|
156
|
+
#
|
157
|
+
# @see Simplabs::Highlight.highlight
|
158
|
+
#
|
159
|
+
def highlight_code(language, code = nil, &block)
|
160
|
+
raise ArgumentError.new('Either pass a srting containing the code or a block, not both!') if !code.nil? && block_given?
|
161
|
+
raise ArgumentError.new('Pass a srting containing the code or a block!') if code.nil? && !block_given?
|
162
|
+
code ||= yield
|
163
|
+
Simplabs::Highlight.highlight(language, code)
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
private
|
169
|
+
|
170
|
+
def self.highlight_with_web_api(language, code)
|
171
|
+
request = Net::HTTP.post_form(URI.parse(WEB_API_URL), {
|
172
|
+
'lang' => language,
|
173
|
+
'code' => code
|
174
|
+
})
|
175
|
+
request.body.gsub(/\A\<div class=\"highlight\"\>\<pre\>/, '').gsub(/\n\<\/pre\>\<\/div\>\n/, '')
|
176
|
+
end
|
177
|
+
|
178
|
+
def self.get_language_sym(name)
|
179
|
+
SUPPORTED_LANGUAGES.each_pair do |key, value|
|
180
|
+
return key if value.any? { |lang| lang == name.to_s }
|
181
|
+
end
|
182
|
+
return false
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Simplabs
|
2
|
+
|
3
|
+
module Highlight
|
4
|
+
|
5
|
+
# Wraps the actual +pygments+ syntax highlighter and
|
6
|
+
# exposes its functionality to Ruby code.
|
7
|
+
#
|
8
|
+
class PygmentsWrapper
|
9
|
+
|
10
|
+
# The code the wrapper highlights
|
11
|
+
#
|
12
|
+
attr_reader :code
|
13
|
+
|
14
|
+
# The language the {Simplabs::Highlight::PygmentsWrapper#code} to highlight is in
|
15
|
+
#
|
16
|
+
attr_reader :language
|
17
|
+
|
18
|
+
# Initializes a new {Simplabs::Highlight::PygmentsWrapper}.
|
19
|
+
#
|
20
|
+
# @param [String] code
|
21
|
+
# the actual code to highlight
|
22
|
+
# @param [String, Symbol] language
|
23
|
+
# the language the +code+ to highlight is in
|
24
|
+
#
|
25
|
+
def initialize(code, language)
|
26
|
+
@code = code
|
27
|
+
@language = language
|
28
|
+
end
|
29
|
+
|
30
|
+
# Highlights the {Simplabs::Highlight::PygmentsWrapper#code}.
|
31
|
+
#
|
32
|
+
# @return [String]
|
33
|
+
# the highlighted code or simply the HTML-escaped code
|
34
|
+
# if the language is not supported.
|
35
|
+
#
|
36
|
+
def highlight
|
37
|
+
command = "pygmentize -f html -O nowrap=true -l #{@language}"
|
38
|
+
IO.popen(command, mode = 'r+') do |pygments|
|
39
|
+
pygments << @code
|
40
|
+
pygments.close_write
|
41
|
+
pygments.read.strip.chomp
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'simplabs/highlight'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module Simplabs
|
5
|
+
|
6
|
+
module Highlight
|
7
|
+
|
8
|
+
class Railtie < Rails::Railtie
|
9
|
+
|
10
|
+
GEM_ROOT = File.join(File.dirname(__FILE__), '..', '..', '..')
|
11
|
+
|
12
|
+
initializer 'simplabs.highlight.initialization' do
|
13
|
+
require File.join(GEM_ROOT, 'rails', 'init')
|
14
|
+
end
|
15
|
+
|
16
|
+
generators do
|
17
|
+
require File.join(GEM_ROOT, 'generators', 'highlight_styles_generator', 'highlight_styles_generator')
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
require 'simplabs/highlight'
|
3
|
+
|
4
|
+
if `which pygmentize`.blank?
|
5
|
+
puts ''
|
6
|
+
puts " ** [Highlight] pygments cannot be found, falling back to #{Simplabs::Highlight::WEB_API_URL}! **"
|
7
|
+
puts ' ** If you have pygments installed, make sure it is in your PATH. **'
|
8
|
+
puts ''
|
9
|
+
Simplabs::Highlight.use_web_api = true
|
10
|
+
else
|
11
|
+
Simplabs::Highlight.use_web_api = false
|
12
|
+
end
|
13
|
+
|
14
|
+
ActionView::Base.class_eval do
|
15
|
+
include Simplabs::Highlight::ViewMethods
|
16
|
+
end
|
data/spec/boot.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
plugin_root = File.join(File.dirname(__FILE__), '..')
|
2
|
+
|
3
|
+
$:.unshift "#{plugin_root}/lib"
|
4
|
+
|
5
|
+
Bundler.require
|
6
|
+
require 'initializer'
|
7
|
+
|
8
|
+
RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + '/../')
|
9
|
+
Rails::Initializer.run(:set_load_path)
|
10
|
+
Rails::Initializer.run(:set_autoload_paths)
|
11
|
+
|
12
|
+
require File.join(File.dirname(__FILE__), '/../rails/init.rb')
|
13
|
+
|
14
|
+
FileUtils.mkdir_p(File.join(File.dirname(__FILE__), 'log'))
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Simplabs::Highlight do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@code = <<-EOC
|
7
|
+
class Test
|
8
|
+
def method test
|
9
|
+
end
|
10
|
+
end
|
11
|
+
EOC
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#highlight' do
|
15
|
+
|
16
|
+
share_examples_for 'the highlight method' do
|
17
|
+
|
18
|
+
describe 'when the language is not supported' do
|
19
|
+
|
20
|
+
it 'should only escape HTML in the passed code' do
|
21
|
+
Simplabs::Highlight.highlight(:unsupported, @code).should == CGI.escapeHTML(@code)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'when the language is supported' do
|
27
|
+
|
28
|
+
it 'should correctly highlight source code passed as parameter' do
|
29
|
+
Simplabs::Highlight.highlight(:ruby, @code).should == %Q(<span class="k">class</span> <span class="nc">Test</span>\n <span class="k">def</span> <span class="nf">method</span> <span class="nb">test</span>\n <span class="k">end</span>\n<span class="k">end</span>)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'when pygments is used directly' do
|
37
|
+
|
38
|
+
before do
|
39
|
+
Simplabs::Highlight.use_web_api = false
|
40
|
+
end
|
41
|
+
|
42
|
+
it_should_behave_like 'the highlight method'
|
43
|
+
|
44
|
+
it 'should initialize a Simplabs::PygmentsWrapper.highlight with the language and code' do
|
45
|
+
wrapper = Simplabs::Highlight::PygmentsWrapper.new(@code, :ruby)
|
46
|
+
|
47
|
+
Simplabs::Highlight::PygmentsWrapper.should_receive(:new).once.with(@code, :ruby).and_return(wrapper)
|
48
|
+
|
49
|
+
Simplabs::Highlight.highlight(:ruby, @code)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'when the web API is used' do
|
55
|
+
|
56
|
+
before do
|
57
|
+
Simplabs::Highlight.use_web_api = true
|
58
|
+
end
|
59
|
+
|
60
|
+
it_should_behave_like 'the highlight method'
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '.get_language_sym' do
|
67
|
+
|
68
|
+
describe 'for an unsupported language' do
|
69
|
+
|
70
|
+
it 'should return false' do
|
71
|
+
Simplabs::Highlight.send(:get_language_sym, 'unsupported language').should == false
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
describe 'for a supported language' do
|
77
|
+
|
78
|
+
it 'should return the respective symbol when the languages was given as String' do
|
79
|
+
Simplabs::Highlight.send(:get_language_sym, 'ruby').should == :ruby
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should return the respective symbol when the languages was given as Symbol' do
|
83
|
+
Simplabs::Highlight.send(:get_language_sym, :rb).should == :ruby
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Simplabs::Highlight::PygmentsWrapper do
|
4
|
+
|
5
|
+
before do
|
6
|
+
code = <<-EOC
|
7
|
+
class Test
|
8
|
+
def method test
|
9
|
+
end
|
10
|
+
end
|
11
|
+
EOC
|
12
|
+
@wrapper = Simplabs::Highlight::PygmentsWrapper.new(code, :ruby)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#highlight' do
|
16
|
+
|
17
|
+
it 'should correctly highlight source code passed as parameter' do
|
18
|
+
@wrapper.highlight.should == %Q(<span class="k">class</span> <span class="nc">Test</span>\n <span class="k">def</span> <span class="nf">method</span> <span class="nb">test</span>\n <span class="k">end</span>\n<span class="k">end</span>)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Simplabs::Highlight::ViewMethods do
|
4
|
+
|
5
|
+
include Simplabs::Highlight::ViewMethods
|
6
|
+
|
7
|
+
before do
|
8
|
+
@ruby_code = 'class Test; end'
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#highlight_code' do
|
12
|
+
|
13
|
+
describe 'when invoked with a language and a string' do
|
14
|
+
|
15
|
+
it 'should highlight the code' do
|
16
|
+
Simplabs::Highlight.should_receive(:highlight).once.with(:ruby, @ruby_code)
|
17
|
+
|
18
|
+
highlight_code(:ruby, @ruby_code)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'when invoked with a language and a block' do
|
24
|
+
|
25
|
+
it 'should highlight the code' do
|
26
|
+
Simplabs::Highlight.should_receive(:highlight).once.with(:ruby, @ruby_code)
|
27
|
+
|
28
|
+
highlight_code(:ruby) do
|
29
|
+
@ruby_code
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'when invoked with both a string and a block' do
|
36
|
+
|
37
|
+
it 'should raise an ArgumentError' do
|
38
|
+
lambda { highlight_code(:ruby, @ruby_code) { @ruby_code } }.should raise_error(ArgumentError)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'when invoked with neither a string nor a block' do
|
44
|
+
|
45
|
+
it 'should raise an ArgumentError' do
|
46
|
+
lambda { highlight_code(:ruby) }.should raise_error(ArgumentError)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: highlight
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 3
|
10
|
+
version: 1.1.3
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Marco Otte-Witte
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-05-13 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activesupport
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 15
|
29
|
+
segments:
|
30
|
+
- 2
|
31
|
+
- 0
|
32
|
+
- 0
|
33
|
+
version: 2.0.0
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: Highlight highlights code in more than 20 languages. It uses the Pygments syntax highlighter and adds a simple Ruby API over it. It also provides helpers for use in your Ruby on Rails views.
|
37
|
+
email: info@simplabs.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- README.md
|
46
|
+
- HISTORY.md
|
47
|
+
- Rakefile
|
48
|
+
- MIT-LICENSE
|
49
|
+
- generators/highlight_styles_generator/highlight_styles_generator.rb
|
50
|
+
- generators/highlight_styles_generator/templates/highlight.css
|
51
|
+
- generators/highlight_styles_generator/templates/NOTES
|
52
|
+
- lib/simplabs/highlight/pygments_wrapper.rb
|
53
|
+
- lib/simplabs/highlight/railtie.rb
|
54
|
+
- lib/simplabs/highlight.rb
|
55
|
+
- rails/init.rb
|
56
|
+
- spec/boot.rb
|
57
|
+
- spec/lib/highlight_spec.rb
|
58
|
+
- spec/lib/pygments_wrapper_spec.rb
|
59
|
+
- spec/lib/view_methods_spec.rb
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
- spec/spec.opts
|
62
|
+
homepage: http://github.com/simplabs/highlight
|
63
|
+
licenses: []
|
64
|
+
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
hash: 3
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
requirements: []
|
89
|
+
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.6
|
92
|
+
signing_key:
|
93
|
+
specification_version: 2
|
94
|
+
summary: Syntax Higlighting plugin for Ruby on Rails
|
95
|
+
test_files: []
|
96
|
+
|