Class: Markawesome::IconTransformer

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

Overview

Transforms icon syntax into wa-icon elements Primary syntax: $$$icon-name (name only, decorative) Alternative syntax: :::wa-icon icon-name [family] [variant] [animation] [canvas]\n[label]\n:::

Examples: $$$settings -> $$$home -> $$$user-circle -> :::wa-icon star spin\n::: -> :::wa-icon star roomy\n::: -> :::wa-icon heart solid\nFavorite\n::: ->

Constant Summary collapse

ALTERNATIVE_REGEX =

First-line params + optional multi-line body. The closer is anchored to a line start; the opener is intentionally not anchored so it still matches mid-prose.

/:::wa-icon[ \t]+([^\n]*?)[ \t]*\n(.*?)^:::/m

Class Method Summary collapse

Class Method Details

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/markawesome/transformers/icon_transformer.rb', line 53

def self.render_as_markdown(content, _options = {})
  protected_content, code_blocks = protect_code_blocks(content)

  # Drop primary-syntax icons entirely.
  result = protected_content.gsub(/\$\$\$([a-zA-Z0-9\-_]+)(?![a-zA-Z0-9\-_]|\s+name\b)/, '')

  # A labeled block degrades to its label text (collapsed, not HTML-escaped — it
  # re-enters a markdown stream); an unlabeled block degrades to ''.
  result = result.gsub(ALTERNATIVE_REGEX) do
    ::Regexp.last_match(2).to_s.strip.gsub(/\s+/, ' ')
  end

  restore_code_blocks(result, code_blocks)
end

.transform(content) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/markawesome/transformers/icon_transformer.rb', line 24

def self.transform(content)
  # Protect code blocks first
  protected_content, code_blocks = protect_code_blocks(content)

  # Apply primary syntax transformation
  # Only block patterns that look like incomplete icon names:
  # $$$icon name (where 'icon name' could be intended as one identifier)
  result = protected_content.gsub(/\$\$\$([a-zA-Z0-9\-_]+)(?![a-zA-Z0-9\-_]|\s+name\b)/) do
    icon_name = ::Regexp.last_match(1)
    build_icon_html(icon_name)
  end

  # Apply alternative syntax transformation
  result = result.gsub(ALTERNATIVE_REGEX) do
    first_line = ::Regexp.last_match(1)
    raw_body   = ::Regexp.last_match(2)

    tokens     = first_line.strip.split(/\s+/)
    icon_name  = tokens.shift # first token is always the name
    attributes = AttributeParser.parse(tokens.join(' '), IconAttributes::SCHEMA)
    label      = normalize_label(raw_body)

    build_icon_html(icon_name, attributes, label)
  end

  # Restore code blocks
  restore_code_blocks(result, code_blocks)
end