File: faqgen.pl

package info (click to toggle)
chrony 1.21z-5
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 2,080 kB
  • ctags: 2,375
  • sloc: ansic: 14,831; yacc: 858; sh: 588; perl: 426; makefile: 148
file content (140 lines) | stat: -rw-r--r-- 3,293 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/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 <<EOF;
<html>
<head>
<title>
Chrony Frequently Asked Questions
</title>
</head>
<body>
<font face=\"arial,helvetica\" size=+4><b>Table of contents</b></font>
EOF
}

# Emit table of contents
print "<ul>\n";
for $sn (0 .. $#sections) {
    print "<b><li> <a href=\"#section_".($sn+1)."\">".($sn+1).".</a> ".$sections[$sn]."</b>\n";
    print "  <ul>\n";
    for $qn (0 .. $#{$questions[$sn]}) {
        $sq = ($sn+1).".".($qn+1);
        print "  <li> <a href=\"#question_".$sq."\">".$sq.".</a> ".$questions[$sn]->[$qn]."\n";
        #print "  <li> ".$sq.". ".$questions[$sn]->[$qn]."\n";
    }
    print "  </ul>\n";
}
print "</ul>\n";

# Emit main sections
for $sn (0 .. $#sections) {
    print "<hr>\n";
    print "<a name=section_".($sn+1).">\n";
    #print "<b><font size=+2 face=\"arial,helvetica\">".($sn+1).". ".$sections[$sn]."</font></b>\n";
    print "<?php pretty_h2(\"".($sn+1).". ".$sections[$sn]."\"); ?>\n";
    if ($sect_text[$sn] ne "") {
        print "<pre>\n";
        print $sect_text[$sn];
        print "</pre>\n";
    }
    for $qn (0 .. $#{$questions[$sn]}) {
        $sq = ($sn+1).".".($qn+1);
        print "<p>\n";
        print "<a name=question_".$sq.">\n";
        print "<font size=+1 face=\"arial,helvetica\">".$sq.". ".$questions[$sn]->[$qn]."</font>\n";
        print "<pre>\n";
        print $answers[$sn]->[$qn];
        print "</pre>\n";
    }
}

# Print footer
if ($#epilogue >= 0) {
    print @epilogue;
} else {
print <<EOF;
</body>
</html>
EOF
}


#{{{  sub guard {
sub guard {
# Hide wierd tags etc
    my ($x) = @_;
    return $x;
}
#}}}