#!/usr/bin/perl
use strict;
use Getopt::Std;
use Encode qw/from_to/;
my %opts;
$opts{l} = 2;
getopts('f:t:cpl:',\%opts);
my @tree;
my $last_title;
my $last_content;
my $last_level;
my @whole_content = <>;
my %LOCALVAR = find_localvar(\@whole_content);
$opts{p} = 1 if $LOCALVAR{"document-multipage-mode"};
my $DEFAULT_CODING_SYSTEM = "big5";
$LOCALVAR{"buffer-file-coding-system"} ||= $DEFAULT_CODING_SYSTEM;
$LOCALVAR{"document-output-coding-system"} ||=
$LOCALVAR{"buffer-file-coding-system"};
unless ($LOCALVAR{"document-output-coding-system"} eq
$LOCALVAR{"buffer-file-coding-system"}) {
foreach (@whole_content) {
from_to($_,
$LOCALVAR{"buffer-file-coding-system"},
$LOCALVAR{"document-output-coding-system"});
}
foreach (keys %LOCALVAR) {
from_to($LOCALVAR{"$_"},
$LOCALVAR{"buffer-file-coding-system"},
$LOCALVAR{"document-output-coding-system"});
}
}
foreach(@whole_content) {
if (m/^(\*+)\s*(.+)$/) {
push @tree,
{ title => $last_title,
content => $last_content,
level => $last_level
};
$last_level = length($1);
$last_title = $2;
$last_content = '';
} else {
$last_content .= $_;
}
}
push @tree,
{ title => $last_title,
content => $last_content,
level => $last_level
};
shift @tree; # the first one is empty.
my $doc_title = $opts{t} || $LOCALVAR{"document-title"} || $ARGV[0];
my $htmlcharset = $LOCALVAR{"document-output-coding-system"};
my $htmlhead =qq{
$doc_title
};
# Generate Index.
my ($title,$before,$after,$close_last,$toc_string);
$last_level = 1;
my $root_i = -1;
$toc_string = "\n
Table of Contents \n
\n";
for my $i (0..$#tree) {
$_ = @tree[$i];
next if ($_->{level} > $opts{l});
if ($_->{level} == 1) {
$root_i++;
if ($opts{p}) {
$title = "$_->{title} ";
} else {
$title = "$_->{title} ";
}
} else {
if ($opts{p}) {
$title = "$_->{title} ";
# $title = "$_->{title} ";
} else {
$title = "$_->{title} ";
}
}
my $liclass = "class='lilevel" . $_->{level} . "'";
my $ulclass = "class='ullevel" . $_->{level} . "'";
my $ulid = "id='ulrootsection${root_i}'" if($_->{level} == 2);
$before = ""; $after = " "; $close_last = '';
if ($_->{level} > $last_level) {
$before = "\n\n";
} elsif ($_->{level} < $last_level) {
$close_last = " " x ($last_level - $_->{level}) . "\n\n";
$before = "";
}
my $this_title = "${close_last}${before}${title}${after}";
$toc_string .= "${this_title}\n";
$last_level = $_->{level};
}
$toc_string .= " \n\n";
# Generate a div foreach section
my @sections_content;
my $section_content;
$section_content = "\n";
for my $i (0..$#tree) {
$_ = @tree[$i];
if ($_->{level} == 1) {
if ($i > 0) {
$section_content .= "
\n";
$section_content .= " \n" if $opts{p};
push @sections_content,$section_content;
$section_content = '';
$section_content = "\n" if $opts{p}
}
$section_content .=
"
\n";
} else {
$section_content .= "
\n";
}
$section_content .= "{level}>$_->{title} {level}>";
$section_content .= gencontent($_->{content}) . "\n"
if ($_->{content} =~ /[^\s]/);
$section_content .= "
\n" unless($_->{level} == 1);
}
$section_content .= "
\n";
$section_content .= "
\n";
push @sections_content,$section_content;
my $footer = $opts{f} ||
$LOCALVAR{"document-footer"} ||
'This page is generated by outline2html';
my $htmlbutt;
if ($footer) {
$htmlbutt = qq{
\n};
} else {
$htmlbutt = qq{