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_GLOB =

Default file pattern to pick up in site

"**/**".freeze
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.



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

def initialize(root_path: DEFAULT_ROOT_PATH)
  self.root_path = root_path
  # When Site#resources is called, the results should be cached in production
  # environments for performance reasons, but not in development environments.
  self.cache_resources = false
end

Instance Attribute Details

#cache_resourcesObject Also known as: cache_resources?

Cache resources for production runs of Sitepress. Development modes don’t cache to optimize for files reloading.



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

def cache_resources
  @cache_resources
end

#resources_pipelineObject (readonly)

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



101
102
103
# File 'lib/sitepress/site.rb', line 101

def resources_pipeline
  @resources_pipeline
end

#root_pathObject

Returns the value of attribute root_path.



14
15
16
# File 'lib/sitepress/site.rb', line 14

def root_path
  @root_path
end

Instance Method Details

#clear_resources_cacheObject



48
49
50
# File 'lib/sitepress/site.rb', line 48

def clear_resources_cache
  @_resources = nil
end

#helpers_pathObject



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

def helpers_path
  root_path.join("helpers")
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 |resource, root|

if resource.request_path.start_with? "/videos/"
  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 |resource, root|

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 “‘



95
96
97
# File 'lib/sitepress/site.rb', line 95

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

#pages_pathObject

Location of website pages.



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

def pages_path
  root_path.join("pages")
end

#resourcesObject

Returns a list of all the resources within #root.



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

def resources
  with_resources_cache do
    ResourceCollection.new(node: root, root_path: root_path)
  end
end

#rootObject

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



34
35
36
37
38
39
# File 'lib/sitepress/site.rb', line 34

def root
  ResourcesNode.new.tap do |node|
    DirectoryCollection.new(assets: pages_assets, path: pages_path).mount(node)
    resources_pipeline.process node
  end
end