Version 1 of A little Slashdot comment parser

Updated 2003-07-08 12:55:00

Ro starts this page on July 8th, 2003

This parses Slashdot [L1 ] comments. I reads in the source of a comment page that is of type nested.

  #!/usr/bin/tclsh

  proc @ij {c_name i_j} {foreach {i j} $i_j break ; upvar $c_name c ; return [string range $c $i $j]}
  proc =@ {c arr_name} {
    upvar $arr_name v ; set n 0
    while {[regexp -indices -start $n {<TR><TD BGCOLOR="#CCCCCC">\s+?<FONT SIZE="3" COLOR="#000000">\s+<A NAME="(\d+)"><B>(.+?)</B></A> \(Score\:(.+?)\)\s+</FONT>\s+

<BR>(.+?)\s+</TD></TR>\s+<TR><TD>\s+(.+?)\s+</TD></TR>\s+<TR><TD>\s+<FONT SIZE="2">.+?[(.+?)\]} $c MATCH i_cid i_title i_rating i_header i_body i_trailer]} {

      #puts -nonewline . ; flush stdout
      foreach var [info vars] {if {[regexp {i_(.+)} $var -> rvar]} {set $rvar [@ij c [set $var]]}}
      lappend v(comments) $cid ; set v($cid,title) $title ; set v($cid,body) $body
      if {![regexp {(.+)\, (.+)} $rating -> v($cid,mod_num) v($cid,mod_type)]} {set v($cid,mod_num) $rating ; set v($cid,mod_type) no}
      if {![regexp {(\d+)">Parent</A>} $trailer -> v($cid,parent)]} {set v($cid,parent) no}
      if {![regexp {by <A HREF="(.+?)">(.+?) \((\d+)\)</A> } [lindex [split $header \n] 0] -> v($cid,u_slash_home) v($cid,u_name) v($cid,u_id)]} \
        {foreach el {u_slash_home u_name u_id} {set v($cid,$el) no}}
      regexp {on (.+?) \(} [lindex [split $header \n] 1] -> v($cid,time)
      set n [expr {[lindex $MATCH 1] + 1}]
      foreach el [lsort [array names v $cid,*]] {puts [format {%20s  %s} $el $v($el)]} ; puts --------------------- ; flush stdout
    }
  }

  set f [open nested_comments.html] ; set c [read $f] ; close $f ; unset f
  =@ $c v
  puts done!

Enjoy!