#!/usr/bin/env perl # $Header # Copyright 2001 Richard P. Curnow # LICENCE # A script to generate an HTML FAQ page from a text input file. The input is assumed to consist of the following: # Lines starting with 'S:'. These introduce sections. # Lines starting with 'Q:'. These are the topics of questions. # Body text (either as an introduction to the sections, or as answers to the questions. # The body text is set as pre-formatted. $| = 1; @prologue = (); @epilogue = (); @sections=(); # section titles @sect_text=(); # introductory text in sections @questions=(); # questions in sections @answers=(); # answers to questions $sn = -1; $had_q = 0; #{{{ Parse input while (<>) { if (m/\@\@PROLOG/o) { while (<>) { last if (m/^\@\@ENDPROLOG/); push (@prologue, $_); } } elsif (m/\@\@EPILOG/o) { while (<>) { last if (m/^\@\@ENDEPILOG/); push (@epilogue, $_); } } elsif (m/^[sS]:[ \t]*(.*)$/) { chomp; $qn = -1; ++$sn; $sections[$sn] = &guard($1); $sect_text[$sn] = ""; $questions[$sn] = [ ]; $answers[$sn] = [ ]; $had_q = 0; } elsif (/^[qQ]:[ \t]*(.*)$/) { chomp; die unless ($sn >= 0); ++$qn; $questions[$sn]->[$qn] = &guard($1); $had_q = 1; } else { if ($had_q) { if ($qn >= 0) { $answers[$sn]->[$qn] .= $_; } } else { if ($sect_text[$sn] ne "" || $_ !~ /^\s*$/) { $sect_text[$sn] .= $_; } } } } #}}} # Emit file header if ($#prologue >= 0) { print @prologue; } else { print < Chrony Frequently Asked Questions Table of contents EOF } # Emit table of contents print "\n"; # Emit main sections for $sn (0 .. $#sections) { print "
\n"; print "\n"; #print "".($sn+1).". ".$sections[$sn]."\n"; print "\n"; if ($sect_text[$sn] ne "") { print "
\n";
        print $sect_text[$sn];
        print "
\n"; } for $qn (0 .. $#{$questions[$sn]}) { $sq = ($sn+1).".".($qn+1); print "

\n"; print "\n"; print "".$sq.". ".$questions[$sn]->[$qn]."\n"; print "

\n";
        print $answers[$sn]->[$qn];
        print "
\n"; } } # Print footer if ($#epilogue >= 0) { print @epilogue; } else { print < EOF } #{{{ sub guard { sub guard { # Hide wierd tags etc my ($x) = @_; return $x; } #}}}