Class: Livetext::Formatter::Delimited

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

Direct Known Subclasses

Bracketed, Double, Single

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, marker, tag) ⇒ Delimited

Delimited



21
22
23
24
25
26
# File 'lib/livetext/formatter.rb', line 21

def initialize(str, marker, tag)    # Delimited
  @str, @marker, @tag = str.dup, marker, tag
  @buffer = ""
  @cdata  = ""
  @state  = :INITIAL
end

Class Method Details

.process(str) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/livetext/formatter.rb', line 155

def self.process(str)
  bold = self.new(str, "*", "b")
  sb   = bold.handle
# return sb
  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

#bufferObject



104
105
106
107
# File 'lib/livetext/formatter.rb', line 104

def buffer
  @buffer << grab
  @state = :LOOPING
end

#cdataObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/livetext/formatter.rb', line 109

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    # "*a *b"  case???
    @cdata = ""
    @state = :LOOPING
  else
    @cdata << grab
    @state = :CDATA
  end
end

#eol?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/livetext/formatter.rb', line 51

def eol?
  @str.empty?
end

#escape?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/livetext/formatter.rb', line 59

def escape?
  front == "\\"
end

#frontObject



35
36
37
# File 'lib/livetext/formatter.rb', line 35

def front
  @str[0]
end

#grab(n = 1) ⇒ Object



39
40
41
42
# File 'lib/livetext/formatter.rb', line 39

def grab(n=1)
  char = @str.slice!(0..(n-1))   # grab n chars
  char
end

#grab_terminatorObject



44
45
46
47
48
49
# File 'lib/livetext/formatter.rb', line 44

def grab_terminator
  @state = :LOOPING
  # goes onto buffer by default
  # Don't? what if searching for space_marker?
  # @buffer << grab  
end

#handleObject



146
147
148
149
150
151
152
153
# File 'lib/livetext/formatter.rb', line 146

def handle
  loop do
    break if @state == :FINAL
    meth = @state.downcase
    send(meth)
  end
  return @buffer
end

#initialObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/livetext/formatter.rb', line 84

def initial
  n = @marker.length
  case
  when escape?
    grab               # backslash
    @buffer << grab    # char
  when space_marker?
    @buffer << grab   # append the space
    grab(n)           # eat the marker
    @state = :CDATA
  when marker?
    grab(n)  # Eat the marker
    @state = :CDATA
  when eol?
    @state = :FINAL
  else
    @state = :BUFFER
  end
end

#loopingObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/livetext/formatter.rb', line 129

def looping
  n = @marker.length
  case
  when escape?
    grab               # backslash
    @buffer << grab    # char
  when space_marker?
    @buffer << grab   # append the space
    grab(n)           # eat the marker
    @state = :CDATA
  when eol?
    @state = :FINAL
  else   # includes marker not preceded by space!
    @buffer << grab
  end
end

#marker?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/livetext/formatter.rb', line 67

def marker?
  @str.start_with?(@marker)
end

#space?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/livetext/formatter.rb', line 55

def space?
  front == " "
end

#space_marker?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/livetext/formatter.rb', line 71

def space_marker?
  @str.start_with?(" " + @marker)
end

#status(where) ⇒ Object



28
29
30
31
32
33
# File 'lib/livetext/formatter.rb', line 28

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

Returns:

  • (Boolean)


63
64
65
# File 'lib/livetext/formatter.rb', line 63

def terminated?
  space?   # Will be overridden except in Single
end

#wrap(text) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/livetext/formatter.rb', line 75

def wrap(text)
  if text.empty?
    result = @marker
    result = "" if @marker[1] == "["
    return result
  end
  "<#{@tag}>#{text}</#{@tag}>"
end