Method: ModelFormatting.extract_regex

Defined in:
lib/model_formatting.rb

.extract_regex(text, *regexes) {|text| ... } ⇒ Object

Yields:

  • (text)


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
# File 'lib/model_formatting.rb', line 142

def self.extract_regex(text, *regexes)
  # Extract pre blocks
  extractions = {}
  regexes.each do |regex|
    text.gsub!(regex) do |match|
      md5 = Digest::MD5.hexdigest(match)
      extractions[md5] ||= []
      extractions[md5] << match
      "{mkd-extraction-#{md5}}"
    end
  end
  yield text
  # In cases where multiple tag names are provided AND the tags mismatch or
  # overlap in non-conforming ways, it's possible for extracted sections to
  # have extractions in them. To keep content from being eaten by the markdown
  # extractor, loop until all of the extractions have been replaced.
  while !extractions.keys.empty?
    # Insert block extractions
    text.gsub!(/\{mkd-extraction-([0-9a-f]{32})\}/) do
      value = extractions[$1].pop
      extractions.delete($1) if extractions[$1].empty?
      value
    end
  end
  text
end