handle custom stylesheets in blog

This commit is contained in:
Sami Samhuri 2011-12-03 15:01:35 -08:00
parent 23dc76a346
commit 87ca9bc101
4 changed files with 56 additions and 27 deletions

View file

@ -1,5 +1,5 @@
JAVASCRIPTS=$(shell echo assets/js/*.js) JAVASCRIPTS=$(shell echo assets/js/*.js)
STYLESHEETS=$(shell echo assets/css/*.css) STYLESHEETS=$(shell echo assets/css/*.css) $(shell echo _blog/styles/*.css)
POSTS=$(shell echo _blog/published/*.html) $(shell echo _blog/published/*.md) POSTS=$(shell echo _blog/published/*.html) $(shell echo _blog/published/*.md)
all: proj blog combine all: proj blog combine
@ -10,7 +10,7 @@ proj: projects.json templates/proj/index.html templates/proj/project.html
blog: _blog/blog.json templates/blog/index.html templates/blog/post.html $(POSTS) blog: _blog/blog.json templates/blog/index.html templates/blog/post.html $(POSTS)
@echo @echo
./bin/blog.rb _blog public/blog ./bin/blog.rb _blog public
minify: $(JAVASCRIPTS) $(STYLESHEETS) minify: $(JAVASCRIPTS) $(STYLESHEETS)
@echo @echo

View file

@ -37,6 +37,8 @@ class Blag
def initialize src, dest def initialize src, dest
@src = src @src = src
@dest = dest @dest = dest
@blog_dest = File.join(dest, 'blog')
@css_dest = File.join(dest, 'css')
read_blog read_blog
end end
@ -46,6 +48,7 @@ class Blag
generate_rss generate_rss
generate_posts_json generate_posts_json
generate_archive generate_archive
copy_assets
end end
def generate_index def generate_index
@ -53,13 +56,13 @@ class Blag
index_template = File.read(File.join('templates', 'blog', 'index.html')) index_template = File.read(File.join('templates', 'blog', 'index.html'))
post = posts.first post = posts.first
values = { :post => post, values = { :post => post,
:article => Mustache.render(template(post[:type]), post), :article => html(post),
:previous => posts[1], :previous => posts[1],
:filename => post[:filename], :filename => post[:filename],
:comments => post[:comments] :comments => post[:comments]
} }
index_html = Mustache.render(index_template, values) index_html = Mustache.render(index_template, values)
File.open(File.join(@dest, 'index.html'), 'w') {|f| f.puts(index_html) } File.open(File.join(@blog_dest, 'index.html'), 'w') {|f| f.puts(index_html) }
end end
def generate_posts def generate_posts
@ -67,7 +70,7 @@ class Blag
posts.each_with_index do |post, i| posts.each_with_index do |post, i|
values = { :title => post[:title], values = { :title => post[:title],
:link => post[:link], :link => post[:link],
:article => Mustache.render(template(post[:type]), post), :article => html(post),
:previous => i < posts.length - 1 && posts[i + 1], :previous => i < posts.length - 1 && posts[i + 1],
:next => i > 0 && posts[i - 1], :next => i > 0 && posts[i - 1],
:filename => post[:filename], :filename => post[:filename],
@ -75,24 +78,30 @@ class Blag
:keywords => (DefaultKeywords + post[:tags]).join(',') :keywords => (DefaultKeywords + post[:tags]).join(',')
} }
post[:html] = Mustache.render(page_template, values) post[:html] = Mustache.render(page_template, values)
File.open(File.join(@dest, post[:filename]), 'w') {|f| f.puts(post[:html]) } File.open(File.join(@blog_dest, post[:filename]), 'w') {|f| f.puts(post[:html]) }
end end
end end
def generate_posts_json def generate_posts_json
json = JSON.generate({ :published => posts.map {|p| p[:filename]} }) json = JSON.generate({ :published => posts.map {|p| p[:filename]} })
File.open(File.join(@dest, 'posts.json'), 'w') { |f| f.puts(json) } File.open(File.join(@blog_dest, 'posts.json'), 'w') { |f| f.puts(json) }
end end
def generate_archive def generate_archive
archive_template = File.read(File.join('templates', 'blog', 'archive.html')) archive_template = File.read(File.join('templates', 'blog', 'archive.html'))
html = Mustache.render(archive_template, :posts => posts) html = Mustache.render(archive_template, :posts => posts)
File.open(File.join(@dest, 'archive.html'), 'w') { |f| f.puts(html) } File.open(File.join(@blog_dest, 'archive.html'), 'w') { |f| f.puts(html) }
end end
def generate_rss def generate_rss
# posts rss # posts rss
File.open(rss_file, 'w') { |f| f.puts(rss.target!) } File.open(rss_file, 'w') { |f| f.puts(rss_for_posts.target!) }
end
def copy_assets
Dir[File.join(@src, 'css', '*.css')].each do |stylesheet|
FileUtils.cp(stylesheet, @css_dest)
end
end end
def posts def posts
@ -112,26 +121,38 @@ class Blag
puts "ignoring unknown header: #{line}" puts "ignoring unknown header: #{line}"
end end
end end
post[:styles] = (post[:styles] || '').split(/\s*,\s*/)
post[:tags] = (post[:tags] || '').split(/\s*,\s*/)
post[:url] = @url + '/' + post[:filename] post[:url] = @url + '/' + post[:filename]
post[:timestamp] = post[:timestamp].to_i post[:timestamp] = post[:timestamp].to_i
post[:content] = lines.join post[:content] = lines.join
post[:body] = RDiscount.new(post[:content]).to_html post[:body] = RDiscount.new(post[:content]).to_html
post[:type] = post[:link] ? :link : :post post[:type] = post[:link] ? :link : :post
post[:rss_html] = Mustache.render(rss_template(post[:type]), {:post => post})
post[:rfc822] = Time.at(post[:timestamp]).rfc822 post[:rfc822] = Time.at(post[:timestamp]).rfc822
post[:tags] = (post[:tags] || '').split(/\s*,\s*/).map(&:strip)
# comments on by default # comments on by default
post[:comments] = true if post[:comments].nil? post[:comments] = true if post[:comments].nil?
post post
end.sort { |a, b| b[:timestamp] <=> a[:timestamp] } end.sort { |a, b| b[:timestamp] <=> a[:timestamp] }
end end
def rss
rss_for_posts
end
private private
def blog_file
File.join(@src, 'blog.json')
end
def read_blog
blog = JSON.parse(File.read(blog_file))
@title = blog['title']
@subtitle = blog['subtitle']
@url = blog['url']
end
def html(post)
Mustache.render(template(post[:type]), post)
end
def template(type) def template(type)
if type == :post if type == :post
@post_template ||= File.read(File.join('templates', 'blog', 'post.mustache')) @post_template ||= File.read(File.join('templates', 'blog', 'post.mustache'))
@ -142,10 +163,6 @@ class Blag
end end
end end
def blog_file
File.join(@src, 'blog.json')
end
def rss_template(type) def rss_template(type)
if type == :post if type == :post
@post_rss_template ||= File.read(File.join('templates', 'blog', 'post.rss.html')) @post_rss_template ||= File.read(File.join('templates', 'blog', 'post.rss.html'))
@ -156,17 +173,14 @@ class Blag
end end
end end
def read_blog
blog = JSON.parse(File.read(blog_file))
@title = blog['title']
@subtitle = blog['subtitle']
@url = blog['url']
end
def rss_file def rss_file
File.join(@dest, 'sjs.rss') File.join(@blog_dest, 'sjs.rss')
end end
def rss_html(post)
Mustache.render(rss_template(post[:type]), { :post => post })
end
def rss_for_posts(options = {}) def rss_for_posts(options = {})
title = options[:title] || @title title = options[:title] || @title
subtitle = options[:subtitle] || @subtitle subtitle = options[:subtitle] || @subtitle
@ -176,6 +190,11 @@ class Blag
xml = Builder::XmlMarkup.new xml = Builder::XmlMarkup.new
xml.instruct! :xml, :version => '1.0' xml.instruct! :xml, :version => '1.0'
xml.instruct! 'xml-stylesheet', :href => 'http://samhuri.net/css/blog-all.min.css', :type => 'text/css' xml.instruct! 'xml-stylesheet', :href => 'http://samhuri.net/css/blog-all.min.css', :type => 'text/css'
post[:styles].each do |style|
xml.instruct! 'xml-stylesheet', :href => "http://samhuri.net/css/#{style}.min.css", :type => 'text/css'
end
xml.rss :version => '2.0' do xml.rss :version => '2.0' do
xml.channel do xml.channel do
xml.title title xml.title title
@ -186,7 +205,7 @@ class Blag
posts.each do |post| posts.each do |post|
xml.item do xml.item do
xml.title post[:link] ? "#{post[:title]} &rarr;" : post[:title] xml.title post[:link] ? "#{post[:title]} &rarr;" : post[:title]
xml.description post[:rss_html] xml.description rss_html(post)
xml.pubDate post[:rfc822] xml.pubDate post[:rfc822]
xml.author post[:author] xml.author post[:author]
xml.link post[:link] || post[:url] xml.link post[:link] || post[:url]

View file

@ -5,6 +5,11 @@
<title>sjs' blog</title> <title>sjs' blog</title>
<link rel="icon" type="image/gif" href="../images/s.gif"> <link rel="icon" type="image/gif" href="../images/s.gif">
<link rel="stylesheet" href="../css/blog-all.min.css"> <link rel="stylesheet" href="../css/blog-all.min.css">
{{#post}}
{{#styles}}
<link rel="stylesheet" href="../css/{{.}}.min.css">
{{/styles}}
{{/post}}
<link rel="stylesheet" media="screen" href="../css/mobile.min.css"> <link rel="stylesheet" media="screen" href="../css/mobile.min.css">
<link rel="alternate" type="application/rss+xml" href="http://samhuri.net/blog/sjs.rss" title="sjs' blog"> <link rel="alternate" type="application/rss+xml" href="http://samhuri.net/blog/sjs.rss" title="sjs' blog">
<body id="blog"> <body id="blog">

View file

@ -7,6 +7,11 @@
<link rel="icon" type="image/gif" href="../images/s.gif"> <link rel="icon" type="image/gif" href="../images/s.gif">
<link rel="stylesheet" href="../css/blog-all.min.css"> <link rel="stylesheet" href="../css/blog-all.min.css">
<link rel="stylesheet" media="screen" href="../css/mobile.min.css"> <link rel="stylesheet" media="screen" href="../css/mobile.min.css">
{{#post}}
{{#styles}}
<link rel="stylesheet" href="../css/{{.}}.min.css">
{{/styles}}
{{/post}}
<link rel="alternate" type="application/rss+xml" href="http://samhuri.net/blog/sjs.rss" title="sjs' blog"> <link rel="alternate" type="application/rss+xml" href="http://samhuri.net/blog/sjs.rss" title="sjs' blog">
<body id="blog"> <body id="blog">
<nav id="breadcrumbs"><a href="../">samhuri.net</a></nav> <nav id="breadcrumbs"><a href="../">samhuri.net</a></nav>