Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Carlo www.carloforghieri.com/blog/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Permalink from post title

Method for permalink creation in ruby, based on drupal code

   1  
   2  def create_permalink(string, separator = '-', max_size = 127)
   3  
   4      # words to ignore, usually the same words ignored by search engines
   5      ignore_words = ['a', 'an', 'the']
   6      ignore_re    = String.new
   7      
   8      # building ignore regexp
   9      ignore_words.each{ |word|
  10        ignore_re << word + '\b|\b'
  11      }
  12      ignore_re = '\b' + ignore_re + '\b'
  13      
  14      # replace apostrophes with separator
  15      permalink = string.gsub("'", separator)
  16      
  17      # delete ignore words
  18      permalink.gsub!(ignore_re, '')
  19  
  20      # all down
  21      permalink.downcase!
  22  
  23      # preserve alphanumerics, everything else becomes a separator
  24      permalink.gsub!(/[^a-z0-9]+/, separator)
  25      
  26      # enforce the maximum component length and return it
  27      permalink = permalink.to(max_size)
  28      
  29      # trim any leading or trailing separators
  30      return permalink.gsub(/^\\#{separator}+|\\#{separator}+$/, '')
  31  
  32  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS