From: Phil Mitchell Date: 2001-10-01T11:28:27+09:00 Subject: [ruby-talk:21849] FAQ indexer -- user this version! Whoops! A typo in the first post... # 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