Simplify
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user