gitlab-rouge 1.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +23 -0
  3. data/LICENSE +186 -0
  4. data/bin/rougify +16 -0
  5. data/gitlab-rouge.gemspec +17 -0
  6. data/lib/rouge.rb +57 -0
  7. data/lib/rouge/cli.rb +363 -0
  8. data/lib/rouge/demos/apache +21 -0
  9. data/lib/rouge/demos/applescript +2 -0
  10. data/lib/rouge/demos/c +8 -0
  11. data/lib/rouge/demos/clojure +5 -0
  12. data/lib/rouge/demos/coffeescript +5 -0
  13. data/lib/rouge/demos/common_lisp +1 -0
  14. data/lib/rouge/demos/conf +4 -0
  15. data/lib/rouge/demos/cpp +8 -0
  16. data/lib/rouge/demos/csharp +5 -0
  17. data/lib/rouge/demos/css +4 -0
  18. data/lib/rouge/demos/dart +6 -0
  19. data/lib/rouge/demos/diff +7 -0
  20. data/lib/rouge/demos/elixir +1 -0
  21. data/lib/rouge/demos/erb +1 -0
  22. data/lib/rouge/demos/erlang +7 -0
  23. data/lib/rouge/demos/factor +5 -0
  24. data/lib/rouge/demos/gherkin +17 -0
  25. data/lib/rouge/demos/glsl +14 -0
  26. data/lib/rouge/demos/go +7 -0
  27. data/lib/rouge/demos/groovy +9 -0
  28. data/lib/rouge/demos/haml +5 -0
  29. data/lib/rouge/demos/handlebars +7 -0
  30. data/lib/rouge/demos/haskell +6 -0
  31. data/lib/rouge/demos/html +8 -0
  32. data/lib/rouge/demos/http +14 -0
  33. data/lib/rouge/demos/ini +4 -0
  34. data/lib/rouge/demos/io +11 -0
  35. data/lib/rouge/demos/java +5 -0
  36. data/lib/rouge/demos/javascript +1 -0
  37. data/lib/rouge/demos/json +1 -0
  38. data/lib/rouge/demos/json-doc +1 -0
  39. data/lib/rouge/demos/liquid +11 -0
  40. data/lib/rouge/demos/literate_coffeescript +3 -0
  41. data/lib/rouge/demos/literate_haskell +7 -0
  42. data/lib/rouge/demos/llvm +20 -0
  43. data/lib/rouge/demos/lua +12 -0
  44. data/lib/rouge/demos/make +6 -0
  45. data/lib/rouge/demos/markdown +4 -0
  46. data/lib/rouge/demos/matlab +6 -0
  47. data/lib/rouge/demos/moonscript +16 -0
  48. data/lib/rouge/demos/nginx +5 -0
  49. data/lib/rouge/demos/nim +27 -0
  50. data/lib/rouge/demos/objective_c +14 -0
  51. data/lib/rouge/demos/ocaml +12 -0
  52. data/lib/rouge/demos/perl +5 -0
  53. data/lib/rouge/demos/php +3 -0
  54. data/lib/rouge/demos/plaintext +1 -0
  55. data/lib/rouge/demos/powershell +49 -0
  56. data/lib/rouge/demos/prolog +9 -0
  57. data/lib/rouge/demos/properties +7 -0
  58. data/lib/rouge/demos/puppet +6 -0
  59. data/lib/rouge/demos/python +6 -0
  60. data/lib/rouge/demos/qml +9 -0
  61. data/lib/rouge/demos/r +8 -0
  62. data/lib/rouge/demos/racket +24 -0
  63. data/lib/rouge/demos/ruby +9 -0
  64. data/lib/rouge/demos/rust +12 -0
  65. data/lib/rouge/demos/sass +3 -0
  66. data/lib/rouge/demos/scala +3 -0
  67. data/lib/rouge/demos/scheme +4 -0
  68. data/lib/rouge/demos/scss +5 -0
  69. data/lib/rouge/demos/sed +4 -0
  70. data/lib/rouge/demos/shell +2 -0
  71. data/lib/rouge/demos/slim +17 -0
  72. data/lib/rouge/demos/smalltalk +6 -0
  73. data/lib/rouge/demos/sml +4 -0
  74. data/lib/rouge/demos/sql +1 -0
  75. data/lib/rouge/demos/swift +5 -0
  76. data/lib/rouge/demos/tcl +1 -0
  77. data/lib/rouge/demos/tex +1 -0
  78. data/lib/rouge/demos/toml +9 -0
  79. data/lib/rouge/demos/tulip +14 -0
  80. data/lib/rouge/demos/vb +4 -0
  81. data/lib/rouge/demos/viml +5 -0
  82. data/lib/rouge/demos/xml +2 -0
  83. data/lib/rouge/demos/yaml +4 -0
  84. data/lib/rouge/formatter.rb +50 -0
  85. data/lib/rouge/formatters/html.rb +117 -0
  86. data/lib/rouge/formatters/null.rb +19 -0
  87. data/lib/rouge/formatters/terminal256.rb +176 -0
  88. data/lib/rouge/lexer.rb +443 -0
  89. data/lib/rouge/lexers/apache.rb +68 -0
  90. data/lib/rouge/lexers/apache/keywords.yml +453 -0
  91. data/lib/rouge/lexers/apple_script.rb +367 -0
  92. data/lib/rouge/lexers/c.rb +212 -0
  93. data/lib/rouge/lexers/clojure.rb +112 -0
  94. data/lib/rouge/lexers/coffeescript.rb +174 -0
  95. data/lib/rouge/lexers/common_lisp.rb +345 -0
  96. data/lib/rouge/lexers/conf.rb +24 -0
  97. data/lib/rouge/lexers/cpp.rb +66 -0
  98. data/lib/rouge/lexers/csharp.rb +88 -0
  99. data/lib/rouge/lexers/css.rb +271 -0
  100. data/lib/rouge/lexers/dart.rb +104 -0
  101. data/lib/rouge/lexers/diff.rb +31 -0
  102. data/lib/rouge/lexers/elixir.rb +108 -0
  103. data/lib/rouge/lexers/erb.rb +56 -0
  104. data/lib/rouge/lexers/erlang.rb +118 -0
  105. data/lib/rouge/lexers/factor.rb +302 -0
  106. data/lib/rouge/lexers/gherkin.rb +137 -0
  107. data/lib/rouge/lexers/gherkin/keywords.rb +14 -0
  108. data/lib/rouge/lexers/glsl.rb +135 -0
  109. data/lib/rouge/lexers/go.rb +178 -0
  110. data/lib/rouge/lexers/groovy.rb +104 -0
  111. data/lib/rouge/lexers/haml.rb +228 -0
  112. data/lib/rouge/lexers/handlebars.rb +79 -0
  113. data/lib/rouge/lexers/haskell.rb +183 -0
  114. data/lib/rouge/lexers/html.rb +94 -0
  115. data/lib/rouge/lexers/http.rb +80 -0
  116. data/lib/rouge/lexers/ini.rb +57 -0
  117. data/lib/rouge/lexers/io.rb +68 -0
  118. data/lib/rouge/lexers/java.rb +76 -0
  119. data/lib/rouge/lexers/javascript.rb +297 -0
  120. data/lib/rouge/lexers/liquid.rb +287 -0
  121. data/lib/rouge/lexers/literate_coffeescript.rb +33 -0
  122. data/lib/rouge/lexers/literate_haskell.rb +36 -0
  123. data/lib/rouge/lexers/llvm.rb +84 -0
  124. data/lib/rouge/lexers/lua.rb +122 -0
  125. data/lib/rouge/lexers/lua/builtins.rb +22 -0
  126. data/lib/rouge/lexers/make.rb +116 -0
  127. data/lib/rouge/lexers/markdown.rb +154 -0
  128. data/lib/rouge/lexers/matlab.rb +74 -0
  129. data/lib/rouge/lexers/matlab/builtins.rb +11 -0
  130. data/lib/rouge/lexers/moonscript.rb +110 -0
  131. data/lib/rouge/lexers/nginx.rb +71 -0
  132. data/lib/rouge/lexers/nim.rb +152 -0
  133. data/lib/rouge/lexers/objective_c.rb +197 -0
  134. data/lib/rouge/lexers/ocaml.rb +111 -0
  135. data/lib/rouge/lexers/perl.rb +197 -0
  136. data/lib/rouge/lexers/php.rb +173 -0
  137. data/lib/rouge/lexers/php/builtins.rb +204 -0
  138. data/lib/rouge/lexers/plain_text.rb +25 -0
  139. data/lib/rouge/lexers/powershell.rb +96 -0
  140. data/lib/rouge/lexers/prolog.rb +64 -0
  141. data/lib/rouge/lexers/properties.rb +55 -0
  142. data/lib/rouge/lexers/puppet.rb +128 -0
  143. data/lib/rouge/lexers/python.rb +228 -0
  144. data/lib/rouge/lexers/qml.rb +72 -0
  145. data/lib/rouge/lexers/r.rb +56 -0
  146. data/lib/rouge/lexers/racket.rb +542 -0
  147. data/lib/rouge/lexers/ruby.rb +415 -0
  148. data/lib/rouge/lexers/rust.rb +191 -0
  149. data/lib/rouge/lexers/sass.rb +74 -0
  150. data/lib/rouge/lexers/sass/common.rb +180 -0
  151. data/lib/rouge/lexers/scala.rb +142 -0
  152. data/lib/rouge/lexers/scheme.rb +112 -0
  153. data/lib/rouge/lexers/scss.rb +34 -0
  154. data/lib/rouge/lexers/sed.rb +170 -0
  155. data/lib/rouge/lexers/shell.rb +152 -0
  156. data/lib/rouge/lexers/slim.rb +228 -0
  157. data/lib/rouge/lexers/smalltalk.rb +116 -0
  158. data/lib/rouge/lexers/sml.rb +347 -0
  159. data/lib/rouge/lexers/sql.rb +140 -0
  160. data/lib/rouge/lexers/swift.rb +144 -0
  161. data/lib/rouge/lexers/tcl.rb +192 -0
  162. data/lib/rouge/lexers/tex.rb +72 -0
  163. data/lib/rouge/lexers/toml.rb +71 -0
  164. data/lib/rouge/lexers/tulip.rb +75 -0
  165. data/lib/rouge/lexers/vb.rb +164 -0
  166. data/lib/rouge/lexers/viml.rb +101 -0
  167. data/lib/rouge/lexers/viml/keywords.rb +12 -0
  168. data/lib/rouge/lexers/xml.rb +59 -0
  169. data/lib/rouge/lexers/yaml.rb +364 -0
  170. data/lib/rouge/plugins/redcarpet.rb +30 -0
  171. data/lib/rouge/regex_lexer.rb +439 -0
  172. data/lib/rouge/template_lexer.rb +22 -0
  173. data/lib/rouge/text_analyzer.rb +48 -0
  174. data/lib/rouge/theme.rb +195 -0
  175. data/lib/rouge/themes/base16.rb +130 -0
  176. data/lib/rouge/themes/colorful.rb +67 -0
  177. data/lib/rouge/themes/github.rb +71 -0
  178. data/lib/rouge/themes/molokai.rb +82 -0
  179. data/lib/rouge/themes/monokai.rb +92 -0
  180. data/lib/rouge/themes/monokai_sublime.rb +90 -0
  181. data/lib/rouge/themes/thankful_eyes.rb +71 -0
  182. data/lib/rouge/token.rb +182 -0
  183. data/lib/rouge/util.rb +101 -0
  184. data/lib/rouge/version.rb +7 -0
  185. metadata +231 -0
@@ -0,0 +1,9 @@
1
+ diff(plus(A,B), X, plus(DA, DB))
2
+ <= diff(A, X, DA) and diff(B, X, DB).
3
+
4
+ diff(times(A,B), X, plus(times(A, DB), times(DA, B)))
5
+ <= diff(A, X, DA) and diff(B, X, DB).
6
+
7
+ equal(X, X).
8
+ diff(X, X, 1).
9
+ diff(Y, X, 0) <= not equal(Y, X).
@@ -0,0 +1,7 @@
1
+ # You are reading the ".properties" entry.
2
+ ! The exclamation mark can also mark text as comments.
3
+ website = http\://en.wikipedia.org/
4
+ language = English
5
+ country : Poland
6
+ continent=Europe
7
+ key.with.dots=This is the value that could be looked up with the key "key.with.dots".
@@ -0,0 +1,6 @@
1
+ service { 'ntp':
2
+ name => $service_name,
3
+ ensure => running,
4
+ enable => true,
5
+ subscribe => File['ntp.conf'],
6
+ }
@@ -0,0 +1,6 @@
1
+ def fib(n): # write Fibonacci series up to n
2
+ """Print a Fibonacci series up to n."""
3
+ a, b = 0, 1
4
+ while a < n:
5
+ print a,
6
+ a, b = b, a+b
@@ -0,0 +1,9 @@
1
+ import QtQuick 2.0
2
+ Item {
3
+ width: 200
4
+ height: 100
5
+ MouseArea {
6
+ anchors.fill: parent
7
+ onClicked: Qt.quit()
8
+ }
9
+ }
@@ -0,0 +1,8 @@
1
+ dbenford <- function(x){
2
+ log10(1 + 1/x)
3
+ }
4
+
5
+ pbenford <- function(q){
6
+ cumprobs <- cumsum(dbenford(1:9))
7
+ return(cumprobs[q])
8
+ }
@@ -0,0 +1,24 @@
1
+ #lang racket
2
+
3
+ ;; draw a graph of cos and deriv^3(cos)
4
+ (require plot)
5
+ (define ((deriv f) x)
6
+ (/ (- (f x) (f (- x 0.001))) 0.001))
7
+ (define (thrice f) (lambda (x) (f (f (f x)))))
8
+ (plot (list (function ((thrice deriv) sin) -5 5)
9
+ (function cos -5 5 #:color 'blue)))
10
+
11
+ ;; Print the Greek alphabet
12
+ (for ([i (in-range 25)])
13
+ (displayln
14
+ (integer->char
15
+ (+ i (char->integer #\u3B1)))))
16
+
17
+ ;; An echo server
18
+ (define listener (tcp-listen 12345))
19
+ (let echo-server ()
20
+ (define-values (in out) (tcp-accept listener))
21
+ (thread (λ ()
22
+ (copy-port in out)
23
+ (close-output-port out)))
24
+ (echo-server))
@@ -0,0 +1,9 @@
1
+ class Greeter
2
+ def initialize(name="World")
3
+ @name = name
4
+ end
5
+
6
+ def say_hi
7
+ puts "Hi #{@name}!"
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ use core::*;
2
+
3
+ fn main() {
4
+ for ["Alice", "Bob", "Carol"].each |&name| {
5
+ do task::spawn {
6
+ let v = rand::Rng().shuffle([1, 2, 3]);
7
+ for v.each |&num| {
8
+ io::print(fmt!("%s says: '%d'\n", name, num))
9
+ }
10
+ }
11
+ }
12
+ }
@@ -0,0 +1,3 @@
1
+ @for $i from 1 through 3
2
+ .item-#{$i}
3
+ width: 2em * $i
@@ -0,0 +1,3 @@
1
+ class Greeter(name: String = "World") {
2
+ def sayHi() { println("Hi " + name + "!") }
3
+ }
@@ -0,0 +1,4 @@
1
+ (define Y
2
+ (lambda (m)
3
+ ((lambda (f) (m (lambda (a) ((f f) a))))
4
+ (lambda (f) (m (lambda (a) ((f f) a)))))))
@@ -0,0 +1,5 @@
1
+ @for $i from 1 through 3 {
2
+ .item-#{$i} {
3
+ width: 2em * $i;
4
+ }
5
+ }
@@ -0,0 +1,4 @@
1
+ /begin/,/end/ {
2
+ /begin/n # skip over the line that has "begin" on it
3
+ s/old/new/
4
+ }
@@ -0,0 +1,2 @@
1
+ # If not running interactively, don't do anything
2
+ [[ -z "$PS1" ]] && return
@@ -0,0 +1,17 @@
1
+ doctype html
2
+ html
3
+ body
4
+ h1 Markup examples
5
+ #content
6
+ p
7
+ | Slim can have #{ruby_code} interpolated!
8
+ /[if IE]
9
+ javascript:
10
+ alert('Slim supports embedded javascript!')
11
+
12
+ - unless items.empty?
13
+ table
14
+ - for item in items do
15
+ tr
16
+ td.name = item.name
17
+ td.price = item.price
@@ -0,0 +1,6 @@
1
+ quadMultiply: i1 and: i2
2
+ "This method multiplies the given numbers by each other
3
+ and the result by 4."
4
+ | mul |
5
+ mul := i1 * i2.
6
+ ^mul * 4
@@ -0,0 +1,4 @@
1
+ datatype shape
2
+ = Circle of loc * real (* center and radius *)
3
+ | Square of loc * real (* upper-left corner and side length; axis-aligned *)
4
+ | Triangle of loc * loc * loc (* corners *)
@@ -0,0 +1 @@
1
+ SELECT * FROM `users` WHERE `user`.`id` = 1
@@ -0,0 +1,5 @@
1
+ // Say hello to poeple
2
+ func sayHello(personName: String) -> String {
3
+ let greeting = "Hello, " + personName + "!"
4
+ return greeting
5
+ }
@@ -0,0 +1 @@
1
+ proc cross_sum {s} {expr [join [split $s ""] +]}
@@ -0,0 +1 @@
1
+ To write \LaTeX\ you would type \verb:\LaTeX:.
@@ -0,0 +1,9 @@
1
+ # This is a TOML document. Boom.
2
+
3
+ title = "TOML Example"
4
+
5
+ [owner]
6
+ name = "Tom Preston-Werner"
7
+ organization = "GitHub"
8
+ bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
9
+ dob = 1979-05-27T07:32:00Z # First class dates? Why not?
@@ -0,0 +1,14 @@
1
+ # ref.tlp
2
+ @module ref
3
+
4
+ ref value = Instance (spawn [! loop value ])
5
+
6
+ loop value = receive [
7
+ .set new-value => loop new-value
8
+ p, .get => (send p value; loop value)
9
+ ]
10
+
11
+ @module Instance pid [
12
+ set val = .set val > send pid
13
+ get! = .get > send-wait pid
14
+ ]
@@ -0,0 +1,4 @@
1
+ Private Sub Form_Load()
2
+ ' Execute a simple message box that says "Hello, World!"
3
+ MsgBox "Hello, World!"
4
+ End Sub
@@ -0,0 +1,5 @@
1
+ set encoding=utf-8
2
+
3
+ filetype off
4
+ call pathogen#runtime_append_all_bundles()
5
+ filetype plugin indent on
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <xsl:template match="/"></xsl:template>
@@ -0,0 +1,4 @@
1
+ ---
2
+ one: Mark McGwire
3
+ two: Sammy Sosa
4
+ three: Ken Griffey
@@ -0,0 +1,50 @@
1
+ # -*- coding: utf-8 -*- #
2
+
3
+ module Rouge
4
+ # A Formatter takes a token stream and formats it for human viewing.
5
+ class Formatter
6
+ # @private
7
+ REGISTRY = {}
8
+
9
+ # Specify or get the unique tag for this formatter. This is used
10
+ # for specifying a formatter in `rougify`.
11
+ def self.tag(tag=nil)
12
+ return @tag unless tag
13
+ REGISTRY[tag] = self
14
+
15
+ @tag = tag
16
+ end
17
+
18
+ # Find a formatter class given a unique tag.
19
+ def self.find(tag)
20
+ REGISTRY[tag]
21
+ end
22
+
23
+ # Format a token stream. Delegates to {#format}.
24
+ def self.format(tokens, opts={}, &b)
25
+ new(opts).format(tokens, &b)
26
+ end
27
+
28
+ # Format a token stream.
29
+ def format(tokens, &b)
30
+ return stream(tokens, &b) if block_given?
31
+
32
+ out = ''
33
+ stream(tokens) { |piece| out << piece }
34
+
35
+ out
36
+ end
37
+
38
+ # @deprecated Use {#format} instead.
39
+ def render(tokens)
40
+ warn 'Formatter#render is deprecated, use #format instead.'
41
+ format(tokens)
42
+ end
43
+
44
+ # @abstract
45
+ # yield strings that, when concatenated, form the formatted output
46
+ def stream(tokens, &b)
47
+ raise 'abstract'
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,117 @@
1
+ # -*- coding: utf-8 -*- #
2
+
3
+ # stdlib
4
+ require 'cgi'
5
+
6
+ module Rouge
7
+ module Formatters
8
+ # Transforms a token stream into HTML output.
9
+ class HTML < Formatter
10
+ tag 'html'
11
+
12
+ # @option opts [String] :css_class ('highlight')
13
+ # @option opts [true/false] :line_numbers (false)
14
+ # @option opts [Rouge::CSSTheme] :inline_theme (nil)
15
+ # @option opts [true/false] :wrap (true)
16
+ #
17
+ # Initialize with options.
18
+ #
19
+ # If `:inline_theme` is given, then instead of rendering the
20
+ # tokens as <span> tags with CSS classes, the styles according to
21
+ # the given theme will be inlined in "style" attributes. This is
22
+ # useful for formats in which stylesheets are not available.
23
+ #
24
+ # Content will be wrapped in a tag (`div` if tableized, `pre` if
25
+ # not) with the given `:css_class` unless `:wrap` is set to `false`.
26
+ def initialize(opts={})
27
+ @css_class = opts.fetch(:css_class, 'highlight')
28
+ @css_class = " class=#{@css_class.inspect}" if @css_class
29
+
30
+ @line_numbers = opts.fetch(:line_numbers, false)
31
+ @start_line = opts.fetch(:start_line, 1)
32
+ @inline_theme = opts.fetch(:inline_theme, nil)
33
+ @inline_theme = Theme.find(@inline_theme).new if @inline_theme.is_a? String
34
+
35
+ @wrap = opts.fetch(:wrap, true)
36
+ end
37
+
38
+ # @yield the html output.
39
+ def stream(tokens, &b)
40
+ if @line_numbers
41
+ stream_tableized(tokens, &b)
42
+ else
43
+ stream_untableized(tokens, &b)
44
+ end
45
+ end
46
+
47
+ private
48
+ def stream_untableized(tokens, &b)
49
+ yield "<pre#@css_class><code>" if @wrap
50
+ tokens.each{ |tok, val| span(tok, val, &b) }
51
+ yield "</code></pre>\n" if @wrap
52
+ end
53
+
54
+ def stream_tableized(tokens)
55
+ num_lines = 0
56
+ last_val = ''
57
+ formatted = ''
58
+
59
+ tokens.each do |tok, val|
60
+ last_val = val
61
+ num_lines += val.scan(/\n/).size
62
+ span(tok, val) { |str| formatted << str }
63
+ end
64
+
65
+ # add an extra line for non-newline-terminated strings
66
+ if last_val[-1] != "\n"
67
+ num_lines += 1
68
+ span(Token::Tokens::Text::Whitespace, "\n") { |str| formatted << str }
69
+ end
70
+
71
+ # generate a string of newline-separated line numbers for the gutter>
72
+ numbers = %<<pre class="lineno">#{(@start_line..num_lines+@start_line-1)
73
+ .to_a.join("\n")}</pre>>
74
+
75
+ yield "<div#@css_class>" if @wrap
76
+ yield '<table style="border-spacing: 0"><tbody><tr>'
77
+
78
+ # the "gl" class applies the style for Generic.Lineno
79
+ yield '<td class="gutter gl" style="text-align: right">'
80
+ yield numbers
81
+ yield '</td>'
82
+
83
+ yield '<td class="code">'
84
+ yield '<pre>'
85
+ yield formatted
86
+ yield '</pre>'
87
+ yield '</td>'
88
+
89
+ yield "</tr></tbody></table>\n"
90
+ yield "</div>\n" if @wrap
91
+ end
92
+
93
+ TABLE_FOR_ESCAPE_HTML = {
94
+ '&' => '&amp;',
95
+ '<' => '&lt;',
96
+ '>' => '&gt;',
97
+ }
98
+
99
+ def span(tok, val)
100
+ val = val.gsub(/[&<>]/, TABLE_FOR_ESCAPE_HTML)
101
+ shortname = tok.shortname or raise "unknown token: #{tok.inspect} for #{val.inspect}"
102
+
103
+ if shortname.empty?
104
+ yield val
105
+ else
106
+ if @inline_theme
107
+ rules = @inline_theme.style_for(tok).rendered_rules
108
+
109
+ yield "<span style=\"#{rules.to_a.join(';')}\">#{val}</span>"
110
+ else
111
+ yield "<span class=\"#{shortname}\">#{val}</span>"
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,19 @@
1
+ # -*- coding: utf-8 -*- #
2
+
3
+ module Rouge
4
+ module Formatters
5
+ # A formatter which renders nothing.
6
+ class Null < Formatter
7
+ tag 'null'
8
+
9
+ def initialize(*)
10
+ end
11
+
12
+ def stream(tokens, &b)
13
+ tokens.each do |tok, val|
14
+ yield "#{tok} #{val.inspect}\n"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,176 @@
1
+ # -*- coding: utf-8 -*- #
2
+
3
+ module Rouge
4
+ module Formatters
5
+ # A formatter for 256-color terminals
6
+ class Terminal256 < Formatter
7
+ tag 'terminal256'
8
+
9
+ # @private
10
+ attr_reader :theme
11
+
12
+
13
+ # @option opts :theme
14
+ # (default is thankful_eyes) the theme to render with.
15
+ def initialize(opts={})
16
+ @theme = opts[:theme] || 'thankful_eyes'
17
+ @theme = Theme.find(@theme) if @theme.is_a? String
18
+ end
19
+
20
+ def stream(tokens, &b)
21
+ tokens.each do |tok, val|
22
+ escape = escape_sequence(tok)
23
+ yield escape.style_string
24
+ yield val.gsub("\n", "\n#{escape.style_string}")
25
+ yield escape.reset_string
26
+ end
27
+ end
28
+
29
+ class EscapeSequence
30
+ attr_reader :style
31
+ def initialize(style)
32
+ @style = style
33
+ end
34
+
35
+ def self.xterm_colors
36
+ @xterm_colors ||= [].tap do |out|
37
+ # colors 0..15: 16 basic colors
38
+ out << [0x00, 0x00, 0x00] # 0
39
+ out << [0xcd, 0x00, 0x00] # 1
40
+ out << [0x00, 0xcd, 0x00] # 2
41
+ out << [0xcd, 0xcd, 0x00] # 3
42
+ out << [0x00, 0x00, 0xee] # 4
43
+ out << [0xcd, 0x00, 0xcd] # 5
44
+ out << [0x00, 0xcd, 0xcd] # 6
45
+ out << [0xe5, 0xe5, 0xe5] # 7
46
+ out << [0x7f, 0x7f, 0x7f] # 8
47
+ out << [0xff, 0x00, 0x00] # 9
48
+ out << [0x00, 0xff, 0x00] # 10
49
+ out << [0xff, 0xff, 0x00] # 11
50
+ out << [0x5c, 0x5c, 0xff] # 12
51
+ out << [0xff, 0x00, 0xff] # 13
52
+ out << [0x00, 0xff, 0xff] # 14
53
+ out << [0xff, 0xff, 0xff] # 15
54
+
55
+ # colors 16..232: the 6x6x6 color cube
56
+ valuerange = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
57
+
58
+ 217.times do |i|
59
+ r = valuerange[(i / 36) % 6]
60
+ g = valuerange[(i / 6) % 6]
61
+ b = valuerange[i % 6]
62
+ out << [r, g, b]
63
+ end
64
+
65
+ # colors 233..253: grayscale
66
+ 1.upto 22 do |i|
67
+ v = 8 + i * 10
68
+ out << [v, v, v]
69
+ end
70
+ end
71
+ end
72
+
73
+ def fg
74
+ return @fg if instance_variable_defined? :@fg
75
+ @fg = style.fg && self.class.color_index(style.fg)
76
+ end
77
+
78
+ def bg
79
+ return @bg if instance_variable_defined? :@bg
80
+ @bg = style.bg && self.class.color_index(style.bg)
81
+ end
82
+
83
+ def style_string
84
+ @style_string ||= begin
85
+ attrs = []
86
+
87
+ attrs << ['38', '5', fg.to_s] if fg
88
+ attrs << ['48', '5', bg.to_s] if bg
89
+ attrs << '01' if style[:bold]
90
+ attrs << '04' if style[:italic] # underline, but hey, whatevs
91
+ escape(attrs)
92
+ end
93
+ end
94
+
95
+ def reset_string
96
+ @reset_string ||= begin
97
+ attrs = []
98
+ attrs << '39' if fg # fg reset
99
+ attrs << '49' if bg # bg reset
100
+ attrs << '00' if style[:bold] || style[:italic]
101
+
102
+ escape(attrs)
103
+ end
104
+ end
105
+
106
+ private
107
+ def escape(attrs)
108
+ return '' if attrs.empty?
109
+ "\e[#{attrs.join(';')}m"
110
+ end
111
+
112
+ def self.color_index(color)
113
+ @color_index_cache ||= {}
114
+ @color_index_cache[color] ||= closest_color(*get_rgb(color))
115
+ end
116
+
117
+ def self.get_rgb(color)
118
+ color = $1 if color =~ /#([0-9a-f]+)/i
119
+ hexes = case color.size
120
+ when 3
121
+ color.chars.map { |c| "#{c}#{c}" }
122
+ when 6
123
+ color.scan(/../)
124
+ else
125
+ raise "invalid color: #{color}"
126
+ end
127
+
128
+ hexes.map { |h| h.to_i(16) }
129
+ end
130
+
131
+ # max distance between two colors, #000000 to #ffffff
132
+ MAX_DISTANCE = 257 * 257 * 3
133
+
134
+ def self.closest_color(r, g, b)
135
+ @@colors_cache ||= {}
136
+ key = (r << 16) + (g << 8) + b
137
+ @@colors_cache.fetch(key) do
138
+ distance = MAX_DISTANCE
139
+
140
+ match = 0
141
+
142
+ xterm_colors.each_with_index do |(cr, cg, cb), i|
143
+ d = (r - cr)**2 + (g - cg)**2 + (b - cb)**2
144
+ next if d >= distance
145
+
146
+ match = i
147
+ distance = d
148
+ end
149
+
150
+ match
151
+ end
152
+ end
153
+ end
154
+
155
+ # private
156
+ def escape_sequence(token)
157
+ @escape_sequences ||= {}
158
+ @escape_sequences[token.name] ||=
159
+ EscapeSequence.new(get_style(token))
160
+ end
161
+
162
+ def get_style(token)
163
+ return text_style if token.ancestors.include? Token::Tokens::Text
164
+
165
+ theme.get_own_style(token) || text_style
166
+ end
167
+
168
+ def text_style
169
+ style = theme.get_style(Token['Text'])
170
+ # don't highlight text backgrounds
171
+ style.delete :bg
172
+ style
173
+ end
174
+ end
175
+ end
176
+ end