2016-06-30 11:34:19 -04:00
|
|
|
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
|
2016-08-12 21:17:18 -04:00
|
|
|
attr_reader :command_definitions
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2016-08-12 21:17:18 -04:00
|
|
|
def initialize(command_definitions)
|
|
|
|
@command_definitions = command_definitions
|
2016-06-30 11:34:19 -04:00
|
|
|
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)
|
2016-08-12 21:17:18 -04:00
|
|
|
# commands = extractor.extract_commands(msg) #=> [['labels', '~foo ~"bar baz"']]
|
2016-06-30 11:34:19 -04:00
|
|
|
# msg #=> "hello\nworld"
|
|
|
|
# ```
|
2016-08-13 12:58:51 -04:00
|
|
|
def extract_commands(content, opts = {})
|
|
|
|
return [content, []] unless content
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2016-08-12 21:17:18 -04:00
|
|
|
content = content.dup
|
|
|
|
|
2016-06-30 11:34:19 -04:00
|
|
|
commands = []
|
|
|
|
|
2016-08-09 16:47:29 -04:00
|
|
|
content.delete!("\r")
|
2016-08-12 21:17:18 -04:00
|
|
|
content.gsub!(commands_regex(opts)) do
|
2016-08-09 16:47:29 -04:00
|
|
|
if $~[:cmd]
|
2016-08-18 15:21:52 -04:00
|
|
|
commands << [$~[:cmd], $~[:arg]].reject(&:blank?)
|
2016-08-09 16:47:29 -04:00
|
|
|
''
|
|
|
|
else
|
|
|
|
$~[0]
|
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-12 21:17:18 -04:00
|
|
|
[content.strip, commands]
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-08-18 15:21:52 -04:00
|
|
|
|
2016-06-30 11:34:19 -04:00
|
|
|
# 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:
|
|
|
|
#
|
2016-08-18 15:21:52 -04:00
|
|
|
# /^\/(?<cmd>close|reopen|...)(?:( |$))(?<arg>[^\/\n]*)(?:\n|$)/
|
2016-08-12 21:17:18 -04:00
|
|
|
def commands_regex(opts)
|
|
|
|
names = command_names(opts).map(&:to_s)
|
|
|
|
|
2016-08-09 16:47:29 -04:00
|
|
|
@commands_regex ||= %r{
|
|
|
|
(?<code>
|
|
|
|
# Code blocks:
|
|
|
|
# ```
|
2016-08-18 15:21:52 -04:00
|
|
|
# Anything, including `/cmd arg` which are ignored by this filter
|
2016-08-09 16:47:29 -04:00
|
|
|
# ```
|
|
|
|
|
|
|
|
^```
|
|
|
|
.+?
|
|
|
|
\n```$
|
|
|
|
)
|
|
|
|
|
|
|
|
|
(?<html>
|
|
|
|
# HTML block:
|
|
|
|
# <tag>
|
2016-08-18 15:21:52 -04:00
|
|
|
# Anything, including `/cmd arg` which are ignored by this filter
|
2016-08-09 16:47:29 -04:00
|
|
|
# </tag>
|
|
|
|
|
|
|
|
^<[^>]+?>\n
|
|
|
|
.+?
|
|
|
|
\n<\/[^>]+?>$
|
|
|
|
)
|
|
|
|
|
|
|
|
|
(?<html>
|
|
|
|
# Quote block:
|
|
|
|
# >>>
|
2016-08-18 15:21:52 -04:00
|
|
|
# Anything, including `/cmd arg` which are ignored by this filter
|
2016-08-09 16:47:29 -04:00
|
|
|
# >>>
|
|
|
|
|
|
|
|
^>>>
|
|
|
|
.+?
|
|
|
|
\n>>>$
|
|
|
|
)
|
|
|
|
|
|
|
|
|
(?:
|
|
|
|
# Command not in a blockquote, blockcode, or HTML tag:
|
|
|
|
# /close
|
|
|
|
|
2016-08-13 12:58:51 -04:00
|
|
|
^\/
|
|
|
|
(?<cmd>#{Regexp.union(names)})
|
|
|
|
(?:
|
|
|
|
[ ]
|
2016-08-18 15:21:52 -04:00
|
|
|
(?<arg>[^\/\n]*)
|
2016-08-13 12:58:51 -04:00
|
|
|
)?
|
|
|
|
(?:\n|$)
|
2016-08-09 16:47:29 -04:00
|
|
|
)
|
|
|
|
}mx
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
2016-08-17 19:58:44 -04:00
|
|
|
|
|
|
|
def command_names(opts)
|
|
|
|
command_definitions.flat_map do |command|
|
|
|
|
next if command.noop?
|
|
|
|
|
|
|
|
command.all_names
|
|
|
|
end.compact
|
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|