Help Wikitravel grow by contributing to an article! Learn how.
New users, please see Help or go to the Pub to ask questions.

User:Jpatokal/listing.rb

From Wikitravel
Jump to: navigation, search
#!/usr/local/bin/ruby
#
# listing.rb
# Converts Manual of Style listings to the tag listing format.
# By User:Jpatokal in 2006
#
# Usage: ./listing.rb <inputfile>
#
# Expects the following format:
# ==Heading==
# * '''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}</#{tag}>"
    puts listing
  else
    puts i
  end
}
infile.close

Variants

Actions

Destination Docents

In other languages