Class: TextLineBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/almirah/doc_items/text_line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder_context) ⇒ TextLineBuilder

Returns a new instance of TextLineBuilder.



128
129
130
# File 'lib/almirah/doc_items/text_line.rb', line 128

def initialize(builder_context)
  @builder_context = builder_context
end

Instance Attribute Details

#builder_contextObject

Returns the value of attribute builder_context.



126
127
128
# File 'lib/almirah/doc_items/text_line.rb', line 126

def builder_context
  @builder_context
end

Instance Method Details

#restore(token_list) ⇒ Object



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
# File 'lib/almirah/doc_items/text_line.rb', line 132

def restore(token_list)
  result = ''
  return '' if token_list.nil?

  sub_list_url_text = nil
  sub_list_url_address = nil
  tl = token_list.length
  ti = 0
  while ti < tl
    case token_list[ti].class.name
    when 'ItalicToken'
      is_found = false
      ti_starting_position = ti
      # try to find closing part
      tii = ti + 1
      while tii < tl
        if token_list[tii].instance_of? ItalicToken
          sub_list = token_list[(ti + 1)..(tii - 1)]
          result += @builder_context.italic(restore(sub_list))
          ti = tii + 1
          is_found = true
          break
        end
        tii += 1
      end
      unless is_found
        result += '*'
        ti = ti_starting_position + 1
      end
    when 'BoldToken'
      is_found = false
      ti_starting_position = ti
      # try to find closing part
      tii = ti + 1
      while tii < tl
        if token_list[tii].instance_of? BoldToken
          sub_list = token_list[(ti + 1)..(tii - 1)]
          result += @builder_context.bold(restore(sub_list))
          ti = tii + 1
          is_found = true
          break
        end
        tii += 1
      end
      unless is_found
        result += '**'
        ti = ti_starting_position + 1
      end
    when 'BoldAndItalicToken'
      is_found = false
      ti_starting_position = ti
      # try to find closing part
      tii = ti + 1
      while tii < tl
        if token_list[tii].instance_of? BoldAndItalicToken
          sub_list = token_list[(ti + 1)..(tii - 1)]
          result += @builder_context.bold_and_italic(restore(sub_list))
          ti = tii + 1
          is_found = true
          break
        end
        tii += 1
      end
      unless is_found
        result += '***'
        ti = ti_starting_position + 1
      end
    when 'SquareBracketLeft'
      # try to find closing part
      is_found = false
      tii = ti + 1
      ti_starting_position = ti
      while tii < tl
        case token_list[tii].class.name
        when 'SquareBracketRightAndParentheseLeft'
          sub_list_url_text = token_list[(ti + 1)..(tii - 1)]
          ti = tii + 1
          tiii = ti
          while tiii < tl
            case token_list[tiii].class.name
            when 'ParentheseRight'
              sub_list_url_address = token_list[(tii + 1)..(tiii - 1)]
              ti = tiii + 1
              is_found = true
              break
            end
            tiii += 1
          end
          break
        when 'SquareBracketRight'
          break
        end
        tii += 1
      end
      if is_found
        result += @builder_context.link(restore(sub_list_url_text), restore(sub_list_url_address))
      else
        result += '['
        ti = ti_starting_position + 1
      end

    when 'TextLineToken', 'ParentheseLeft', 'ParentheseRight', 'SquareBracketRight'
      result += token_list[ti].value
      ti += 1
    else
      ti += 1
    end
  end
  result
end