-
-
Save oleander/7593235de6b6d6f93445 to your computer and use it in GitHub Desktop.
pre-processor.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PreProcessor < Struct.new(:raw) | |
attr_accessor :store | |
def adjust!(regexp, &block) | |
inner_raw = @raw | |
@raw.scan(regexp) do | |
$~.names.each do |name| | |
value = $~[name] | |
key = SecureRandom.hex | |
inner_raw = inner_raw.gsub(value, "<#{key}>") | |
@store << { | |
value: yield(value.strip), | |
key: "<#{key}>" | |
} | |
end | |
end | |
inner_raw | |
end | |
end | |
processor = PreProcessor.new("THINGS 1. IS THIS A QUESTION? STUFF") | |
output = processor.adjust!(/\.\s+(?<question>[^.?]+\?)/) do |value| | |
"**#{value}**" | |
end | |
output # => "THINGS <d58e3582afa99040e27b92b13c8f2280> STUFF" | |
processor.store # => [{ value: **1. IS THIS A QUESTION?**, key: "d58e3582afa99040e27b92b13c8f2280" }] | |
formated = output.downcase # => "things <d58e3582afa99040e27b92b13c8f2280> stuff" | |
processor.store.each do |data| | |
formated.sub!(data.key, data.value) | |
end | |
formated # => "things 1. IS THIS A QUESTION? stuff" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment