Class: Middleman::Blog::BlogData
- Inherits:
-
Object
- Object
- Middleman::Blog::BlogData
- Extended by:
- Gem::Deprecate
- Includes:
- UriTemplates
- Defined in:
- lib/middleman-blog/blog_data.rb
Overview
A store of all the blog articles in the site, with accessors for the articles by various dimensions. Accessed via “blog” in templates.
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#options ⇒ Thor::CoreExt::HashWithIndifferentAccess
readonly
The configured options for this blog.
-
#source_template ⇒ URITemplate
readonly
A URITemplate for the source file path relative to :source_dir.
Instance Method Summary collapse
-
#articles ⇒ Array<Middleman::Sitemap::Resource>
A list of all blog articles, sorted by descending date.
-
#articles_by_locale(locale = ::I18n.locale) ⇒ Array<Middleman::Sitemap::Resource>
A list of all blog articles with the given language, sorted by descending date.
- #extract_source_params(path) ⇒ Object
- #extract_subdir_params(path) ⇒ Object
- #inspect ⇒ Object
-
#local_articles(locale = ::I18n.locale) ⇒ Array<Middleman::Sitemap::Resource>
deprecated
Deprecated.
Use #articles_by_locale instead.
-
#manipulate_resource_list(resources)
Updates’ blog articles destination paths to be the permalink.
-
#publishable?(article) ⇒ Boolean
Whether or not a given article should be included in the sitemap.
-
#tags ⇒ Hash<String, Array<Middleman::Sitemap::Resource>>
Returns a map from tag name to an array of BlogArticles associated with that tag and assigns the tag array back into it.
Methods included from UriTemplates
apply_uri_template, date_to_params, extract_directory_path, extract_params, safe_parameterize, uri_template
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
24 25 26 |
# File 'lib/middleman-blog/blog_data.rb', line 24 def controller @controller end |
#options ⇒ Thor::CoreExt::HashWithIndifferentAccess (readonly)
The configured options for this blog
22 23 24 |
# File 'lib/middleman-blog/blog_data.rb', line 22 def @options end |
#source_template ⇒ URITemplate (readonly)
A URITemplate for the source file path relative to :source_dir
18 19 20 |
# File 'lib/middleman-blog/blog_data.rb', line 18 def source_template @source_template end |
Instance Method Details
#articles ⇒ Array<Middleman::Sitemap::Resource>
A list of all blog articles, sorted by descending date
53 54 55 |
# File 'lib/middleman-blog/blog_data.rb', line 53 def articles @_articles.select(&.filter || proc { |a| a }).sort_by(&:date).reverse end |
#articles_by_locale(locale = ::I18n.locale) ⇒ Array<Middleman::Sitemap::Resource>
should use the @_articles if represented in this method.
A list of all blog articles with the given language, sorted by descending date
80 81 82 83 |
# File 'lib/middleman-blog/blog_data.rb', line 80 def articles_by_locale(locale = ::I18n.locale) locale = locale.to_sym if locale.is_a? String articles.select { |article| article.locale == locale } end |
#extract_source_params(path) ⇒ Object
111 112 113 |
# File 'lib/middleman-blog/blog_data.rb', line 111 def extract_source_params(path) @_parsed_url_cache[:source][path] ||= extract_params(@source_template, path) end |
#extract_subdir_params(path) ⇒ Object
118 119 120 |
# File 'lib/middleman-blog/blog_data.rb', line 118 def extract_subdir_params(path) @_parsed_url_cache[:subdir][path] ||= extract_params(@subdir_template, path) end |
#inspect ⇒ Object
183 184 185 |
# File 'lib/middleman-blog/blog_data.rb', line 183 def inspect "#<Middleman::Blog::BlogData: #{articles.inspect}>" end |
#local_articles(locale = ::I18n.locale) ⇒ Array<Middleman::Sitemap::Resource>
Use #articles_by_locale instead.
A list of all blog articles with the given language, sorted by descending date
66 67 68 |
# File 'lib/middleman-blog/blog_data.rb', line 66 def local_articles(locale = ::I18n.locale) articles_by_locale(locale) end |
#manipulate_resource_list(resources)
This method returns an undefined value.
Updates’ blog articles destination paths to be the permalink.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/middleman-blog/blog_data.rb', line 127 def manipulate_resource_list(resources) @_articles = [] used_resources = [] resources.each do |resource| if resource.ignored? # Don't bother blog-processing ignored stuff used_resources << resource next end if (params = extract_source_params(resource.path)) article = convert_to_article(resource) next unless publishable?(article) # Add extra parameters from the URL to the page metadata extra_data = params.except 'year', 'month', 'day', 'title', 'lang', 'locale' article. page: extra_data unless extra_data.empty? # compute output path: substitute date parts to path pattern article.destination_path = template_path @permalink_template, article, extra_data @_articles << article elsif (params = extract_subdir_params(resource.path)) # It's not an article, but it's the companion files for an article # (in a subdirectory named after the article) # figure out the matching article for this subdirectory file article_path = @source_template.(params).to_s if (article = @app.sitemap.find_resource_by_path(article_path)) # The article may not yet have been processed, so convert it here. article = convert_to_article(article) next unless publishable?(article) # Add extra parameters from the URL to the page metadata extra_data = params.except 'year', 'month', 'day', 'title', 'lang', 'locale' article. page: extra_data unless extra_data.empty? # The subdir path is the article path with the index file name # or file extension stripped off. new_destination_path = template_path @subdir_permalink_template, article, extra_data resource.destination_path = Middleman::Util.normalize_path(new_destination_path) end end used_resources << resource end used_resources end |
#publishable?(article) ⇒ Boolean
Whether or not a given article should be included in the sitemap. Skip articles that are not published unless the environment is :development
.
194 195 196 |
# File 'lib/middleman-blog/blog_data.rb', line 194 def publishable?(article) @app.environment == :development || article.published? end |
#tags ⇒ Hash<String, Array<Middleman::Sitemap::Resource>>
Returns a map from tag name to an array of BlogArticles associated with that tag and assigns the tag array back into it.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/middleman-blog/blog_data.rb', line 91 def = {} # Reference the filtered articles articles.each do |article| # Reference the tags assigned to an article article..each do |tag| # tag = safe_parameterize(tag) [tag] ||= [] [tag] << article end end # Return tags end |