Permalink
Browse files

initial commit

  • Loading branch information...
pengwynn committed Sep 24, 2011
0 parents commit 1469033e024b79cd70a6f10b89ff8c28abf3b803
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in nesta-plugin-sluggable.gemspec
+gemspec
@@ -0,0 +1 @@
+require 'bundler/gem_tasks'
@@ -0,0 +1,3 @@
+require "nesta-plugin-sluggable/version"
+
+Nesta::Plugin.register(__FILE__)
@@ -0,0 +1,54 @@
+module Nesta
+ module Plugin
+ module Sluggable
+ module Helpers
+ # If your plugin needs any helper methods, add them here...
+ end
+
+ SLUGGED_FORMAT = /^\d+$/
+
+ def self.resolve_path(path)
+ segments = path.split('/')
+ path = segments[0..-2].join('/') if segments[-2].to_s.match(SLUGGED_FORMAT)
+
+ path
+ end
+ end
+ end
+
+ class App
+ helpers Nesta::Plugin::Sluggable::Helpers
+
+ before do
+ params[:splat] = [ Nesta::Sluggable.resolve_path(request.path) ]
+ end
+
+ after do
+ redirect(@page.permalink, 301) if (@page and !@page.best_path?(request.path))
+ end
+ end
+
+ class Page
+
+ def best_path?(path)
+ path == permalink
+ end
+
+ def slug
+ return nil unless File.basename(self.filename, File.extname(self.filename)).match(Plugin::Sluggable::SLUGGED_FORMAT)
+
+ s = self.metadata('slug')
+ s = self.heading.to_s.downcase.gsub(/[^a-z1-9]+/, '-').chomp('-') if s.nil?
+
+ s
+ end
+
+ def permalink
+ p = self.abspath
+ p += '/' + self.slug if self.slug
+
+ p
+ end
+
+ end
+end
@@ -0,0 +1,7 @@
+module Nesta
+ module Plugin
+ module Sluggable
+ VERSION = "0.0.1"
+ end
+ end
+end
@@ -0,0 +1,22 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "nesta-plugin-sluggable/version"
+
+Gem::Specification.new do |s|
+ s.name = "nesta-plugin-sluggable"
+ s.version = Nesta::Plugin::Sluggable::VERSION
+ s.authors = ["Wynn Netherland"]
+ s.email = ["wynn.netherland@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "nesta-plugin-sluggable"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+ s.add_dependency("nesta", ">= 0.9.11")
+ s.add_development_dependency("rake")
+end

0 comments on commit 1469033

Please sign in to comment.