From 8c47154e666348162d2ca4f2aa7a59ac1a68d31d Mon Sep 17 00:00:00 2001 From: muflax Date: Sat, 23 Jun 2012 20:20:36 +0200 Subject: [PATCH] support for moved sites --- commands/sites.rb | 4 +++- content/htaccess.erb | 5 +++++ lib/default.rb | 4 +++- lib/episteme.rb | 6 +----- lib/links.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 lib/links.rb diff --git a/commands/sites.rb b/commands/sites.rb index 90a46c0..de5c7d2 100644 --- a/commands/sites.rb +++ b/commands/sites.rb @@ -19,13 +19,15 @@ site_cmds = [ module ::Nanoc class Site attr_accessor :name + attr_reader :site_yaml, :moved_yaml def extended_build_config(dir_or_config_hash, site) puts "load extended config..." @name = site @site_yaml = YAML.load(File.open("sites.yaml")) - + @moved_yaml = YAML.load(File.open("moved.yaml")) + @config[:output_dir] = "out/#{site}" @config[:data_sources] = [{ diff --git a/content/htaccess.erb b/content/htaccess.erb index 49e9048..53783fd 100644 --- a/content/htaccess.erb +++ b/content/htaccess.erb @@ -16,6 +16,11 @@ RewriteRule ^feed/? /rss.xml <% @site.slug_items.each do |item| %>RewriteRule ^<%= item[:slug].chop %> <%= item.identifier%> [R=301] <% end %> +# redirect sites if they moved +<% @site.moved_pages.each do |from, to| %>RewriteRule ^<%= from %> <%= to %> [R=301] +<% end %> + + # serve gzipped files if available RewriteCond %{HTTP:Accept-Encoding} gzip RewriteCond %{REQUEST_FILENAME} !\.gz$ diff --git a/lib/default.rb b/lib/default.rb index fe9ca29..158274a 100644 --- a/lib/default.rb +++ b/lib/default.rb @@ -20,7 +20,7 @@ class Nanoc::Item # shared content or not? def shared? - self[:filename].start_with? "content/" + self[:filename].nil? or self[:filename].start_with? "content/" end def draft? @@ -93,6 +93,8 @@ class Nanoc::Site # slugs for wordpress links attr_reader :slug_items + + # pages that got moved around def initialize_items find_printed_items diff --git a/lib/episteme.rb b/lib/episteme.rb index ec96c5d..187b554 100644 --- a/lib/episteme.rb +++ b/lib/episteme.rb @@ -44,10 +44,6 @@ class Nanoc::Item def merged_link raise "no merged link for #{self.identifier}" unless self[:merged] - if m = self[:merged].match(/^(?\w+):(?.+)$/) - "http://#{m[:site] == "muflax" ? "" : "#{m[:site]}."}muflax.com/#{m[:page]}" - else - raise "invalid format: #{self[:merged]}" - end + local_link self[:merged] end end diff --git a/lib/links.rb b/lib/links.rb new file mode 100644 index 0000000..1ccec1d --- /dev/null +++ b/lib/links.rb @@ -0,0 +1,40 @@ +def local_link url + if m = url.match(/^(?\w+):(?.+)$/) + "#{site_url m[:site]}/#{m[:page]}" + else + raise "invalid format: #{self[:merged]}" + end +end + +def site_url site + "http://#{site == "muflax" ? "" : "#{site}."}muflax.com" +end + +def site_link site + site = site.to_s + + url = site_url site + title = @site.site_yaml["sites"][site]["title"] + + "#{url} - #{title}" +end + +def topic_link topic, target + url = local_link target + + "#{url} - #{topic}" +end + +class Nanoc::Site + def moved_pages + moved = [] + sites = @moved_yaml["sites"] + if sites.include? @name + sites[@name].each do |m| + moved << [m["from"], local_link(m["to"])] + end + end + + moved + end +end