class Anaximander::Renderer

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]

Constants

MEMBER_PIPE
SPACE_PIPE
TAIL_PIPE
VERTICAL_PIPE

Attributes

root[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/anaximander/renderer.rb, line 36
def initialize(options={})
  @color = options.fetch(:color, true)
end

Public Instance Methods

draw(page=self.root, prefix="", tail=true) click to toggle source

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.
# 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) click to toggle source

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.

# 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) click to toggle source

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

See ‘draw_child` for “connecting pipe” definition.

# 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