#!/usr/bin/env ruby
#
# create cities.inc from http://www.indymedia.org/cities.xml
#
#   Copyright (c) 2005-2006  Dmitry Borodaenko <angdraug@debian.org>
#
#   This program is free software.
#   You can distribute/modify this program under the terms of
#   the GNU General Public License version 2 or later.
#
# vim: et sw=4 sts=4 ts=8 tw=0

require 'open-uri'
require 'rexml/document'

# borrowed from CGI
def escape_html(string)
  string.gsub(/&/n, '&amp;').gsub(/\"/n, '&quot;').gsub(/\'/n, '&apos;').gsub(/>/n, '&gt;').gsub(/</n, '&lt;')
end

# recursive unwrapping of cities.xml
def print_key(key)
    case key.next_element.name
    when 'array'
        text = key.text.strip
        if '' != text and 'NULL' != text then
            print %{<strong>#{text}:</strong>\n}
        end
        key.next_element.each_element('dict/key') {|e| print_key e }
    when 'string'
        print %{<a href="#{key.next_element.text.strip}">#{escape_html(key.text.strip)}</a>\n}
    end
end

cities = open('http://www.indymedia.org/cities.xml') {|f| f.read }
root = REXML::Document.new(cities).root
root.each_element('/plist/dict/key') {|e| print_key e }
