pulling blog feature into extension

This commit is contained in:
Thomas Reynolds 2011-08-19 17:45:10 -07:00
parent 5f2df57f33
commit b39798a781
12 changed files with 9 additions and 288 deletions

View file

@ -1,6 +1,14 @@
2.0.3
=====
- Pulled out undocumented Blog feature into its own extension
2.0.2
=====
- Fixed Sprockets circular error
- Added auto-requiring extensions
2.0.0 2.0.0
===== =====
- Guard-powered auto-reloading of config.rb - Guard-powered auto-reloading of config.rb
- Guard LiveReload - Guard LiveReload
- Sprockets JS - Sprockets JS

View file

@ -141,9 +141,6 @@ module Middleman
# words, paragraphs, fake images, names and email addresses. # words, paragraphs, fake images, names and email addresses.
autoload :Lorem, "middleman/features/lorem" autoload :Lorem, "middleman/features/lorem"
# Treat project as a blog
autoload :Blog, "middleman/features/blog"
# guard-livereload # guard-livereload
autoload :LiveReload, "middleman/features/live_reload" autoload :LiveReload, "middleman/features/live_reload"

View file

@ -1,156 +0,0 @@
module Middleman
module Features
module Blog
class << self
def registered(app)
# Include helpers
app.helpers Middleman::Features::Blog::Helpers
app.after_configuration do
if !app.settings.respond_to? :blog_permalink
app.set :blog_permalink, ":year/:month/:day/:title.html"
end
if !app.settings.respond_to? :blog_taglink
app.set :blog_taglink, "tags/:tag.html"
end
if !app.settings.respond_to? :blog_layout
app.set :blog_layout, "layout"
end
if !app.settings.respond_to? :blog_summary_separator
app.set :blog_summary_separator, /READMORE/
end
if !app.settings.respond_to? :blog_summary_length
app.set :blog_summary_length, 250
end
if !app.settings.respond_to? :blog_layout_engine
app.set :blog_layout_engine, "erb"
end
if !app.settings.respond_to? :blog_index_template
app.set :blog_index_template, "index_template"
end
if !app.settings.respond_to? :blog_article_template
app.set :blog_article_template, "article_template"
end
if !app.build?
$stderr.puts "== Blog: #{app.settings.blog_permalink}"
end
app.get("/#{app.blog_permalink}") do
process_request({
:layout => app.blog_layout,
:layout_engine => app.blog_layout_engine
})
# No need for separator on permalink page
body body.gsub!(app.blog_summary_separator, "")
end
end
app.before_processing do
articles_glob = File.join(app.views, app.settings.blog_permalink.gsub(/(:\w+)/, "*") + ".*")
articles = Dir[articles_glob].map do |article|
template_content = File.read(article)
data, content = app.parse_front_matter(template_content)
data["date"] = Date.parse(data["date"])
data["raw"] = content
data["url"] = article.gsub(app.views, "").split(".html").first + ".html"
all_content = Tilt.new(article).render
data["body"] = all_content.gsub!(app.settings.blog_summary_separator, "")
sum = if data["raw"] =~ app.settings.blog_summary_separator
data["raw"].split(app.settings.blog_summary_separator).first
else
data["raw"].match(/(.{1,#{app.settings.blog_summary_length}}.*?)(\n|\Z)/m).to_s
end
engine = app.settings.markdown_engine.new { sum }
data["summary"] = engine.render
data
end.sort { |a, b| b["date"] <=> a["date"] }
tags = {}
articles.each do |article|
article["tags"] ||= ""
if !article["tags"].empty?
tags_array = article["tags"].split(',').map{|t| t.strip}
tags_array.each do |tag_title|
tag_key = tag_title.parameterize
tag_path = blog_taglink.gsub(/(:\w+)/, tag_key)
(tags[tag_path] ||= {})["title"] = tag_title
tags[tag_path]["ident"] = tag_key
(tags[tag_path]["pages"] ||= {})[article["title"]] = article["url"]
end
end
end
app.data_content("blog", { :articles => articles, :tags => tags })
true
end
end
alias :included :registered
end
module Helpers
def is_blog_article?
!current_article_title.blank?
end
def blog_title
end
def current_article_date
DateTime.parse(current_article_metadata.date)
end
def current_article_title
current_article_metadata.title
end
def current_article_metadata
data.page
end
def current_article_tags
article_tags_hash = {}
if is_blog_article? && current_article_metadata.tags
article_tags = current_article_metadata.tags.split(',').map{|t| t.strip}
article_tags.each do |tag_title|
article_tags_hash[tag_title] = self.class.blog_taglink.gsub(/(:\w+)/, tag_title.parameterize)
end
end
article_tags_hash
end
def blog_tags
data.blog.tags
end
def current_tag_data
data.blog.tags[request.path]
end
def current_tag_articles
data.blog.articles.map do |article|
article if current_tag_data.pages.has_value?(article.url)
end.compact
end
def current_tag_title
current_tag_data[:title]
end
end
end
end
end

View file

@ -46,9 +46,6 @@ end
# Default template # Default template
require "middleman/templates/default" require "middleman/templates/default"
# Blog template
require "middleman/templates/blog"
# HTML5 template # HTML5 template
require "middleman/templates/html5" require "middleman/templates/html5"

View file

@ -1,20 +0,0 @@
class Middleman::Templates::Blog < Middleman::Templates::Base
def self.source_root
File.dirname(__FILE__)
end
def build_scaffold
template "blog/config.tt", File.join(location, "config.rb")
directory "blog/source", File.join(location, "source")
empty_directory File.join(location, "source", options[:css_dir])
empty_directory File.join(location, "source", options[:js_dir])
empty_directory File.join(location, "source", options[:images_dir])
end
def generate_rack
template "blog/config.ru", File.join(location, "config.ru")
end
end
Middleman::Templates.register(:blog, Middleman::Templates::Blog)

View file

@ -1,9 +0,0 @@
# Rack config
# Look for index files in folders like Apache
require "rack/contrib/try_static"
use Rack::TryStatic, :root => "build", :urls => %w[/], :try => ['.html', 'index.html', '/index.html']
# Cache static assets
require "rack/contrib/static_cache"
use Rack::StaticCache, :urls => ['/'], :root => 'build'

View file

@ -1,18 +0,0 @@
activate :blog
# set :blog_permalink, ":year/:month/:day/:title.html"
# set :blog_summary_separator, /READMORE/
# set :blog_summary_length, 500
page "/feed.xml", :layout => false
# Build-specific configuration
configure :build do
# For example, change the Compass output style for deployment
# activate :minify_css
# Minify Javascript on build
# activate :minify_javascript
# Enable cache buster
# activate :cache_buster
end

View file

@ -1,6 +0,0 @@
---
title: "New Article title"
date: 01/01/2011
---
Content of my article

View file

@ -1,10 +0,0 @@
<h1>Archive: <%# @path %></h1>
<ul class="archives">
<% for entry in data.blog.articles %>
<li>
<a href="<%= entry.url %>"><%= entry.title %></a>
<span><%= entry.date.strftime('%b %e') %></span>
</li>
<% end %>
</ul>

View file

@ -1,23 +0,0 @@
xml.instruct!
xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do
xml.title "Blog Name"
xml.subtitle "Blog subtitle"
xml.id "http://blog.url.com/"
xml.link "href" => "http://blog.url.com/"
xml.link "href" => "http://blog.url.com/feed.xml", "rel" => "self"
xml.updated data.blog.articles.first.date.to_time.iso8601
xml.author { xml.name "Blog Author" }
data.blog.articles.each do |article|
xml.entry do
xml.title article.title
xml.link "rel" => "alternate", "href" => article.url
xml.id article.url
xml.published article.date.to_time.iso8601
xml.updated article.date.to_time.iso8601
xml.author { xml.name "Article Author" }
xml.summary article.summary, "type" => "html"
xml.content article.body, "type" => "html"
end
end
end

View file

@ -1,9 +0,0 @@
<% data.blog.articles[0...5].each_with_index do |article, i| %>
<article class="<%= (i == 0) ? 'first' : '' %>">
<h1><a href="<%= article.url %>"><%= article.title %></a> <span><%= article.date.strftime('%b %e %Y') %></span></h1>
<%= article.summary %>
<div class="more"><a href="<%= article.url %>">read on &raquo;</a></div>
</article>
<% end %>

View file

@ -1,30 +0,0 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv='X-UA-Compatible' content='IE=edge;chrome=1' />
</head>
<body>
<div id="main" role="main">
<% if is_blog_article? %>
<% content_for :blog_article, yield %>
<%= partial settings.blog_article_template %>
<% else %>
<%= yield %>
<% end %>
</div>
<aside>
<h2>
Recent Articles
<a href="/archives">Archive</a>
</h2>
<ol>
<% data.blog.articles[0...10].each do |article| %>
<li><a href="<%= article.url %>"><%= article.title %></a> <span><%= article.date.strftime('%b %e') %></span></li>
<% end %>
</li>
</aside>
</body>
</html>