hljs 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +8 -8
  2. data/README.md +11 -1
  3. data/lib/hljs.rb +17 -5
  4. data/lib/hljs/adapter.rb +15 -4
  5. data/lib/hljs/adapters/highlight_js.rb +3 -3
  6. data/lib/hljs/adapters/syntax_highlighter.rb +25 -0
  7. data/lib/hljs/version.rb +1 -1
  8. data/spec/fixtures/SyntaxHighlighter/html.xml.html +1 -0
  9. data/spec/fixtures/SyntaxHighlighter/javascript.js.html +1 -0
  10. data/spec/fixtures/SyntaxHighlighter/python.py.html +1 -0
  11. data/spec/fixtures/SyntaxHighlighter/ruby.rb.html +1 -0
  12. data/spec/fixtures/highlight.js/{output/output.xml.html → html.xml.html} +0 -0
  13. data/spec/fixtures/highlight.js/{output/output.js.html → javascript.js.html} +0 -0
  14. data/spec/fixtures/highlight.js/{output/output.py.html → python.py.html} +0 -0
  15. data/spec/fixtures/highlight.js/{output/output.rb.html → ruby.rb.html} +0 -0
  16. data/spec/fixtures/{highlight.js/input/input.xml → input/html.xml} +0 -0
  17. data/spec/fixtures/{highlight.js/input/input.js → input/javascript.js} +0 -0
  18. data/spec/fixtures/{highlight.js/input/input.py → input/python.py} +0 -0
  19. data/spec/fixtures/{highlight.js/input/input.rb → input/ruby.rb} +0 -0
  20. data/spec/higlight_js_spec.rb +24 -9
  21. data/spec/hljs_spec.rb +25 -15
  22. data/spec/shared_examples_for_adapter.rb +14 -0
  23. data/spec/spec_helper.rb +9 -6
  24. data/spec/syntax_highlighter_spec.rb +24 -0
  25. data/vendor/SyntaxHighlighter/shBrushAS3.js +59 -0
  26. data/vendor/SyntaxHighlighter/shBrushAppleScript.js +75 -0
  27. data/vendor/SyntaxHighlighter/shBrushBash.js +59 -0
  28. data/vendor/SyntaxHighlighter/shBrushCSharp.js +65 -0
  29. data/vendor/SyntaxHighlighter/shBrushColdFusion.js +100 -0
  30. data/vendor/SyntaxHighlighter/shBrushCpp.js +97 -0
  31. data/vendor/SyntaxHighlighter/shBrushCss.js +91 -0
  32. data/vendor/SyntaxHighlighter/shBrushDelphi.js +55 -0
  33. data/vendor/SyntaxHighlighter/shBrushDiff.js +41 -0
  34. data/vendor/SyntaxHighlighter/shBrushErlang.js +52 -0
  35. data/vendor/SyntaxHighlighter/shBrushGroovy.js +67 -0
  36. data/vendor/SyntaxHighlighter/shBrushJScript.js +52 -0
  37. data/vendor/SyntaxHighlighter/shBrushJava.js +57 -0
  38. data/vendor/SyntaxHighlighter/shBrushJavaFX.js +58 -0
  39. data/vendor/SyntaxHighlighter/shBrushPerl.js +72 -0
  40. data/vendor/SyntaxHighlighter/shBrushPhp.js +88 -0
  41. data/vendor/SyntaxHighlighter/shBrushPlain.js +33 -0
  42. data/vendor/SyntaxHighlighter/shBrushPowerShell.js +74 -0
  43. data/vendor/SyntaxHighlighter/shBrushPython.js +64 -0
  44. data/vendor/SyntaxHighlighter/shBrushRuby.js +55 -0
  45. data/vendor/SyntaxHighlighter/shBrushSass.js +94 -0
  46. data/vendor/SyntaxHighlighter/shBrushScala.js +51 -0
  47. data/vendor/SyntaxHighlighter/shBrushSql.js +66 -0
  48. data/vendor/SyntaxHighlighter/shBrushVb.js +56 -0
  49. data/vendor/SyntaxHighlighter/shBrushXml.js +69 -0
  50. data/vendor/SyntaxHighlighter/shCore.js +17 -0
  51. metadata +57 -18
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjA4OTU4ODYxMDVjNmQxYWJjZTY4MWU3NGE3ZjY4OGViZTBmZmQzZA==
4
+ OTY3N2FjODA4YjU0MDI4ZTA5Y2M5ZWZjMDFiMmRhNWEwN2E0NzZiYw==
5
5
  data.tar.gz: !binary |-
6
- MDdjZDk1MjgyZGRlNDZmZTY5MWM3OGJmNGYzYTQ2MDY3MTJjODYxOA==
6
+ OGY2YTkwMTM4ZTE5NjQ5YzEwNjZhNzM5MWZmZDdlOGUxNjg2MTU4NA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZTFhMGExYzBmZjJhZTg4YWUyNzc1NWU4NTEyY2YxMzU0ZjhkN2E5Nzk5ZGM1
10
- MTcyNzk4NTg3MGM2ZjNmNGU2MmFhMzFjOWRhNWVhZWQ2ZTJmMzAxNDlhMzMz
11
- N2E3MGQ0YjExYjg0MTEyYWEwMzE0NGE2MWZkNDQzZWJjOTdmYmU=
9
+ MGEzMWQ3OGViNGNiOWU2NmJhOTI4ZGFhNGJmYmUzOWI5MThiZTgyOWY4Njk2
10
+ MjA3NWFlNDQ2NWVjNzE1MGVhN2JkZGE0YTJjYzlhZTRkNzUwYjg0Zjc4YzYy
11
+ ZTVjNzY5YTczZTI2MTcyZjRkY2QyNWE4MzA1YTg0NDgwYjUwYjA=
12
12
  data.tar.gz: !binary |-
13
- ZjA5NzllZWIxZGRkOTE0YTJiYjQ3MDcxYzVjMjhlOTE1M2ZmMmY4YWFkN2Ux
14
- MzgyODI0N2E3MTM3MTA2YjFkMTc3MTcwODI5ZjZiZDQxZGFiMzJiNjkyYjQy
15
- N2YzNWMxNTk3ZjI1MWE3MDRiYWQzODEwNGZjOTAzMDQ0NDZkMjY=
13
+ OGNhZGYwNDhkMGEyNzRmYjZkZTJjOTNhZTM4M2JmODRkYmZkYzBiN2ZkZWE4
14
+ M2EwZTRlZjkyZDA2YTk2NTJmZTA4YjU0NWVhOTQ5YWMzMGI4NDFhNTUyNjll
15
+ OTNmY2MwYTgxYzI2MDVlYWExMzc2MDcxMmYyNWY5ZWMzMjM4NzE=
data/README.md CHANGED
@@ -21,13 +21,23 @@ Or install it yourself as:
21
21
 
22
22
  ## Usage
23
23
 
24
- For now only highlight.js adapter is supported.
24
+ For now only two adapters are supported: [highlight.js](https://github.com/isagalaev/highlight.js)
25
+ and [SyntaxHighlighter](https://github.com/alexgorbatchev/SyntaxHighlighter).
25
26
 
26
27
  ```ruby
27
28
  HLJS.highlight source, language # => highlighted source
28
29
  HLJS.supported_syntaxes # => the list of all supported syntaxes
29
30
  ```
30
31
 
32
+ Swithing adapters is quite straightforward:
33
+
34
+ ```ruby
35
+ >> HLJS.adapter = :highlightjs
36
+ => #<HLJS::Adapters::HighlightJS:0x007ffb5a0a0a48 Version 7.3, 54 syntaxes supported>
37
+ >> HLJS.adapter = :syntaxhighlighter
38
+ => #<HLJS::Adapters::SyntaxHighlighter:0x007fb35448a990 Version 3.0.83, 25 syntaxes supported>
39
+ ```
40
+
31
41
  ## Contributing
32
42
 
33
43
  1. Fork it
@@ -5,15 +5,27 @@ module HLJS
5
5
  class UnsupportedSyntaxError < SyntaxError; end
6
6
 
7
7
  extend Forwardable
8
+ def_delegators :adapter, :highlight, :supported_syntaxes
8
9
 
9
10
  def adapter
10
- @adapter ||= begin
11
+ self.adapter = :highlightjs unless @adapter
12
+ @adapter
13
+ end
14
+
15
+ def adapter=(new_adapter)
16
+ @adapter = case new_adapter.to_s
17
+ when /highlight[\s_]?js/i
11
18
  require "hljs/adapters/highlight_js"
12
- Adapters::HighlightJS.new
19
+ Adapters::HighlightJS.instance
20
+ when /syntax[\s_]?highlighter/i
21
+ require "hljs/adapters/syntax_highlighter"
22
+ Adapters::SyntaxHighlighter.instance
23
+ else
24
+ raise ArgumentError, "invalid adapter #{new_adapter.inspect}"
13
25
  end
14
- end
15
26
 
16
- def_delegators :adapter, :highlight, :supported_syntaxes
27
+ @adapter
28
+ end
17
29
 
18
- module_function :adapter, :highlight, :supported_syntaxes
30
+ module_function :adapter, :adapter=, :highlight, :supported_syntaxes
19
31
  end
@@ -1,23 +1,34 @@
1
1
  require "execjs"
2
+ require "singleton"
2
3
 
3
4
  module HLJS
4
5
  class Adapter
6
+ include Singleton
5
7
 
6
8
  def inspect
7
9
  additions = "Version #{version}, #{supported_syntaxes.count} syntaxes supported"
8
10
  to_s.sub(/>\z/, " #{additions}>")
9
11
  end
10
12
 
13
+ def supported_syntaxes
14
+ @supported_syntaxes ||= yield.sort
15
+ end
16
+
11
17
  private
12
18
 
13
- def self.source_file(path)
14
- define_method :source_path do
15
- File.expand_path("../../../vendor/#{path}", __FILE__)
19
+ def self.source_paths(*patterns)
20
+ define_method :source_paths do
21
+ base_path = File.expand_path("../../../vendor", __FILE__)
22
+ patterns.map{ |pattern| Dir["#{base_path}/#{pattern}"] }.flatten.uniq
16
23
  end
17
24
  end
18
25
 
19
26
  def source
20
- File.read(source_path)
27
+ @source ||= String.new.tap do |source|
28
+ source_paths.each do |path|
29
+ source << File.read(path)
30
+ end
31
+ end
21
32
  end
22
33
 
23
34
  def context
@@ -3,7 +3,7 @@ require 'hljs/adapter'
3
3
  module HLJS
4
4
  module Adapters
5
5
  class HighlightJS < Adapter
6
- source_file "highlight.js"
6
+ source_paths "highlight.js"
7
7
 
8
8
  def highlight(code, lang=nil)
9
9
  args = [code]
@@ -19,11 +19,11 @@ module HLJS
19
19
  end
20
20
 
21
21
  def supported_syntaxes
22
- @supported_languages ||= context.eval("Object.keys(hljs.LANGUAGES)")
22
+ super{ context.eval("Object.keys(hljs.LANGUAGES)") }
23
23
  end
24
24
 
25
25
  def version
26
- '7.3'
26
+ "7.3"
27
27
  end
28
28
 
29
29
  end
@@ -0,0 +1,25 @@
1
+ require 'hljs/adapter'
2
+
3
+ module HLJS
4
+ module Adapters
5
+ class SyntaxHighlighter < Adapter
6
+
7
+ source_paths "SyntaxHighlighter/shCore.js", "SyntaxHighlighter/shBrush*.js"
8
+
9
+ def highlight(code, lang)
10
+ check_if_syntax_supported! lang
11
+ func = "(function(code){ var a = new SyntaxHighlighter.brushes.#{lang}; a.init(); return a.getHtml(code); })"
12
+ context.call func, code
13
+ end
14
+
15
+ def supported_syntaxes
16
+ super{ context.eval("Object.keys(SyntaxHighlighter.brushes)") }
17
+ end
18
+
19
+ def version
20
+ source[/@version\n[\s*]+([\d.]+)/, 1]
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module HLJS
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
@@ -0,0 +1 @@
1
+ <div id="highlighter_1337" class="syntaxhighlighter "><div class="toolbar"><span><a href="#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="plain">&lt;!DOCTYPE html></code></div><div class="line number2 index1 alt1"><code class="plain">&lt;</code><code class="keyword">title</code><code class="plain">>Title&lt;/</code><code class="keyword">title</code><code class="plain">></code></div><div class="line number3 index2 alt2">&nbsp;</div><div class="line number4 index3 alt1"><code class="plain">&lt;</code><code class="keyword">style</code><code class="plain">>body {width: 500px;}&lt;/</code><code class="keyword">style</code><code class="plain">></code></div><div class="line number5 index4 alt2">&nbsp;</div><div class="line number6 index5 alt1"><code class="plain">&lt;</code><code class="keyword">script</code> <code class="color1">type</code><code class="plain">=</code><code class="string">"application/javascript"</code><code class="plain">></code></div><div class="line number7 index6 alt2"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="plain">function $init() {return true;}</code></div><div class="line number8 index7 alt1"><code class="plain">&lt;/</code><code class="keyword">script</code><code class="plain">></code></div><div class="line number9 index8 alt2">&nbsp;</div><div class="line number10 index9 alt1"><code class="plain">&lt;</code><code class="keyword">body</code><code class="plain">></code></div><div class="line number11 index10 alt2"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="plain">&lt;</code><code class="keyword">p</code> <code class="plain">checked </code><code class="color1">class</code><code class="plain">=</code><code class="string">"title"</code> <code class="color1">id</code><code class="plain">=</code><code class="string">'title'</code><code class="plain">>Title&lt;/</code><code class="keyword">p</code><code class="plain">></code></div><div class="line number12 index11 alt1"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="comments">&lt;!-- here goes the rest of the page --></code></div><div class="line number13 index12 alt2"><code class="plain">&lt;/</code><code class="keyword">body</code><code class="plain">></code></div></div></td></tr></tbody></table></div>
@@ -0,0 +1 @@
1
+ <div id="highlighter_1337" class="syntaxhighlighter "><div class="toolbar"><span><a href="#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="keyword">function</code> <code class="plain">$initHighlight(block, flags) {</code></div><div class="line number2 index1 alt1"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="keyword">try</code> <code class="plain">{</code></div><div class="line number3 index2 alt2"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="keyword">if</code> <code class="plain">(block.className.search(/\bno\-highlight\b/) != -1)</code></div><div class="line number4 index3 alt1"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="keyword">return</code> <code class="plain">processBlock(block, </code><code class="keyword">true</code><code class="plain">, 0x0F) + </code><code class="string">' class=""'</code><code class="plain">;</code></div><div class="line number5 index4 alt2"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="plain">} </code><code class="keyword">catch</code> <code class="plain">(e) {</code></div><div class="line number6 index5 alt1"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="comments">/* handle exception */</code></div><div class="line number7 index6 alt2">&nbsp;</div><div class="line number8 index7 alt1"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="keyword">var</code> <code class="plain">e4x =</code></div><div class="line number9 index8 alt2"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="plain">&lt;div>Example</code></div><div class="line number10 index9 alt1"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="plain">&lt;p>1234&lt;/p>&lt;/div>;</code></div><div class="line number11 index10 alt2"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="plain">}</code></div><div class="line number12 index11 alt1"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">var</code> <code class="plain">i = 0 / 2; i &lt; classes.length; i++) { </code><code class="comments">// "0 / 2" should not be parsed as regexp</code></div><div class="line number13 index12 alt2"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="keyword">if</code> <code class="plain">(checkCondition(classes[i]) === undefined)</code></div><div class="line number14 index13 alt1"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="keyword">return</code> <code class="plain">/\d+/g;</code></div><div class="line number15 index14 alt2"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="plain">}</code></div><div class="line number16 index15 alt1"><code class="plain">}</code></div></div></td></tr></tbody></table></div>
@@ -0,0 +1 @@
1
+ <div id="highlighter_1337" class="syntaxhighlighter "><div class="toolbar"><span><a href="#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="decorator">@requires_authorization</code></div><div class="line number2 index1 alt1"><code class="keyword">def</code> <code class="plain">somefunc(param1</code><code class="keyword">=</code><code class="plain">'', param2</code><code class="keyword">=</code><code class="value">0</code><code class="plain">):</code></div><div class="line number3 index2 alt2"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="plain">r</code><code class="comments">'''A docstring'''</code></div><div class="line number4 index3 alt1"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="keyword">if</code> <code class="plain">param1 > param2: </code><code class="comments"># interesting</code></div><div class="line number5 index4 alt2"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="functions">print</code> <code class="string">'Gre\'ater'</code></div><div class="line number6 index5 alt1"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="keyword">return</code> <code class="plain">(param2 </code><code class="keyword">-</code> <code class="plain">param1 </code><code class="keyword">+</code> <code class="value">1</code><code class="plain">) </code><code class="keyword">or</code> <code class="color1">None</code></div><div class="line number7 index6 alt2">&nbsp;</div><div class="line number8 index7 alt1"><code class="keyword">class</code> <code class="plain">SomeClass:</code></div><div class="line number9 index8 alt2"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="keyword">pass</code></div><div class="line number10 index9 alt1">&nbsp;</div><div class="line number11 index10 alt2"><code class="plain">>>> message </code><code class="keyword">=</code> <code class="comments">'''interpreter</code></div><div class="line number12 index11 alt1"><code class="comments">... prompt'''</code></div></div></td></tr></tbody></table></div>
@@ -0,0 +1 @@
1
+ <div id="highlighter_1337" class="syntaxhighlighter "><div class="toolbar"><span><a href="#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="keyword">class</code> <code class="constants">A</code> <code class="plain">&lt; </code><code class="constants">B</code><code class="plain">; </code><code class="keyword">def</code> <code class="keyword">self</code><code class="plain">.create(object = User) object </code><code class="keyword">end</code> <code class="keyword">end</code></div><div class="line number2 index1 alt1"><code class="keyword">class</code> <code class="plain">Zebra; </code><code class="keyword">def</code> <code class="plain">inspect; </code><code class="string">"X#{2 + self.object_id}"</code> <code class="keyword">end</code> <code class="keyword">end</code></div><div class="line number3 index2 alt2">&nbsp;</div><div class="line number4 index3 alt1"><code class="keyword">module</code> <code class="constants">ABC</code><code class="plain">::</code><code class="constants">DEF</code></div><div class="line number5 index4 alt2"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="plain">include Comparable</code></div><div class="line number6 index5 alt1">&nbsp;</div><div class="line number7 index6 alt2"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="comments"># @param test</code></div><div class="line number8 index7 alt1"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="comments"># @return [String] nothing</code></div><div class="line number9 index8 alt2"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="keyword">def</code> <code class="plain">foo(test)</code></div><div class="line number10 index9 alt1"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="color1">Thread</code><code class="plain">.</code><code class="keyword">new</code> <code class="keyword">do</code> <code class="plain">|blockvar|</code></div><div class="line number11 index10 alt2"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="constants">ABC</code><code class="plain">::</code><code class="constants">DEF</code><code class="plain">.reverse(</code><code class="color2">:a_symbol</code><code class="plain">, :</code><code class="string">'a symbol'</code><code class="plain">, :&lt;=>, </code><code class="string">'test'</code> <code class="plain">+ test)</code></div><div class="line number12 index11 alt1"><code class="undefined spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="keyword">end</code><code class="plain">.join</code></div><div class="line number13 index12 alt2"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="keyword">end</code></div><div class="line number14 index13 alt1">&nbsp;</div><div class="line number15 index14 alt2"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="keyword">def</code> <code class="plain">[](index) </code><code class="keyword">self</code><code class="plain">[index] </code><code class="keyword">end</code></div><div class="line number16 index15 alt1"><code class="undefined spaces">&nbsp;&nbsp;</code><code class="keyword">def</code> <code class="plain">==(other) other == </code><code class="keyword">self</code> <code class="keyword">end</code></div><div class="line number17 index16 alt2"><code class="keyword">end</code></div><div class="line number18 index17 alt1">&nbsp;</div><div class="line number19 index18 alt2"><code class="plain">anIdentifier = an_identifier</code></div><div class="line number20 index19 alt1"><code class="plain">Constant = </code><code class="constants">1</code></div><div class="line number21 index20 alt2"><code class="plain">render action: </code><code class="color2">:new</code></div></div></td></tr></tbody></table></div>
@@ -1,17 +1,32 @@
1
1
  require "spec_helper"
2
+ require "shared_examples_for_adapter"
2
3
  require "hljs/adapters/highlight_js"
3
4
 
4
5
  describe HLJS::Adapters::HighlightJS do
5
- assert_highlighted "highlight.js", :xml
6
- assert_highlighted "highlight.js", :py, :python
7
- assert_highlighted "highlight.js", :rb, :ruby
8
- assert_highlighted "highlight.js", :js, :javascript
6
+ subject{ described_class.instance }
9
7
 
10
- its(:inspect){ should include("Version 7.3, 54 syntaxes supported") }
8
+ it_behaves_like "an adapter"
11
9
 
12
- it "raises an error when syntax is unsupported" do
13
- broken_hl_call = ->{ subject.highlight "foo", "bar" }
14
- expected_error = [ HLJS::UnsupportedSyntaxError, "\"bar\" syntax is not supported" ]
15
- expect(&broken_hl_call).to raise_error(*expected_error)
10
+ assert_highlighted "highlight.js", "html.xml", "xml"
11
+ assert_highlighted "highlight.js", "python.py", "python"
12
+ assert_highlighted "highlight.js", "ruby.rb", "ruby"
13
+ assert_highlighted "highlight.js", "javascript.js", "javascript"
14
+
15
+ syntaxes = %w(1c actionscript apache applescript avrasm axapta bash brainfuck
16
+ clojure cmake coffeescript cpp cs css d delphi diff django dos erlang
17
+ erlang-repl glsl go haskell http ini java javascript json lisp lua markdown
18
+ matlab mel nginx objectivec parser3 perl php profile python r rib rsl ruby
19
+ rust scala smalltalk sql tex vala vbscript vhdl xml)
20
+
21
+ it "supports 54 syntaxes" do
22
+ expect(subject.supported_syntaxes).to eq(syntaxes)
23
+ end
24
+
25
+ its(:version){ should eq("7.3") }
26
+
27
+ it "tries to guess the syntax if argument is not provided" do
28
+ code = %{var foo = function(){ return "bar"; };}
29
+ highlighted = %{<span class="keyword">var</span> foo = <span class="keyword">function</span>(){ <span class="keyword">return</span> <span class="string">"bar"</span>; };}
30
+ expect(subject.highlight(code)).to eq(highlighted)
16
31
  end
17
32
  end
@@ -1,29 +1,39 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe HLJS do
4
- describe "#adapter" do
5
- it "should be HighlightJS" do
6
- expect(described_class.adapter).to be_kind_of(described_class::Adapters::HighlightJS)
7
- end
4
+ it "delegates #highlight to adapter" do
5
+ expect(described_class.adapter).to receive(:highlight).with("foo", "bar")
6
+ described_class.highlight "foo", "bar"
8
7
  end
9
8
 
10
- describe do
11
- let(:adapter){ double("Adapter") }
9
+ it "delegates #supported_syntaxes to adapter" do
10
+ expect(described_class.adapter).to receive(:supported_syntaxes).once
11
+ described_class.supported_syntaxes
12
+ end
12
13
 
13
- before{ described_class.stub adapter: adapter }
14
+ describe "#adapter=" do
15
+ args = ["highlightjs", "highlight_js", "highlight js", "HIGHLIGHTJS",
16
+ :highlightjs, :highlight_js]
14
17
 
15
- describe "#highlight" do
16
- it "delegates to adapter" do
17
- expect(adapter).to receive(:highlight).with("foo", "bar")
18
- described_class.highlight "foo", "bar"
18
+ args.each do |arg|
19
+ it "should enable HighlightJS with #{arg.inspect}" do
20
+ described_class.adapter = arg
21
+ expect(described_class.adapter).to be_instance_of(described_class::Adapters::HighlightJS)
19
22
  end
20
23
  end
21
24
 
22
- describe "#supported_syntaxes" do
23
- it "delegates to adapter" do
24
- expect(adapter).to receive(:supported_syntaxes).once
25
- described_class.supported_syntaxes
25
+ args = ["SyntaxHighlighter", "Syntax Highlighter", "syntax_highlighter",
26
+ :syntax_highlighter, :syntaxhighlighter, :SyntaxHighlighter]
27
+
28
+ args.each do |arg|
29
+ it "should enable SyntaxHighlighter with #{arg.inspect}" do
30
+ described_class.adapter = arg
31
+ expect(described_class.adapter).to be_instance_of(described_class::Adapters::SyntaxHighlighter)
26
32
  end
27
33
  end
34
+
35
+ it "raises an ArgumentError on invalid input" do
36
+ expect{HLJS.adapter=:foo}.to raise_error(ArgumentError, "invalid adapter :foo")
37
+ end
28
38
  end
29
39
  end
@@ -0,0 +1,14 @@
1
+ shared_examples_for "an adapter" do
2
+ it "raises an error when syntax is unsupported" do
3
+ broken_hl_call = ->{ subject.highlight "foo", "bar" }
4
+ expected_error = [ HLJS::UnsupportedSyntaxError, "\"bar\" syntax is not supported" ]
5
+ expect(&broken_hl_call).to raise_error(*expected_error)
6
+ end
7
+
8
+ it "inspect should include version and syntaxes info" do
9
+ version = subject.version
10
+ syntaxes = subject.supported_syntaxes.count
11
+
12
+ expect(subject.inspect).to include("Version #{version}, #{syntaxes} syntaxes supported")
13
+ end
14
+ end
@@ -6,14 +6,17 @@ RSpec.configure do |config|
6
6
  end
7
7
  end
8
8
 
9
- def assert_highlighted(adapter, extension, lang=extension)
10
- it "highlights #{lang} correctly" do
11
- path = File.expand_path("../fixtures/#{adapter}", __FILE__)
12
- source_path = File.join(path, "input/input.#{extension}")
13
- output_path = File.join(path, "output/output.#{extension}.html")
9
+ def assert_highlighted(adapter, filename, lang)
10
+ it "highlights #{lang}" do
11
+ path = File.expand_path("../fixtures", __FILE__)
12
+ source_path = File.join(path, "input", filename)
13
+ output_path = File.join(path, adapter, "#{filename}.html")
14
14
  source = File.read(source_path)
15
15
  output = File.read(output_path)
16
16
 
17
- expect(subject.highlight(source, lang)).to eq(output)
17
+ highlighted = subject.highlight(source, lang)
18
+ highlighted.sub! /highlighter_\d+/, "highlighter_1337"
19
+
20
+ expect(highlighted).to eq(output)
18
21
  end
19
22
  end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+ require "shared_examples_for_adapter"
3
+ require "hljs/adapters/syntax_highlighter"
4
+
5
+ describe HLJS::Adapters::SyntaxHighlighter do
6
+ subject{ described_class.instance }
7
+
8
+ it_behaves_like "an adapter"
9
+
10
+ assert_highlighted "SyntaxHighlighter", "html.xml", "Xml"
11
+ assert_highlighted "SyntaxHighlighter", "python.py", "Python"
12
+ assert_highlighted "SyntaxHighlighter", "ruby.rb", "Ruby"
13
+ assert_highlighted "SyntaxHighlighter", "javascript.js", "JScript"
14
+
15
+ syntaxes = %w(AS3 AppleScript Bash CSS CSharp ColdFusion Cpp Delphi Diff
16
+ Erlang Groovy JScript Java JavaFX Perl Php Plain PowerShell Python Ruby
17
+ Sass Scala Sql Vb Xml)
18
+
19
+ it "supports 25 syntaxes" do
20
+ expect(subject.supported_syntaxes).to eq(syntaxes)
21
+ end
22
+
23
+ its(:version){ should eq("3.0.83") }
24
+ end
@@ -0,0 +1,59 @@
1
+ /**
2
+ * SyntaxHighlighter
3
+ * http://alexgorbatchev.com/SyntaxHighlighter
4
+ *
5
+ * SyntaxHighlighter is donationware. If you are using it, please donate.
6
+ * http://alexgorbatchev.com/SyntaxHighlighter/donate.html
7
+ *
8
+ * @version
9
+ * 3.0.83 (July 02 2010)
10
+ *
11
+ * @copyright
12
+ * Copyright (C) 2004-2010 Alex Gorbatchev.
13
+ *
14
+ * @license
15
+ * Dual licensed under the MIT and GPL licenses.
16
+ */
17
+ ;(function()
18
+ {
19
+ // CommonJS
20
+ typeof(require) != 'undefined' ? SyntaxHighlighter = require('shCore').SyntaxHighlighter : null;
21
+
22
+ function Brush()
23
+ {
24
+ // Created by Peter Atoria @ http://iAtoria.com
25
+
26
+ var inits = 'class interface function package';
27
+
28
+ var keywords = '-Infinity ...rest Array as AS3 Boolean break case catch const continue Date decodeURI ' +
29
+ 'decodeURIComponent default delete do dynamic each else encodeURI encodeURIComponent escape ' +
30
+ 'extends false final finally flash_proxy for get if implements import in include Infinity ' +
31
+ 'instanceof int internal is isFinite isNaN isXMLName label namespace NaN native new null ' +
32
+ 'Null Number Object object_proxy override parseFloat parseInt private protected public ' +
33
+ 'return set static String super switch this throw true try typeof uint undefined unescape ' +
34
+ 'use void while with'
35
+ ;
36
+
37
+ this.regexList = [
38
+ { regex: SyntaxHighlighter.regexLib.singleLineCComments, css: 'comments' }, // one line comments
39
+ { regex: SyntaxHighlighter.regexLib.multiLineCComments, css: 'comments' }, // multiline comments
40
+ { regex: SyntaxHighlighter.regexLib.doubleQuotedString, css: 'string' }, // double quoted strings
41
+ { regex: SyntaxHighlighter.regexLib.singleQuotedString, css: 'string' }, // single quoted strings
42
+ { regex: /\b([\d]+(\.[\d]+)?|0x[a-f0-9]+)\b/gi, css: 'value' }, // numbers
43
+ { regex: new RegExp(this.getKeywords(inits), 'gm'), css: 'color3' }, // initializations
44
+ { regex: new RegExp(this.getKeywords(keywords), 'gm'), css: 'keyword' }, // keywords
45
+ { regex: new RegExp('var', 'gm'), css: 'variable' }, // variable
46
+ { regex: new RegExp('trace', 'gm'), css: 'color1' } // trace
47
+ ];
48
+
49
+ this.forHtmlScript(SyntaxHighlighter.regexLib.scriptScriptTags);
50
+ };
51
+
52
+ Brush.prototype = new SyntaxHighlighter.Highlighter();
53
+ Brush.aliases = ['actionscript3', 'as3'];
54
+
55
+ SyntaxHighlighter.brushes.AS3 = Brush;
56
+
57
+ // CommonJS
58
+ typeof(exports) != 'undefined' ? exports.Brush = Brush : null;
59
+ })();
@@ -0,0 +1,75 @@
1
+ /**
2
+ * SyntaxHighlighter
3
+ * http://alexgorbatchev.com/SyntaxHighlighter
4
+ *
5
+ * SyntaxHighlighter is donationware. If you are using it, please donate.
6
+ * http://alexgorbatchev.com/SyntaxHighlighter/donate.html
7
+ *
8
+ * @version
9
+ * 3.0.83 (July 02 2010)
10
+ *
11
+ * @copyright
12
+ * Copyright (C) 2004-2010 Alex Gorbatchev.
13
+ *
14
+ * @license
15
+ * Dual licensed under the MIT and GPL licenses.
16
+ */
17
+ ;(function()
18
+ {
19
+ // CommonJS
20
+ typeof(require) != 'undefined' ? SyntaxHighlighter = require('shCore').SyntaxHighlighter : null;
21
+
22
+ function Brush()
23
+ {
24
+ // AppleScript brush by David Chambers
25
+ // http://davidchambersdesign.com/
26
+ var keywords = 'after before beginning continue copy each end every from return get global in local named of set some that the then times to where whose with without';
27
+ var ordinals = 'first second third fourth fifth sixth seventh eighth ninth tenth last front back middle';
28
+ var specials = 'activate add alias AppleScript ask attachment boolean class constant delete duplicate empty exists false id integer list make message modal modified new no paragraph pi properties quit real record remove rest result reveal reverse run running save string true word yes';
29
+
30
+ this.regexList = [
31
+
32
+ { regex: /(--|#).*$/gm,
33
+ css: 'comments' },
34
+
35
+ { regex: /\(\*(?:[\s\S]*?\(\*[\s\S]*?\*\))*[\s\S]*?\*\)/gm, // support nested comments
36
+ css: 'comments' },
37
+
38
+ { regex: /"[\s\S]*?"/gm,
39
+ css: 'string' },
40
+
41
+ { regex: /(?:,|:|¬|'s\b|\(|\)|\{|\}|«|\b\w*»)/g,
42
+ css: 'color1' },
43
+
44
+ { regex: /(-)?(\d)+(\.(\d)?)?(E\+(\d)+)?/g, // numbers
45
+ css: 'color1' },
46
+
47
+ { regex: /(?:&(amp;|gt;|lt;)?|=|� |>|<|≥|>=|≤|<=|\*|\+|-|\/|÷|\^)/g,
48
+ css: 'color2' },
49
+
50
+ { regex: /\b(?:and|as|div|mod|not|or|return(?!\s&)(ing)?|equals|(is(n't| not)? )?equal( to)?|does(n't| not) equal|(is(n't| not)? )?(greater|less) than( or equal( to)?)?|(comes|does(n't| not) come) (after|before)|is(n't| not)?( in)? (back|front) of|is(n't| not)? behind|is(n't| not)?( (in|contained by))?|does(n't| not) contain|contain(s)?|(start|begin|end)(s)? with|((but|end) )?(consider|ignor)ing|prop(erty)?|(a )?ref(erence)?( to)?|repeat (until|while|with)|((end|exit) )?repeat|((else|end) )?if|else|(end )?(script|tell|try)|(on )?error|(put )?into|(of )?(it|me)|its|my|with (timeout( of)?|transaction)|end (timeout|transaction))\b/g,
51
+ css: 'keyword' },
52
+
53
+ { regex: /\b\d+(st|nd|rd|th)\b/g, // ordinals
54
+ css: 'keyword' },
55
+
56
+ { regex: /\b(?:about|above|against|around|at|below|beneath|beside|between|by|(apart|aside) from|(instead|out) of|into|on(to)?|over|since|thr(ough|u)|under)\b/g,
57
+ css: 'color3' },
58
+
59
+ { regex: /\b(?:adding folder items to|after receiving|choose( ((remote )?application|color|folder|from list|URL))?|clipboard info|set the clipboard to|(the )?clipboard|entire contents|display(ing| (alert|dialog|mode))?|document( (edited|file|nib name))?|file( (name|type))?|(info )?for|giving up after|(name )?extension|quoted form|return(ed)?|second(?! item)(s)?|list (disks|folder)|text item(s| delimiters)?|(Unicode )?text|(disk )?item(s)?|((current|list) )?view|((container|key) )?window|with (data|icon( (caution|note|stop))?|parameter(s)?|prompt|properties|seed|title)|case|diacriticals|hyphens|numeric strings|punctuation|white space|folder creation|application(s( folder)?| (processes|scripts position|support))?|((desktop )?(pictures )?|(documents|downloads|favorites|home|keychain|library|movies|music|public|scripts|sites|system|users|utilities|workflows) )folder|desktop|Folder Action scripts|font(s| panel)?|help|internet plugins|modem scripts|(system )?preferences|printer descriptions|scripting (additions|components)|shared (documents|libraries)|startup (disk|items)|temporary items|trash|on server|in AppleTalk zone|((as|long|short) )?user name|user (ID|locale)|(with )?password|in (bundle( with identifier)?|directory)|(close|open for) access|read|write( permission)?|(g|s)et eof|using( delimiters)?|starting at|default (answer|button|color|country code|entr(y|ies)|identifiers|items|name|location|script editor)|hidden( answer)?|open(ed| (location|untitled))?|error (handling|reporting)|(do( shell)?|load|run|store) script|administrator privileges|altering line endings|get volume settings|(alert|boot|input|mount|output|set) volume|output muted|(fax|random )?number|round(ing)?|up|down|toward zero|to nearest|as taught in school|system (attribute|info)|((AppleScript( Studio)?|system) )?version|(home )?directory|(IPv4|primary Ethernet) address|CPU (type|speed)|physical memory|time (stamp|to GMT)|replacing|ASCII (character|number)|localized string|from table|offset|summarize|beep|delay|say|(empty|multiple) selections allowed|(of|preferred) type|invisibles|showing( package contents)?|editable URL|(File|FTP|News|Media|Web) [Ss]ervers|Telnet hosts|Directory services|Remote applications|waiting until completion|saving( (in|to))?|path (for|to( (((current|frontmost) )?application|resource))?)|POSIX (file|path)|(background|RGB) color|(OK|cancel) button name|cancel button|button(s)?|cubic ((centi)?met(re|er)s|yards|feet|inches)|square ((kilo)?met(re|er)s|miles|yards|feet)|(centi|kilo)?met(re|er)s|miles|yards|feet|inches|lit(re|er)s|gallons|quarts|(kilo)?grams|ounces|pounds|degrees (Celsius|Fahrenheit|Kelvin)|print( (dialog|settings))?|clos(e(able)?|ing)|(de)?miniaturized|miniaturizable|zoom(ed|able)|attribute run|action (method|property|title)|phone|email|((start|end)ing|home) page|((birth|creation|current|custom|modification) )?date|((((phonetic )?(first|last|middle))|computer|host|maiden|related) |nick)?name|aim|icq|jabber|msn|yahoo|address(es)?|save addressbook|should enable action|city|country( code)?|formatte(r|d address)|(palette )?label|state|street|zip|AIM [Hh]andle(s)?|my card|select(ion| all)?|unsaved|(alpha )?value|entr(y|ies)|group|(ICQ|Jabber|MSN) handle|person|people|company|department|icon image|job title|note|organization|suffix|vcard|url|copies|collating|pages (across|down)|request print time|target( printer)?|((GUI Scripting|Script menu) )?enabled|show Computer scripts|(de)?activated|awake from nib|became (key|main)|call method|of (class|object)|center|clicked toolbar item|closed|for document|exposed|(can )?hide|idle|keyboard (down|up)|event( (number|type))?|launch(ed)?|load (image|movie|nib|sound)|owner|log|mouse (down|dragged|entered|exited|moved|up)|move|column|localization|resource|script|register|drag (info|types)|resigned (active|key|main)|resiz(e(d)?|able)|right mouse (down|dragged|up)|scroll wheel|(at )?index|should (close|open( untitled)?|quit( after last window closed)?|zoom)|((proposed|screen) )?bounds|show(n)?|behind|in front of|size (mode|to fit)|update(d| toolbar item)?|was (hidden|miniaturized)|will (become active|close|finish launching|hide|miniaturize|move|open|quit|(resign )?active|((maximum|minimum|proposed) )?size|show|zoom)|bundle|data source|movie|pasteboard|sound|tool(bar| tip)|(color|open|save) panel|coordinate system|frontmost|main( (bundle|menu|window))?|((services|(excluded from )?windows) )?menu|((executable|frameworks|resource|scripts|shared (frameworks|support)) )?path|(selected item )?identifier|data|content(s| view)?|character(s)?|click count|(command|control|option|shift) key down|context|delta (x|y|z)|key( code)?|location|pressure|unmodified characters|types|(first )?responder|playing|(allowed|selectable) identifiers|allows customization|(auto saves )?configuration|visible|image( name)?|menu form representation|tag|user(-| )defaults|associated file name|(auto|needs) display|current field editor|floating|has (resize indicator|shadow)|hides when deactivated|level|minimized (image|title)|opaque|position|release when closed|sheet|title(d)?)\b/g,
60
+ css: 'color3' },
61
+
62
+ { regex: new RegExp(this.getKeywords(specials), 'gm'), css: 'color3' },
63
+ { regex: new RegExp(this.getKeywords(keywords), 'gm'), css: 'keyword' },
64
+ { regex: new RegExp(this.getKeywords(ordinals), 'gm'), css: 'keyword' }
65
+ ];
66
+ };
67
+
68
+ Brush.prototype = new SyntaxHighlighter.Highlighter();
69
+ Brush.aliases = ['applescript'];
70
+
71
+ SyntaxHighlighter.brushes.AppleScript = Brush;
72
+
73
+ // CommonJS
74
+ typeof(exports) != 'undefined' ? exports.Brush = Brush : null;
75
+ })();