This commit is contained in:
Erik Schierboom
2023-09-20 15:43:12 +02:00
parent b07d208303
commit cac363f691

View File

@@ -1,29 +1,7 @@
class AdmonitionBlockFormatter class AdmonitionBlockFormatter
def self.format(code) def self.format(code) = code.gsub(BACKTICK_MATCH_REGEX, TILDE_REPLACE)
[:format_four, :format_three].each do |meth|
while code != (new_code = send(meth, code))
code = new_code
end
end
code BACKTICK_MATCH_REGEX = /(?<start>`{3,})(?<type>exercism\/[a-z]+)(?<admonition>[\s\S]*?)(?<end>\k<start>)/.freeze
end TILDE_REPLACE = '~~~~\k<type>\k<admonition>~~~~'.freeze
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>`{3,})(?<type>exercism\/[a-z]+)(?<admonition>[\s\S]*?)(?<end>\k<start>)(?<after>[\s\S]*)/.freeze
FOUR_TILDE_REGEX = /(?<before>[\s\S]*)(?<start>`{4,})(?<type>exercism\/[a-z]+)(?<admonition>[\s\S]*?)(?<end>\k<start>)(?<after>[\s\S]*)/.freeze
end end