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-3 of 3 total  RSS 

Absolutize URLs in a string

Courtesy of Mauricio Fernandez and chris2

require 'cgi'
require 'uri'

def absolutize_hyperlinks(htmlcontent, root, proto, host)
  htmlcontent.gsub(/(<(img|a)\b[^>]*(src|href)=)(["'])(.*?)\4/) do
    md = $~
    prefix = proto + '://' + host
    begin
      url = URI.parse CGI.unescapeHTML(md[5])
    rescue URI::InvalidURIError
      next
    end
    
    if url.relative? && url.path !~ /^\//
      md[1] + md[4] + prefix + root + File.expand_path("/" + md[5] + md[4])
    else
      md[1] + md[4] + prefix + md[5] + md[4]
    end
  end
end

str = "Here is <a href='/some_link'>link</a> and <a href='../things'>another</a>"
puts absolutize_hyperlinks(str, "/stuff/bla/badaboum", 'http', 'site.net')

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.

#!/usr/bin/env ruby
SitesDir = '/Sites'
DomainSuffix = `hostname`.gsub('.local', '').downcase.gsub(/\s/, '')

###############################################
`nidump hosts .`.split("\n").uniq.sort.each do | dir |
  next unless (dir.split('.').pop == DomainSuffix)
  host = dir.split("\t").pop
  cmd = `niutil -destroy . /machines/#{host}`
end

Dir.entries(SitesDir).each do | entry |
  next if (!File.stat("#{SitesDir}/#{entry}").directory? || entry == '.' || entry == '..')
  puts "> Adding website #{entry}.#{DomainSuffix}"
  `niutil -create . /machines/#{entry}.#{DomainSuffix}`
  `niutil -createprop . /machines/#{entry}.#{DomainSuffix} ip_address 127.0.0.1`
end

Add a watermark to a multi-page PDF (a-la Pragmatic Programmers)

This adds a text watermark to every page of a PDF document, as used by Pragmatic Programmers. Nothing secret here, more of a way to excercise "human" DRM. Used as:

   perl makepdf.pl "John Doe"


use PDF::Reuse;
use PDF::Reuse::Util;
use strict;

my ($name) = @ARGV;

prFile('book_for_someone.pdf');

my $sourcePdf = 'pdf_that_your_designer_made.pdf';
my $greeting = "This book is personalized for " . $name;

my $left = 1;
while ($left) {
   prFont('HO');
   prAdd("0 0 0 rg\n0 g\nf\n");
   prText( 38, 800, $greeting);
   $left = prSinglePage($sourcePdf);   
} 

prEnd;
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS