TXT2HTML变换

戻る

把一个TXT文件转化HTML文件

# Change TXT file to HTML file # txt2html.pl use strict; my(%t,$n,$n1,@fld); print "Input file name(*.txt)="; chop($t{input}=<STDIN>); print "Link file(*.html)="; chop($t{link1}=<STDIN>); $t{link1} = $t{link1} . '.html'; $t{file1} = $t{input} . '.txt'; $t{file2} = $t{input} . '.htm'; @{ $t{list} } = (); open(FH,$t{file1}) or die "Can't open the file $t{file1}\n"; while(<FH>){ @fld = split; if ( /^TITLE/ ) { $t{title} = $fld[1]; for $n1 ( 2 .. $#fld ) { $t{title} .= ' ' . $fld[$n1]; } } elsif ( /^LINK/ ) { $t{link} = $fld[1]; } else { push( @{ $t{list} }, $_); } } close(FH); open(OUT,">$t{file2}"); print OUT '<HTML><HEAD>',"\n"; print OUT '<META http-equiv=Content-Type content="text/html;charset=UTF-8">',"\n"; print OUT '<TITLE>',$t{title},"\n"; print OUT '</TITLE>',"\n"; print OUT '<style type="text/css">',"\n"; print OUT 'body { background:lightcyan; color:black; margin-left:2em;margin-right:2em;}',"\n"; print OUT '</style>',"\n"; print OUT '</HEAD>',"\n"; print OUT '<BODY>',"\n"; print OUT '<h3>',$t{title},'</h3>',"\n"; print OUT '<a href="' . $t{link1} . '">',$t{link},'</a>',"\n"; print OUT '<hr color="#003366">',"\n"; for $n1 ( 0 .. $#{ $t{list} } ) { print OUT '<p>',$t{list}[$n1]; } print OUT '</p>',"\n"; print OUT '<hr color="#003366">',"\n"; print OUT '<a href="' . $t{link1} . '">',$t{link},'</a>',"\n"; print OUT '</BODY>',"\n"; print OUT '</HTML>',"\n"; close(OUT); __END__;

选择目录文件后再变换

# txt2html.pl use strict; my(%t,$n,$n1,@fld); print "Please input kind(1-7)="; chop($t{kind} = <STDIN>); if ( $t{kind} == 1 ) { $t{rfile} = 'c_laws.htm'; } else { $t{rfile} = 'c_laws' . $t{kind} . '.htm'; } print "Please input filename(*.txt)="; chop($t{name} = <STDIN>); $t{file1} = $t{name} . '.txt'; $t{file2} = $t{name} . '.htm'; open(FH,$t{file1}) or die "Can't open the file $t{file1}\n"; while(<FH>){ @fld = split; if ( /^TITLE/ ) { $t{title} = $fld[1]; } elsif ( /^LINK/ ) { $t{link} = $fld[1]; } else { push( @{ $t{list} }, $_); } } close(FH); open(OUT,">../$t{kind}/$t{file2}"); print OUT '<HTML><HEAD>',"\n"; print OUT '<META http-equiv=Content-Type content="text/html;charset=UTF-8">',"\n"; print OUT '<TITLE>',$t{title},"\n"; print OUT '</TITLE>',"\n"; print OUT '<style type="text/css">',"\n"; print OUT 'body { background:#fcf5ca; color:black; margin-left:2em;margin-right:2em;}',"\n"; print OUT '</style>',"\n"; print OUT '</HEAD>',"\n"; print OUT '<BODY>',"\n"; print OUT '<h3>',$t{title},'</h3>',"\n"; print OUT '<a href="../',$t{rfile}; print OUT '">',$t{link},'</a>',"\n"; print OUT '<hr>',"\n"; for $n1 ( 0 .. $#{ $t{list} } ) { print OUT '<p>',$t{list}[$n1]; } print OUT '</p>',"\n"; print OUT '<hr>',"\n"; print OUT '<a href="../',$t{rfile}; print OUT '">',$t{link},'</a>',"\n"; print OUT '</BODY>',"\n"; print OUT '</HTML>',"\n"; close(OUT); __END__;
戻る