Class: TextLineParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTextLineParser

rubocop:disable Metrics/AbcSize



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/almirah/doc_items/text_line.rb', line 60

def initialize # rubocop:disable Metrics/AbcSize
  @supported_tokens = []
  @supported_tokens.append(BoldAndItalicToken.new)
  @supported_tokens.append(BoldToken.new)
  @supported_tokens.append(ItalicToken.new)
  @supported_tokens.append(SquareBracketRightAndParentheseLeft.new)
  @supported_tokens.append(ParentheseLeft.new)
  @supported_tokens.append(ParentheseRight.new)
  @supported_tokens.append(SquareBracketLeft.new)
  @supported_tokens.append(SquareBracketRight.new)
  @supported_tokens.append(TextLineToken.new)
end

Instance Attribute Details

#supported_tokensObject

Returns the value of attribute supported_tokens.



58
59
60
# File 'lib/almirah/doc_items/text_line.rb', line 58

def supported_tokens
  @supported_tokens
end

Instance Method Details

#tokenize(str) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/almirah/doc_items/text_line.rb', line 73

def tokenize(str)
  result = []
  sl = str.length
  si = 0
  while si < sl
    @supported_tokens.each do |t|
      tl = t.value.length
      if tl != 0 # literal is the last supported token in the list
        projected_end_position = si + tl - 1
        next if projected_end_position > sl

        buf = str[si..projected_end_position]
        if buf == t.value
          result.append(t)
          si = projected_end_position + 1
          break
        end
      else
        if result.length.positive? && (result[-1].instance_of? TextLineToken)
          literal = result[-1]
          literal.value += str[si]
        else
          literal = TextLineToken.new
          literal.value = str[si]
          result.append(literal)
        end
        si += 1
      end
    end
  end
  result
end