blob: 8c66d3811fdf5d183ed9ce44b17391034600a3af (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Lexers
class Escape < Lexer
tag 'escape'
aliases 'esc'
desc 'A generic lexer for including escaped content - see Formatter.enable_escape!'
option :start, 'the beginning of the escaped section, default "<!"'
option :end, 'the end of the escaped section, e.g. "!>"'
option :lang, 'the language to lex in unescaped sections'
attr_reader :start
attr_reader :end
attr_reader :lang
def initialize(*)
super
@start = string_option(:start) { '<!' }
@end = string_option(:end) { '!>' }
@lang = lexer_option(:lang) { PlainText.new }
end
def to_start_regex
@to_start_regex ||= /(.*?)(#{Regexp.escape(@start)})/m
end
def to_end_regex
@to_end_regex ||= /(.*?)(#{Regexp.escape(@end)})/m
end
def stream_tokens(str, &b)
stream = StringScanner.new(str)
loop do
if stream.scan(to_start_regex)
puts "pre-escape: #{stream[1].inspect}" if @debug
@lang.continue_lex(stream[1], &b)
else
# no more start delimiters, scan til the end
@lang.continue_lex(stream.rest, &b)
return
end
if stream.scan(to_end_regex)
yield Token::Tokens::Escape, stream[1]
else
yield Token::Tokens::Escape, stream.rest
return
end
end
end
end
end
end
|