Class: Anaximander::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/anaximander/renderer.rb

Overview

Draws the crawled tree of URLs and assets, generated by ‘Anaximander::Crawler`.

Output

└── <url> [assets]
    ├── <url> [assets]
       └── <url> [assets]
    └── <url> [assets]

Example

root
# => #<Anaximander::Page url="http://example.com"/>

root.children
# => [#<Anaximander::Page url="http://example.com/foo"/>, #<Anaximander::Page url="http://example.com"/bar>]

renderer = Anaximander::Renderer.new(root)
renderer.draw

# => └── http://example.com [main.css]
# =>     ├── http://example.com/foo [main.css, foo.js]
# =>     └── http://example.com/bar [main.css, bar.js]

Constant Summary collapse

VERTICAL_PIPE =
""
MEMBER_PIPE =
"├── "
TAIL_PIPE =
"└── "
SPACE_PIPE =
"    "

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Renderer

Returns a new instance of Renderer.



36
37
38
# File 'lib/anaximander/renderer.rb', line 36

def initialize(options={})
  @color = options.fetch(:color, true)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



34
35
36
# File 'lib/anaximander/renderer.rb', line 34

def root
  @root
end

Instance Method Details

#draw(page = self.root, prefix = "", tail = true) ⇒ Object

Draws a page URL, its assets and recursively does the same for all of its children.

Parameters

[Anaximander::Page] page The page to render
[String] prefix A string that should preceed the actual URLa
                and asset information.
[Boolean] tail Is this node the last one in the collection.


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/anaximander/renderer.rb', line 50

def draw(page=self.root, prefix="", tail=true)
  pipe = tail ? TAIL_PIPE : MEMBER_PIPE

  url    = "#{prefix}#{pipe}#{page.url} "
  assets = "#{page.assets.to_a}"
  assets = assets.colorize(:light_black) if @color

  print url
  puts assets

  page.children[0..-2].each { |child| draw_child(child, prefix, tail) }
  draw_tail(page.children.last, prefix, tail) if page.children.size >= 1
end

#draw_child(page, prefix, parent_is_tail) ⇒ Object

Draws a child node, with the appropriate “connecting pipe”.

The “connecting pipe” is the character at the beginning of this line, which connects this to the previous tier of the tree.



69
70
71
72
# File 'lib/anaximander/renderer.rb', line 69

def draw_child(page, prefix, parent_is_tail)
  connecting_pipe = parent_is_tail ? SPACE_PIPE : VERTICAL_PIPE
  draw(page, "#{prefix}#{connecting_pipe}", false)
end

#draw_tail(page, prefix, parent_is_tail) ⇒ Object

Draws a leaf node, with the appropriate “connecting pipe”.

See ‘draw_child` for “connecting pipe” definition.



78
79
80
81
# File 'lib/anaximander/renderer.rb', line 78

def draw_tail(page, prefix, parent_is_tail)
  connecting_pipe = parent_is_tail ? SPACE_PIPE : VERTICAL_PIPE
  draw(page, "#{prefix}#{connecting_pipe}", true)
end