summaryrefslogtreecommitdiff
path: root/www/faq2html.pl
blob: 3417128ab9ffcf0f024c3f09556beda2f6bdfdc8 (plain)
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
#!/usr/bin/perl

# this is really a faq2html and should only be used for this purpose

sub fixline {
    $_ =~ s/\</&lt;/g;
    $_ =~ s/\>/&gt;/g;

    $_ =~ s/(http:\/\/([a-zA-Z0-9_.\#\/-]*)[^\) .\n])/\<a href=\"$1\"\>$1\<\/a\>/g;

    $_ =~ s/(\\|\/)$/$1&nbsp;/g; # clobber backslash on end of line
}

sub show {
    if(@q) {
        print @q;
        undef @q;
    }
    if(@a) {
        print @a;
        undef @a;
    }
    if(@p) {
        print "<pre>\n";
        print @p;
        print "</pre>\n";
        undef @p;
    }
}

while(<STDIN>) {

    fixline($_);

    # detect and mark Q-sections
    if( $_ =~ /^(Q(\d*)[.:] )(.*)/) {

        show();

        # collect the full Q
        push @q, "<a name=\"$2\"></a><p class=\"faqq\">";
        push @q, "$2. $3";
        my $line;

        $indent = length($1);
        $first = " " x $indent;

        #print "$indent|$first|$1|\n";

        while(<STDIN>) {

            fixline($_);

            $line = $_;

            if($_ !~ /^A/) {
                push @q, "$_";
            }
            else {
                last;
            }
        }
        # first line of A
        $line =~ s/^A(\d*)[.:] *//g; # cut off the "A[num]."
        push @a, "<p class=\"faqa\">";
        push @a, $line;

        $prev='a';
        next;
    }
  #  print "$_ matches '$first'?\n";

    if($_ =~ /^$first(\S)/) {


        if($prev ne 'a') {
            show();
            push @a, "<p class=\"faqa\">";
        }

        push @a, $_;
        $prev='a';
    }
    else {
        if($prev ne 'p') {
            show();
        }
        if(@p) {
            # if we have data, we fix blank lines
            $_ =~ s/^\s*$/\&nbsp;\n/g;   # empty lines are nbsp
            push @p, $_; # add it
        }
        elsif($_ !~ /^\s*$/) {
            # this is not a blank line, add it
            push @p, $_;
        }
        $prev = 'p';
    }
}
show();