Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRares Sfirlogea <rrr.junior@gmail.com>2016-11-16 14:09:09 +0300
committerAdam Niedzielski <adamsunday@gmail.com>2017-05-04 18:02:25 +0300
commit45e4c665653cd511b0d96119d3973652c43238bb (patch)
treeb614bb895b59dfb2f27d1408d0ed7594c4182c73 /lib/gitlab/slash_commands
parent2d43f8a2f4b1352067755609c9e3110d382d06c6 (diff)
Display slash commands outcome when previewing Markdown
Remove slash commands from Markdown preview and display their outcome next to the text field. Introduce new "explanation" block to our slash commands DSL. Introduce optional "parse_params" block to slash commands DSL that allows to process a parameter before it is passed to "explanation" or "command" blocks. Pass path for previewing Markdown as "data" attribute instead of setting a variable on "window".
Diffstat (limited to 'lib/gitlab/slash_commands')
-rw-r--r--lib/gitlab/slash_commands/command_definition.rb46
-rw-r--r--lib/gitlab/slash_commands/dsl.rb52
2 files changed, 83 insertions, 15 deletions
diff --git a/lib/gitlab/slash_commands/command_definition.rb b/lib/gitlab/slash_commands/command_definition.rb
index 60d35be2599..12a385f90fd 100644
--- a/lib/gitlab/slash_commands/command_definition.rb
+++ b/lib/gitlab/slash_commands/command_definition.rb
@@ -1,16 +1,19 @@
module Gitlab
module SlashCommands
class CommandDefinition
- attr_accessor :name, :aliases, :description, :params, :condition_block, :action_block
+ attr_accessor :name, :aliases, :description, :explanation, :params,
+ :condition_block, :parse_params_block, :action_block
def initialize(name, attributes = {})
@name = name
- @aliases = attributes[:aliases] || []
- @description = attributes[:description] || ''
- @params = attributes[:params] || []
+ @aliases = attributes[:aliases] || []
+ @description = attributes[:description] || ''
+ @explanation = attributes[:explanation] || ''
+ @params = attributes[:params] || []
@condition_block = attributes[:condition_block]
- @action_block = attributes[:action_block]
+ @parse_params_block = attributes[:parse_params_block]
+ @action_block = attributes[:action_block]
end
def all_names
@@ -28,14 +31,20 @@ module Gitlab
context.instance_exec(&condition_block)
end
+ def explain(context, opts, arg)
+ return unless available?(opts)
+
+ if explanation.respond_to?(:call)
+ execute_block(explanation, context, arg)
+ else
+ explanation
+ end
+ end
+
def execute(context, opts, arg)
return if noop? || !available?(opts)
- if arg.present?
- context.instance_exec(arg, &action_block)
- elsif action_block.arity == 0
- context.instance_exec(&action_block)
- end
+ execute_block(action_block, context, arg)
end
def to_h(opts)
@@ -52,6 +61,23 @@ module Gitlab
params: params
}
end
+
+ private
+
+ def execute_block(block, context, arg)
+ if arg.present?
+ parsed = parse_params(arg, context)
+ context.instance_exec(parsed, &block)
+ elsif block.arity == 0
+ context.instance_exec(&block)
+ end
+ end
+
+ def parse_params(arg, context)
+ return arg unless parse_params_block
+
+ context.instance_exec(arg, &parse_params_block)
+ end
end
end
end
diff --git a/lib/gitlab/slash_commands/dsl.rb b/lib/gitlab/slash_commands/dsl.rb
index 50b0937d267..614bafbe1b2 100644
--- a/lib/gitlab/slash_commands/dsl.rb
+++ b/lib/gitlab/slash_commands/dsl.rb
@@ -44,6 +44,22 @@ module Gitlab
@params = params
end
+ # Allows to give an explanation of what the command will do when
+ # executed. This explanation is shown when rendering the Markdown
+ # preview.
+ #
+ # Example:
+ #
+ # explanation do |arguments|
+ # "Adds label(s) #{arguments.join(' ')}"
+ # end
+ # command :command_key do |arguments|
+ # # Awesome code block
+ # end
+ def explanation(text = '', &block)
+ @explanation = block_given? ? block : text
+ end
+
# Allows to define conditions that must be met in order for the command
# to be returned by `.command_names` & `.command_definitions`.
# It accepts a block that will be evaluated with the context given to
@@ -61,6 +77,24 @@ module Gitlab
@condition_block = block
end
+ # Allows to perform initial parsing of parameters. The result is passed
+ # both to `command` and `explanation` blocks, instead of the raw
+ # parameters.
+ # It accepts a block that will be evaluated with the context given to
+ # `CommandDefintion#to_h`.
+ #
+ # Example:
+ #
+ # parse_params do |raw|
+ # raw.strip
+ # end
+ # command :command_key do |parsed|
+ # # Awesome code block
+ # end
+ def parse_params(&block)
+ @parse_params_block = block
+ end
+
# Registers a new command which is recognizeable from body of email or
# comment.
# It accepts aliases and takes a block.
@@ -75,11 +109,13 @@ module Gitlab
definition = CommandDefinition.new(
name,
- aliases: aliases,
- description: @description,
- params: @params,
- condition_block: @condition_block,
- action_block: block
+ aliases: aliases,
+ description: @description,
+ explanation: @explanation,
+ params: @params,
+ condition_block: @condition_block,
+ parse_params_block: @parse_params_block,
+ action_block: block
)
self.command_definitions << definition
@@ -89,8 +125,14 @@ module Gitlab
end
@description = nil
+ @explanation = nil
@params = nil
@condition_block = nil
+ @parse_params_block = nil
+ end
+
+ def definition_by_name(name)
+ command_definitions_by_name[name.to_sym]
end
end
end