minisyntax 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +28 -0
- data/MIT-LICENSE.txt +20 -0
- data/Rakefile +1 -1
- data/lib/minisyntax/highlighter/bash.rb +23 -0
- data/lib/minisyntax/highlighter/command_line.rb +21 -0
- data/lib/minisyntax/highlighter/css.rb +95 -0
- data/lib/minisyntax/highlighter/erb.rb +13 -0
- data/lib/minisyntax/highlighter/haml.rb +26 -0
- data/lib/minisyntax/highlighter/html.rb +29 -0
- data/lib/minisyntax/highlighter/javascript.rb +24 -0
- data/lib/minisyntax/highlighter/php.rb +34 -0
- data/lib/minisyntax/highlighter/ruby.rb +43 -0
- data/lib/minisyntax/highlighter/yaml.rb +13 -0
- data/lib/minisyntax/highlighter.rb +3 -0
- data/lib/minisyntax/integration/rack.rb +61 -0
- data/lib/minisyntax.rb +6 -6
- data/minisyntax.gemspec +77 -0
- metadata +31 -13
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.5.2)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.8.7)
|
11
|
+
rcov (0.9.9)
|
12
|
+
rspec (2.3.0)
|
13
|
+
rspec-core (~> 2.3.0)
|
14
|
+
rspec-expectations (~> 2.3.0)
|
15
|
+
rspec-mocks (~> 2.3.0)
|
16
|
+
rspec-core (2.3.1)
|
17
|
+
rspec-expectations (2.3.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.3.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
bundler (~> 1.0.0)
|
26
|
+
jeweler (~> 1.5.2)
|
27
|
+
rcov
|
28
|
+
rspec (~> 2.3.0)
|
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Nico Hagenburger
|
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/Rakefile
CHANGED
@@ -13,7 +13,7 @@ require 'jeweler'
|
|
13
13
|
$: << File.join(File.dirname(__FILE__), 'lib')
|
14
14
|
require 'minisyntax'
|
15
15
|
Jeweler::Tasks.new do |gem|
|
16
|
-
gem.version =
|
16
|
+
gem.version = MiniSyntax::Version::STRING
|
17
17
|
gem.name = "minisyntax"
|
18
18
|
gem.homepage = "http://github.com/hagenburger/minisyntax"
|
19
19
|
gem.license = "MIT"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module MiniSyntax
|
2
|
+
module Highlighter
|
3
|
+
module Bash
|
4
|
+
def self.highlight(code)
|
5
|
+
code.gsub! /\$[a-z\-_]+/, "<var>\\0</var>"
|
6
|
+
code.gsub! /("(.*?)"|'.*?')/ do |text|
|
7
|
+
%Q(<q>#{text}</q>)
|
8
|
+
end
|
9
|
+
code.gsub! %r((\#.*?)$) do |comment|
|
10
|
+
if comment.gsub(%r(<q>(.*?)</q>), "\\1") =~ %r(</q>)
|
11
|
+
comment
|
12
|
+
else
|
13
|
+
comment.gsub! %r(</?(b|i|em|var|code)>), ""
|
14
|
+
%Q(<i>#{comment}</i>)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
code
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
MiniSyntax.register(:bash, MiniSyntax::Highlighter::Bash)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module MiniSyntax
|
2
|
+
module Highlighter
|
3
|
+
module CommandLine
|
4
|
+
def self.highlight(code)
|
5
|
+
code = '<kbd>' + code.gsub(/\n/, %Q(</kbd>\n<kbd>)) + '</kbd>'
|
6
|
+
code.gsub! %r((\#.*?)$) do |comment|
|
7
|
+
if comment =~ %r(</q>)
|
8
|
+
comment
|
9
|
+
else
|
10
|
+
comment.gsub! %r(</?(b|i|em|var|code|kbd)>), ""
|
11
|
+
%Q(<i>#{comment}</i>)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
code.gsub! %r(<kbd><i>), "<i>"
|
15
|
+
code
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
MiniSyntax.register(:command_line, MiniSyntax::Highlighter::CommandLine)
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module MiniSyntax
|
2
|
+
module Highlighter
|
3
|
+
module CSS
|
4
|
+
def self.highlight(code)
|
5
|
+
code.gsub! %r(( *)((\$[a-z\-_]+):(.+?);|([_\*]?[a-z\-]+:)(("|[^&])+?);|@import (.+?);|(([\.\#]?[a-z0-9\-_&:]+([,\s]\s*[\.\#]?[a-z0-9\-_&:]+)*))(\s*)\{(.*?\n\1)\})|@media (.+?)\{|@(include|extend) (.+?);)im do
|
6
|
+
intendation = $1
|
7
|
+
if $3
|
8
|
+
%Q(#{intendation}<var>#{$3}</var>:#{highlight_value($4)};)
|
9
|
+
elsif $5
|
10
|
+
%Q(#{intendation}<b>#{$5}</b>#{highlight_value($6)};)
|
11
|
+
elsif $8
|
12
|
+
%Q(#{intendation}@<em>import</em> <q>#{$8}</q>;)
|
13
|
+
elsif $10
|
14
|
+
whitespace = $12
|
15
|
+
rules = $13
|
16
|
+
# selector = $10.gsub(/([\.\#\b])([a-z0-9\-_]+)\b/i) do
|
17
|
+
# if $1 == '.'
|
18
|
+
# %Q(<b><i>#{$1}#{$2}</i></b>)
|
19
|
+
# elsif $1 == '#'
|
20
|
+
# %Q(<b>#{$1}#{$2}</b>)
|
21
|
+
# else
|
22
|
+
# %Q(<em>#{$2}</em>)
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
selector = %Q(<b><i>#{$10}</i></b>)
|
26
|
+
%Q(#{intendation}#{selector}#{whitespace}{#{highlight(rules)}})
|
27
|
+
elsif $14
|
28
|
+
%Q(#{intendation}@<em>media</em> #{$14.gsub('and', '<em>and</em>')}{)
|
29
|
+
elsif $15
|
30
|
+
keyword = $15
|
31
|
+
call = $16
|
32
|
+
rule = call.gsub(/^([\.#]?[a-z\-_]+).*$/, '<b>\\1</b>')
|
33
|
+
parameter = call.gsub(/^.*?\((.+?)\)/) { "(#{highlight_value($1)})" }
|
34
|
+
%Q(#{intendation}@<em>#{keyword}</em> #{rule}#{parameter};)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
code.gsub! %r((<i>)?(//.*?$|/\*.*?\*/)) do
|
38
|
+
comment = $2
|
39
|
+
if $1 == '<i>' or comment.gsub(%r(<q>(.*?)</q>), "\\1") =~ %r(</q>)
|
40
|
+
comment
|
41
|
+
else
|
42
|
+
comment.gsub! %r(</?(b|i|em|var|code)>), ""
|
43
|
+
%Q(<i>#{comment}</i>)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
code
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def self.highlight_value(code)
|
51
|
+
keywords = %w(!important left-side far-left left center-left center center-right right far-right right-side behind leftwards rightwards inherit)
|
52
|
+
keywords << %w(scroll fixed transparent none top center bottom middle)
|
53
|
+
keywords << %w(repeat repeat-x repeat-y no-repeat collapse separate auto both normal)
|
54
|
+
keywords << %w(attr open-quote close-quote no-open-quote no-close-quote)
|
55
|
+
keywords << %w(crosshair default pointer move e-resize ne-resize nw-resize n-resize se-resize sw-resize s-resize w-resize text wait help progress)
|
56
|
+
keywords << %w(ltr rtl)
|
57
|
+
keywords << %w(inline block list-item run-in inline-block table inline-table table-row-group table-header-group table-footer-group table-row table-column-group table-column table-cell table-caption)
|
58
|
+
keywords << %w(below level above higher lower)
|
59
|
+
keywords << %w(show hide italic oblique small-caps bold bolder lighter)
|
60
|
+
keywords << %w(caption icon menu message-box small-caption status-bar)
|
61
|
+
keywords << %w(inside outside disc circle square decimal decimal-leading-zero lower-roman upper-roman lower-greek lower-latin upper-latin armenian georgian lower-alpha upper-alpha)
|
62
|
+
keywords << %w(invert)
|
63
|
+
keywords << %w(visible hidden scroll)
|
64
|
+
keywords << %w(always avoid)
|
65
|
+
keywords << %w(x-low low medium high x-high)
|
66
|
+
keywords << %w(static relative absolute fixed)
|
67
|
+
keywords << %w(spell-out)
|
68
|
+
keywords << %w(x-slow slow medium fast x-fast faster slower)
|
69
|
+
keywords << %w(left right center justify)
|
70
|
+
keywords << %w(underline overline line-through blink)
|
71
|
+
keywords << %w(capitalize uppercase lowercase)
|
72
|
+
keywords << %w(embed bidi-override)
|
73
|
+
keywords << %w(baseline sub super top text-top middle bottom text-bottom)
|
74
|
+
keywords << %w(silent x-soft soft medium loud x-loud)
|
75
|
+
keywords << %w(normal pre nowrap pre-wrap pre-line)
|
76
|
+
keywords << %w(maroon red yellow olive purple fuchsia white lime green navy blue aqua teal black silver gray orange)
|
77
|
+
code.gsub! /\$[a-z\-_]+/, "<var>\\0</var>"
|
78
|
+
code.gsub! /\b#{keywords.join('|')}\b/, "<b>\\0</b>"
|
79
|
+
code.gsub! /("|')(.*?)\1/ do |q|
|
80
|
+
q.gsub! %r(<(b|i|em|var)>(.*?)</\1>), "\\2"
|
81
|
+
q.gsub!(/#\{(.*?)\}/) do
|
82
|
+
%Q(<code>\#{#{highlight_value($1)}}</code>)
|
83
|
+
end
|
84
|
+
%Q(<q>#{q}</q>)
|
85
|
+
end
|
86
|
+
code.gsub! %r(<var>(.+?)</var>) do |var|
|
87
|
+
"<var>#{var.gsub(%r(</?[a-z]+>), '')}</var>"
|
88
|
+
end
|
89
|
+
code
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
MiniSyntax.register(:css, MiniSyntax::Highlighter::CSS)
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module MiniSyntax
|
2
|
+
module Highlighter
|
3
|
+
module ERB
|
4
|
+
def self.highlight(code)
|
5
|
+
code.gsub /<%(.*?)%>/ do
|
6
|
+
"<code><%" + MiniSyntax.highlight($1, :ruby) + "%></code>"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
MiniSyntax.register(:erb, MiniSyntax::Highlighter::ERB)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module MiniSyntax
|
2
|
+
module Highlighter
|
3
|
+
module Haml
|
4
|
+
def self.highlight(code)
|
5
|
+
code.gsub! /^( *)(%[a-z\-]+)?(([\.\#][a-z\-_]+)*)((<)?(>)?&?)(=.+?$)?/i do
|
6
|
+
result = $1 || ''
|
7
|
+
tag = $2
|
8
|
+
classes_and_id = $3
|
9
|
+
options = $5
|
10
|
+
ruby = $8
|
11
|
+
result << %Q(<em>#{tag}</em>) if tag
|
12
|
+
result << classes_and_id if classes_and_id
|
13
|
+
result << options if options
|
14
|
+
result << MiniSyntax.highlight(ruby, :ruby) if ruby
|
15
|
+
result
|
16
|
+
end
|
17
|
+
code.gsub! /^( )*(-.+?)$/ do
|
18
|
+
%Q(#{$1}#{MiniSyntax.highlight($2, :ruby)})
|
19
|
+
end
|
20
|
+
code
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
MiniSyntax.register(:haml, MiniSyntax::Highlighter::Haml)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module MiniSyntax
|
2
|
+
module Highlighter
|
3
|
+
module HTML
|
4
|
+
def self.highlight(code)
|
5
|
+
code.gsub! %r((<script( [a-z\-]+(=("|'|\w).*?\4)?)*>)(.*?)(</script>))m do
|
6
|
+
%Q(#{$1}#{MiniSyntax.highlight($5, :javascript)}#{$6})
|
7
|
+
end
|
8
|
+
code.gsub! %r(<([a-z\-]+[1-6]?)(( [a-z\-]+(=".*?")?)*)( /)?>)m do
|
9
|
+
tag = $1
|
10
|
+
xml_close_tag = $5
|
11
|
+
attributes = $2.gsub %r( ([a-z\-]+)(=(")(.*?)("))?)m do
|
12
|
+
if %(onload onclick onmouseover onmousemove onmouseout onfocus onblur onkeyup onkeydown onkeypress).include?($1)
|
13
|
+
%Q( <b>#{$1}</b>=#{$3}#{MiniSyntax.highlight($4, :javascript)}#{$3})
|
14
|
+
else
|
15
|
+
%Q( <b>#{$1}</b>#{$2})
|
16
|
+
end
|
17
|
+
end if $2
|
18
|
+
%Q(<b><<em>#{tag}</em></b>#{attributes}<b>#{xml_close_tag}></b>)
|
19
|
+
end
|
20
|
+
code.gsub! %r(</([a-z\-]+[1-6]?)>) do
|
21
|
+
%Q(<b></<em>#{$1}</em>></b>)
|
22
|
+
end
|
23
|
+
code
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
MiniSyntax.register(:html, MiniSyntax::Highlighter::HTML)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module MiniSyntax
|
2
|
+
module Highlighter
|
3
|
+
module JavaScript
|
4
|
+
def self.highlight(code)
|
5
|
+
keywords = %w(break continue do for import new this void case default else function in return typeof while comment delete export if label switch var with)
|
6
|
+
keywords += %w(catch enum throw class extends try const finally debugger super)
|
7
|
+
code.gsub! /\b(#{keywords.join('|')})\b/, "<em>\\1</em>"
|
8
|
+
code.gsub! /\b([A-Z_][a-zA-Z0-9_]+)\b/, "<b>\\1</b>"
|
9
|
+
code.gsub! /("(.*?)"|'.*?')/, "<q>\\1</q>"
|
10
|
+
code.gsub! %r((//.*?)$) do |comment|
|
11
|
+
if comment.gsub(%r(<q>(.*?)</q>), "\\1") =~ %r(</q>)
|
12
|
+
comment
|
13
|
+
else
|
14
|
+
comment.gsub! %r(</?(b|i|em|var|code)>), ""
|
15
|
+
%Q(<i>#{comment}</i>)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
code
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
MiniSyntax.register(:javascript, MiniSyntax::Highlighter::JavaScript)
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module MiniSyntax
|
2
|
+
module Highlighter
|
3
|
+
module PHP
|
4
|
+
def self.highlight(code)
|
5
|
+
keywords = %w(abstract and array as break case catch cfunction class clone const continue declare default do else elseif enddeclare endfor endforeach endif endswitch endwhile extends final for foreach function global goto if implements interface instanceof namespace new old_function or private protected public static switch throw try use var while xor)
|
6
|
+
keywords += %w(die echo empty exit eval include include_once isset list require require_once return print unset )
|
7
|
+
code.gsub! /\b(#{keywords.join('|')})\b/, "<em>\\1</em>"
|
8
|
+
code.gsub! /\b([A-Z_][a-zA-Z0-9_]+)\b/, "<b>\\1</b>"
|
9
|
+
code.gsub! /\$[a-zA-Z0-9_]+/, "<var>\\0</var>"
|
10
|
+
code.gsub! /("(.*?)"|'.*?')/ do |q|
|
11
|
+
q.gsub! %r(<(b|i|em|var)>(.*?)</\1>), "\\2"
|
12
|
+
if q[0..5] == '"'
|
13
|
+
q.gsub! /(\$[a-zA-Z0-9_]+)(\[(.+?)\])?/ do
|
14
|
+
hash = $3['$'] ? %Q([<var>#{$3}</var>]) : $2 if $2
|
15
|
+
%Q(<code><var>#{$1}</var>#{hash}</code>)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
%Q(<q>#{q}</q>)
|
19
|
+
end
|
20
|
+
code.gsub! %r((//.*?)$) do |comment|
|
21
|
+
if comment.gsub(%r(<q>(.*?)</q>), "\\1") =~ %r(</q>)
|
22
|
+
comment
|
23
|
+
else
|
24
|
+
comment.gsub! %r(</?(b|i|em|var|code)>), ""
|
25
|
+
%Q(<i>#{comment}</i>)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
code
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
MiniSyntax.register(:php, MiniSyntax::Highlighter::PHP)
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module MiniSyntax
|
2
|
+
module Highlighter
|
3
|
+
module Ruby
|
4
|
+
def self.highlight(code)
|
5
|
+
keywords = %w(BEGIN begin case class else elsif END end ensure for if in module rescue then unless until when while)
|
6
|
+
keywords += %w(def do and not or require)
|
7
|
+
keywords += %w(alias alias_method break next redo retry return super undef yield)
|
8
|
+
keywords += %w(initialize new loop include extend raise attr_reader attr_writer attr_accessor attr catch throw private module_function public protected)
|
9
|
+
code.gsub! /\b(#{keywords.join('|')})\b(?!\?)/, "<em>\\1</em>"
|
10
|
+
code.gsub! /\b([A-Z_][a-zA-Z0-9_]+)\b/, "<b>\\1</b>"
|
11
|
+
code.gsub! /(\#([^\{].*?)?)\n/, "<i>\\1</i>\n"
|
12
|
+
code.gsub! /("(.*?)"|'.*?'|%[qrw]\(.*?\)|%([QRW])\((.*?)\))/ do |text|
|
13
|
+
if $2 or type = $3
|
14
|
+
text.gsub! /#\{(.*?)\}/ do
|
15
|
+
%Q(<code>\#{#{highlight($1)}}</code>)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
%Q(<q>#{text}</q>)
|
19
|
+
end
|
20
|
+
code.gsub! %r(<<-<b>HALLO</b>.*<b>HALLO</b>) do |text|
|
21
|
+
if $2 or type = $3
|
22
|
+
text.gsub! /#\{(.*?)\}/ do
|
23
|
+
%Q(<code>\#{#{highlight($1)}}</code>)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
%Q(<q>#{text}</q>)
|
27
|
+
end
|
28
|
+
code.gsub! %r((\#.*?)$) do |comment|
|
29
|
+
if comment.gsub(%r(<q>(.*?)</q>), "\\1") =~ %r(</q>)
|
30
|
+
comment
|
31
|
+
else
|
32
|
+
comment.gsub! %r(</?(b|i|em|var|code)>), ""
|
33
|
+
%Q(<i>#{comment}</i>)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
code.gsub!('<i><i>', '<i>')
|
37
|
+
"hallo" +code
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
MiniSyntax.register(:ruby, MiniSyntax::Highlighter::Ruby)
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module MiniSyntax
|
2
|
+
module Highlighter
|
3
|
+
module YAML
|
4
|
+
def self.highlight(code)
|
5
|
+
code.gsub! /^( )*([a-z_-]+:)/, "\\1<b>\\2</b>"
|
6
|
+
code.gsub! /(\#([^\{].*?)?)\n/, "<i>\\1</i>\n"
|
7
|
+
code
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
MiniSyntax.register(:yaml, MiniSyntax::Highlighter::YAML)
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'rack/utils'
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
# “Inspired” by Wlodek Bzyl’s rack-codehighlighter:
|
5
|
+
# https://github.com/wbzyl/rack-codehighlighter
|
6
|
+
# Thanks!
|
7
|
+
|
8
|
+
module Rack
|
9
|
+
class MiniSyntax
|
10
|
+
include Rack::Utils
|
11
|
+
|
12
|
+
def initialize(app, options = {})
|
13
|
+
@app = app
|
14
|
+
@options = {
|
15
|
+
:element => 'code',
|
16
|
+
:pattern => /^##+\s*([-_\w\+ ]+)[\s#]*(\n|
|$)/i
|
17
|
+
}
|
18
|
+
@options.merge! options
|
19
|
+
end
|
20
|
+
|
21
|
+
def call(env)
|
22
|
+
status, headers, response = @app.call(env)
|
23
|
+
headers = HeaderHash.new(headers)
|
24
|
+
|
25
|
+
if !STATUS_WITH_NO_ENTITY_BODY.include?(status) and
|
26
|
+
!headers['transfer-encoding'] and
|
27
|
+
headers['content-type'] and
|
28
|
+
headers['content-type'].include?("text/html")
|
29
|
+
|
30
|
+
content = ""
|
31
|
+
response.each { |part| content += part }
|
32
|
+
doc = Nokogiri::HTML(content, nil, 'UTF-8')
|
33
|
+
nodes = doc.search(@options[:element])
|
34
|
+
nodes.each do |node|
|
35
|
+
code = node.inner_html || ''
|
36
|
+
node.swap(highlight(code, node.name))
|
37
|
+
end
|
38
|
+
|
39
|
+
body = doc.to_html
|
40
|
+
headers['content-length'] = bytesize(body).to_s
|
41
|
+
|
42
|
+
[status, headers, [body]]
|
43
|
+
else
|
44
|
+
[status, headers, response]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def highlight(code, tag_name)
|
50
|
+
if code =~ @options[:pattern]
|
51
|
+
lang = $1
|
52
|
+
code.gsub! @options[:pattern], ''
|
53
|
+
if code =~ /^([ \t]+)/
|
54
|
+
code.gsub! /^#{$1}/, ''
|
55
|
+
end
|
56
|
+
code = ::MiniSyntax.highlight(code, lang)
|
57
|
+
end
|
58
|
+
%Q(<#{tag_name}>#{code}</#{tag_name}>)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/minisyntax.rb
CHANGED
@@ -2,18 +2,18 @@ module MiniSyntax
|
|
2
2
|
module Version
|
3
3
|
MAJOR = 0
|
4
4
|
MINOR = 1
|
5
|
-
PATCH =
|
6
|
-
BUILD =
|
5
|
+
PATCH = 1
|
6
|
+
BUILD = nil
|
7
7
|
|
8
8
|
STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
@@languages = {}
|
12
|
-
|
12
|
+
|
13
13
|
def self.register(lang, lang_module)
|
14
14
|
@@languages[lang] = lang_module
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def self.highlight(code, lang)
|
18
18
|
if highlighter = @@languages[lang.to_sym]
|
19
19
|
highlighter.highlight(code)
|
@@ -32,4 +32,4 @@ require 'minisyntax/highlighter'
|
|
32
32
|
|
33
33
|
if defined? Rack
|
34
34
|
require 'minisyntax/integration/rack'
|
35
|
-
end
|
35
|
+
end
|
data/minisyntax.gemspec
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "minisyntax"
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Nico Hagenburger"]
|
12
|
+
s.date = "2012-12-06"
|
13
|
+
s.description = "A simple powerful syntax highlighter with minimal HTML output"
|
14
|
+
s.email = "nico@hagenburger.net"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".document",
|
20
|
+
".rspec",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"MIT-LICENSE.txt",
|
24
|
+
"README.md",
|
25
|
+
"Rakefile",
|
26
|
+
"lib/minisyntax.rb",
|
27
|
+
"lib/minisyntax/highlighter.rb",
|
28
|
+
"lib/minisyntax/highlighter/bash.rb",
|
29
|
+
"lib/minisyntax/highlighter/command_line.rb",
|
30
|
+
"lib/minisyntax/highlighter/css.rb",
|
31
|
+
"lib/minisyntax/highlighter/erb.rb",
|
32
|
+
"lib/minisyntax/highlighter/haml.rb",
|
33
|
+
"lib/minisyntax/highlighter/html.rb",
|
34
|
+
"lib/minisyntax/highlighter/javascript.rb",
|
35
|
+
"lib/minisyntax/highlighter/php.rb",
|
36
|
+
"lib/minisyntax/highlighter/ruby.rb",
|
37
|
+
"lib/minisyntax/highlighter/yaml.rb",
|
38
|
+
"lib/minisyntax/integration/rack.rb",
|
39
|
+
"minisyntax.gemspec",
|
40
|
+
"spec/minisyntax_spec.rb",
|
41
|
+
"spec/spec_helper.rb"
|
42
|
+
]
|
43
|
+
s.homepage = "http://github.com/hagenburger/minisyntax"
|
44
|
+
s.licenses = ["MIT"]
|
45
|
+
s.require_paths = ["lib"]
|
46
|
+
s.rubygems_version = "1.8.10"
|
47
|
+
s.summary = "A simple powerful syntax highlighter with minimal HTML output"
|
48
|
+
s.test_files = [
|
49
|
+
"spec/minisyntax_spec.rb",
|
50
|
+
"spec/spec_helper.rb"
|
51
|
+
]
|
52
|
+
|
53
|
+
if s.respond_to? :specification_version then
|
54
|
+
s.specification_version = 3
|
55
|
+
|
56
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
57
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
58
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
60
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
61
|
+
s.add_runtime_dependency(%q<nokogiri>, ["~> 1.4.4"])
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
64
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
65
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
66
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
67
|
+
s.add_dependency(%q<nokogiri>, ["~> 1.4.4"])
|
68
|
+
end
|
69
|
+
else
|
70
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
71
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
72
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
73
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
74
|
+
s.add_dependency(%q<nokogiri>, ["~> 1.4.4"])
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minisyntax
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-12-06 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70137403490000 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.3.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70137403490000
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &70137403489520 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 1.0.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70137403489520
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: jeweler
|
38
|
-
requirement: &
|
38
|
+
requirement: &70137403489040 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.5.2
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70137403489040
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rcov
|
49
|
-
requirement: &
|
49
|
+
requirement: &70137403488520 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70137403488520
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: nokogiri
|
60
|
-
requirement: &
|
60
|
+
requirement: &70137403488040 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: 1.4.4
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70137403488040
|
69
69
|
description: A simple powerful syntax highlighter with minimal HTML output
|
70
70
|
email: nico@hagenburger.net
|
71
71
|
executables: []
|
@@ -76,11 +76,26 @@ files:
|
|
76
76
|
- .document
|
77
77
|
- .rspec
|
78
78
|
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- MIT-LICENSE.txt
|
81
|
+
- README.md
|
79
82
|
- Rakefile
|
80
83
|
- lib/minisyntax.rb
|
84
|
+
- lib/minisyntax/highlighter.rb
|
85
|
+
- lib/minisyntax/highlighter/bash.rb
|
86
|
+
- lib/minisyntax/highlighter/command_line.rb
|
87
|
+
- lib/minisyntax/highlighter/css.rb
|
88
|
+
- lib/minisyntax/highlighter/erb.rb
|
89
|
+
- lib/minisyntax/highlighter/haml.rb
|
90
|
+
- lib/minisyntax/highlighter/html.rb
|
91
|
+
- lib/minisyntax/highlighter/javascript.rb
|
92
|
+
- lib/minisyntax/highlighter/php.rb
|
93
|
+
- lib/minisyntax/highlighter/ruby.rb
|
94
|
+
- lib/minisyntax/highlighter/yaml.rb
|
95
|
+
- lib/minisyntax/integration/rack.rb
|
96
|
+
- minisyntax.gemspec
|
81
97
|
- spec/minisyntax_spec.rb
|
82
98
|
- spec/spec_helper.rb
|
83
|
-
- README.md
|
84
99
|
homepage: http://github.com/hagenburger/minisyntax
|
85
100
|
licenses:
|
86
101
|
- MIT
|
@@ -94,6 +109,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
109
|
- - ! '>='
|
95
110
|
- !ruby/object:Gem::Version
|
96
111
|
version: '0'
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
hash: -1766550032169555269
|
97
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
116
|
none: false
|
99
117
|
requirements:
|