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
17
# File 'lib/brief/document/transformer.rb', line 13

def all
  transform_dynamic_links
  include_images
  inline_svg_content
end

#briefcaseObject



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

def briefcase
  @briefcase ||= document.briefcase
end

#include_imagesObject



27
28
29
# File 'lib/brief/document/transformer.rb', line 27

def include_images
  process_image_paths(inline_image_tags)
end

#inline_svg_contentObject



23
24
25
# File 'lib/brief/document/transformer.rb', line 23

def inline_svg_content
  process_image_paths(inline_svg_images)
end

#process_image_paths(set_of_tags) ⇒ Object



31
32
33
34
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
# File 'lib/brief/document/transformer.rb', line 31

def process_image_paths(set_of_tags)
  set_of_tags.each do |img|
    src = img['src'].to_s

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

    begin
      asset = briefcase.find_asset(value)

      if !asset

      end

      if asset && asset.extname == ".svg"
        img.replace("<div class='svg-wrapper'>#{ asset.read }</div>")
      end

      if asset && %w(.gif .jpg .jpeg .png).include?(asset.extname.to_s.downcase)
        src = briefcase.get_external_url_for(asset)
        img['src'] = src
      end
    rescue
      nil
    end
  end
end


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/brief/document/transformer.rb', line 62

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