From: Phil Mitchell Date: 2001-10-01T11:28:24+09:00 Subject: [ruby-talk:21846] FAQ indexer offerred for ruby faq As a newbie project, I wrote a faq indexer, to add a section of links at the front of the ruby faq. I think it is nice to have an overview of all the faqs in the faq. Feed it your rubyfaqall.html file and it will add a links section to it. # Takes a faq that is organized into numbered headings and creates a set of links at # the top of the file. It expects headings to be numbered something like: 1., 1.1, 1.2, etc. # Expects filename on the command line. # It assumes headings are embedded in

tags. # It will strip off existing tags. # Edits existing file in place. file = open(ARGV[0], "r") fileStr = file.read file.close hash = {} # FIND HEADINGS, HASH THEM BY MAJOR NUMBER, AND ADD BACK fileStr.gsub!(/

(.*)<\/h2>/i) do |h| heading = $1 heading.sub!(/]*>/i, "") heading.sub!(/<\/A>/i, "") if (heading =~ /(\d+)\.(\d*)/) majorNum = $1.to_i minorNum = ($2 == "") ? 0 : $2.to_i hash.has_key?(majorNum) ? hash[majorNum].push( [minorNum, heading] ) : hash[majorNum] = [ [minorNum, heading] ] "

" + heading + "

" end end # CREATE LINK SECTION lines = [] hash.sort.each do |a| majNumber, array = a[0], a[1] for subArr in array minNumber, heading = subArr[0], subArr[1] minNumber = (minNumber == 0) ? "" : minNumber lines.push("" + heading + "
") end lines.push("

") end fileStr.sub!(/

/i, lines.to_s + "

") file.open(ARGV[0], "w") File.truncate(ARGV[0], 0) file.print(fileStr) file.close