module Bridgetown::Site::Content

Content is king!

Public Instance Methods

add_generated_page(generated_page) click to toggle source
# File lib/bridgetown-core/concerns/site/content.rb, line 220
def add_generated_page(generated_page)
  generated_pages << generated_page
end
categories() click to toggle source

Returns a hash of “categories” using {#post_attr_hash} where each tag is

a key and each value is a post which contains the key.

@example

categories
# => { 'tech': [<Post A>, <Post B>],
#      'ruby': [<Post C> }

@return [Hash{String, Array<Post>}] Returns a hash of all categories and

their corresponding posts

@see post_attr_hash

# File lib/bridgetown-core/concerns/site/content.rb, line 68
def categories
  uses_resource? ? taxonomies.category : post_attr_hash("categories")
end
collection_names() click to toggle source

An array of collection names. @return [Array<String>] an array of collection names from the configuration,

or an empty array if the `config["collections"]` key is not set.
# File lib/bridgetown-core/concerns/site/content.rb, line 136
def collection_names
  Array(config.collections&.keys)
end
collections() click to toggle source

The list of collections labels and their corresponding {Collection} instances.

If `config['collections']` is set, a new instance of {Collection} is created
for each entry in the collections configuration.

If `config["collections"]` is not specified, a blank hash is returned.

@return [Hash{String, Symbol => Collection}] A Hash

containing a collection name-to-instance pairs.

@return [Hash] Returns a blank hash if no items found

# File lib/bridgetown-core/concerns/site/content.rb, line 125
def collections
  @collections ||= collection_names.each_with_object(
    HashWithDotAccess::Hash.new
  ) do |name, hsh|
    hsh[name] = Bridgetown::Collection.new(self, name)
  end
end
contents() click to toggle source

Get all pages and documents (posts and collection items) in a single array.

@return [Array]

# File lib/bridgetown-core/concerns/site/content.rb, line 214
def contents
  return resources if uses_resource?

  pages + documents
end
docs_to_write() click to toggle source

Get the documents to be written

@return [Array<Document>] an array of documents which should be

written and that `respond_to :write?`

@see documents @see Collection

# File lib/bridgetown-core/concerns/site/content.rb, line 172
def docs_to_write
  documents.select(&:write?)
end
documents() click to toggle source

Get all documents. @return [Array<Document>] an array of documents from the configuration

# File lib/bridgetown-core/concerns/site/content.rb, line 160
def documents
  collections.each_with_object(Set.new) do |(_, collection), set|
    set.merge(collection.docs)
  end.to_a
end
frontend_manifest() click to toggle source

Loads and memoizes the parsed Webpack manifest file (if available) @return [Hash]

# File lib/bridgetown-core/concerns/site/content.rb, line 226
def frontend_manifest
  @frontend_manifest ||= begin
    manifest_file = in_root_dir(".bridgetown-webpack", "manifest.json")

    JSON.parse(File.read(manifest_file)) if File.exist?(manifest_file)
  end
end
metadata() click to toggle source

Returns the value of `data` or creates a new instance of

`HashWithDotAccess::Hash`

@return [Hash] Returns a hash of site metadata

# File lib/bridgetown-core/concerns/site/content.rb, line 75
def metadata
  data["site_metadata"] ||= HashWithDotAccess::Hash.new
end
post_attr_hash(post_attr) click to toggle source

Construct a Hash of Posts indexed by the specified Post attribute.

Returns a hash like so: `{ attr => posts }` where:

  • `attr` - One of the values for the requested attribute.

  • `posts` - The array of Posts with the given attr value.

@param post_attr [String] The String name of the Post attribute.

@example

post_attr_hash('categories')
# => { 'tech' => [<Post A>, <Post B>],
#      'ruby' => [<Post B>] }

@return [Hash{String, Symbol => Array<Post>}]

Returns a hash of !{attr => posts}
# File lib/bridgetown-core/concerns/site/content.rb, line 22
def post_attr_hash(post_attr)
  # Build a hash map based on the specified post attribute ( post attr =>
  # array of posts ) then sort each array in reverse order.
  @post_attr_hash[post_attr] ||= begin
    hash = Hash.new { |h, key| h[key] = [] }
    posts.docs.each do |p|
      p.data[post_attr]&.each { |t| hash[t] << p }
    end
    hash.each_value { |posts| posts.sort!.reverse! }
    hash
  end
end
posts() click to toggle source

Get all posts. Deprecated, to be removed in v1.0.

@return [Collection] Returns {#collections}`[“posts”]`, creating it if need be @see Collection

# File lib/bridgetown-core/concerns/site/content.rb, line 192
def posts
  unless @wrote_deprecation_msg
    Bridgetown::Deprecator.deprecation_message "Call site.collections.posts " \
                                               "instead of site.posts (Ruby code)"
  end
  @wrote_deprecation_msg ||= true
  collections["posts"] ||= Bridgetown::Collection.new(self, "posts")
end
resources() click to toggle source

Get all loaded resources. @return [Array<Bridgetown::Resource::Base>] an array of resources

# File lib/bridgetown-core/concerns/site/content.rb, line 178
def resources
  collections.each_with_object(Set.new) do |(_, collection), set|
    set.merge(collection.resources)
  end.to_a
end
resources_grouped_by_taxonomy(taxonomy) click to toggle source
# File lib/bridgetown-core/concerns/site/content.rb, line 35
def resources_grouped_by_taxonomy(taxonomy)
  @post_attr_hash[taxonomy.label] ||= begin
    taxonomy.terms.transform_values { |terms| terms.map(&:resource).sort.reverse }
  end
end
resources_to_write() click to toggle source
# File lib/bridgetown-core/concerns/site/content.rb, line 184
def resources_to_write
  resources.select(&:write?)
end
site_payload() click to toggle source

The Hash payload containing site-wide data.

@example

site_payload
# => { "site" => data } Where data is a Hash. See example below

site = site_payload["site"]
# => Returns a Hash with the following keys:
#
# site["time"]       - The Time as specified in the configuration or the
#                      current time if none was specified.
#
# site["posts"]      - The Array of Posts, sorted chronologically by post date
#                      and then title.
#
# site["pages"]      - The Array of all Pages.
#
# site["html_pages"] - The Array of HTML Pages.
#
# site["categories"] - The Hash of category values and Posts.
#                      See Site#post_attr_hash for type info.
#
# site["tags"]       - The Hash of tag values and Posts.
#                      See Site#post_attr_hash for type info.

@return [Hash] Returns a hash in the structure of { “site” => data }

See above example for usage.

@see post_attr_hash

# File lib/bridgetown-core/concerns/site/content.rb, line 109
def site_payload
  Bridgetown::Drops::UnifiedPayloadDrop.new self
end
Also aliased as: to_liquid
static_files_to_write() click to toggle source

Get the static files to be written

@return [Array<StaticFile>] an array of files which should be

written and that `respond_to :write?`

@see static_files @see StaticFile

# File lib/bridgetown-core/concerns/site/content.rb, line 207
def static_files_to_write
  static_files.select(&:write?)
end
tags() click to toggle source

Returns a hash of “tags” using {#post_attr_hash} where each tag is a key

and each value is a post which contains the key.

@example

tags
# => { 'tech': [<Post A>, <Post B>],
#      'ruby': [<Post C> }

@return [Hash{String, Array<Post>}] Returns a hash of all tags and their corresponding posts @see post_attr_hash

# File lib/bridgetown-core/concerns/site/content.rb, line 55
def tags
  uses_resource? ? taxonomies.tag : post_attr_hash("tags")
end
taxonomies() click to toggle source
# File lib/bridgetown-core/concerns/site/content.rb, line 41
def taxonomies
  taxonomy_types.transform_values do |taxonomy|
    resources_grouped_by_taxonomy(taxonomy)
  end
end
taxonomy_types() click to toggle source

@return [Array<Bridgetown::Resource::TaxonomyType>]

# File lib/bridgetown-core/concerns/site/content.rb, line 141
def taxonomy_types
  @taxonomy_types ||= config.taxonomies.map do |label, key_or_metadata|
    key = key_or_metadata
    tax_metadata = if key_or_metadata.is_a? Hash
                     key = key_or_metadata["key"]
                     key_or_metadata.reject { |k| k == "key" }
                   else
                     HashWithDotAccess::Hash.new
                   end

    [label, Bridgetown::Resource::TaxonomyType.new(
      site: self, label: label, key: key, metadata: tax_metadata
    ),]
  end.to_h.with_dot_access
end
to_liquid()
Alias for: site_payload