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,Metrics/MethodLength



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/almirah/doc_items/text_line.rb', line 74

def initialize # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  @supported_tokens = []
  @supported_tokens.append(BoldAndItalicToken.new)
  @supported_tokens.append(BoldToken.new)
  @supported_tokens.append(ItalicToken.new)
  @supported_tokens.append(BacktickToken.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.



72
73
74
# File 'lib/almirah/doc_items/text_line.rb', line 72

def supported_tokens
  @supported_tokens
end

Instance Method Details

#tokenize(str) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/AbcSize,Metrics/PerceivedComplexity



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

def tokenize(str) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/AbcSize,Metrics/PerceivedComplexity
  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]
        next unless buf == t.value

        if emphasis_token?(t) && !can_flank?(str, si, projected_end_position)
          append_literal(result, buf)
        else
          result.append(t)
        end
        si = projected_end_position + 1
        break
      else
        append_literal(result, str[si])
        si += 1
      end
    end
  end
  fuse_backticks(result)
end