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

http://live.julik.nl

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

Absolutize URLs in a string

Courtesy of Mauricio Fernandez and chris2

   1  
   2  require 'cgi'
   3  require 'uri'
   4  
   5  def absolutize_hyperlinks(htmlcontent, root, proto, host)
   6    htmlcontent.gsub(/(<(img|a)\b[^>]*(src|href)=)(["'])(.*?)\4/) do
   7      md = $~
   8      prefix = proto + '://' + host
   9      begin
  10        url = URI.parse CGI.unescapeHTML(md[5])
  11      rescue URI::InvalidURIError
  12        next
  13      end
  14      
  15      if url.relative? && url.path !~ /^\//
  16        md[1] + md[4] + prefix + root + File.expand_path("/" + md[5] + md[4])
  17      else
  18        md[1] + md[4] + prefix + md[5] + md[4]
  19      end
  20    end
  21  end
  22  
  23  str = "Here is <a href='/some_link'>link</a> and <a href='../things'>another</a>"
  24  puts absolutize_hyperlinks(str, "/stuff/bla/badaboum", 'http', 'site.net')
  25  

Create virtual host entries in your NetInfo hosts database

This will generate entries for every folder in your /Sites folder so that you can address your local machine as client1.box, client2.box etc. "box' in this case is the name you give the machine in the "Sharing" prefpane. Very convenient for rapid multisite work.

ATTN: this is OS X only but you can refactor it to work on *X.

   1  
   2  #!/usr/bin/env ruby
   3  SitesDir = '/Sites'
   4  DomainSuffix = `hostname`.gsub('.local', '').downcase.gsub(/\s/, '')
   5  
   6  ###############################################
   7  `nidump hosts .`.split("\n").uniq.sort.each do | dir |
   8    next unless (dir.split('.').pop == DomainSuffix)
   9    host = dir.split("\t").pop
  10    cmd = `niutil -destroy . /machines/#{host}`
  11  end
  12  
  13  Dir.entries(SitesDir).each do | entry |
  14    next if (!File.stat("#{SitesDir}/#{entry}").directory? || entry == '.' || entry == '..')
  15    puts "> Adding website #{entry}.#{DomainSuffix}"
  16    `niutil -create . /machines/#{entry}.#{DomainSuffix}`
  17    `niutil -createprop . /machines/#{entry}.#{DomainSuffix} ip_address 127.0.0.1`
  18  end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS