Created
December 23, 2008 23:04
-
-
Save devyn/39502 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'uri' | |
module CrumbleURL; extend self | |
def crumble_to_url c | |
c =~ /^([A-Za-z0-9_-]+)\((\d+)\) ([A-Za-z0-9_.-]+)/ | |
uri = URI::Generic.build(:scheme => $1, :port => $2, :host => $3.split('.').reverse.join('.')) | |
if c =~ /> ([A-Za-z0-9()_.-^]+)/ | |
uri.path = "/" + $1.split('.').collect{|pt|pt.gsub(/\(\d+\)$/){|o|'/'+o[1..-2]}.gsub("^", ".")}.join("/") | |
end | |
if c =~ /\{(.*)\}$/ | |
uri.query = $1.gsub(": ", "=").gsub(", ", "&") | |
end | |
uri.to_s | |
end | |
alias c2u crumble_to_url | |
def url_to_crumble u | |
uri = URI.parse(u) | |
pth = [] | |
uri.path.split("/").each do |pt| | |
next if pt.empty? | |
if (pt.to_i.to_s == pt) and pth.last | |
pth.last << "(#{pt})" | |
else | |
pth << pt.gsub(".", "^") | |
end | |
end | |
"#{uri.scheme}(#{uri.port}) #{(uri.host.split(".").reverse - ['www']).join(".")}#{" > #{pth.join('.')}" unless pth.empty?}#{" {#{uri.query.gsub("&", ", ").gsub("=", ": ")}}" if uri.query}" | |
end | |
alias u2c url_to_crumble | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment