giga-fast-rb 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/coderay-1.1.3/MIT-LICENSE +22 -0
- data/coderay-1.1.3/README_INDEX.rdoc +123 -0
- data/coderay-1.1.3/bin/coderay +215 -0
- data/coderay-1.1.3/lib/coderay/duo.rb +81 -0
- data/coderay-1.1.3/lib/coderay/encoders/_map.rb +17 -0
- data/coderay-1.1.3/lib/coderay/encoders/comment_filter.rb +25 -0
- data/coderay-1.1.3/lib/coderay/encoders/count.rb +39 -0
- data/coderay-1.1.3/lib/coderay/encoders/debug.rb +49 -0
- data/coderay-1.1.3/lib/coderay/encoders/debug_lint.rb +63 -0
- data/coderay-1.1.3/lib/coderay/encoders/div.rb +23 -0
- data/coderay-1.1.3/lib/coderay/encoders/encoder.rb +190 -0
- data/coderay-1.1.3/lib/coderay/encoders/filter.rb +58 -0
- data/coderay-1.1.3/lib/coderay/encoders/html/css.rb +65 -0
- data/coderay-1.1.3/lib/coderay/encoders/html/numbering.rb +108 -0
- data/coderay-1.1.3/lib/coderay/encoders/html/output.rb +164 -0
- data/coderay-1.1.3/lib/coderay/encoders/html.rb +333 -0
- data/coderay-1.1.3/lib/coderay/encoders/json.rb +83 -0
- data/coderay-1.1.3/lib/coderay/encoders/lines_of_code.rb +45 -0
- data/coderay-1.1.3/lib/coderay/encoders/lint.rb +59 -0
- data/coderay-1.1.3/lib/coderay/encoders/null.rb +18 -0
- data/coderay-1.1.3/lib/coderay/encoders/page.rb +24 -0
- data/coderay-1.1.3/lib/coderay/encoders/span.rb +23 -0
- data/coderay-1.1.3/lib/coderay/encoders/statistic.rb +95 -0
- data/coderay-1.1.3/lib/coderay/encoders/terminal.rb +195 -0
- data/coderay-1.1.3/lib/coderay/encoders/text.rb +46 -0
- data/coderay-1.1.3/lib/coderay/encoders/token_kind_filter.rb +111 -0
- data/coderay-1.1.3/lib/coderay/encoders/xml.rb +72 -0
- data/coderay-1.1.3/lib/coderay/encoders/yaml.rb +50 -0
- data/coderay-1.1.3/lib/coderay/encoders.rb +18 -0
- data/coderay-1.1.3/lib/coderay/for_redcloth.rb +95 -0
- data/coderay-1.1.3/lib/coderay/helpers/file_type.rb +151 -0
- data/coderay-1.1.3/lib/coderay/helpers/plugin.rb +55 -0
- data/coderay-1.1.3/lib/coderay/helpers/plugin_host.rb +221 -0
- data/coderay-1.1.3/lib/coderay/helpers/word_list.rb +72 -0
- data/coderay-1.1.3/lib/coderay/scanners/_map.rb +24 -0
- data/coderay-1.1.3/lib/coderay/scanners/c.rb +189 -0
- data/coderay-1.1.3/lib/coderay/scanners/clojure.rb +217 -0
- data/coderay-1.1.3/lib/coderay/scanners/cpp.rb +217 -0
- data/coderay-1.1.3/lib/coderay/scanners/css.rb +196 -0
- data/coderay-1.1.3/lib/coderay/scanners/debug.rb +75 -0
- data/coderay-1.1.3/lib/coderay/scanners/delphi.rb +144 -0
- data/coderay-1.1.3/lib/coderay/scanners/diff.rb +221 -0
- data/coderay-1.1.3/lib/coderay/scanners/erb.rb +81 -0
- data/coderay-1.1.3/lib/coderay/scanners/go.rb +208 -0
- data/coderay-1.1.3/lib/coderay/scanners/groovy.rb +268 -0
- data/coderay-1.1.3/lib/coderay/scanners/haml.rb +168 -0
- data/coderay-1.1.3/lib/coderay/scanners/html.rb +275 -0
- data/coderay-1.1.3/lib/coderay/scanners/java/builtin_types.rb +421 -0
- data/coderay-1.1.3/lib/coderay/scanners/java.rb +174 -0
- data/coderay-1.1.3/lib/coderay/scanners/java_script.rb +236 -0
- data/coderay-1.1.3/lib/coderay/scanners/json.rb +98 -0
- data/coderay-1.1.3/lib/coderay/scanners/lua.rb +280 -0
- data/coderay-1.1.3/lib/coderay/scanners/php.rb +527 -0
- data/coderay-1.1.3/lib/coderay/scanners/python.rb +287 -0
- data/coderay-1.1.3/lib/coderay/scanners/raydebug.rb +75 -0
- data/coderay-1.1.3/lib/coderay/scanners/ruby/patterns.rb +178 -0
- data/coderay-1.1.3/lib/coderay/scanners/ruby/string_state.rb +79 -0
- data/coderay-1.1.3/lib/coderay/scanners/ruby.rb +477 -0
- data/coderay-1.1.3/lib/coderay/scanners/sass.rb +232 -0
- data/coderay-1.1.3/lib/coderay/scanners/scanner.rb +337 -0
- data/coderay-1.1.3/lib/coderay/scanners/sql.rb +169 -0
- data/coderay-1.1.3/lib/coderay/scanners/taskpaper.rb +36 -0
- data/coderay-1.1.3/lib/coderay/scanners/text.rb +26 -0
- data/coderay-1.1.3/lib/coderay/scanners/xml.rb +17 -0
- data/coderay-1.1.3/lib/coderay/scanners/yaml.rb +140 -0
- data/coderay-1.1.3/lib/coderay/scanners.rb +27 -0
- data/coderay-1.1.3/lib/coderay/styles/_map.rb +7 -0
- data/coderay-1.1.3/lib/coderay/styles/alpha.rb +153 -0
- data/coderay-1.1.3/lib/coderay/styles/style.rb +18 -0
- data/coderay-1.1.3/lib/coderay/styles.rb +15 -0
- data/coderay-1.1.3/lib/coderay/token_kinds.rb +85 -0
- data/coderay-1.1.3/lib/coderay/tokens.rb +164 -0
- data/coderay-1.1.3/lib/coderay/tokens_proxy.rb +55 -0
- data/coderay-1.1.3/lib/coderay/version.rb +3 -0
- data/coderay-1.1.3/lib/coderay.rb +284 -0
- data/giga-fast-rb.gemspec +12 -0
- metadata +117 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
# = Encoder
|
|
5
|
+
#
|
|
6
|
+
# The Encoder base class. Together with Scanner and
|
|
7
|
+
# Tokens, it forms the highlighting triad.
|
|
8
|
+
#
|
|
9
|
+
# Encoder instances take a Tokens object and do something with it.
|
|
10
|
+
#
|
|
11
|
+
# The most common Encoder is surely the HTML encoder
|
|
12
|
+
# (CodeRay::Encoders::HTML). It highlights the code in a colorful
|
|
13
|
+
# html page.
|
|
14
|
+
# If you want the highlighted code in a div or a span instead,
|
|
15
|
+
# use its subclasses Div and Span.
|
|
16
|
+
class Encoder
|
|
17
|
+
extend Plugin
|
|
18
|
+
plugin_host Encoders
|
|
19
|
+
|
|
20
|
+
class << self
|
|
21
|
+
|
|
22
|
+
# If FILE_EXTENSION isn't defined, this method returns the
|
|
23
|
+
# downcase class name instead.
|
|
24
|
+
def const_missing sym
|
|
25
|
+
if sym == :FILE_EXTENSION
|
|
26
|
+
(defined?(@plugin_id) && @plugin_id || name[/\w+$/].downcase).to_s
|
|
27
|
+
else
|
|
28
|
+
super
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The default file extension for output file of this encoder class.
|
|
33
|
+
def file_extension
|
|
34
|
+
self::FILE_EXTENSION
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Subclasses are to store their default options in this constant.
|
|
40
|
+
DEFAULT_OPTIONS = { }
|
|
41
|
+
|
|
42
|
+
# The options you gave the Encoder at creating.
|
|
43
|
+
attr_accessor :options, :scanner
|
|
44
|
+
|
|
45
|
+
# Creates a new Encoder.
|
|
46
|
+
# +options+ is saved and used for all encode operations, as long
|
|
47
|
+
# as you don't overwrite it there by passing additional options.
|
|
48
|
+
#
|
|
49
|
+
# Encoder objects provide three encode methods:
|
|
50
|
+
# - encode simply takes a +code+ string and a +lang+
|
|
51
|
+
# - encode_tokens expects a +tokens+ object instead
|
|
52
|
+
#
|
|
53
|
+
# Each method has an optional +options+ parameter. These are
|
|
54
|
+
# added to the options you passed at creation.
|
|
55
|
+
def initialize options = {}
|
|
56
|
+
@options = self.class::DEFAULT_OPTIONS.merge options
|
|
57
|
+
@@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN = false
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Encode a Tokens object.
|
|
61
|
+
def encode_tokens tokens, options = {}
|
|
62
|
+
options = @options.merge options
|
|
63
|
+
@scanner = tokens.scanner if tokens.respond_to? :scanner
|
|
64
|
+
setup options
|
|
65
|
+
compile tokens, options
|
|
66
|
+
finish options
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Encode the given +code+ using the Scanner for +lang+.
|
|
70
|
+
def encode code, lang, options = {}
|
|
71
|
+
options = @options.merge options
|
|
72
|
+
@scanner = Scanners[lang].new code, CodeRay.get_scanner_options(options).update(:tokens => self)
|
|
73
|
+
setup options
|
|
74
|
+
@scanner.tokenize
|
|
75
|
+
finish options
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# You can use highlight instead of encode, if that seems
|
|
79
|
+
# more clear to you.
|
|
80
|
+
alias highlight encode
|
|
81
|
+
|
|
82
|
+
# The default file extension for this encoder.
|
|
83
|
+
def file_extension
|
|
84
|
+
self.class.file_extension
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def << token
|
|
88
|
+
unless @@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN
|
|
89
|
+
warn 'Using old Tokens#<< interface.'
|
|
90
|
+
@@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN = true
|
|
91
|
+
end
|
|
92
|
+
self.token(*token)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Called with +content+ and +kind+ of the currently scanned token.
|
|
96
|
+
# For simple scanners, it's enougth to implement this method.
|
|
97
|
+
#
|
|
98
|
+
# By default, it calls text_token, begin_group, end_group, begin_line,
|
|
99
|
+
# or end_line, depending on the +content+.
|
|
100
|
+
def token content, kind
|
|
101
|
+
case content
|
|
102
|
+
when String
|
|
103
|
+
text_token content, kind
|
|
104
|
+
when :begin_group
|
|
105
|
+
begin_group kind
|
|
106
|
+
when :end_group
|
|
107
|
+
end_group kind
|
|
108
|
+
when :begin_line
|
|
109
|
+
begin_line kind
|
|
110
|
+
when :end_line
|
|
111
|
+
end_line kind
|
|
112
|
+
else
|
|
113
|
+
raise ArgumentError, 'Unknown token content type: %p, kind = %p' % [content, kind]
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Called for each text token ([text, kind]), where text is a String.
|
|
118
|
+
def text_token text, kind
|
|
119
|
+
@out << text
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Starts a token group with the given +kind+.
|
|
123
|
+
def begin_group kind
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Ends a token group with the given +kind+.
|
|
127
|
+
def end_group kind
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Starts a new line token group with the given +kind+.
|
|
131
|
+
def begin_line kind
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Ends a new line token group with the given +kind+.
|
|
135
|
+
def end_line kind
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
protected
|
|
139
|
+
|
|
140
|
+
# Called with merged options before encoding starts.
|
|
141
|
+
# Sets @out to an empty string.
|
|
142
|
+
#
|
|
143
|
+
# See the HTML Encoder for an example of option caching.
|
|
144
|
+
def setup options
|
|
145
|
+
@out = get_output(options)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def get_output options
|
|
149
|
+
options[:out] || ''.dup
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Append data.to_s to the output. Returns the argument.
|
|
153
|
+
def output data
|
|
154
|
+
@out << data.to_s
|
|
155
|
+
data
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Called with merged options after encoding starts.
|
|
159
|
+
# The return value is the result of encoding, typically @out.
|
|
160
|
+
def finish options
|
|
161
|
+
@out
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Do the encoding.
|
|
165
|
+
#
|
|
166
|
+
# The already created +tokens+ object must be used; it must be a
|
|
167
|
+
# Tokens object.
|
|
168
|
+
def compile tokens, options = {}
|
|
169
|
+
content = nil
|
|
170
|
+
for item in tokens
|
|
171
|
+
if item.is_a? Array
|
|
172
|
+
raise ArgumentError, 'Two-element array tokens are no longer supported.'
|
|
173
|
+
end
|
|
174
|
+
if content
|
|
175
|
+
token content, item
|
|
176
|
+
content = nil
|
|
177
|
+
else
|
|
178
|
+
content = item
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
raise 'odd number list for Tokens' if content
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
alias tokens compile
|
|
185
|
+
public :tokens
|
|
186
|
+
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
end
|
|
190
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
# A Filter encoder has another Tokens instance as output.
|
|
5
|
+
# It can be subclass to select, remove, or modify tokens in the stream.
|
|
6
|
+
#
|
|
7
|
+
# Subclasses of Filter are called "Filters" and can be chained.
|
|
8
|
+
#
|
|
9
|
+
# == Options
|
|
10
|
+
#
|
|
11
|
+
# === :tokens
|
|
12
|
+
#
|
|
13
|
+
# The Tokens object which will receive the output.
|
|
14
|
+
#
|
|
15
|
+
# Default: Tokens.new
|
|
16
|
+
#
|
|
17
|
+
# See also: TokenKindFilter
|
|
18
|
+
class Filter < Encoder
|
|
19
|
+
|
|
20
|
+
register_for :filter
|
|
21
|
+
|
|
22
|
+
protected
|
|
23
|
+
def setup options
|
|
24
|
+
super
|
|
25
|
+
|
|
26
|
+
@tokens = options[:tokens] || Tokens.new
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def finish options
|
|
30
|
+
output @tokens
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
public
|
|
34
|
+
|
|
35
|
+
def text_token text, kind # :nodoc:
|
|
36
|
+
@tokens.text_token text, kind
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def begin_group kind # :nodoc:
|
|
40
|
+
@tokens.begin_group kind
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def begin_line kind # :nodoc:
|
|
44
|
+
@tokens.begin_line kind
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def end_group kind # :nodoc:
|
|
48
|
+
@tokens.end_group kind
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def end_line kind # :nodoc:
|
|
52
|
+
@tokens.end_line kind
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
class HTML
|
|
5
|
+
class CSS # :nodoc:
|
|
6
|
+
|
|
7
|
+
attr :stylesheet
|
|
8
|
+
|
|
9
|
+
def CSS.load_stylesheet style = nil
|
|
10
|
+
CodeRay::Styles[style]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def initialize style = :default
|
|
14
|
+
@styles = Hash.new
|
|
15
|
+
style = CSS.load_stylesheet style
|
|
16
|
+
@stylesheet = [
|
|
17
|
+
style::CSS_MAIN_STYLES,
|
|
18
|
+
style::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ')
|
|
19
|
+
].join("\n")
|
|
20
|
+
parse style::TOKEN_COLORS
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def get_style_for_css_classes css_classes
|
|
24
|
+
cl = @styles[css_classes.first]
|
|
25
|
+
return '' unless cl
|
|
26
|
+
style = ''
|
|
27
|
+
1.upto css_classes.size do |offset|
|
|
28
|
+
break if style = cl[css_classes[offset .. -1]]
|
|
29
|
+
end
|
|
30
|
+
# warn 'Style not found: %p' % [styles] if style.empty?
|
|
31
|
+
return style
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
CSS_CLASS_PATTERN = /
|
|
37
|
+
( # $1 = selectors
|
|
38
|
+
(?:
|
|
39
|
+
(?: \s* \. [-\w]+ )+
|
|
40
|
+
\s* ,?
|
|
41
|
+
)+
|
|
42
|
+
)
|
|
43
|
+
\s* \{ \s*
|
|
44
|
+
( [^\}]+ )? # $2 = style
|
|
45
|
+
\s* \} \s*
|
|
46
|
+
|
|
|
47
|
+
( [^\n]+ ) # $3 = error
|
|
48
|
+
/mx
|
|
49
|
+
def parse stylesheet
|
|
50
|
+
stylesheet.scan CSS_CLASS_PATTERN do |selectors, style, error|
|
|
51
|
+
raise "CSS parse error: '#{error.inspect}' not recognized" if error
|
|
52
|
+
for selector in selectors.split(',')
|
|
53
|
+
classes = selector.scan(/[-\w]+/)
|
|
54
|
+
cl = classes.pop
|
|
55
|
+
@styles[cl] ||= Hash.new
|
|
56
|
+
@styles[cl][classes] = style.to_s.strip.delete(' ').chomp(';')
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
class HTML
|
|
5
|
+
|
|
6
|
+
module Numbering # :nodoc:
|
|
7
|
+
|
|
8
|
+
def self.number! output, mode = :table, options = {}
|
|
9
|
+
return self unless mode
|
|
10
|
+
|
|
11
|
+
options = DEFAULT_OPTIONS.merge options
|
|
12
|
+
|
|
13
|
+
start = options[:line_number_start]
|
|
14
|
+
unless start.is_a? Integer
|
|
15
|
+
raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
anchor_prefix = options[:line_number_anchors]
|
|
19
|
+
anchor_prefix = 'line' if anchor_prefix == true
|
|
20
|
+
anchor_prefix = anchor_prefix.to_s[/[\w-]+/] if anchor_prefix
|
|
21
|
+
anchoring =
|
|
22
|
+
if anchor_prefix
|
|
23
|
+
proc do |line|
|
|
24
|
+
line = line.to_s
|
|
25
|
+
anchor = anchor_prefix + line
|
|
26
|
+
"<a href=\"##{anchor}\" name=\"#{anchor}\">#{line}</a>"
|
|
27
|
+
end
|
|
28
|
+
else
|
|
29
|
+
:to_s.to_proc
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
bold_every = options[:bold_every]
|
|
33
|
+
highlight_lines = options[:highlight_lines]
|
|
34
|
+
bolding =
|
|
35
|
+
if bold_every == false && highlight_lines == nil
|
|
36
|
+
anchoring
|
|
37
|
+
elsif highlight_lines.is_a? Enumerable
|
|
38
|
+
highlight_lines = highlight_lines.to_set
|
|
39
|
+
proc do |line|
|
|
40
|
+
if highlight_lines.include? line
|
|
41
|
+
"<strong class=\"highlighted\">#{anchoring[line]}</strong>" # highlighted line numbers in bold
|
|
42
|
+
else
|
|
43
|
+
anchoring[line]
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
elsif bold_every.is_a? Integer
|
|
47
|
+
raise ArgumentError, ":bolding can't be 0." if bold_every == 0
|
|
48
|
+
proc do |line|
|
|
49
|
+
if line % bold_every == 0
|
|
50
|
+
"<strong>#{anchoring[line]}</strong>" # every bold_every-th number in bold
|
|
51
|
+
else
|
|
52
|
+
anchoring[line]
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
else
|
|
56
|
+
raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
if position_of_last_newline = output.rindex(RUBY_VERSION >= '1.9' ? /\n/ : ?\n)
|
|
60
|
+
after_last_newline = output[position_of_last_newline + 1 .. -1]
|
|
61
|
+
ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/]
|
|
62
|
+
|
|
63
|
+
if ends_with_newline
|
|
64
|
+
line_count = output.count("\n")
|
|
65
|
+
else
|
|
66
|
+
line_count = output.count("\n") + 1
|
|
67
|
+
end
|
|
68
|
+
else
|
|
69
|
+
line_count = 1
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
case mode
|
|
73
|
+
when :inline
|
|
74
|
+
max_width = (start + line_count).to_s.size
|
|
75
|
+
line_number = start
|
|
76
|
+
output.gsub!(/^.*$\n?/) do |line|
|
|
77
|
+
line_number_text = bolding.call line_number
|
|
78
|
+
indent = ' ' * (max_width - line_number.to_s.size)
|
|
79
|
+
line_number += 1
|
|
80
|
+
"<span class=\"line-numbers\">#{indent}#{line_number_text}</span>#{line}"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
when :table
|
|
84
|
+
line_numbers = (start ... start + line_count).map(&bolding).join("\n")
|
|
85
|
+
line_numbers << "\n"
|
|
86
|
+
line_numbers_table_template = Output::TABLE.apply('LINE_NUMBERS', line_numbers)
|
|
87
|
+
|
|
88
|
+
output.gsub!(/<\/div>\n/, '</div>')
|
|
89
|
+
output.wrap_in! line_numbers_table_template
|
|
90
|
+
output.wrapped_in = :div
|
|
91
|
+
|
|
92
|
+
when :list
|
|
93
|
+
raise NotImplementedError, 'The :list option is no longer available. Use :table.'
|
|
94
|
+
|
|
95
|
+
else
|
|
96
|
+
raise ArgumentError, 'Unknown value %p for mode: expected one of %p' %
|
|
97
|
+
[mode, [:table, :inline]]
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
output
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
module CodeRay
|
|
2
|
+
module Encoders
|
|
3
|
+
|
|
4
|
+
class HTML
|
|
5
|
+
|
|
6
|
+
# This module is included in the output String of the HTML Encoder.
|
|
7
|
+
#
|
|
8
|
+
# It provides methods like wrap, div, page etc.
|
|
9
|
+
#
|
|
10
|
+
# Remember to use #clone instead of #dup to keep the modules the object was
|
|
11
|
+
# extended with.
|
|
12
|
+
#
|
|
13
|
+
# TODO: Rewrite this without monkey patching.
|
|
14
|
+
module Output
|
|
15
|
+
|
|
16
|
+
attr_accessor :css
|
|
17
|
+
|
|
18
|
+
class << self
|
|
19
|
+
|
|
20
|
+
# Raises an exception if an object that doesn't respond to to_str is extended by Output,
|
|
21
|
+
# to prevent users from misuse. Use Module#remove_method to disable.
|
|
22
|
+
def extended o # :nodoc:
|
|
23
|
+
warn "The Output module is intended to extend instances of String, not #{o.class}." unless o.respond_to? :to_str
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def make_stylesheet css, in_tag = false # :nodoc:
|
|
27
|
+
sheet = css.stylesheet
|
|
28
|
+
sheet = <<-'CSS' if in_tag
|
|
29
|
+
<style type="text/css">
|
|
30
|
+
#{sheet}
|
|
31
|
+
</style>
|
|
32
|
+
CSS
|
|
33
|
+
sheet
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def page_template_for_css css # :nodoc:
|
|
37
|
+
sheet = make_stylesheet css
|
|
38
|
+
PAGE.apply 'CSS', sheet
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def wrapped_in? element
|
|
44
|
+
wrapped_in == element
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def wrapped_in
|
|
48
|
+
@wrapped_in ||= nil
|
|
49
|
+
end
|
|
50
|
+
attr_writer :wrapped_in
|
|
51
|
+
|
|
52
|
+
def wrap_in! template
|
|
53
|
+
Template.wrap! self, template, 'CONTENT'
|
|
54
|
+
self
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def apply_title! title
|
|
58
|
+
self.sub!(/(<title>)(<\/title>)/) { $1 + title + $2 }
|
|
59
|
+
self
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def wrap! element, *args
|
|
63
|
+
return self if not element or element == wrapped_in
|
|
64
|
+
case element
|
|
65
|
+
when :div
|
|
66
|
+
raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? nil
|
|
67
|
+
wrap_in! DIV
|
|
68
|
+
when :span
|
|
69
|
+
raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? nil
|
|
70
|
+
wrap_in! SPAN
|
|
71
|
+
when :page
|
|
72
|
+
wrap! :div if wrapped_in? nil
|
|
73
|
+
raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? :div
|
|
74
|
+
wrap_in! Output.page_template_for_css(@css)
|
|
75
|
+
if args.first.is_a?(Hash) && title = args.first[:title]
|
|
76
|
+
apply_title! title
|
|
77
|
+
end
|
|
78
|
+
self
|
|
79
|
+
else
|
|
80
|
+
raise "Unknown value %p for :wrap" % element
|
|
81
|
+
end
|
|
82
|
+
@wrapped_in = element
|
|
83
|
+
self
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def stylesheet in_tag = false
|
|
87
|
+
Output.make_stylesheet @css, in_tag
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
#-- don't include the templates in docu
|
|
91
|
+
|
|
92
|
+
class Template < String # :nodoc:
|
|
93
|
+
|
|
94
|
+
def self.wrap! str, template, target
|
|
95
|
+
target = Regexp.new(Regexp.escape("<%#{target}%>"))
|
|
96
|
+
if template =~ target
|
|
97
|
+
str[0,0] = $`
|
|
98
|
+
str << $'
|
|
99
|
+
else
|
|
100
|
+
raise "Template target <%%%p%%> not found" % target
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def apply target, replacement
|
|
105
|
+
target = Regexp.new(Regexp.escape("<%#{target}%>"))
|
|
106
|
+
if self =~ target
|
|
107
|
+
Template.new($` + replacement + $')
|
|
108
|
+
else
|
|
109
|
+
raise "Template target <%%%p%%> not found" % target
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
SPAN = Template.new '<span class="CodeRay"><%CONTENT%></span>'
|
|
116
|
+
|
|
117
|
+
DIV = Template.new <<-DIV
|
|
118
|
+
<div class="CodeRay">
|
|
119
|
+
<div class="code"><pre><%CONTENT%></pre></div>
|
|
120
|
+
</div>
|
|
121
|
+
DIV
|
|
122
|
+
|
|
123
|
+
TABLE = Template.new <<-TABLE
|
|
124
|
+
<table class="CodeRay"><tr>
|
|
125
|
+
<td class="line-numbers"><pre><%LINE_NUMBERS%></pre></td>
|
|
126
|
+
<td class="code"><pre><%CONTENT%></pre></td>
|
|
127
|
+
</tr></table>
|
|
128
|
+
TABLE
|
|
129
|
+
|
|
130
|
+
PAGE = Template.new <<-PAGE
|
|
131
|
+
<!DOCTYPE html>
|
|
132
|
+
<html>
|
|
133
|
+
<head>
|
|
134
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
135
|
+
<title></title>
|
|
136
|
+
<style type="text/css">
|
|
137
|
+
.CodeRay .line-numbers a {
|
|
138
|
+
text-decoration: inherit;
|
|
139
|
+
color: inherit;
|
|
140
|
+
}
|
|
141
|
+
body {
|
|
142
|
+
background-color: white;
|
|
143
|
+
padding: 0;
|
|
144
|
+
margin: 0;
|
|
145
|
+
}
|
|
146
|
+
<%CSS%>
|
|
147
|
+
.CodeRay {
|
|
148
|
+
border: none;
|
|
149
|
+
}
|
|
150
|
+
</style>
|
|
151
|
+
</head>
|
|
152
|
+
<body>
|
|
153
|
+
|
|
154
|
+
<%CONTENT%>
|
|
155
|
+
</body>
|
|
156
|
+
</html>
|
|
157
|
+
PAGE
|
|
158
|
+
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
end
|
|
164
|
+
end
|