Class: FormatLine

Inherits:
Object
  • Object
show all
Defined in:
lib/formatline.rb

Constant Summary collapse

EOL =
:eol
Alpha =
/[a-z]/
Alpha2 =
/[a-z0-9_]/
Other =
Object
SimpleFormats =
{}

Instance Method Summary collapse

Constructor Details

#initializeFormatLine

Returns a new instance of FormatLine.



13
14
15
# File 'lib/formatline.rb', line 13

def initialize
  @buffer, @vname, @fname, @param, @substr = "", "", "", "", ""
end

Instance Method Details

#boldObject



68
69
70
71
72
# File 'lib/formatline.rb', line 68

def bold
  d0, d1 = SimpleFormats[:b]
  @buffer << "#{d0}#@substr#{d1}"
  @substr = ""
end

#emit(str = "") ⇒ Object



43
44
45
# File 'lib/formatline.rb', line 43

def emit(str = "")
  @buffer << str
end

#fcallObject



63
64
65
66
# File 'lib/formatline.rb', line 63

def fcall
  @buffer << funcall(@fname, @param)
  @fname, @param = "", ""
end

#funcall(name, param) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/formatline.rb', line 47

def funcall(name, param)
  if self.respond_to?("func_" + name.to_s)
    self.send("func_" + name.to_s, param)
  else
    fobj = ::Livetext::Functions.new
    ::Livetext::Functions.param = param       # is this screwed up???
    ::Livetext::Functions.context = @context   # is this screwed up???
    fobj.send(name)
  end
end

#grabObject



23
24
25
26
27
# File 'lib/formatline.rb', line 23

def grab
  @enum.next
rescue StopIteration
  EOL
end

#italicsObject



80
81
82
83
84
# File 'lib/formatline.rb', line 80

def italics
  d0, d1 = SimpleFormats[:i]
  @buffer << "#{d0}#@substr#{d1}"
  @substr = ""
end

#keep(initial = "") ⇒ Object



36
37
38
39
40
41
# File 'lib/formatline.rb', line 36

def keep(initial = "")
  @buffer << initial
  @buffer << @enum.next
rescue StopIteration
  EOL
end

#parse(line, context = nil) ⇒ Object



92
93
94
95
96
97
98
99
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/formatline.rb', line 92

def parse(line, context = nil)
  context ||= binding
  @context = context
  @enum = line.chomp.each_char
  @buffer = ""
  @substr = ""
  @fname  = ""
  @vname  = ""
  @param  = ""
  
  loop do   # starting state
    char = peek
#   puts "char = #{char.inspect}"
    case char
      when "\\"
        char = skip
        case char
          when "$", "*", "_", "`", "~"
            emit(char)
            skip
          when " "
            emit("\\ ")
            skip
          when EOL
            emit("\\")
            break
          when Other
            emit("\\")   # logic??
        end
      when EOL
        break
      when "$"        # var or func or $
        case skip
          when EOL
            emit("$")
            break
          when Alpha
            loop { ch = peek; break if ch == EOL; @vname << grab; break unless Alpha2 === peek } 
            vsub
          when "$" 
            case skip
              when EOL
                emit("$$")
                break
              when Alpha
                loop { ch = peek; break if ch == EOL; @fname << grab; break unless Alpha2 === peek }
                case peek
                  when " "   # no param - just call
                    @param = nil
                    fcall    # no param? Hmm
                  when "["   # long param
                    skip
                    loop do 
                      if peek == "\\"
                        skip
                        @param << grab
                      end
                      break if ["]", EOL].include?(peek)
                      @param << grab
                    end
                    skip
                    fcall
                  when ":"   # param (single token or to-eol)
                    case skip
                      when ":"   # param to eol
                        skip
                        loop { break if peek == EOL; @param << grab }
                      when Other # grab until space or eol
                        loop { @param << grab; break if [" ", EOL].include?(peek) }
                        fcall
                    end
                  when Other # no param - just call
                    fcall
                end
              when Other
                emit "$$"
            end
          when Other
            emit "$"
        end
      when "*"
        case skip
          when EOL
            emit "*"
          when " "
            emit "*"
          when "["
            skip
            loop do
              if peek == "\\"
                skip
                @substr << grab
                next
              end
              break if ["]", EOL].include?(peek)
              @substr << grab
            end
            skip
            bold
          when Other
            loop { @substr << grab; break if [" ", EOL].include?(peek) }
            bold
        end
      when "_"
        case skip
          when EOL
            emit "_"
          when " "
            emit "_"
          when "["
            skip
            loop do
              if peek == "\\"
                skip
                @substr << grab
                next
              end
              break if ["]", EOL].include?(peek)
              @substr << grab
            end
            skip
            italics
          when "_"   # doubled...
            skip
            loop do
              if peek == "\\"
                skip
                @substr << grab
                next
              end
              break if [".", ",", ")", EOL].include?(peek)
              @substr << grab
            end
            italics
          when Other
            loop { @substr << grab; break if [" ", EOL].include?(peek) }
            italics
        end
      when "`"
        case skip
          when EOL
            emit "`"
          when " "
            emit "`"
          when "["
            skip
            loop do
              if peek == "\\"
                skip
                @substr << grab
                next
              end
              break if ["]", EOL].include?(peek)
              @substr << grab
            end
            skip
            ttype
          when "`"   # doubled...
            skip
            loop { break if [".", ",", ")", EOL].include?(peek); @substr << grab }   # ";" ?? FIXME
            ttype
          when Other
            loop { @substr << grab; break if [" ", EOL].include?(peek) }
            ttype
        end
      when "~"
        case skip
          when EOL
            emit "~"
          when " "
            emit "~"
          when "["
            skip
            loop do
              if peek == "\\"
                skip
                @substr << grab
                next
              end
              break if ["]", EOL].include?(peek)
              @substr << grab
            end
            skip
            strike
          when "~"   # doubled...
            skip
            loop { break if [".", ",", ")", EOL].include?(peek); @substr << grab }   # ";" ?? FIXME
            strike
          when Other
            loop { @substr << grab; break if [" ", EOL].include?(peek) }
            strike
        end
      when Other
        keep
    end
  end

  @buffer
end

#peekObject



17
18
19
20
21
# File 'lib/formatline.rb', line 17

def peek
  @enum.peek
rescue StopIteration
  EOL
end

#skipObject



29
30
31
32
33
34
# File 'lib/formatline.rb', line 29

def skip
  @enum.next
  @enum.peek
rescue StopIteration
  EOL
end

#strikeObject



86
87
88
89
90
# File 'lib/formatline.rb', line 86

def strike
  d0, d1 = SimpleFormats[:s]
  @buffer << "#{d0}#@substr#{d1}"
  @substr = ""
end

#ttypeObject



74
75
76
77
78
# File 'lib/formatline.rb', line 74

def ttype
  d0, d1 = SimpleFormats[:t]
  @buffer << "#{d0}#@substr#{d1}"
  @substr = ""
end

#vsubObject



58
59
60
61
# File 'lib/formatline.rb', line 58

def vsub
  @buffer << Livetext::Vars[@vname]
  @vname = ""
end