User:Jpatokal/listing.rb


 * 1) !/usr/local/bin/ruby
 * 2) listing.rb
 * 3) Converts Manual of Style listings to the tag listing format.
 * 4) By User:Jpatokal in 2006
 * 5) Usage: ./listing.rb
 * 6) Expects the following format:
 * 7) ==Heading==
 * 8) * Name, Address (Directions), tel. Phone, [Url]. Description.
 * 1) Expects the following format:
 * 2) ==Heading==
 * 3) * Name, Address (Directions), tel. Phone, [Url]. Description.

infile = File.new(ARGV[0], "r") linearray = infile.readlines tag = "other" linearray.each{|i| if i =~ /^==[^=]/ tag = i.downcase.chomp.delete "=" end i.gsub!('tel.', 'tel')

# match "*", but don't match "**", "* <" if i =~ /^\* ?[^ <*]/ name = "Unknown" url, address, dir, phone = nil if i.index('. ') head = i[0, i.index('. ')] foot = i[i.index('. ') + 1, i.length].strip else head = i     foot = "" end #puts "HEAD #{head}\nFOOT #{foot}" head.split(/[,*]/).each do |s| s.strip! #puts "S #{s}" case s     # Name when /^'/ name = s.delete "\'"

# [Url] when /^\[/ url = s.delete "[] "

# tel Phone, phone Phone when /^[Tt]el/, /^[Pp]hone/ s.gsub!(/[Tt]el/, "") s.gsub!(/[Ph]one/, "") phone = s.strip

when "" # do nothing

# Address (Directions) else s.gsub!(/\((.*)\)/) {|match| dir = $1 ""       }         address = s.strip end end

listing = "* <#{tag} name=\"#{name}\"" listing += " address=\"#{address}\"" if address listing += " directions=\"#{dir}\"" if dir listing += " phone=\"#{phone}\"" if phone listing += " url=\"#{url}\"" if url listing += ">#{foot}" puts listing else puts i end } infile.close