class WulffeldSlug::PrepareString
Constants
- VALID_WORD_REGEX
Attributes
options[RW]
slug[RW]
words[RW]
Public Class Methods
new(words, options = {})
click to toggle source
# File lib/wulffeld_slug/prepare_string.rb, line 9 def initialize(words, options = {}) # Original words. Will not be touched. @words = [*words] # Converted words. @slug_words = [] @options = options @options[:max] ||= 239 @options[:case] ||= :downcase @options[:kinds] ||= [:latin, :danish, :norwegian, :german, :swedish, :spanish, :russian, :bulgarian, :cyrillic, :greek, :macedonian, :romanian, :serbian, :ukrainian] @slug = '' prepare_string end
Public Instance Methods
prepare_string()
click to toggle source
Use dashes to join. www.mattcutts.com/blog/dashes-vs-underscores/
# File lib/wulffeld_slug/prepare_string.rb, line 28 def prepare_string prepare_words prepare_case combine_words end
prepare_word(word)
click to toggle source
# File lib/wulffeld_slug/prepare_string.rb, line 34 def prepare_word(word) word = word.dup # Blow away apostrophes word.gsub! /['`ยด]/, '' # @ --> at, and & --> and word.gsub! /\s*@\s*/, " at " word.gsub! /\s*&\s*/, " and " # First see if transliteration produces something worthwhile. options[:kinds].each do |kind| w = Babosa::Identifier.new(word).transliterate!(kind) if w =~ VALID_WORD_REGEX && word.length - w.length < 10 word = w break end end # More brutality needed. if word !~ VALID_WORD_REGEX potential = [] options[:kinds].each do |kind| potential << Babosa::Identifier.new(word).transliterate!(kind).gsub(/[^\sa-zA-Z0-9]+/i, '-').split('-').map(&:strip).reject(&:blank?).join('-') end potential.sort {|a,b| b.length <=> a.length }.each do |w| if w =~ VALID_WORD_REGEX word = w break end end end word.strip! # Convert spaces to dashes. word.gsub!(/[\s]+/i, '-') # Remove non-alpha/num. word.gsub!(/[^a-zA-Z0-9-]+/i, '') # Remove dashes at start and end of word. word.gsub!(/^\-+/, '') word.gsub!(/\-+$/, '') @slug_words << word unless word.blank? end
Protected Instance Methods
combine_words()
click to toggle source
# File lib/wulffeld_slug/prepare_string.rb, line 84 def combine_words @slug = @slug_words.join('-')[0..@options[:max]] end
prepare_case()
click to toggle source
# File lib/wulffeld_slug/prepare_string.rb, line 92 def prepare_case case options[:case] when :downcase @slug_words.map!(&:downcase) when :capitalize @slug_words.map!(&:capitalize) when :upcase @slug_words.map!(&:upcase) when :preserve end end
prepare_words()
click to toggle source
# File lib/wulffeld_slug/prepare_string.rb, line 88 def prepare_words @words.each { |s| prepare_word(s) } end