Class: Middleman::Blog::BlogData
- Inherits:
-
Object
- Object
- Middleman::Blog::BlogData
- 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.
Constant Summary collapse
- DEFAULT_PERMALINK_COMPONENTS =
[:year, :month, :day, :title]
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#matcher_indexes ⇒ Hash
readonly
A hash of indexes into the path_matcher captures.
-
#options ⇒ Thor::CoreExt::HashWithIndifferentAccess
readonly
The configured options for this blog.
-
#path_matcher ⇒ Regex
readonly
A regex for matching blog article source paths.
Instance Method Summary collapse
-
#article(path) ⇒ Middleman::Sitemap::Resource
The BlogArticle for the given path, or nil if one doesn’t exist.
-
#articles ⇒ Array<Middleman::Sitemap::Resource>
A list of all blog articles, sorted by descending date.
- #custom_permalink_components ⇒ Object
- #inspect ⇒ Object
-
#manipulate_resource_list(resources)
Updates’ blog articles destination paths to be the permalink.
- #parse_permalink_options(resource) ⇒ Object
- #permalink_url_components ⇒ Object
-
#tags ⇒ Hash<String, Array<Middleman::Sitemap::Resource>>
Returns a map from tag name to an array of BlogArticles associated with that tag.
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
19 20 21 |
# File 'lib/middleman-blog/blog_data.rb', line 19 def controller @controller end |
#matcher_indexes ⇒ Hash (readonly)
A hash of indexes into the path_matcher captures
13 14 15 |
# File 'lib/middleman-blog/blog_data.rb', line 13 def matcher_indexes @matcher_indexes end |
#options ⇒ Thor::CoreExt::HashWithIndifferentAccess (readonly)
The configured options for this blog
17 18 19 |
# File 'lib/middleman-blog/blog_data.rb', line 17 def @options end |
#path_matcher ⇒ Regex (readonly)
A regex for matching blog article source paths
9 10 11 |
# File 'lib/middleman-blog/blog_data.rb', line 9 def path_matcher @path_matcher end |
Instance Method Details
#article(path) ⇒ Middleman::Sitemap::Resource
The BlogArticle for the given path, or nil if one doesn’t exist.
62 63 64 65 66 67 68 69 |
# File 'lib/middleman-blog/blog_data.rb', line 62 def article(path) article = @app.sitemap.find_resource_by_path(path.to_s) if article && article.is_a?(BlogArticle) article else nil end end |
#articles ⇒ Array<Middleman::Sitemap::Resource>
A list of all blog articles, sorted by descending date
56 57 58 |
# File 'lib/middleman-blog/blog_data.rb', line 56 def articles @_articles.sort_by(&:date).reverse end |
#custom_permalink_components ⇒ Object
157 158 159 |
# File 'lib/middleman-blog/blog_data.rb', line 157 def custom_permalink_components permalink_url_components.reject { |component| DEFAULT_PERMALINK_COMPONENTS.include? component.to_sym } end |
#inspect ⇒ Object
165 166 167 |
# File 'lib/middleman-blog/blog_data.rb', line 165 def inspect "#<Middleman::Blog::BlogData: #{articles.inspect}>" end |
#manipulate_resource_list(resources)
This method returns an undefined value.
Updates’ blog articles destination paths to be the permalink.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/middleman-blog/blog_data.rb', line 93 def manipulate_resource_list(resources) @_articles = [] used_resources = [] resources.each do |resource| if resource.path =~ path_matcher resource.extend BlogArticle if @controller resource.blog_controller = controller end # Skip articles that are not published (in non-development environments) next unless @app.environment == :development || resource.published? # compute output path: # substitute date parts to path pattern resource.destination_path = Middleman::Util.normalize_path (resource) @_articles << resource elsif resource.path =~ @subdir_matcher match = $~.captures article_path = .sources %w(year month day title).each do |token| article_path = article_path.sub(":#{token}", match[@matcher_indexes[token]]) if @matcher_indexes[token] end article = @app.sitemap.find_resource_by_path(article_path) raise "Article for #{resource.path} not found" if article.nil? article.extend BlogArticle # Skip files that belong to articles that are not published (in non-development environments) next unless @app.environment == :development || article.published? # The subdir path is the article path with the index file name # or file extension stripped off. resource.destination_path = (article). sub(/(\/#{@app.index_file}$)|(\.[^.]+$)|(\/$)/, match[@matcher_indexes["path"]]) resource.destination_path = Middleman::Util.normalize_path(resource.destination_path) end used_resources << resource end used_resources end |
#parse_permalink_options(resource) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/middleman-blog/blog_data.rb', line 143 def (resource) permalink = .permalink. sub(':year', resource.date.year.to_s). sub(':month', resource.date.month.to_s.rjust(2, '0')). sub(':day', resource.date.day.to_s.rjust(2, '0')). sub(':title', resource.slug) custom_permalink_components.each do |component| permalink = permalink.sub(":#{component}", resource.data[component].parameterize) end permalink end |
#permalink_url_components ⇒ Object
161 162 163 |
# File 'lib/middleman-blog/blog_data.rb', line 161 def permalink_url_components .permalink.scan(/:([A-Za-z0-9]+)/).flatten end |
#tags ⇒ Hash<String, Array<Middleman::Sitemap::Resource>>
Returns a map from tag name to an array of BlogArticles associated with that tag.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/middleman-blog/blog_data.rb', line 74 def = {} @_articles.each do |article| article..each do |tag| [tag] ||= [] [tag] << article end end .each do |tag, articles| [tag] = articles.sort_by(&:date).reverse end end |