4590276 [rkeene@sledge /home/rkeene/projects/rivet-cgi/rivet-tcl]$ cat -n wrap.tcl
   1: proc wrap {string maxlen {html ""}} {
   2:     set splitstring {}
   3:     foreach line [split $string "\n"] {
   4: 	lappend splitstring [wrapline $line $maxlen $html]
   5:     }
   6:     if {$html == "-html"} {
   7: 	return [join $splitstring "<br>"]
   8:     } else {
   9: 	return [join $splitstring "\n"]
  10:     }
  11: }
  12: 
  13: proc wrapline {line maxlen {html ""}} {
  14:     set string [split $line " "]
  15:     set newline [list [lindex $string 0]]
  16:     foreach word [lrange $string 1 end] {
  17: 	if {[string length $newline]+[string length $word] > $maxlen} {
  18: 	    lappend lines [join $newline " "]
  19: 	    set newline {}
  20: 	}
  21: 	lappend newline $word
  22:     }
  23:     lappend lines [join $newline " "]
  24:     if {$html == "-html"} {
  25: 	return [join $lines <br>]
  26:     } else {
  27: 	return [join $lines "\n"]
  28:     }
  29: }
4590277 [rkeene@sledge /home/rkeene/projects/rivet-cgi/rivet-tcl]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2002-01-09 15:55:03