ActiveState Code

Recipe 65428: Text to HTML


Scan the files in the current directory and wrap them into html/body tags so that netscape will display them as HTML and not as text.

Tcl
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/usr/local/bin/tclsh
# -*- tcl -*-
# Scan the files in the current directory and wrap them into html/body
# tags so that netscape will display them as HTML and not as text.

foreach f [glob *] {
    if {[file extension $f] == ".html"} {continue}
    if {[file isdirectory $f]} {continue}
    set data [read [set fh [open $f r]]]
    close $fh
    set    fh [open html/$f.html w]
    puts  $fh "<html><body>\n$data</body></html>"
    close $fh
}
exit

Comments

  1. 1. At 9:39 a.m. on 13 aug 2001, Marty Backe said:

    Use HTML character entities. Wrapping arbitrary text files such that they can be displayed as html requires all '<', '>', '&', and '"' characters be converted to their HTML character entities: &lt; &gt; &amp; &quot;

    This program accomplishes the conversion.

    Usage: txt2html foo.bar > output.html

    #!/usr/local/bin/tclsh
    
    proc txt2html { file } {
    
        set fileH [open $file r]
    
        while { 1 } {
            set inputLine [gets $fileH]
            if {[eof $fileH]} {
                break
            }
            set outputLine [string map {&lt; &amp;lt; &gt; &amp;gt; &amp; &amp;amp; \&quot; &amp;quot;} $inputLine]
            puts $outputLine
        }
    }
    
    txt2html [lindex $argv 0]
    

Sign in to comment