#! /usr/bin/env ruby #= add_prefix_href.rb #== Overview OVERVIEW = <<-"EOF" 標準入力または引数に与えられた HTML ファイル内の など の href 属性の値にプレフィックスを追加し, 標準出力に返す. 値が "#" や "/" や "http://" や "ftp://" で始まるものにはプレフィックスを追 加しない. 本来 HTML の base タグによって行うことだが, この方法だとアクセス先 のサーバを一意に指定せねばならず, 複数のサーバ上で分散して資源を公 開し, ある HTML から, 個々の別のサーバ上のリソースにアクセスする場 合には向かない. EOF #== USAGE USAGE = " add_prefix_href.rb --prefix PREFIX 標準入力または引数のファイル内の href 属性の値の先頭に PREFIX で指定された文字列を付加し, 標準出力へ返します." #== VERSION VERSION_addprefhref = "0.1" UPDATE = "2006-02-08" #== LICENCE # #This library is distributed under the terms of the Ruby license. #You can freely distribute/modify this library. # #==COPYRIGHT # COPYRIGHT = "Copyright (C) 2006 morikawa@ep.sci.hokudai.ac.jp" require "optparse" opt = OptionParser.new OPTS = {} ARGV.options{|opt| opt.summary_width = 23 opt.summary_indent = ' '*6 opt.on( '-p=VAL', '--prefix=VAL', "指定するプレフィックスです" ){|v| OPTS[:prefix] = v} opt.parse! } n = nil if !OPTS[:prefix] then print <<-"EOF" =========================================================================== add_prefix_href.rb: OVERVIEW: #{OVERVIEW} USAGE: #{USAGE} OPTION: \n#{opt.to_a[1..-1].join("")} VERSION: Version: #{VERSION_addprefhref}, Last Update: #{UPDATE} #{COPYRIGHT} =========================================================================== EOF exit end puts readlines.collect { |line| next line unless line =~ /href=\"(.+?)\"/ url = $1 if url =~ /^(http:\/\/|ftp:\/\/|\#|\/)/ then next line end prefix = OPTS[:prefix].chomp.strip prefix.gsub!(/\"/, "") prefix = prefix + "/" unless prefix =~ /\/$/ new = line.gsub(/href=\"(.+?)\"/, "href=\"#{prefix}\\1\"") next new }.compact