Refactoring * Tag page generation * Utility modules
h3rald h3rald@h3rald.com
Fri, 28 Aug 2009 09:30:32 +0200
44 files changed,
174 insertions(+),
173 deletions(-)
jump to
M
layouts/default.htm
→
layouts/default.htm
@@ -27,17 +27,6 @@ <link href="/css/layout.css" media="all" rel="stylesheet" type="text/css" />
<link href="/css/text.css" media="all" rel="stylesheet" type="text/css" /> <link href="/css/elements.css" media="all" rel="stylesheet" type="text/css" /> <link href="/css/code.css" media="all" rel="stylesheet" type="text/css" /> - <!-- - <script src="/js/jquery-1.3.2.js" type="text/javascript"></script> - <script src="/js/cufon-yui.js" type="text/javascript"></script> - <script src="/js/Mutlu_400.font.js" type="text/javascript"></script> - <script src="/js/jquery.preload.js" type="text/javascript"></script> - <script src="/js/jquery.timeago.js" type="text/javascript"></script> - <script src="/js/date.js" type="text/javascript"></script> - <script src="/js/feeds.js" type="text/javascript"></script> - <script src="/js/search.js" type="text/javascript"></script> - <script src="/js/init.js" type="text/javascript"></script> - --> </head> <body> <div id="wrapper">
M
lib/helpers.rb
→
lib/helpers.rb
@@ -25,6 +25,10 @@ def sorted_site_tags
site_tags.sort{|a, b| a[1] <=> b[1]}.reverse end + def articles_tagged_with(tag) + @site.items.select{|p| p.attributes[:tags] && p.attributes[:tags].include?(tag)}.sort{|a,b| a.attributes[:date] <=> b.attributes[:date]}.reverse + end + end module Nanoc3::Helpers::Blogging
A
lib/utils.rb
@@ -0,0 +1,118 @@
+module SiteUtils + + def write_tag_page(dir, tag, count) + # Create tag page + meta = {} + meta[:title] = "Tag: #{tag}" + meta[:type] = 'page' + meta[:filters_pre] = ['erb', 'redcloth'] + meta[:permalink] = tag + pl = (count == 1) ? ' is' : 's are' + contents = %{\n#{count} item#{pl} tagged with _#{tag}_: + +<% articles_tagged_with('#{tag}').each do |pg| +%>* <span class="<%= pg.attributes[:type] %>_link"> <a href="/articles/<%= pg.attributes[:permalink] %>/"><%= pg.attributes[:title] %></a></span> +<% end %> + } + # Write html page + write_item dir/"#{tag}.textile", meta, contents + end + + def write_archive_page(dir, name, count) + # Create archive page + meta = {} + meta[:title] = "Archive: #{name}" + meta[:type] = 'page' + meta[:filters_pre] = ['erb', 'redcloth'] + meta[:permalink] = name.downcase.gsub /\s/, '-' + pl = (count == 1) ? ' was' : 's were' + contents = %{\n#{count} item#{pl} written in _#{name}_: + +<% articles_by_month.select{|i| i[0] == "#{name}"}[0][1].each do |pg| +%>* <span class="<%= pg.attributes[:type] %>_link"> <a href="/articles/<%= pg.attributes[:permalink] %>/"><%= pg.attributes[:title] %></a></span> +<% end %> + } + # Write file + write_item dir/"#{meta[:permalink]}.textile", meta, contents + end + + def write_item(path, meta, contents) + path.parent.mkpath + (path).open('w+') do |f| + f.print "--" + f.puts meta.to_yaml + f.puts "-----" + f.puts contents + end + end + +end + +module TypoUtils extend SiteUtils + + # Ignored by Nanoc 3 + def get_filter(db, fid) + filter = db[:text_filters].where("id = ?", fid).get(:name).downcase + # Multiple filters are not handled (e.g. markdown smartypants) + filter = filter.split(' ')[0] + # Prepare metadata + case filter + when 'textile' then + return ['redcloth'], 'textile' + when 'markdown' then + return ['bluecloth'], 'markdown' + when 'bbcode' then + return ['bbcode'], 'bbcode' + else + return [], 'txt' + end + end + + def get_tags(keywords=nil) + tags = [] + if keywords then + if keywords.match ',' then + tags = keywords.downcase.split(", ") + else + tags = keywords.downcase.split(" ") + end + end + tags + end + + def get_comments(db, aid) + dataset = db[:feedback].where("article_id = ? && state LIKE '%ham%'", aid) + comments = [] + dataset.each do |c| + comment = {} + comment[:id] = c[:id] + comment[:author] = c[:author] + comment[:body] = c[:body].to_s + comment[:url] = c[:url] + comment[:date] = c[:published_at] + comments << comment + end + comments + end + + def convert_code_blocks(meta, contents) + if contents.match /<typo:code/ then + # troubles if erb filter is enabled! + contents.gsub! /<%/, '<%' + contents.gsub! /%>/, '%>' + contents.gsub!(/<typo:code lang="([a-zA-Z0-9]+)">/, '<% highlight :\1 do %>') + contents.gsub!(/<typo:code>/, '<% highlight :text do %>') + contents.gsub!(/<\/typo:code>/, "<% end %>") + meta['filters_pre'] = ['erb'].concat meta['filters_pre'] + end + contents + end + + def write_page(meta, contents, extension) + path = (meta['type'] == 'article') ? Pathname.new(Dir.pwd)/"content/articles/" : Pathname.new(Dir.pwd)/"content/" + name = "#{meta['permalink']}.#{extension}" + write_item path/name, meta, contents + end + +end +
A
resources/js/README.textile
@@ -0,0 +1,11 @@
+h1. Javascript Loading Order + +# jquery-1.3.2.js +# cufon-yui.js +# Mutlu_400.font.js +# jquery.preload.js +# jquery.timeago.js +# date.js +# feeds.js +# search.js +# init.js
M
tasks/site.rake
→
tasks/site.rake
@@ -3,55 +3,7 @@ require 'extlib'
require 'pathname' require 'fileutils' require 'nanoc3' - -module SiteUtils - - def write_tag_page(dir, name, count) - # Create tag page - meta = {} - meta[:title] = "Tag: #{name}" - meta[:type] = 'page' - meta[:filters_pre] = ['erb', 'redcloth'] - meta[:permalink] = name - pl = (count == 1) ? ' is' : 's are' - contents = %{\n#{count} item#{pl} tagged with _#{name}_: - -<% @site.items.select{|p| p.attributes[:tags] && p.attributes[:tags].include?('#{name}')}.sort{|a,b| a.attributes[:date] <=> b.attributes[:date]}.reverse.each do |pg| -%>* <span class="<%= pg.attributes[:type] %>_link"> <a href="/articles/<%= pg.attributes[:permalink] %>/"><%= pg.attributes[:title] %></a></span> -<% end %> - } - # Write file - (dir/"#{name}.textile").open('w+') do |f| - f.print "--" - f.puts meta.to_yaml - f.puts "-----" - f.puts contents - end - end - - def write_archive_page(dir, name, count) - # Create archive page - meta = {} - meta[:title] = "Archive: #{name}" - meta[:type] = 'page' - meta[:filters_pre] = ['erb', 'redcloth'] - meta[:permalink] = name.downcase.gsub /\s/, '-' - pl = (count == 1) ? ' was' : 's were' - contents = %{\n#{count} item#{pl} written in _#{name}_: - -<% articles_by_month.select{|i| i[0] == "#{name}"}[0][1].each do |pg| -%>* <span class="<%= pg.attributes[:type] %>_link"> <a href="/articles/<%= pg.attributes[:permalink] %>/"><%= pg.attributes[:title] %></a></span> -<% end %> - } - # Write file - (dir/"#{meta[:permalink]}.textile").open('w+') do |f| - f.print "--" - f.puts meta.to_yaml - f.puts "-----" - f.puts contents - end - end -end +require 'lib/utils.rb' include SiteUtils@@ -69,7 +21,7 @@ system "nanoc3 co"
end task :run => [:copy_resources] do - system "nanoc3 aco" + system "nanoc3 aco -s thin" end task :rebuild => [:clear_output, :update] do
M
tasks/typo.rake
→
tasks/typo.rake
@@ -9,80 +9,7 @@ rescue Exception => e
end require 'yaml' require 'iconv' - -module TypoUtils - - # Ignored by Nanoc 3 - def get_filter(db, fid) - filter = db[:text_filters].where("id = ?", fid).get(:name).downcase - # Multiple filters are not handled (e.g. markdown smartypants) - filter = filter.split(' ')[0] - # Prepare metadata - case filter - when 'textile' then - return ['redcloth'], 'textile' - when 'markdown' then - return ['bluecloth'], 'markdown' - when 'bbcode' then - return ['bbcode'], 'bbcode' - else - return [], 'txt' - end - end - - def get_tags(keywords=nil) - tags = [] - if keywords then - if keywords.match ',' then - tags = keywords.downcase.split(", ") - else - tags = keywords.downcase.split(" ") - end - end - tags - end - - def get_comments(db, aid) - dataset = db[:feedback].where("article_id = ? && state LIKE '%ham%'", aid) - comments = [] - dataset.each do |c| - comment = {} - comment[:id] = c[:id] - comment[:author] = c[:author] - comment[:body] = c[:body].to_s - comment[:url] = c[:url] - comment[:date] = c[:published_at] - comments << comment - end - comments - end - - def convert_code_blocks(meta, contents) - if contents.match /<typo:code/ then - # troubles if erb filter is enabled! - contents.gsub! /<%/, '<%' - contents.gsub! /%>/, '%>' - contents.gsub!(/<typo:code lang="([a-zA-Z0-9]+)">/, '<% highlight :\1 do %>') - contents.gsub!(/<typo:code>/, '<% highlight :text do %>') - contents.gsub!(/<\/typo:code>/, "<% end %>") - meta['filters_pre'] = ['erb'].concat meta['filters_pre'] - end - contents - end - - def write_page(meta, contents, extension) - path = (meta['type'] == 'article') ? Pathname.new(Dir.pwd)/"content/articles/" : Pathname.new(Dir.pwd)/"content/" - name = "#{meta['permalink']}.#{extension}" - path.mkpath - (path/name).open('w+') do |f| - f.print "--" - f.puts meta.to_yaml - f.puts "-----" - f.puts contents - end - end - -end +require 'lib/utils.rb' include TypoUtils