Class: Markawesome::ComparisonTransformer

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

Overview

Transforms comparison syntax into wa-comparison elements Primary syntax: |||n!(before.jpg)n!(after.jpg)n||| Primary syntax with position: |||50n!(before.jpg)n!(after.jpg)n|||

Alternative syntax: :::wa-comparisonn!(before.jpg)n!(after.jpg)n:
Alternative syntax with position: :::wa-comparison 50n!(before.jpg)n!(after.jpg)n:

Expects exactly two image elements inside the wrapper

Class Method Summary collapse

Class Method Details

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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/markawesome/transformers/comparison_transformer.rb', line 41

def self.render_as_markdown(content, _options = {})
  content = content.gsub(/^\|\|\|(\d+)?\n(.*?)\n\|\|\|/m) do |match|
    images = extract_images(::Regexp.last_match(2))
    images.length == 2 ? render_comparison_markdown(images) : match
  end

  content.gsub(/^:::wa-comparison\s*(\d+)?\n(.*?)\n:::/m) do |match|
    images = extract_images(::Regexp.last_match(2))
    images.length == 2 ? render_comparison_markdown(images) : match
  end
end

.transform(content) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/markawesome/transformers/comparison_transformer.rb', line 13

def self.transform(content)
  # Process primary syntax
  content = content.gsub(/^\|\|\|(\d+)?\n(.*?)\n\|\|\|/m) do |match|
    position = Regexp.last_match(1)
    inner_content = Regexp.last_match(2).strip
    images = extract_images(inner_content)

    if images.length == 2
      build_comparison_html(inner_content, position)
    else
      match # Return original match if not exactly 2 images
    end
  end

  # Process alternative syntax
  content.gsub(/^:::wa-comparison\s*(\d+)?\n(.*?)\n:::/m) do |match|
    position = Regexp.last_match(1)
    inner_content = Regexp.last_match(2).strip
    images = extract_images(inner_content)

    if images.length == 2
      build_comparison_html(inner_content, position)
    else
      match # Return original match if not exactly 2 images
    end
  end
end