Skip to content

Commit 88b8122

Browse files
committed
refactor check functionality to DebugLint encoder
1 parent df21150 commit 88b8122

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

Changes.textile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ h2. Changes in 1.1
2020
* Override Bootstrap's pre word-break setting for line numbers [#102, thanks to lightswitch05]
2121
* Fixed @:docstring@ token type style
2222
* @Plugin@ does not warn about fallback when default is defined
23+
* @Debug@ encoder refactored; use @DebugLint@ if you want strict checking now
2324

2425
h2. Changes in 1.0.9
2526

lib/coderay/encoders/debug.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,7 @@ class Debug < Encoder
1818

1919
FILE_EXTENSION = 'raydebug'
2020

21-
def initialize options = {}
22-
super
23-
@opened = []
24-
end
25-
2621
def text_token text, kind
27-
raise 'empty token' if $CODERAY_DEBUG && text.empty?
28-
2922
if kind == :space
3023
@out << text
3124
else
@@ -36,26 +29,18 @@ def text_token text, kind
3629
end
3730

3831
def begin_group kind
39-
@opened << kind if $CODERAY_DEBUG
40-
4132
@out << "#{kind}<"
4233
end
4334

4435
def end_group kind
45-
raise "We are inside #{@opened.inspect}, not #{kind}" if $CODERAY_DEBUG && @opened.pop != kind
46-
4736
@out << '>'
4837
end
4938

5039
def begin_line kind
51-
@opened << kind if $CODERAY_DEBUG
52-
5340
@out << "#{kind}["
5441
end
5542

5643
def end_line kind
57-
raise "We are inside #{@opened.inspect}, not #{kind}" if $CODERAY_DEBUG && @opened.pop != kind
58-
5944
@out << ']'
6045
end
6146

lib/coderay/encoders/debug_lint.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module CodeRay
2+
module Encoders
3+
4+
# = Debug Lint Encoder
5+
#
6+
# Debug encoder with additional checks for:
7+
#
8+
# - empty tokens
9+
# - incorrect nesting
10+
#
11+
# It will raise an InvalidTokenStream exception when any of the above occurs.
12+
#
13+
# See also: Encoders::Debug
14+
class DebugLint < Debug
15+
16+
register_for :debug_lint
17+
18+
InvalidTokenStream = Class.new StandardError
19+
EmptyToken = Class.new InvalidTokenStream
20+
IncorrectTokenGroupNesting = Class.new InvalidTokenStream
21+
22+
def initialize options = {}
23+
super
24+
@opened = []
25+
end
26+
27+
def text_token text, kind
28+
raise EmptyToken, 'empty token' if text.empty?
29+
super
30+
end
31+
32+
def begin_group kind
33+
@opened << kind
34+
super
35+
end
36+
37+
def end_group kind
38+
raise IncorrectTokenGroupNesting, "We are inside #{@opened.inspect}, not #{kind}" if @opened.pop != kind
39+
super
40+
end
41+
42+
def begin_line kind
43+
@opened << kind
44+
super
45+
end
46+
47+
def end_line kind
48+
raise IncorrectTokenGroupNesting, "We are inside #{@opened.inspect}, not #{kind}" if @opened.pop != kind
49+
super
50+
end
51+
52+
end
53+
54+
end
55+
end

0 commit comments

Comments
 (0)