From e28d1b809a0494519c3860b7030a9f1f1cfffda3 Mon Sep 17 00:00:00 2001 From: Brett Terpstra Date: Mon, 6 Feb 2023 11:13:00 -0600 Subject: [PATCH] code cleanup --- IncrementTemplated.popclipext/script.rb | 39 +++++++++++++------------ 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/IncrementTemplated.popclipext/script.rb b/IncrementTemplated.popclipext/script.rb index f79b05d..b649d05 100755 --- a/IncrementTemplated.popclipext/script.rb +++ b/IncrementTemplated.popclipext/script.rb @@ -6,6 +6,25 @@ NUMERIC_RX = /(\?\:)?##(\d+)(?:\.\.(\d+))?\.\.(\d+)##/.freeze ARRAY_RX = /##(.*?)##/.freeze +def get_modifiers(input) + input.scan(%r{##(([ix0-9()+\-/*%]+)*)(#([^#]+))?##}).map do + m = Regexp.last_match + padding = if m[2].nil? + '%d' + else + t = m[2].match(/\b(0+)([1-9]\d*)?/) + t.nil? || m[2] =~ /^0$/ ? '%d' : "%0#{t[0].length}d" + end + + options_array = m[3].nil? ? nil : m[4].split(/,/).map(&:strip) + + base = m[1].nil? ? 'x' : m[1] + inc = m[2].nil? ? '' : m[2].gsub(/\b(0+\d+)/, &:to_i) + + [Regexp.escape(m[0]), inc, padding, base, options_array] + end +end + def process_array(input) template = input.match(ARRAY_RX) replacements = template[1].split(/,/).map(&:strip) @@ -32,25 +51,6 @@ def process_array(input) output.join("\n") end -def get_modifiers(input) - input.scan(%r{##(([ix0-9()+\-/*%]+)*)(#([^#]+))?##}).map do - m = Regexp.last_match - padding = if m[2].nil? - '%d' - else - t = m[2].match(/\b(0+)([1-9]\d*)?/) - t.nil? || m[2] =~ /^0$/ ? '%d' : "%0#{t[0].length}d" - end - - options_array = m[3].nil? ? nil : m[4].split(/,/).map(&:strip) - - base = m[1].nil? ? 'x' : m[1] - inc = m[2].nil? ? '' : m[2].gsub(/\b(0+\d+)/, &:to_i) - - [Regexp.escape(m[0]), inc, padding, base, options_array] - end -end - def process_numeric(input) template = input.match(NUMERIC_RX) disp = template[1].nil? ? true : false @@ -76,6 +76,7 @@ def process_numeric(input) out = input.sub(/#{Regexp.escape(template[0])}/, replacement) modified.each do |mod| next if mod.nil? + equat = mod[3].gsub(/\b0+/, '').gsub(/x/, (idx + 1).to_s).gsub(/i/, idx.to_s) if mod[4]