Class: Middleman::BlogPage::BlogPageData
- Inherits:
-
Object
- Object
- Middleman::BlogPage::BlogPageData
- Defined in:
- lib/middleman-blog_page/blog_page_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.
-
#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
- #custom_permalink_components ⇒ Object
-
#initialize(app, options = {}, controller = nil) ⇒ BlogPageData
constructor
A new instance of BlogPageData.
- #inspect ⇒ Object
-
#manipulate_resource_list(resources) ⇒ void
Updates’ blog articles destination paths to be the permalink.
-
#page(path) ⇒ Middleman::Sitemap::Resource
The BlogArticle for the given path, or nil if one doesn’t exist.
-
#pages ⇒ Array<Middleman::Sitemap::Resource>
A list of all blog articles, sorted by descending priority.
- #parse_permalink_options(resource) ⇒ Object
- #permalink_url_components ⇒ Object
Constructor Details
#initialize(app, options = {}, controller = nil) ⇒ BlogPageData
Returns a new instance of BlogPageData.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/middleman-blog_page/blog_page_data.rb', line 22 def initialize(app, ={}, controller=nil) @app = app @options = @controller = controller # A list of resources corresponding to blog page articles @_articles = [] matcher = Regexp.escape(.sources). sub(/^\//, ""). sub(":title", "([^/]+)") subdir_matcher = matcher.sub(/\\\.[^.]+$/, "(/.*)$") @path_matcher = /^#{matcher}/ @subdir_matcher = /^#{subdir_matcher}/ # Build a hash of part name to capture index, e.g. {"year" => 0} @matcher_indexes = {} .sources.scan(/:title/). each_with_index do |key, i| @matcher_indexes[key[1..-1]] = i end # The path always appears at the end. @matcher_indexes["path"] = @matcher_indexes.size end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
19 20 21 |
# File 'lib/middleman-blog_page/blog_page_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_page/blog_page_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_page/blog_page_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_page/blog_page_data.rb', line 9 def path_matcher @path_matcher end |
Instance Method Details
#custom_permalink_components ⇒ Object
128 129 130 |
# File 'lib/middleman-blog_page/blog_page_data.rb', line 128 def custom_permalink_components permalink_url_components.reject { |component| component.to_sym == :title } end |
#inspect ⇒ Object
136 137 138 |
# File 'lib/middleman-blog_page/blog_page_data.rb', line 136 def inspect "#<Middleman::BlogPage::BlogPageData: #{articles.inspect}>" end |
#manipulate_resource_list(resources) ⇒ void
This method returns an undefined value.
Updates’ blog articles destination paths to be the permalink.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/middleman-blog_page/blog_page_data.rb', line 69 def manipulate_resource_list(resources) @_articles = [] used_resources = [] resources.each do |resource| if resource.path =~ path_matcher resource.extend BlogPageArticle if @controller resource.blog_page_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 article_path = article_path.sub(":title", match[@matcher_indexes["title"]]) if @matcher_indexes["title"] puts article_path article = @app.sitemap.find_resource_by_path(article_path) raise "Article for #{resource.path} not found" if article.nil? article.extend BlogPageArticle # 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 |
#page(path) ⇒ Middleman::Sitemap::Resource
The BlogArticle for the given path, or nil if one doesn’t exist.
57 58 59 60 61 62 63 64 |
# File 'lib/middleman-blog_page/blog_page_data.rb', line 57 def page(path) article = @app.sitemap.find_resource_by_path(path.to_s) if article && article.is_a?(BlogPageArticle) article else nil end end |
#pages ⇒ Array<Middleman::Sitemap::Resource>
A list of all blog articles, sorted by descending priority
51 52 53 |
# File 'lib/middleman-blog_page/blog_page_data.rb', line 51 def pages @_articles.sort_by(&:priority).reverse end |
#parse_permalink_options(resource) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/middleman-blog_page/blog_page_data.rb', line 118 def (resource) permalink = .permalink.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
132 133 134 |
# File 'lib/middleman-blog_page/blog_page_data.rb', line 132 def permalink_url_components .permalink.scan(/:([A-Za-z0-9]+)/).flatten end |