about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
authorFire Demon <firedemon@creature.cafe>2020-07-27 11:33:34 -0500
committerFire Demon <firedemon@creature.cafe>2020-08-30 05:45:16 -0500
commitfd69bc85ec5d7af0033471d9079d9ed3fba4d93e (patch)
treedd4cda8d3e51e04a55ad61ce9b090f0e9f3b386a /app/lib
parent1a89095596667005d403416737d79f0258f02f4d (diff)
[Command Tags] Do not convert body text to list / Parse and replace command tags in a single pass
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/command_tag/processor.rb18
1 files changed, 9 insertions, 9 deletions
diff --git a/app/lib/command_tag/processor.rb b/app/lib/command_tag/processor.rb
index da6d49160..4f11c0c0d 100644
--- a/app/lib/command_tag/processor.rb
+++ b/app/lib/command_tag/processor.rb
@@ -16,7 +16,6 @@ class CommandTag::Processor
   include CommandTag::Commands
 
   STATEMENT_RE = /^\s*#!\s*([^\n]+ (?:start|begin|do)$.*?)\n\s*#!\s*(?:end|stop|done)\s*$|^\s*#!\s*(.*?)\s*$/im.freeze
-  STATEMENT_STRIP_RE = /^\s*#!\s*(?:[^\n]+ (?:start|begin|do)$.*?)\n\s*#!\s*(?:end|stop|done)\s*$\n?|^\s*#!\s*(?:.*?)\s*$\n?/im.freeze
   TEMPLATE_RE = /%%\s*(.*?)\s*%%/.freeze
   ESCAPE_MAP = {
     '\n' => "\n",
@@ -42,10 +41,9 @@ class CommandTag::Processor
     reset_status_caches
 
     parse_statements
-    @text = @text.gsub(STATEMENT_STRIP_RE, '').split("\n")
 
     %w(at_start once_at_start).each { |suffix| execute_statements(suffix) }
-    @text = parse_templates(@text.join("\n")).rstrip
+    @text = parse_templates(@text).rstrip
     %w(before_save once_before_save).each { |suffix| execute_statements(suffix) }
 
     if @text.blank? || @text.gsub(Account::MENTION_RE, '').strip.blank?
@@ -113,14 +111,16 @@ class CommandTag::Processor
   end
 
   def parse_statements
-    @text.scan(STATEMENT_RE).flatten.compact.each do |statement|
+    @text.gsub!(STATEMENT_RE) do
+      statement = Regexp.last_match[1..-1].flatten.compact
       next if statement.blank?
 
-      statement = statement.scan(/'([^']*)'|"([^"]*)"|(\S+)|\s+(?:start|begin|do)\s*$\n+(.*)/im).flatten.compact
-      statement[0] = statement[0].strip.tr(':.\- ', '_').gsub(/__+/, '_').downcase
-      statement[-1].rstrip! if statement.count > 1
+      statement_array = statement.scan(/'([^']*)'|"([^"]*)"|(\S+)|\s+(?:start|begin|do)\s*$\n+(.*)/im).flatten.compact
+      statement_array[0] = statement_array[0].strip.tr(':.\- ', '_').gsub(/__+/, '_').downcase
+      next unless statement_array[0].match?(/\A\w+\z/)
 
-      add_statement_handlers_for(statement) if statement[0].present?
+      statement_array[-1].rstrip! if statement_array.count > 1
+      add_statement_handlers_for(statement_array)
     end
   end
 
@@ -138,7 +138,7 @@ class CommandTag::Processor
     end
   end
 
-  def add_statement_handlers_for(statement_array)
+  def add_statement_handlers_for(statement_array, _index)
     potential_handlers_for(statement_array[0]) do |handler, once|
       (@statements << [handler, statement_array[1..-1]]) if respond_to?(handler) && !(once && @run_once.include?(handler))
       @run_once << handler if once