BigW Consortium Gitlab

extractor.rb 3.09 KB
Newer Older
1 2 3 4 5 6 7 8 9
module Gitlab
  module SlashCommands
    # This class takes an array of commands that should be extracted from a
    # given text.
    #
    # ```
    # extractor = Gitlab::SlashCommands::Extractor.new([:open, :assign, :labels])
    # ```
    class Extractor
10
      attr_reader :command_definitions
11

12 13
      def initialize(command_definitions)
        @command_definitions = command_definitions
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
      end

      # Extracts commands from content and return an array of commands.
      # The array looks like the following:
      # [
      #   ['command1'],
      #   ['command3', 'arg1 arg2'],
      # ]
      # The command and the arguments are stripped.
      # The original command text is removed from the given `content`.
      #
      # Usage:
      # ```
      # extractor = Gitlab::SlashCommands::Extractor.new([:open, :assign, :labels])
      # msg = %(hello\n/labels ~foo ~"bar baz"\nworld)
29
      # commands = extractor.extract_commands(msg) #=> [['labels', '~foo ~"bar baz"']]
30 31
      # msg #=> "hello\nworld"
      # ```
32 33
      def extract_commands(content, opts = {})
        return [content, []] unless content
34

35 36
        content = content.dup

37 38
        commands = []

39
        content.delete!("\r")
40
        content.gsub!(commands_regex(opts)) do
41
          if $~[:cmd]
42
            commands << [$~[:cmd], $~[:arg]].reject(&:blank?)
43 44 45 46
            ''
          else
            $~[0]
          end
47 48
        end

49
        [content.strip, commands]
50 51 52
      end

      private
53

54 55 56 57 58 59
      # Builds a regular expression to match known commands.
      # First match group captures the command name and
      # second match group captures its arguments.
      #
      # It looks something like:
      #
60
      #   /^\/(?<cmd>close|reopen|...)(?:( |$))(?<arg>[^\/\n]*)(?:\n|$)/
61 62 63
      def commands_regex(opts)
        names = command_names(opts).map(&:to_s)

64 65 66 67
        @commands_regex ||= %r{
            (?<code>
              # Code blocks:
              # ```
68
              # Anything, including `/cmd arg` which are ignored by this filter
69 70 71 72 73 74 75 76 77 78
              # ```

              ^```
              .+?
              \n```$
            )
          |
            (?<html>
              # HTML block:
              # <tag>
79
              # Anything, including `/cmd arg` which are ignored by this filter
80 81 82 83 84 85 86 87 88 89
              # </tag>

              ^<[^>]+?>\n
              .+?
              \n<\/[^>]+?>$
            )
          |
            (?<html>
              # Quote block:
              # >>>
90
              # Anything, including `/cmd arg` which are ignored by this filter
91 92 93 94 95 96 97 98 99 100 101
              # >>>

              ^>>>
              .+?
              \n>>>$
            )
          |
            (?:
              # Command not in a blockquote, blockcode, or HTML tag:
              # /close

102 103 104 105
              ^\/
              (?<cmd>#{Regexp.union(names)})
              (?:
                [ ]
106
                (?<arg>[^\/\n]*)
107 108
              )?
              (?:\n|$)
109 110
            )
        }mx
111
      end
Douwe Maan committed
112 113 114 115 116 117 118 119

      def command_names(opts)
        command_definitions.flat_map do |command|
          next if command.noop?

          command.all_names
        end.compact
      end
120 121 122
    end
  end
end