Class: Markawesome::CopyButtonTransformer

Inherits:
BaseTransformer show all
Defined in:
lib/markawesome/transformers/copy_button_transformer.rb

Overview

Transforms copy button syntax into wa-copy-button elements Primary syntax: <<<params?ncontentn<<<

Alternative syntax: :::wa-copy-button params?ncontentn:

Params: space-separated tokens, any order (rightmost-wins for conflicts) Placement: top, right, bottom, left Duration: numeric value (feedback-duration in milliseconds) Flags: disabled Labels: copy-label=“text”, success-label=“text”, error-label=“text” From: from=“element-id” (copy from another element)

Usage: <<< This text will be copied to clipboard <<<

<<<disabled top 2000 Disabled copy button with top tooltip and 2s feedback <<<

<<<copy-label=“Click to copy” success-label=“Copied!” Custom labels <<<

:::wa-copy-button right from=“my-element” Copy from element with ID “my-element”

:

Constant Summary collapse

COPY_BUTTON_ATTRIBUTES =
{
  placement: %w[top right bottom left],
  disabled: %w[disabled]
}.freeze

Class Method Summary collapse

Class Method Details

.render_as_markdown(content, _options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/markawesome/transformers/copy_button_transformer.rb', line 69

def self.render_as_markdown(content, _options = {})
  primary_regex = /^<<<(.*?)\n(.*?)\n<<</m
  alternative_regex = /^:::wa-copy-button\s*(.*?)\n(.*?)\n:::/m

  transform_proc = proc do |_params_string, copy_content|
    copy_content.to_s.strip
  end

  patterns = dual_syntax_patterns(primary_regex, alternative_regex, transform_proc)
  apply_multiple_patterns(content, patterns)
end

.transform(content) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/markawesome/transformers/copy_button_transformer.rb', line 40

def self.transform(content)
  # Define both regex patterns - capture params as a single string
  primary_regex = /^<<<(.*?)\n(.*?)\n<<</m
  alternative_regex = /^:::wa-copy-button\s*(.*?)\n(.*?)\n:::/m

  # Define shared transformation logic
  transform_proc = proc do |params_string, copy_content|
    copy_content = copy_content.strip

    # Parse space-separated parameters
    attributes = AttributeParser.parse(params_string, COPY_BUTTON_ATTRIBUTES)

    # Extract quoted labels and from attribute
    attributes[:copy_label] = extract_quoted_attribute(params_string, 'copy-label')
    attributes[:success_label] = extract_quoted_attribute(params_string, 'success-label')
    attributes[:error_label] = extract_quoted_attribute(params_string, 'error-label')
    attributes[:from] = extract_quoted_attribute(params_string, 'from')

    # Extract numeric feedback-duration
    attributes[:feedback_duration] = ::Regexp.last_match(1) if params_string =~ /\b(\d+)\b/

    build_copy_button_html(copy_content, attributes)
  end

  # Apply both patterns
  patterns = dual_syntax_patterns(primary_regex, alternative_regex, transform_proc)
  apply_multiple_patterns(content, patterns)
end