Class: Sitepress::Site

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sitepress/site.rb

Overview

A collection of pages from a directory.

Constant Summary collapse

DEFAULT_ROOT_PATH =

Default root_path for site.

Pathname.new(".").freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path: DEFAULT_ROOT_PATH) ⇒ Site

Returns a new instance of Site.



19
20
21
# File 'lib/sitepress/site.rb', line 19

def initialize(root_path: DEFAULT_ROOT_PATH)
  self.root_path = root_path
end

Instance Attribute Details

#resources_pipelineObject

An array of procs that manipulate the tree and resources from the Node returned by #root.



127
128
129
# File 'lib/sitepress/site.rb', line 127

def resources_pipeline
  @resources_pipeline ||= ResourcesPipeline.new
end

#root_pathObject

Returns the value of attribute root_path.



11
12
13
# File 'lib/sitepress/site.rb', line 11

def root_path
  @root_path
end

Instance Method Details

#asset_node_mapper(root_node) ⇒ Object

Maps a path of directories and files into the root node.



33
34
35
# File 'lib/sitepress/site.rb', line 33

def asset_node_mapper(root_node)
  AssetNodeMapper.new(path: pages_path, node: root_node)
end

#assets_pathObject

Location of rails assets



71
72
73
# File 'lib/sitepress/site.rb', line 71

def assets_path
  @assets_path ||= root_path.join("assets")
end

#assets_path=(path) ⇒ Object



75
76
77
# File 'lib/sitepress/site.rb', line 75

def assets_path=(path)
  @assets_path = Pathname.new(path)
end

#helpers_pathObject

Location of helper files.



62
63
64
# File 'lib/sitepress/site.rb', line 62

def helpers_path
  @helpers_path ||= root_path.join("helpers")
end

#helpers_path=(path) ⇒ Object



66
67
68
# File 'lib/sitepress/site.rb', line 66

def helpers_path=(path)
  @helpers_path = Pathname.new(path)
end

#manipulate(&block) ⇒ Object

Quick and dirty way to manipulate resources in the site without creating classes that implement the #process_resources method.

A common example may be adding data to a resource if it begins with a certain path:

“‘ruby Sitepress.site.manipulate do |root|

root.get("videos").each do |resource|
  resource.data["layout"] = "video"
end

end “‘

A more complex, contrived example that sets index.html as the root node in the site:

“‘ruby Sitepress.site.manipulate do |root|

root.get("blog").each do |post|
  post.move_to root
end

if resource.request_path == "/index"
  # Remove the HTML format of index from the current resource level
  # so we can level it up.
  node = resource.node
  node.formats.remove ".html"
  node.remove
  root.add path: "/", asset: resource.asset # Now we can get to this from `/`.
end

end “‘



121
122
123
# File 'lib/sitepress/site.rb', line 121

def manipulate(&block)
  resources_pipeline << Extensions::ProcManipulator.new(block)
end

#models_pathObject

Location of pages models.



80
81
82
# File 'lib/sitepress/site.rb', line 80

def models_path
  @models_path ||= root_path.join("models")
end

#models_path=(path) ⇒ Object



84
85
86
# File 'lib/sitepress/site.rb', line 84

def models_path=(path)
  @models_path = Pathname.new(path)
end

#pages_pathObject

Location of website pages.



53
54
55
# File 'lib/sitepress/site.rb', line 53

def pages_path
  @pages_path ||= root_path.join("pages")
end

#pages_path=(path) ⇒ Object



57
58
59
# File 'lib/sitepress/site.rb', line 57

def pages_path=(path)
  @pages_path = Pathname.new(path)
end

#reload!Object



42
43
44
45
# File 'lib/sitepress/site.rb', line 42

def reload!
  @resources = @root = nil
  self
end

#resourcesObject

Returns a list of all the resources within #root.



38
39
40
# File 'lib/sitepress/site.rb', line 38

def resources
  @resources ||= ResourceIndexer.new(node: root, root_path: pages_path)
end

#rootObject

A tree representation of the resourecs wthin the site. The root is a node that’s processed by the ‘resources_pipeline`.



25
26
27
28
29
30
# File 'lib/sitepress/site.rb', line 25

def root
  @root ||= Node.new.tap do |root|
    asset_node_mapper(root).map
    resources_pipeline.process root
  end
end