Class: Formatter::Delimited
Overview
Hmmm…
Double: b, i, t, s
Single: bits
Brackt: bits
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(str, marker, tag) ⇒ Delimited
19
20
21
22
23
24
|
# File 'lib/livetext/formatter.rb', line 19
def initialize(str, marker, tag)
@str, @marker, @tag = str.dup, marker, tag
@buffer = ""
@cdata = ""
@state = :INITIAL
end
|
Class Method Details
.process(str) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/livetext/formatter.rb', line 153
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
102
103
104
105
|
# File 'lib/livetext/formatter.rb', line 102
def buffer
@buffer << grab
@state = :LOOPING
end
|
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/livetext/formatter.rb', line 107
def cdata
case
when eol?
if @cdata.empty?
@buffer << @marker unless @marker[1] == "["
else
@buffer << wrap(@cdata)
end
@state = :FINAL
when terminated?
@buffer << wrap(@cdata)
grab_terminator
@cdata = ""
@state = :LOOPING
else
@cdata << grab
@state = :CDATA
end
end
|
#eol? ⇒ Boolean
49
50
51
|
# File 'lib/livetext/formatter.rb', line 49
def eol?
@str.empty?
end
|
#escape? ⇒ Boolean
57
58
59
|
# File 'lib/livetext/formatter.rb', line 57
def escape?
front == "\\"
end
|
33
34
35
|
# File 'lib/livetext/formatter.rb', line 33
def front
@str[0]
end
|
#grab(n = 1) ⇒ Object
37
38
39
40
|
# File 'lib/livetext/formatter.rb', line 37
def grab(n=1)
char = @str.slice!(0..(n-1))
char
end
|
#grab_terminator ⇒ Object
42
43
44
45
46
47
|
# File 'lib/livetext/formatter.rb', line 42
def grab_terminator
@state = :LOOPING
end
|
144
145
146
147
148
149
150
151
|
# File 'lib/livetext/formatter.rb', line 144
def handle
loop do
break if @state == :FINAL
meth = @state.downcase
send(meth)
end
return @buffer
end
|
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/livetext/formatter.rb', line 82
def initial
n = @marker.length
case
when escape?
grab
@buffer << grab
when space_marker?
@buffer << grab
grab(n)
@state = :CDATA
when marker?
grab(n)
@state = :CDATA
when eol?
@state = :FINAL
else
@state = :BUFFER
end
end
|
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/livetext/formatter.rb', line 127
def looping
n = @marker.length
case
when escape?
grab
@buffer << grab
when space_marker?
@buffer << grab
grab(n)
@state = :CDATA
when eol?
@state = :FINAL
else
@buffer << grab
end
end
|
#marker? ⇒ Boolean
65
66
67
|
# File 'lib/livetext/formatter.rb', line 65
def marker?
@str.start_with?(@marker)
end
|
#space? ⇒ Boolean
53
54
55
|
# File 'lib/livetext/formatter.rb', line 53
def space?
front == " "
end
|
#space_marker? ⇒ Boolean
69
70
71
|
# File 'lib/livetext/formatter.rb', line 69
def space_marker?
@str.start_with?(" " + @marker)
end
|
#status(where) ⇒ Object
26
27
28
29
30
31
|
# File 'lib/livetext/formatter.rb', line 26
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
61
62
63
|
# File 'lib/livetext/formatter.rb', line 61
def terminated?
space?
end
|
#wrap(text) ⇒ Object
73
74
75
76
77
78
79
80
|
# File 'lib/livetext/formatter.rb', line 73
def wrap(text)
if text.empty?
result = @marker
result = "" if @marker[1] == "["
return result
end
"<#{@tag}>#{text}</#{@tag}>"
end
|