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.



8
9
10
11
# File 'lib/brief/document/transformer.rb', line 8

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

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



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

def document
  @document
end

#fragmentObject (readonly)

Returns the value of attribute fragment.



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

def fragment
  @fragment
end

Instance Method Details

#allObject



13
14
15
16
# File 'lib/brief/document/transformer.rb', line 13

def all
  transform_dynamic_links
  inline_svg_content
end

#briefcaseObject



18
19
20
# File 'lib/brief/document/transformer.rb', line 18

def briefcase
  @briefcase ||= document.briefcase
end

#inline_svg_contentObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/brief/document/transformer.rb', line 22

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 = briefcase.find_asset(value)
        img.replace("<div class='svg-wrapper'>#{ asset.read }</div>")
      end
    rescue
      nil
    end
  end
end


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
78
79
80
81
82
83
84
# File 'lib/brief/document/transformer.rb', line 42

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 = briefcase.document_at(value)

        if link_to_doc.exist?
          text = link_to_doc.send(strategy)
          node.inner_html = text
          node['href'] = briefcase.get_href_for("brief://#{ link_to_doc.path }")
        else
          node['href'] = briefcase.get_href_for("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 = 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