uttsu.com > Perl >

pl2html

Perl言語のソースファイルをHTMLファイルに変換するスクリプトです。

使い方

pl2html.pl infile > outfile

pl2html.pl

#!/usr/local/bin/perl

# pl2html.pl - Perl言語のソースファイルをHTMLファイルに変換します。
# ver 1.00
# Copyright (C) Takashi Utsunomiya. All Rights Reserved.
# http://uttsu.com/

# Description:
# ・Perl言語のソースファイルを読み込み、HTMLファイルを出力します。
# ・Perl言語のコメント分を色分け表示にします(プログラム中のコメントでない#文字には未対応)。
# ・HTMLファイルのヘッダーとフッターを指定できます。
#   ・ヘッダー中で$titleはスクリプトファイル名、$dateは実行時の日付け(yyyy.mm.dd)に置き換わる。

# Usage: pl2html.pl infile > outfile

# History:
# ver 1.00  2002.11.20

use strict;

# HTMLファイルのヘッダー
my $head = <<'END';
<title>$title</title>
<link rel=stylesheet href=../style.css>
<style>
<!--
pre { padding: 50px 40px; border: 1px solid; background: white; }
.comment { color: green; }
-->
</style>

<a href=../><b>uttsu.com</b></a> >
<h1>$title</h1>
END

# HTMLファイルのフッター
my $foot =<<'END';
<p><hr>
Copyright (C) Takashi Utsunomiya. All Rights Reserved.<br>
$date掲載
END

# TABサイズ
my $tabstop = 4;

my $target = ($ARGV[0] || show_usage());

main();
exit;

# ◆
sub main {
    my @body;
    my $date = get_date();

    open(IN, $target) or die;
    foreach (<IN>) {
        chomp;
        s/&/&/g;
        s/</</g;
        s/(.*?)\t/$1.(' ' x ((length($1) % $tabstop) || $tabstop))/eg;
        /^(.*?)(#.*)?$/;
        push(@body, $1 . ($2? "<span class=comment>$2</span>" : '') . "\n");
    }
    close(IN);

    $_ = $target;
    s/.*\\//;
    $head =~ s/\$title/$_/g;
    $head =~ s/\$date/$date/g;
    $foot =~ s/\$title/$_/g;
    $foot =~ s/\$date/$date/g;
    print $head, "\n<pre>\n", @body, "</pre>\n\n", $foot;
}

# ◆
sub show_usage {
    print STDERR "Usage: pl2html.pl infile > outfile\n";
    exit(1);
}

# ◆
sub get_date {
    my ($sec, $min, $hour, $day, $month, $year) = localtime(time);
    return sprintf("%4d.%02d.%02d", $year+1900, $month+1, $day);
}


Copyright (C) Takashi Utsunomiya. All Rights Reserved.
2002.11.20掲載