Skip to content
This repository
Browse code

Initial import of Permalizer

  • Loading branch information...
commit 8951359f34a9504d6172c9275b374841ee42f41f 0 parents
authored February 26, 2008
1  .gitignore
... ...
@@ -0,0 +1 @@
  1
+pkg/*
20  LICENSE
... ...
@@ -0,0 +1,20 @@
  1
+Copyright (c) 2008 Robert R Evans
  2
+
  3
+Permission is hereby granted, free of charge, to any person obtaining
  4
+a copy of this software and associated documentation files (the
  5
+"Software"), to deal in the Software without restriction, including
  6
+without limitation the rights to use, copy, modify, merge, publish,
  7
+distribute, sublicense, and/or sell copies of the Software, and to
  8
+permit persons to whom the Software is furnished to do so, subject to
  9
+the following conditions:
  10
+
  11
+The above copyright notice and this permission notice shall be
  12
+included in all copies or substantial portions of the Software.
  13
+
  14
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4  README
... ...
@@ -0,0 +1,4 @@
  1
+permalizer
  2
+==========
  3
+
  4
+A plugin for the Merb framework that provides ...
44  Rakefile
... ...
@@ -0,0 +1,44 @@
  1
+require 'rubygems'
  2
+require 'rake/gempackagetask'
  3
+
  4
+PLUGIN = "permalizer"
  5
+NAME = "permalizer"
  6
+VERSION = "0.0.5"
  7
+AUTHOR = "Robert R Evans"
  8
+EMAIL = "revans@robertrevans.com"
  9
+HOMEPAGE = "http://merb-plugins.rubyforge.org/permalizer/"
  10
+SUMMARY = "Creates clean URLs for whatever string you send to it."
  11
+
  12
+spec = Gem::Specification.new do |s|
  13
+  s.name = NAME
  14
+  s.version = VERSION
  15
+  s.platform = Gem::Platform::RUBY
  16
+  s.has_rdoc = true
  17
+  s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
  18
+  s.summary = SUMMARY
  19
+  s.description = s.summary
  20
+  s.author = AUTHOR
  21
+  s.email = EMAIL
  22
+  s.homepage = HOMEPAGE
  23
+  s.add_dependency('merb', '>= 0.9.0')
  24
+  s.require_path = 'lib'
  25
+  s.autorequire = PLUGIN
  26
+  s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
  27
+end
  28
+
  29
+Rake::GemPackageTask.new(spec) do |pkg|
  30
+  pkg.gem_spec = spec
  31
+end
  32
+
  33
+task :install => [:package] do
  34
+  sh %{sudo gem install pkg/#{NAME}-#{VERSION}}
  35
+end
  36
+
  37
+namespace :jruby do
  38
+
  39
+  desc "Run :package and install the resulting .gem with jruby"
  40
+  task :install => :package do
  41
+    sh %{#{SUDO} jruby -S gem install pkg/#{NAME}-#{Merb::VERSION}.gem --no-rdoc --no-ri}
  42
+  end
  43
+  
  44
+end
5  TODO
... ...
@@ -0,0 +1,5 @@
  1
+TODO:
  2
+Fix LICENSE with your name
  3
+Fix Rakefile with your name and contact info
  4
+Add your code to lib/permalizer.rb
  5
+Add your Merb rake tasks to lib/permalizer/merbtasks.rb
42  lib/permalizer.rb
... ...
@@ -0,0 +1,42 @@
  1
+if defined?(Merb::Plugins)
  2
+  require 'iconv' # required for handling unicode/various languages
  3
+
  4
+  module Merb #:nodoc:
  5
+
  6
+    # Permalizer adds functionality to the String Object so that clean URLs creation is made simple
  7
+    module Permalizer
  8
+
  9
+      # permalize! is a destructive method that will make the given string for use as a clean URL
  10
+      # <tt>example:</tt>
  11
+      # blog.title = "My Cool Merb Plugin!"
  12
+      # blog.title.permalize! # => "my-cool-merb-plugin"
  13
+      #
  14
+      def permalize!
  15
+        permalink!(self)
  16
+      end
  17
+
  18
+      # permalize is the same as permalize! except that it is not a destructive method
  19
+      # it creates a duplicate of the string and returns it as a clean string for URL usage
  20
+      #
  21
+      def permalize
  22
+        string = self.dup
  23
+        permalink!(string)
  24
+      end
  25
+
  26
+      private
  27
+
  28
+        # permalink!
  29
+        # Fix unicode characters, regex unwanted characters, split string, thus removing all whitespace, join it, and downcase it                   
  30
+        #
  31
+        def permalink!(word)
  32
+          (Iconv.new('US-ASCII//TRANSLIT', 'utf-8').iconv word).gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase  
  33
+        end  
  34
+    end
  35
+  end
  36
+
  37
+  # mixin Permalizer in the String Object Class
  38
+  String.class_eval do
  39
+    include Merb::Permalizer
  40
+  end
  41
+  
  42
+end
7  spec/permalizer_spec.rb
... ...
@@ -0,0 +1,7 @@
  1
+require File.dirname(__FILE__) + '/spec_helper'
  2
+
  3
+describe "permalizer" do
  4
+  it "should do nothing" do
  5
+    true.should == true
  6
+  end
  7
+end
2  spec/spec_helper.rb
... ...
@@ -0,0 +1,2 @@
  1
+$TESTING=true
  2
+$:.push File.join(File.dirname(__FILE__), '..', 'lib')

0 notes on commit 8951359

Please sign in to comment.
Something went wrong with that request. Please try again.