#!/usr/bin/perl use strict; use Getopt::Std; my %opts; $opts{l} = 2; getopts('cpl:',\%opts); my $whole_content; my @tree; my $last_title; my $last_content; my $last_level; while(<>) { $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 $htmlhead =' '; # 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\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"; } $section_content .= "
\n"; $section_content .= "
\n"; push @sections_content,$section_content; my $htmlbutt = "\n"; if ($opts{p}) { for my $i (0..$#sections_content) { $_ = $sections_content[$i]; open(FH,">root-$i.html"); print FH $htmlhead; if ($opts{c}) { print FH "\n"; } else { print FH "\n"; } print FH $_; print FH $toc_string; print FH $htmlbutt; close(FH); } } else { print $htmlhead; print $toc_string; print foreach(@sections_content); print $htmlbutt; } # Other subroutines sub gencontent { my $content = shift; $content =~ s/\[image:\s*(.+)\s*\]//ig; $content =~ s{\[link:\s*(.+)\s*[,\.]\s*((http|ftp)://.+)\s*\]} {\1}ig; $content =~ s{\[((http|ftp)://.+)\|(.+)\]} # Wiki style {\2}ig; $content =~ s{([^\s]+?)\s*\(((http|ftp)://.+)\)\s*} {\1}ig; my @paragraphs = split(/\n\n/,$content); # enum, list