Class: Broadway::Processor::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/broadway/processors/tree.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Tree

Returns a new instance of Tree.



72
73
74
# File 'lib/broadway/processors/tree.rb', line 72

def initialize(*args, &block)
  self.root = ::Broadway::Definition::Post.new(*args, &block)
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



6
7
8
# File 'lib/broadway/processors/tree.rb', line 6

def root
  @root
end

Class Method Details

.build(path) ⇒ Object

lists are xml files we’ve collect, but we want to make sure we’ve created all the posts we need to beforehand this also sorts everything



13
14
15
16
17
18
19
20
21
22
# File 'lib/broadway/processors/tree.rb', line 13

def build(path)
  list = ::File.join(source, "index.xml")
  lists << list if ::File.exists?(list)
  unless lists.blank?
    require 'nokogiri'
    lists.each do |path|
      new_tree(path)
    end
  end
end

.new_tree(path) ⇒ Object



24
25
26
# File 'lib/broadway/processors/tree.rb', line 24

def new_tree(path)
  site.tree.concat parse_tree(Nokogiri::XML(IO.read(path)).root)
end

.parse_tree(parent) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/broadway/processors/tree.rb', line 28

def parse_tree(parent)
  result = []
  return result if parent.nil? || parent.children.nil? || parent.children.empty?
  parent.children.each_with_index do |child, index|
    next unless child.elem?
    post = post_from_xml(child)
    next unless post
    if post.respond_to?(:children)
      post.children.concat parse_tree(child)
    end
    post.position = index
    result << post
  end
  result
end

.post_from_xml(post) ⇒ Object



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
# File 'lib/broadway/processors/tree.rb', line 44

def post_from_xml(post)
  # post
  # 1. If a "src" is defined, then we should have already created the post
  # 2. If no "src", then post is specified inline (or there is no post yet)                                               
  path = (post["src"] || ::File.join(site.source, post["path"]) || "").gsub(/^\//, "").gsub(/\/$/, "").squeeze("/")
  post = site.find_post_by_path(path)
  unless post
    if ::File.directory?(path)
      page = Dir.entries(path)[2..-1].detect do |file|
        ::File.basename(file).split(".")[0..-2].join(".").downcase == "index"
      end
      if page
        path = ::File.join(path, page)
        post = site.find_post_by_path(path)
        post ||= Broadway::Builder::Post.new_post(path)
      end
    end
  end

  return unless post
      
  %w(title image excerpt menu_title tooltip show_children post layout).each do |key|
    post.data[key] = post[key] if post.has_attribute?(key)
  end
  post
end