Class: PointBlank::Parsing::CodeInline

Inherits:
NullInline
  • Object
show all
Defined in:
lib/mmmd/blankshell.rb

Overview

Code inline parser

Class Method Summary collapse

Methods inherited from NullInline

build, check_contents, check_unescaped, construct_literal, construct_text, find_unescaped, iterate_tokens, reverse_walk

Class Method Details

.forward_walk(parts) ⇒ Object

TODO: optimize, buffer only after walking (see ::PointBlank::Parsing::NullInline#forward_walk)



1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
# File 'lib/mmmd/blankshell.rb', line 1165

def self.forward_walk(parts)
  buffer = ""
  opening = parts.first.first
  cutoff = 0
  parts.each_with_index do |part, idx|
    text = (part.is_a?(Array) ? part.first : part)
    buffer += text
    next unless part.is_a? Array

    break (cutoff = idx) if part.first == opening &&
                            part.last == :close
  end
  buffer = construct_literal(buffer[opening.length..(-1 - opening.length)])
  [cutoff.positive? ? build([buffer]) : opening, parts[(cutoff + 1)..]]
end

.tokenize(string) ⇒ Object



1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
# File 'lib/mmmd/blankshell.rb', line 1145

def self.tokenize(string)
  open = {}
  iterate_tokens(string, "`") do |_before, current_text, matched|
    if matched
      match = current_text.match(/^`+/)[0]
      if open[match]
        open[match] = nil
        [match, self, :close]
      else
        open[match] = true
        [match, self, :open]
      end
    else
      current_text[0]
    end
  end
end