Welcome, guest | Sign In | My Account | Store | Cart

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, 15 lines
 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

1 comment

Marty Backe 22 years, 7 months ago  # | flag

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]
Created by Jeff Hobbs on Thu, 21 Jun 2001 (MIT)
Tcl recipes (162)
Jeff Hobbs's recipes (16)

Required Modules

  • (none specified)

Other Information and Tasks