# frozen_string_literal: true
module WikiHelper
def command_usage(command_name, arguments: {}, only: nil, skip_arguments: [])
skip_arguments = skip_arguments.map(&:to_s)
only = [:arguments, :placeholder] if only.nil?
arguments = arguments.with_indifferent_access
command = CommandDetail.all_commands[command_name]
argument_html = []
if only.include?(:arguments) && command.command_arguments.size > 0
command.command_arguments.each do |name, template|
next if skip_arguments.include?(name)
display_name = template["display_name"]
color = CommandDetail.argument_colors[name]
value =
if arguments.key?(name)
arguments[name]
elsif arguments.key?(display_name)
arguments[display_name]
end
use_placeholders = only.include?(:placeholder)
format_flags = {
placeholder_text: (use_placeholders ? template["placeholder"] : nil),
only: [
((value.present? || use_placeholders) ? :key : nil),
(use_placeholders ? :placeholder : nil)
].compact
}
argument_html << format_argument(display_name, value, color, **format_flags)
end
end
content_tag(:span, class: "esm-text-color-toast-blue command-syntax") do
concat command.command_usage
if argument_html.size > 0
concat " "
concat argument_html.join(" ").html_safe
end
end
end
def argument(name, value = nil, aliases: [], only: nil)
generate_syntax = lambda do |name, value, color, opts = {}|
content_tag(:code, class: "command-syntax") do
format_argument(name, value, color, **opts)
end
end
name = name.to_s
color = CommandDetail.argument_colors[name]
template = CommandDetail.all_arguments[name]
generation_arty = [value, color, {placeholder_text: template["placeholder"], only: only}]
capture do
aliases.each do |name|
concat generate_syntax.call(name, *generation_arty)
concat ", " if aliases.size > 1
end
concat " and " if aliases.size > 0
concat generate_syntax.call(name, *generation_arty)
end
end
def format_argument(name, value, color, placeholder_text: "", only: nil)
only = [:key, :placeholder] if only.nil?
value =
if value.present?
content_tag(:span, value, class: color)
elsif only.include?(:placeholder) && placeholder_text.present?
"<#{placeholder_text}>".html_safe
end
show_key = only.include?(:key)
capture do
concat content_tag(:span, name, class: color) if show_key
concat content_tag(:span, ":", class: "uk-text-muted") if show_key && value
concat content_tag(:span, value, class: color) if value
end
end
end