<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: slug code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 03:36:44 GMT</pubDate>
    <description>DZone Snippets: slug code</description>
    <item>
      <title>Ruby: Escape, Unescape, Encode, Decode, HTML, XML, URI, URL</title>
      <link>http://snippets.dzone.com/posts/show/5180</link>
      <description>This example will show you how to escape and un-escape a value to be included in a URI and within HTML. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'cgi'&lt;br /&gt;&lt;br /&gt;# escape&lt;br /&gt;name = "ruby?"&lt;br /&gt;value = "yes"&lt;br /&gt;url = "http://example.com/?" + CGI.escape(name) + '=' + CGI.escape(value) + "&amp;var=T"&lt;br /&gt;# url: http://example.com/?ruby%3F=yes&amp;var=T&lt;br /&gt;html = %(&lt;a href="#{CGI.escapeHTML(url)}"&gt;example&lt;/a&gt;)&lt;br /&gt;# html: &lt;a href="http://example.com/?ruby%3F=yes&amp;amp;var=T"&gt;example&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;# unescape&lt;br /&gt;name_encoded = html.match(/http:([^"]+)/)[0]&lt;br /&gt;# name_encoded: http://example.com/?ruby%3F=yes&amp;amp;var=T&lt;br /&gt;href = CGI.unescapeHTML(name_encoded)&lt;br /&gt;# href: http://example.com/?ruby%3F=yes&amp;var=T&lt;br /&gt;query = href.match(/\?(.*)$/)[1]&lt;br /&gt;# query: ruby%3F=yes&amp;var=T&lt;br /&gt;pairs = query.split('&amp;')&lt;br /&gt;# pairs: ["ruby%3F=yes", "var=T"]&lt;br /&gt;name, value = pairs[0].split('=').map{|v| CGI.unescape(v)}&lt;br /&gt;# name, value: ["ruby?", "yes"]&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 26 Feb 2008 23:23:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5180</guid>
      <author>sikelianos (Zeke Sikelianos)</author>
    </item>
    <item>
      <title>Nice post slug</title>
      <link>http://snippets.dzone.com/posts/show/2384</link>
      <description>This code replaces accents to normal chars(e.g "&#195;&#161;" =&gt; "a"), everything that isn&#194;&#180;t in "a-zA-Z0-9" to "", multiples spaces to one space, and one space to "-".&lt;br /&gt;&lt;br /&gt;This is useful to make URLs from titles, like Netscape.com does...&lt;br /&gt;&lt;br /&gt;"A new report recommends only work 4 -6 hrs a day" =&gt; "A-new-report-recommends-only-work-4-6-hrs-a-day"&lt;br /&gt;&lt;br /&gt;"Video: \"Popular Mechanics\" editor debunks 9/11 myths" =&gt; "Video-Popular-Mechanics-editor-debunks-911-myths"&lt;br /&gt;&lt;br /&gt;etc..&lt;br /&gt;The code is 95% based on http://textsnippets.com/posts/show/451&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def self.nice_slug(str)&lt;br /&gt;		&lt;br /&gt;		accents = { &lt;br /&gt;		  ['&#195;&#161;','&#195;&#160;','&#195;&#162;','&#195;&#164;','&#195;&#163;'] =&gt; 'a',&lt;br /&gt;		  ['&#195;&#402;','&#195;&#8222;','&#195;&#8218;','&#195;&#8364;','&#195;?'] =&gt; 'A',&lt;br /&gt;		  ['&#195;&#169;','&#195;&#168;','&#195;&#170;','&#195;&#171;'] =&gt; 'e',&lt;br /&gt;		  ['&#195;&#8249;','&#195;&#8240;','&#195;&#710;','&#195;&#352;'] =&gt; 'E',&lt;br /&gt;		  ['&#195;&#173;','&#195;&#172;','&#195;&#174;','&#195;&#175;'] =&gt; 'i',&lt;br /&gt;		  ['&#195;?','&#195;&#381;','&#195;&#338;','&#195;?'] =&gt; 'I',&lt;br /&gt;		  ['&#195;&#179;','&#195;&#178;','&#195;&#180;','&#195;&#182;','&#195;&#181;'] =&gt; 'o',&lt;br /&gt;		  ['&#195;&#8226;','&#195;&#8211;','&#195;&#8221;','&#195;&#8217;','&#195;&#8220;'] =&gt; 'O',&lt;br /&gt;		  ['&#195;&#186;','&#195;&#185;','&#195;&#187;','&#195;&#188;'] =&gt; 'u',&lt;br /&gt;		  ['&#195;&#353;','&#195;&#8250;','&#195;&#8482;','&#195;&#339;'] =&gt; 'U',&lt;br /&gt;		  ['&#195;&#167;'] =&gt; 'c', ['&#195;&#8225;'] =&gt; 'C',&lt;br /&gt;		  ['&#195;&#177;'] =&gt; 'n', ['&#195;&#8216;'] =&gt; 'N'&lt;br /&gt;		  }&lt;br /&gt;		accents.each do |ac,rep|&lt;br /&gt;		  ac.each do |s|&lt;br /&gt;			str = str.gsub(s, rep)&lt;br /&gt;		  end&lt;br /&gt;		end&lt;br /&gt;		str = str.gsub(/[^a-zA-Z0-9 ]/,"")&lt;br /&gt;		&lt;br /&gt;		str = str.gsub(/[ ]+/," ")&lt;br /&gt;		&lt;br /&gt;&lt;br /&gt;		str = str.gsub(/ /,"-")&lt;br /&gt;		&lt;br /&gt;		#str = str.downcase&lt;br /&gt;&lt;br /&gt;	end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 10 Aug 2006 00:09:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2384</guid>
      <author>sligowaths (Tiago Serafim)</author>
    </item>
  </channel>
</rss>
