gitlab-rouge 1.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +23 -0
- data/LICENSE +186 -0
- data/bin/rougify +16 -0
- data/gitlab-rouge.gemspec +17 -0
- data/lib/rouge.rb +57 -0
- data/lib/rouge/cli.rb +363 -0
- data/lib/rouge/demos/apache +21 -0
- data/lib/rouge/demos/applescript +2 -0
- data/lib/rouge/demos/c +8 -0
- data/lib/rouge/demos/clojure +5 -0
- data/lib/rouge/demos/coffeescript +5 -0
- data/lib/rouge/demos/common_lisp +1 -0
- data/lib/rouge/demos/conf +4 -0
- data/lib/rouge/demos/cpp +8 -0
- data/lib/rouge/demos/csharp +5 -0
- data/lib/rouge/demos/css +4 -0
- data/lib/rouge/demos/dart +6 -0
- data/lib/rouge/demos/diff +7 -0
- data/lib/rouge/demos/elixir +1 -0
- data/lib/rouge/demos/erb +1 -0
- data/lib/rouge/demos/erlang +7 -0
- data/lib/rouge/demos/factor +5 -0
- data/lib/rouge/demos/gherkin +17 -0
- data/lib/rouge/demos/glsl +14 -0
- data/lib/rouge/demos/go +7 -0
- data/lib/rouge/demos/groovy +9 -0
- data/lib/rouge/demos/haml +5 -0
- data/lib/rouge/demos/handlebars +7 -0
- data/lib/rouge/demos/haskell +6 -0
- data/lib/rouge/demos/html +8 -0
- data/lib/rouge/demos/http +14 -0
- data/lib/rouge/demos/ini +4 -0
- data/lib/rouge/demos/io +11 -0
- data/lib/rouge/demos/java +5 -0
- data/lib/rouge/demos/javascript +1 -0
- data/lib/rouge/demos/json +1 -0
- data/lib/rouge/demos/json-doc +1 -0
- data/lib/rouge/demos/liquid +11 -0
- data/lib/rouge/demos/literate_coffeescript +3 -0
- data/lib/rouge/demos/literate_haskell +7 -0
- data/lib/rouge/demos/llvm +20 -0
- data/lib/rouge/demos/lua +12 -0
- data/lib/rouge/demos/make +6 -0
- data/lib/rouge/demos/markdown +4 -0
- data/lib/rouge/demos/matlab +6 -0
- data/lib/rouge/demos/moonscript +16 -0
- data/lib/rouge/demos/nginx +5 -0
- data/lib/rouge/demos/nim +27 -0
- data/lib/rouge/demos/objective_c +14 -0
- data/lib/rouge/demos/ocaml +12 -0
- data/lib/rouge/demos/perl +5 -0
- data/lib/rouge/demos/php +3 -0
- data/lib/rouge/demos/plaintext +1 -0
- data/lib/rouge/demos/powershell +49 -0
- data/lib/rouge/demos/prolog +9 -0
- data/lib/rouge/demos/properties +7 -0
- data/lib/rouge/demos/puppet +6 -0
- data/lib/rouge/demos/python +6 -0
- data/lib/rouge/demos/qml +9 -0
- data/lib/rouge/demos/r +8 -0
- data/lib/rouge/demos/racket +24 -0
- data/lib/rouge/demos/ruby +9 -0
- data/lib/rouge/demos/rust +12 -0
- data/lib/rouge/demos/sass +3 -0
- data/lib/rouge/demos/scala +3 -0
- data/lib/rouge/demos/scheme +4 -0
- data/lib/rouge/demos/scss +5 -0
- data/lib/rouge/demos/sed +4 -0
- data/lib/rouge/demos/shell +2 -0
- data/lib/rouge/demos/slim +17 -0
- data/lib/rouge/demos/smalltalk +6 -0
- data/lib/rouge/demos/sml +4 -0
- data/lib/rouge/demos/sql +1 -0
- data/lib/rouge/demos/swift +5 -0
- data/lib/rouge/demos/tcl +1 -0
- data/lib/rouge/demos/tex +1 -0
- data/lib/rouge/demos/toml +9 -0
- data/lib/rouge/demos/tulip +14 -0
- data/lib/rouge/demos/vb +4 -0
- data/lib/rouge/demos/viml +5 -0
- data/lib/rouge/demos/xml +2 -0
- data/lib/rouge/demos/yaml +4 -0
- data/lib/rouge/formatter.rb +50 -0
- data/lib/rouge/formatters/html.rb +117 -0
- data/lib/rouge/formatters/null.rb +19 -0
- data/lib/rouge/formatters/terminal256.rb +176 -0
- data/lib/rouge/lexer.rb +443 -0
- data/lib/rouge/lexers/apache.rb +68 -0
- data/lib/rouge/lexers/apache/keywords.yml +453 -0
- data/lib/rouge/lexers/apple_script.rb +367 -0
- data/lib/rouge/lexers/c.rb +212 -0
- data/lib/rouge/lexers/clojure.rb +112 -0
- data/lib/rouge/lexers/coffeescript.rb +174 -0
- data/lib/rouge/lexers/common_lisp.rb +345 -0
- data/lib/rouge/lexers/conf.rb +24 -0
- data/lib/rouge/lexers/cpp.rb +66 -0
- data/lib/rouge/lexers/csharp.rb +88 -0
- data/lib/rouge/lexers/css.rb +271 -0
- data/lib/rouge/lexers/dart.rb +104 -0
- data/lib/rouge/lexers/diff.rb +31 -0
- data/lib/rouge/lexers/elixir.rb +108 -0
- data/lib/rouge/lexers/erb.rb +56 -0
- data/lib/rouge/lexers/erlang.rb +118 -0
- data/lib/rouge/lexers/factor.rb +302 -0
- data/lib/rouge/lexers/gherkin.rb +137 -0
- data/lib/rouge/lexers/gherkin/keywords.rb +14 -0
- data/lib/rouge/lexers/glsl.rb +135 -0
- data/lib/rouge/lexers/go.rb +178 -0
- data/lib/rouge/lexers/groovy.rb +104 -0
- data/lib/rouge/lexers/haml.rb +228 -0
- data/lib/rouge/lexers/handlebars.rb +79 -0
- data/lib/rouge/lexers/haskell.rb +183 -0
- data/lib/rouge/lexers/html.rb +94 -0
- data/lib/rouge/lexers/http.rb +80 -0
- data/lib/rouge/lexers/ini.rb +57 -0
- data/lib/rouge/lexers/io.rb +68 -0
- data/lib/rouge/lexers/java.rb +76 -0
- data/lib/rouge/lexers/javascript.rb +297 -0
- data/lib/rouge/lexers/liquid.rb +287 -0
- data/lib/rouge/lexers/literate_coffeescript.rb +33 -0
- data/lib/rouge/lexers/literate_haskell.rb +36 -0
- data/lib/rouge/lexers/llvm.rb +84 -0
- data/lib/rouge/lexers/lua.rb +122 -0
- data/lib/rouge/lexers/lua/builtins.rb +22 -0
- data/lib/rouge/lexers/make.rb +116 -0
- data/lib/rouge/lexers/markdown.rb +154 -0
- data/lib/rouge/lexers/matlab.rb +74 -0
- data/lib/rouge/lexers/matlab/builtins.rb +11 -0
- data/lib/rouge/lexers/moonscript.rb +110 -0
- data/lib/rouge/lexers/nginx.rb +71 -0
- data/lib/rouge/lexers/nim.rb +152 -0
- data/lib/rouge/lexers/objective_c.rb +197 -0
- data/lib/rouge/lexers/ocaml.rb +111 -0
- data/lib/rouge/lexers/perl.rb +197 -0
- data/lib/rouge/lexers/php.rb +173 -0
- data/lib/rouge/lexers/php/builtins.rb +204 -0
- data/lib/rouge/lexers/plain_text.rb +25 -0
- data/lib/rouge/lexers/powershell.rb +96 -0
- data/lib/rouge/lexers/prolog.rb +64 -0
- data/lib/rouge/lexers/properties.rb +55 -0
- data/lib/rouge/lexers/puppet.rb +128 -0
- data/lib/rouge/lexers/python.rb +228 -0
- data/lib/rouge/lexers/qml.rb +72 -0
- data/lib/rouge/lexers/r.rb +56 -0
- data/lib/rouge/lexers/racket.rb +542 -0
- data/lib/rouge/lexers/ruby.rb +415 -0
- data/lib/rouge/lexers/rust.rb +191 -0
- data/lib/rouge/lexers/sass.rb +74 -0
- data/lib/rouge/lexers/sass/common.rb +180 -0
- data/lib/rouge/lexers/scala.rb +142 -0
- data/lib/rouge/lexers/scheme.rb +112 -0
- data/lib/rouge/lexers/scss.rb +34 -0
- data/lib/rouge/lexers/sed.rb +170 -0
- data/lib/rouge/lexers/shell.rb +152 -0
- data/lib/rouge/lexers/slim.rb +228 -0
- data/lib/rouge/lexers/smalltalk.rb +116 -0
- data/lib/rouge/lexers/sml.rb +347 -0
- data/lib/rouge/lexers/sql.rb +140 -0
- data/lib/rouge/lexers/swift.rb +144 -0
- data/lib/rouge/lexers/tcl.rb +192 -0
- data/lib/rouge/lexers/tex.rb +72 -0
- data/lib/rouge/lexers/toml.rb +71 -0
- data/lib/rouge/lexers/tulip.rb +75 -0
- data/lib/rouge/lexers/vb.rb +164 -0
- data/lib/rouge/lexers/viml.rb +101 -0
- data/lib/rouge/lexers/viml/keywords.rb +12 -0
- data/lib/rouge/lexers/xml.rb +59 -0
- data/lib/rouge/lexers/yaml.rb +364 -0
- data/lib/rouge/plugins/redcarpet.rb +30 -0
- data/lib/rouge/regex_lexer.rb +439 -0
- data/lib/rouge/template_lexer.rb +22 -0
- data/lib/rouge/text_analyzer.rb +48 -0
- data/lib/rouge/theme.rb +195 -0
- data/lib/rouge/themes/base16.rb +130 -0
- data/lib/rouge/themes/colorful.rb +67 -0
- data/lib/rouge/themes/github.rb +71 -0
- data/lib/rouge/themes/molokai.rb +82 -0
- data/lib/rouge/themes/monokai.rb +92 -0
- data/lib/rouge/themes/monokai_sublime.rb +90 -0
- data/lib/rouge/themes/thankful_eyes.rb +71 -0
- data/lib/rouge/token.rb +182 -0
- data/lib/rouge/util.rb +101 -0
- data/lib/rouge/version.rb +7 -0
- metadata +231 -0
@@ -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".
|
data/lib/rouge/demos/qml
ADDED
data/lib/rouge/demos/r
ADDED
@@ -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))
|
data/lib/rouge/demos/sed
ADDED
@@ -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
|
data/lib/rouge/demos/sml
ADDED
data/lib/rouge/demos/sql
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
SELECT * FROM `users` WHERE `user`.`id` = 1
|
data/lib/rouge/demos/tcl
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
proc cross_sum {s} {expr [join [split $s ""] +]}
|
data/lib/rouge/demos/tex
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
To write \LaTeX\ you would type \verb:\LaTeX:.
|
@@ -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
|
+
]
|
data/lib/rouge/demos/vb
ADDED
data/lib/rouge/demos/xml
ADDED
@@ -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
|
+
'&' => '&',
|
95
|
+
'<' => '<',
|
96
|
+
'>' => '>',
|
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
|