Skip to content

Commit

Permalink
Move calendar/tag stuff to separate files, clean up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
bhollis committed Apr 21, 2012
1 parent d0076f6 commit 8e93501
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 143 deletions.
5 changes: 3 additions & 2 deletions lib/middleman-blog/blog_article.rb
Expand Up @@ -2,9 +2,10 @@

module Middleman
module Blog
# A class encapsulating the properties of a blog article.
# Access the underlying page object with {#page}.
# A module that adds blog-article methods to Resources.
module BlogArticle
# The "slug" of the article that shows up in its URL.
# @return [String]
attr_accessor :slug

# Render this resource
Expand Down
6 changes: 3 additions & 3 deletions lib/middleman-blog/blog_data.rb
Expand Up @@ -9,7 +9,7 @@ class BlogData
def initialize(app)
@app = app

# A map from path to BlogArticle
# A map from path to blog article
@_articles = []

matcher = Regexp.escape(@app.blog_sources).
Expand All @@ -23,7 +23,7 @@ def initialize(app)
end

# A list of all blog articles, sorted by date
# @return [Array<Middleman::Extensions::Blog::BlogArticle>]
# @return [Array<Middleman::Sitemap::Resource>]
def articles
@_articles.sort do |a,b|
b.date <=> a.date
Expand All @@ -38,7 +38,7 @@ def article(path)

# Returns a map from tag name to an array
# of BlogArticles associated with that tag.
# @return [Hash<String, Array<Middleman::Extensions::Blog::BlogArticle>>]
# @return [Hash<String, Array<Middleman::Sitemap::Resource>>]
def tags
tags = {}
@_articles.each do |article|
Expand Down
97 changes: 97 additions & 0 deletions lib/middleman-blog/calendar_pages.rb
@@ -0,0 +1,97 @@
module Middleman
module Blog

# A sitemap plugin that adds month/day/year pages to the sitemap
# based on the dates of blog articles.
class CalendarPages
def initialize(app)
@app = app
end

# Update the main sitemap resource list
# @return [void]
def manipulate_resource_list(resources)
new_resources = []
# Set up date pages if the appropriate templates have been specified
@app.blog.articles.group_by {|a| a.date.year }.each do |year, year_articles|
if @app.respond_to? :blog_year_template
@app.ignore @app.blog_year_template

path = Middleman::Util.normalize_path(@app.blog_year_path(year))

p = ::Middleman::Sitemap::Resource.new(
@app.sitemap,
path
)
p.proxy_to(@app.blog_year_template)

set_locals_year = Proc.new do
@year = year
@articles = year_articles
end

@app.sitemap.provides_metadata_for_path path do |path|
{ :blocks => set_locals_year }
end

new_resources << p
end

year_articles.group_by {|a| a.date.month }.each do |month, month_articles|
if @app.respond_to? :blog_month_template
@app.ignore @app.blog_month_template

path = Middleman::Util.normalize_path(@app.blog_month_path(year, month))

p = ::Middleman::Sitemap::Resource.new(
@app.sitemap,
path
)
p.proxy_to(@app.blog_month_template)

set_locals_month = Proc.new do
@year = year
@month = month
@articles = month_articles
end

@app.sitemap.provides_metadata_for_path path do |path|
{ :blocks => [ set_locals_month ] }
end

new_resources << p
end

month_articles.group_by {|a| a.date.day }.each do |day, day_articles|
if @app.respond_to? :blog_day_template
@app.ignore @app.blog_day_template

path = Middleman::Util.normalize_path(@app.blog_day_path(year, month, day))
p = ::Middleman::Sitemap::Resource.new(
@app.sitemap,
path
)
p.proxy_to(@app.blog_month_template)

set_locals_day = Proc.new do
@year = year
@month = month
@day = day
@articles = day_articles
end

@app.sitemap.provides_metadata_for_path path do |path|
{ :blocks => [ set_locals_day ] }
end

new_resources << p
end
end
end
end

resources + new_resources
end
end
end
end
142 changes: 4 additions & 138 deletions lib/middleman-blog/extension.rb
@@ -1,5 +1,7 @@
require 'middleman-blog/blog_data'
require 'middleman-blog/blog_article'
require 'middleman-blog/calendar_pages'
require 'middleman-blog/tag_pages'

module Middleman
module Blog
Expand Down Expand Up @@ -71,147 +73,11 @@ def registered(app)

sitemap.rebuild_resource_list!(:registered_new)
end

sitemap.provides_metadata file_matcher do
{
:options => {
:layout => blog_layout
}
}
end
end

#app.ready do
end
alias :included :registered
end

class TagPages
def initialize(app)
@app = app
end

# Update the main sitemap resource list
# @return [void]
def manipulate_resource_list(resources)
# TODO: gotta get tags out of the list of resources passed in?

resources + @app.blog.tags.map do |tag, articles|
path = @app.tag_path(tag)

p = ::Middleman::Sitemap::Resource.new(
@app.sitemap,
path
)
p.proxy_to(@app.blog_tag_template)

set_locals = Proc.new do
@tag = tag
@articles = articles
end

# TODO: how to keep from adding duplicates?
# How could we better set locals?
@app.sitemap.provides_metadata_for_path path do |path|
{ :blocks => [ set_locals ] }
end

p
end
end
end

class CalendarPages
def initialize(app)
@app = app
end

# Update the main sitemap resource list
# @return [void]
def manipulate_resource_list(resources)
new_resources = []
# Set up date pages if the appropriate templates have been specified
@app.blog.articles.group_by {|a| a.date.year }.each do |year, year_articles|
if @app.respond_to? :blog_year_template
@app.ignore @app.blog_year_template

path = Middleman::Util.normalize_path(@app.blog_year_path(year))

p = ::Middleman::Sitemap::Resource.new(
@app.sitemap,
path
)
p.proxy_to(@app.blog_year_template)

set_locals_year = Proc.new do
@year = year
@articles = year_articles
end

@app.sitemap.provides_metadata_for_path path do |path|
{ :blocks => set_locals_year }
end

new_resources << p
end

year_articles.group_by {|a| a.date.month }.each do |month, month_articles|
if @app.respond_to? :blog_month_template
@app.ignore @app.blog_month_template

path = Middleman::Util.normalize_path(@app.blog_month_path(year, month))

p = ::Middleman::Sitemap::Resource.new(
@app.sitemap,
path
)
p.proxy_to(@app.blog_month_template)

set_locals_month = Proc.new do
@year = year
@month = month
@articles = month_articles
end

@app.sitemap.provides_metadata_for_path path do |path|
{ :blocks => [ set_locals_month ] }
end

new_resources << p
end

month_articles.group_by {|a| a.date.day }.each do |day, day_articles|
if @app.respond_to? :blog_day_template
@app.ignore @app.blog_day_template

path = Middleman::Util.normalize_path(@app.blog_day_path(year, month, day))
p = ::Middleman::Sitemap::Resource.new(
@app.sitemap,
path
)
p.proxy_to(@app.blog_month_template)

set_locals_day = Proc.new do
@year = year
@month = month
@day = day
@articles = day_articles
end

@app.sitemap.provides_metadata_for_path path do |path|
{ :blocks => [ set_locals_day ] }
end

new_resources << p
end
end
end
end

resources + new_resources
end
end

# Helpers for use within templates and layouts.
module Helpers
# Get the {BlogData} for this site.
Expand All @@ -227,8 +93,8 @@ def is_blog_article?
!current_article.nil?
end

# Get a {BlogArticle} representing the current article.
# @return [BlogArticle]
# Get a {Resource} with mixed in {BlogArticle} methods representing the current article.
# @return [Middleman::Sitemap::Resource]
def current_article
blog.article(current_page.path)
end
Expand Down
39 changes: 39 additions & 0 deletions lib/middleman-blog/tag_pages.rb
@@ -0,0 +1,39 @@
module Middleman
module Blog

# A sitemap plugin that adds tag pages to the sitemap
# based on the tags of blog articles.
class TagPages
def initialize(app)
@app = app
end

# Update the main sitemap resource list
# @return [void]
def manipulate_resource_list(resources)
resources + @app.blog.tags.map do |tag, articles|
path = @app.tag_path(tag)

p = ::Middleman::Sitemap::Resource.new(
@app.sitemap,
path
)
p.proxy_to(@app.blog_tag_template)

set_locals = Proc.new do
@tag = tag
@articles = articles
end

# TODO: how to keep from adding duplicates here?
# How could we better set locals?
@app.sitemap.provides_metadata_for_path path do |path|
{ :blocks => [ set_locals ] }
end

p
end
end
end
end
end

0 comments on commit 8e93501

Please sign in to comment.