Created
June 1, 2015 16:08
-
-
Save bighairydave/279ae059a303a779e25c to your computer and use it in GitHub Desktop.
Monthly Atom feed of Jekyll posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Jekyll | |
class MonthlyFeed < Page | |
include Convertible | |
attr_accessor :site, :pager, :name, :ext, :basename, :dir, :data, :content, :output | |
# Initialize new ArchivePage | |
# +site+ is the Site | |
# +month+ is the month | |
# +posts+ is the list of posts for the month | |
# | |
# Returns <ArchivePage> | |
def initialize(site, feeddata) | |
@site = site | |
@feeddata = feeddata | |
self.ext = '.xml' | |
self.basename = 'monthlyfeed' | |
self.content = <<-EOS | |
{% assign months = "January,February,March,April,May,June,July,August,September,October,November,December" | split: "," %} | |
{% for month in page.feeddata %} | |
{% assign thisdate = month[0] | split: '/' %} | |
{% assign thisyear = thisdate[0] %} | |
{% assign monthindex = thisdate[1] | minus: 1 %} | |
{% assign thismonth = months[monthindex] %} | |
{% assign currentmonth = site.time | date: "%Y/%m" %} | |
{% unless month[0] == currentmonth %} | |
<entry> | |
<title>Birdblog posts from {{ thismonth }} {{ thisyear }}</title> | |
<link href="{{ site.url }}{{ site.baseurl }}/archive/{{ thisyear}}/{{ thisdate[1] }}/index.html"/> | |
<id>{{ site.url }}{{ site.baseurl }}/archive/{{ thisyear}}/{{ thisdate[1] }}/index.html</id> | |
<updated>{{ month[1].last.date | date_to_xmlschema }}</updated> | |
<summary>Birdblog posts for {{ thismonth }} {{ thisyear }}.</summary> | |
<content type = "html">{% capture content %} | |
<p>This month’s posts from my <a href="http://birdblog.bighairydave.com/">Bird Blog:</a> | |
<ul>{% for post in month[1] %} | |
<li>{{ post.date | date: "%B %-d:"}} <a href="{{ post.url | prepend: site.url }}">{{ post.title }}</a></li> | |
{% endfor %}</ul>{% endcapture %} | |
{{ content | xml_escape }} | |
</content> | |
</entry> | |
{% endunless %} | |
{% endfor %} | |
EOS | |
self.data = { | |
'layout' => 'feed', | |
'type' => 'archive', | |
'title' => "Birdblog Monthly Feed", | |
'feeddata' => @feeddata | |
} | |
end | |
# Add any necessary layouts | |
# +layouts+ is a Hash of {"name" => "layout"} | |
# +site_payload+ is the site payload hash | |
# | |
# Returns nothing | |
def render(layouts, site_payload) | |
payload = { | |
"page" => self.to_liquid, | |
"paginator" => pager.to_liquid | |
} | |
merged_payload = Utils.deep_merge_hashes(site_payload, payload) | |
do_layout(merged_payload, layouts) | |
end | |
def url | |
File.join("/", "monthlyfeed.xml") | |
end | |
def to_liquid | |
Utils.deep_merge_hashes(self.data, { | |
"url" => self.url, | |
"content" => self.content | |
}) | |
end | |
# Write the generated page file to the destination directory. | |
# +dest_prefix+ is the String path to the destination dir | |
# +dest_suffix+ is a suffix path to the destination dir | |
# | |
# Returns nothing | |
def write(dest_prefix, dest_suffix = nil) | |
dest = dest_prefix | |
dest = File.join(dest, dest_suffix) if dest_suffix | |
FileUtils.mkdir_p(dest) | |
# The url needs to be unescaped in order to preserve the | |
# correct filename | |
path = File.join(dest, CGI.unescape(self.url)) | |
FileUtils.mkdir_p(File.dirname(path)) | |
File.open(path, 'w') do |f| | |
f.write(self.output) | |
end | |
end | |
def html? | |
true | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment