Class: Rouge::Lexers::ConsoleLexer
Constant Summary
Token::Tokens::Num, Token::Tokens::Str
Instance Attribute Summary
Attributes inherited from Rouge::Lexer
#options
Instance Method Summary
collapse
aliases, all, #as_bool, #as_lexer, #as_list, #as_string, #as_token, assert_utf8!, #bool_option, debug_enabled?, demo, demo_file, desc, detect?, disable_debug!, enable_debug!, filenames, find, find_fancy, guess, guess_by_filename, guess_by_mimetype, guess_by_source, guesses, #hash_option, lex, #lex, #lexer_option, #list_option, mimetypes, option, option_docs, #reset!, #string_option, tag, #tag, title, #token_option
token
Constructor Details
Returns a new instance of ConsoleLexer.
17
18
19
20
21
22
23
|
# File 'lib/rouge/lexers/console.rb', line 17
def initialize(*)
super
@prompt = list_option(:prompt) { nil }
@lang = lexer_option(:lang) { 'shell' }
@output = lexer_option(:output) { PlainText.new(token: Generic::Output) }
@comments = bool_option(:comments) { :guess }
end
|
Instance Method Details
whether to allow comments. if manually specifying a prompt that isn’t simply “#”, we flag this to on
41
42
43
44
45
46
47
48
|
# File 'lib/rouge/lexers/console.rb', line 41
def
case @comments
when :guess
@prompt && !@prompt.empty? && !end_chars.include?('#')
else
@comments
end
end
|
88
89
90
|
# File 'lib/rouge/lexers/console.rb', line 88
def
/\A\s*?#/
end
|
#end_chars ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/rouge/lexers/console.rb', line 31
def end_chars
@end_chars ||= if @prompt.any?
@prompt.reject { |c| c.empty? }
else
%w($ # > ;)
end
end
|
#lang_lexer ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/rouge/lexers/console.rb', line 58
def lang_lexer
@lang_lexer ||= case @lang
when Lexer
@lang
when nil
Shell.new(options)
when Class
@lang.new(options)
when String
Lexer.find(@lang).new(options)
end
end
|
#line_regex ⇒ Object
84
85
86
|
# File 'lib/rouge/lexers/console.rb', line 84
def line_regex
/(\\.|[^\\])*?(\n|$)/m
end
|
#output_lexer ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/rouge/lexers/console.rb', line 71
def output_lexer
@output_lexer ||= case @output
when nil
PlainText.new(token: Generic::Output)
when Lexer
@output
when Class
@output.new(options)
when String
Lexer.find(@output).new(options)
end
end
|
#process_line(input, &output) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/rouge/lexers/console.rb', line 100
def process_line(input, &output)
input.scan(line_regex)
if input[0] =~ /\A\s*(?:<[.]+>|[.]+)\s*\z/
puts "console: matched snip #{input[0].inspect}" if @debug
output_lexer.reset!
lang_lexer.reset!
yield Comment, input[0]
elsif prompt_regex =~ input[0]
puts "console: matched prompt #{input[0].inspect}" if @debug
output_lexer.reset!
yield Generic::Prompt, $&
$' =~ /\A\s*/
yield Text, $& unless $&.empty?
lang_lexer.lex($', continue: true, &output)
elsif =~ input[0].strip
puts "console: matched comment #{input[0].inspect}" if @debug
output_lexer.reset!
lang_lexer.reset!
yield Comment, input[0]
else
puts "console: matched output #{input[0].inspect}" if @debug
lang_lexer.reset!
output_lexer.lex(input[0], continue: true, &output)
end
end
|
#prompt_prefix_regex ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/rouge/lexers/console.rb', line 50
def prompt_prefix_regex
if
/[^<#]*?/m
else
/.*?/m
end
end
|
#prompt_regex ⇒ Object
25
26
27
28
29
|
# File 'lib/rouge/lexers/console.rb', line 25
def prompt_regex
@prompt_regex ||= begin
/^#{prompt_prefix_regex}(?:#{end_chars.map(&Regexp.method(:escape)).join('|')})/
end
end
|
#stream_tokens(input, &output) ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/rouge/lexers/console.rb', line 92
def stream_tokens(input, &output)
input = StringScanner.new(input)
lang_lexer.reset!
output_lexer.reset!
process_line(input, &output) while !input.eos?
end
|