Class: Formatter::Delimited
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(str, marker, tag) ⇒ Delimited
Returns a new instance of Delimited.
20
21
22
23
24
25
|
# File 'lib/livetext/formatter.rb', line 20
def initialize(str, marker, tag)
@str, @marker, @tag = str.dup, marker, tag
@buffer = ""
@cdata = ""
@state = :INITIAL
end
|
Class Method Details
.process(str) ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/livetext/formatter.rb', line 174
def self.process(str)
bold = self.new(str, "*", "b")
sb = bold.handle
ital = self.new(sb, "_", "i")
si = ital.handle
code = self.new(si, "`", "tt")
sc = code.handle
stri = self.new(sc, "~", "strike")
si = stri.handle
si
end
|
Instance Method Details
113
114
115
116
117
|
# File 'lib/livetext/formatter.rb', line 113
def buffer
status("** buffer 0")
@buffer << grab
@state = :LOOPING
end
|
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/livetext/formatter.rb', line 119
def cdata
status("** cdata 0")
case
when eol?
status("** cdata 1")
if @cdata.empty?
status("** cdata 2")
@buffer << @marker unless @marker[1] == "["
else
status("** cdata 3")
@buffer << wrap(@cdata)
end
@state = :FINAL
when terminated?
status("** cdata 4")
@buffer << wrap(@cdata)
grab_terminator
@cdata = ""
@state = :LOOPING
else
status("** cdata 5")
@cdata << grab
@state = :CDATA
end
end
|
#eol? ⇒ Boolean
51
52
53
|
# File 'lib/livetext/formatter.rb', line 51
def eol?
@str.empty?
end
|
#escape? ⇒ Boolean
59
60
61
|
# File 'lib/livetext/formatter.rb', line 59
def escape?
front == "\\"
end
|
34
35
36
|
# File 'lib/livetext/formatter.rb', line 34
def front
@str[0]
end
|
#grab(n = 1) ⇒ Object
38
39
40
41
|
# File 'lib/livetext/formatter.rb', line 38
def grab(n=1)
char = @str.slice!(0..(n-1))
char
end
|
#grab_terminator ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/livetext/formatter.rb', line 43
def grab_terminator
status("** grabterm 0")
@state = :LOOPING
end
|
165
166
167
168
169
170
171
172
|
# File 'lib/livetext/formatter.rb', line 165
def handle
loop do
break if @state == :FINAL
meth = @state.downcase
send(meth)
end
return @buffer
end
|
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/livetext/formatter.rb', line 85
def initial
status("** init 0")
n = @marker.length
case
when escape?
status("** esc i0")
grab
status("** esc i1")
@buffer << grab
status("** esc i2")
when space_marker?
status("** init 1")
@buffer << grab
grab(n)
@state = :CDATA
when marker?
status("** init 2")
grab(n)
@state = :CDATA
when eol?
status("** init 3")
@state = :FINAL
else
status("** init 4")
@state = :BUFFER
end
end
|
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/livetext/formatter.rb', line 145
def looping
n = @marker.length
case
when escape?
status("** esc l0")
grab
status("** esc l1")
@buffer << grab
status("** esc l2")
when space_marker?
@buffer << grab
grab(n)
@state = :CDATA
when eol?
@state = :FINAL
else
@buffer << grab
end
end
|
#marker? ⇒ Boolean
67
68
69
|
# File 'lib/livetext/formatter.rb', line 67
def marker?
@str.start_with?(@marker)
end
|
#space? ⇒ Boolean
55
56
57
|
# File 'lib/livetext/formatter.rb', line 55
def space?
front == " "
end
|
#space_marker? ⇒ Boolean
71
72
73
|
# File 'lib/livetext/formatter.rb', line 71
def space_marker?
@str.start_with?(" " + @marker)
end
|
#status(where) ⇒ Object
27
28
29
30
31
32
|
# File 'lib/livetext/formatter.rb', line 27
def status(where)
if $debug
STDERR.printf "%-11s %-7s #{@marker.inspect} \n #{' '*11} state = %-8s str = %-20s buffer = %-20s cdata = %-20s\n",
where, self.class, @state, @str.inspect, @buffer.inspect, @cdata.inspect
end
end
|
#terminated? ⇒ Boolean
63
64
65
|
# File 'lib/livetext/formatter.rb', line 63
def terminated?
space?
end
|
#wrap(text) ⇒ Object
75
76
77
78
79
80
81
82
83
|
# File 'lib/livetext/formatter.rb', line 75
def wrap(text)
status("** wrap 0")
if text.empty?
result = @marker
result = "" if @marker[1] == "["
return result
end
"<#{@tag}>#{text}</#{@tag}>"
end
|