Files
admonition-block-formatter/lib/admonition_block_formatter.rb
Jeremy Walker d08fcf62b9 Add first pass
2023-09-20 15:04:24 +02:00

30 lines
923 B
Ruby

class AdmonitionBlockFormatter
def self.format(code)
[:format_four, :format_three].each do |meth|
while code != (new_code = send(meth, code))
code = new_code
end
end
code
end
def self.format_three(code)
matches = THREE_TILDE_REGEX.match(code)
return code unless matches
matches['before'] + "~~~~" + matches['type'] + matches['admonition'] + "~~~~" + matches['after']
end
def self.format_four(code)
matches = FOUR_TILDE_REGEX.match(code)
return code unless matches
matches['before'] + "~~~~" + matches['type'] + matches['admonition'] + "~~~~" + matches['after']
end
THREE_TILDE_REGEX = /(?<before>[\s\S]*)(?<start>```)(?<type>exercism\/[a-z]+)(?<admonition>[\s\S]*?)(?<end>```)(?<after>[\s\S]*)/.freeze
FOUR_TILDE_REGEX = /(?<before>[\s\S]*)(?<start>````)(?<type>exercism\/[a-z]+)(?<admonition>[\s\S]*?)(?<end>````)(?<after>[\s\S]*)/.freeze
end