rdbundle.rb

#
# rdbundle.rb
# usage: ruby rdbundle.rb ruby-script
# 
# 複数ファイルをRDとそれ以外に分けるフィルタ
# 

require 'optparse'
require 'tempfile'

option = {}
op = OptionParser.new
op.on("-r", "--rd-only", "output only bundled RD part"){ option[:rd_only] = true }


ARGV.empty? and raise "no file direction"

begin
  temp = Tempfile.open("_rdbundle")
  
  op.parse!(ARGV)
  
  puts '=begin'
  ARGF.each do |line|
    if /\A=begin\s/ === line
      while line = ARGF.gets
        break if /\A=end\s/ === line
        print line
      end
    else
      temp.print line unless option[:rd_only]
    end
  end
  puts '=end'
  
  unless option[:rd_only]
    temp.rewind
    temp.each{|line| print line}
  end
ensure
  temp.close if temp
end

pathutils.rb

require 'pathname'
require 'find'

=begin
= PathUtils
=end
module PathUtils
=begin
--- PathUtils.find(dir[, ...]){|file| ... }
  find(1)のようにdir配下のすべてのファイルやディレクトリを
  Pathnameオブジェクトで一つずつ引数fileに渡してブロックを実行します。
  fileに渡される順序は不定です。
=end
  def find(*args)
    Find.find(*args) do |path|
      yield Pathname.new(path)
    end
  end
  module_function :find
end


=begin
= Pathname additions
=end
class Pathname
=begin
--- Pathname#nest_level(base_directory=nil)
  base_directoryから何階層下を指すパスなのかをFixnumで返す。
  指定しないとルートディレクトリから数える。
=end
  def nest_level(base=nil)
    lv = 0
    path = base ? relative_path_from(base) : self
    path.each_filename{|x| lv += 1}
    lv -= 1 unless File.directory?(to_str)
    lv
  end
  
=begin
--- Pathname#root_change(root_from, root_to)
  root_fromディレクトリ以下にあるパスを、root_to以下にあるとした
  パスにして返す。
=end
  def root_change(root1, root2)
    root2 + relative_path_from(root1)
  end
  
=begin
--- Pathname#ext_change(new_ext)
  拡張子をnew_extに付け替えたPathnameオブジェクトを返す。
=end
  def ext_change(ext)
    extension = ext.dup
    extension.slice!(/\A\.*/)
    name = to_s
    name.slice!(/#{extname}\Z/)
    Pathname.new("#{name}.#{extension}")
  end
  
=begin
--- Pathname#ext_remove
  拡張子を取り払ったPathnameオブジェクトを返す。
=end
  def ext_remove
    temp = ext_change("").to_s
    temp.slice!(/\.\Z/)
    Pathname.new(temp)
  end
  
=begin
--- Pathname#ext?(other_ext)
  自らの拡張子がother_extに等しい場合真を返す。
=end
  def ext?(other_ext)
    ext1 = extname
    ext1.slice!(/\A\./)
    ext2 = other_ext.dup
    ext2.slice!(/\A\./)
    ext1 == ext2
  end
  
=begin
--- Pathname#slice(regexp)
  自らがregexpにマッチした部分((*以外*))からなるPathnameオブジェクトを返す。
  String#slice(regxep)とは違う。
=end
  def slice(regexp)
    temp = to_s
    temp.slice!(regexp)
    Pathname.new(temp)
  end
end

configpool.rb

class ConfigPool
  class ConfigError < RuntimeError ; end
  
  def initialize
    @readonly_list = []
  end
  
  def self.load(path)
    conf = self.new
    File.open(path) do |f|
      conf.instance_eval(f.read)
    end
    conf
  end
  
  def [](name)
    instance_variable_get("@#{name}") or
      raise ConfigError, "unset value `#{name}'"
  end
  
  def []=(name, val)
    @readonly_list.include?(name.to_sym) and
      raise ConfigError, "read only value `#{name}'"
    instance_variable_set("@#{name}", val)
  end

private
  #以下、設定記述DSL用
  def readonly(name)
    @readonly_list.push name.to_sym
  end
  alias ro readonly
end


if __FILE__ == $0
  conf = ConfigPool.load("config.txt")
  p conf[:bin_dir]
  conf[:bin_dir] = "aaa"
  p conf[:bin_dir]
  conf[:home] = "bbb"
end

__END__
#(config.txt)
@home     = "D:/local_hp/"
@rd_dir   = "#{@home}rd/"
@html_dir = "#{@home}html/"
@bin_dir  = "#{@home}bin/"
@css_dir  = "#{@html_dir}css/"
@include  = "#{@home}include"

@html_ext = "html"
@encording = "sjis"      #sjis|euc|jis
@base_css = "rdstyle.css"  #@css_dir内にあるとされる

@log_level = "INFO"    #FATAL|ERROR|WARN|INFO|DEBUG