Class: Flutterby::Node

Inherits:
Object
  • Object
show all
Includes:
Paths, Reading, Rendering, Staging, Tree
Defined in:
lib/flutterby/node.rb

Defined Under Namespace

Modules: Paths, Reading, Rendering, Staging, Tree

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent: nil, fs_path: nil, source: nil) ⇒ Node

Returns a new instance of Node.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flutterby/node.rb', line 10

def initialize(name, parent: nil, fs_path: nil, source: nil)
  @fs_path = fs_path ? ::File.expand_path(fs_path) : nil
  @source  = source
  @_setup_procs = []

  # Extract name, extension, and filters from given name
  parts    = name.split(".")
  @name    = parts.shift
  @ext     = parts.shift
  @filters = parts.reverse

  # Register this node with its parent
  if parent
    self.parent = parent
  end

  reload!
end

Instance Attribute Details

#_setup_procsObject (readonly)

Returns the value of attribute _setup_procs.



8
9
10
# File 'lib/flutterby/node.rb', line 8

def _setup_procs
  @_setup_procs
end

#childrenObject (readonly)

Returns the value of attribute children.



6
7
8
# File 'lib/flutterby/node.rb', line 6

def children
  @children
end

#extObject

Returns the value of attribute ext.



5
6
7
# File 'lib/flutterby/node.rb', line 5

def ext
  @ext
end

#filtersObject (readonly)

Returns the value of attribute filters.



6
7
8
# File 'lib/flutterby/node.rb', line 6

def filters
  @filters
end

#fs_pathObject (readonly)

Returns the value of attribute fs_path.



6
7
8
# File 'lib/flutterby/node.rb', line 6

def fs_path
  @fs_path
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/flutterby/node.rb', line 5

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/flutterby/node.rb', line 6

def parent
  @parent
end

#prefixObject (readonly)

Returns the value of attribute prefix.



7
8
9
# File 'lib/flutterby/node.rb', line 7

def prefix
  @prefix
end

#slugObject (readonly)

Returns the value of attribute slug.



7
8
9
# File 'lib/flutterby/node.rb', line 7

def slug
  @slug
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/flutterby/node.rb', line 5

def source
  @source
end

Instance Method Details

#copy(new_name, data = {}) ⇒ Object



393
394
395
396
397
398
399
# File 'lib/flutterby/node.rb', line 393

def copy(new_name, data = {})
  dup.tap do |c|
    c.name = new_name
    c.data.merge!(data)
    parent.children << c
  end
end

#create(name, **args) ⇒ Object Originally defined in module Tree

Creates a new node, using the specified arguments, as a child of this node.

#dataObject Originally defined in module Reading

#emit_child(name) ⇒ Object Originally defined in module Tree

#extend_all(nodes, *mods, &blk) ⇒ Object Originally defined in module Staging

Extend all of the specified ‘nodes` with the specified module(s). If a block is given, the nodes will be extended with the code found in the block.

#extend_parent(*mods, &blk) ⇒ Object Originally defined in module Staging

Extend this node’s parent. See #extend_all.

#extend_siblings(*mods, &blk) ⇒ Object Originally defined in module Staging

Extend all of this node’s siblings. See #extend_all.

#file?Boolean

Returns:

  • (Boolean)


377
378
379
# File 'lib/flutterby/node.rb', line 377

def file?
  !folder? && should_publish?
end

#find(path, opts = {}) ⇒ Object Originally defined in module Tree

#find!(path, *args) ⇒ Object Originally defined in module Tree

Like #find, but raises an exception when the specified node could not be found.

#find_child(name, opts = {}) ⇒ Object Originally defined in module Tree

Among this node’s children, find a node by its name. If the name passed as an argument includes a dot, the name will match against the full name of the children; otherwise, just the base name.

Examples:

# returns the first child called "index"
find_child("index")

# returns the child called "index" with extension "html"
find_child("index.html")

#folder?Boolean

Returns:

  • (Boolean)


373
374
375
# File 'lib/flutterby/node.rb', line 373

def folder?
  children.any?
end

#full_nameObject



369
370
371
# File 'lib/flutterby/node.rb', line 369

def full_name
  [name, ext].compact.join(".")
end

#layoutObject

Returns the layout(s) configured for this node. This is sourced from the node’s Flutterby::Node::Reading#data attribute, so it can be set from front matter.



361
362
363
# File 'lib/flutterby/node.rb', line 361

def layout
  data[:layout]
end

#loggerObject



389
390
391
# File 'lib/flutterby/node.rb', line 389

def logger
  Flutterby.logger
end

#page?Boolean

Returns:

  • (Boolean)


381
382
383
# File 'lib/flutterby/node.rb', line 381

def page?
  file? && ext == "html"
end

#pagesObject Originally defined in module Tree

Returns all children that will compile to a HTML page.

#parent=(new_parent) ⇒ Object Originally defined in module Tree

#perform_setup!Object Originally defined in module Staging

Perform setup for this node. The setup step is run after the tree has been built up completely. It allows you to perform one-time setup operations that, for example, modify the tree (like sorting blog posts into date-specific subnodes.)

Your nodes (or their extensions) may overload this method, but you may also simply use the ‘setup { … }` syntax in a node extension to define a block of code to be run at setup time.

#reload!Object Originally defined in module Reading

(Re-)loads the node from the filesystem, if it’s a filesystem based node.

#render(opts = {}) ⇒ Object Originally defined in module Rendering

Creates a new View instance and uses it to render this node. Returns the rendered page as a string.

#rootObject Originally defined in module Tree

Returns the tree’s root node.

#root?Boolean Originally defined in module Tree

Returns true if this node is also the tree’s root node.

Returns:

  • (Boolean)

#should_publish?Boolean

Returns:

  • (Boolean)


385
386
387
# File 'lib/flutterby/node.rb', line 385

def should_publish?
  !name.start_with?("_")
end

#sibling(name) ⇒ Object Originally defined in module Tree

#siblingsObject Originally defined in module Tree

Returns this node’s siblings (ie. other nodes within the same folder node.)

#stage!Object Originally defined in module Staging

#titleObject

Returns the node’s title. If there is a ‘:title` key in Flutterby::Node::Reading#data, its value will be used; otherwise, as a fallback, it will generate a human-readable title from #slug.



354
355
356
# File 'lib/flutterby/node.rb', line 354

def title
  data[:title] || slug.try(:titleize)
end

#to_sObject



365
366
367
# File 'lib/flutterby/node.rb', line 365

def to_s
  "<#{self.class} #{self.url}>"
end

#tree_sizeObject Originally defined in module Tree

#urlObject Originally defined in module Paths

Returns the node’s URL.