Class: Brief::Document::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/brief/document/transformer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fragment, document) ⇒ Transformer

Returns a new instance of Transformer.



5
6
7
8
# File 'lib/brief/document/transformer.rb', line 5

def initialize(fragment, document)
  @fragment = fragment
  @document = document
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



3
4
5
# File 'lib/brief/document/transformer.rb', line 3

def document
  @document
end

#fragmentObject (readonly)

Returns the value of attribute fragment.



3
4
5
# File 'lib/brief/document/transformer.rb', line 3

def fragment
  @fragment
end

Instance Method Details

#allObject



10
11
12
13
# File 'lib/brief/document/transformer.rb', line 10

def all
  transform_dynamic_links
  inline_svg_content
end

#inline_svg_contentObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/brief/document/transformer.rb', line 15

def inline_svg_content
  inline_svg_images.each do |img|
    src = img['src'].to_s

    if src.match(/=/)
      _, value = img['src'].to_s.split("=")
    else
      value = src
    end

    begin
      if asset = document.briefcase.find_asset(value)
        img.replace("<div class='svg-wrapper'>#{ asset.read }</div>")
      end
    rescue
      nil
    end
  end
end


35
36
37
38
39
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
68
69
70
71
72
73
74
75
76
77
# File 'lib/brief/document/transformer.rb', line 35

def transform_dynamic_links
  dynamic_link_elements.each do |node|
    attribute, value = node['href'].to_s.split("=")
    instruction, strategy = node.text.to_s.split(':')

    if instruction == "link" && attribute == "path"
      begin
        link_to_doc = document.briefcase.document_at(value)

        if link_to_doc.exist?
          text = link_to_doc.send(strategy)
          node.inner_html = text
          node['href'] = "brief://#{ link_to_doc.path }"
        else
          node['href'] = "brief://document-not-found"
        end
      rescue
        nil
      end
    end
  end

  include_link_elements.each do |node|
    attribute, value = node['href'].to_s.split("=")
    instruction, strategy = node.text.to_s.split(':')

    if instruction == "include" && attribute == "path"
      include_doc = document.briefcase.document_at(value)

      replacement = nil

      if strategy == "raw_content"
        replacement = include_doc.unwrapped_html
      elsif strategy == "content"
        replacement = include_doc.to_html
      end

      node.replace(replacement) if replacement
    end
  end
rescue
  nil
end