, &, |, (, ) を置換

<, >, &, |, (, ) を置換する。

SourceToHtml3.plx


#ヘッダ部
open(F, ".\\Template\\header.txt") || die "open: $!";
while ()
{
print;
}
close(F);

#ソース部
while (<>)
{
while(($pos = index($_, "\t")) >= 0) #TABがあるか
{
$num = 4 - ($pos % 4); #空白何文字分に置き換えればよいか
$_ = substr($_,0,$pos).(' ' x $num).substr($_,$pos+1); #空白に置き換え
}

# <, >, &, |, (, ) を置換
s/&/&#x26;/g; # &
s/</&#x3C;/g; # <
s/>/&#x3E;/g; # >
s/\(/&#x28;/g; # ( はてな
s/\)/&#x29;/g; # ) はてな
s/\|/&#x7C;/g; # | はてな

print;
}

#フッタ部
open(F, ".\\Template\\footer.txt") || die "open: $!";
while ()
{
print;
}
close(F);

実行形式


C:\Perl5>SourceToHtml3.plx input.txt > output.txt