#!/usr/bin/env perl use warnings; use strict; # Intended to read FAQ.txt (input file in the database format) and # output its content as an html file, including internal and external links. my $infile = shift || "../dat/database/FAQ.txt"; my $outfile = shift || "../../docs/FAQ.html"; open (FILE, $infile) or die "Cannot read file '$infile'.\n"; my @keys; my %questions; my %answers; my $key = ""; my $current = ""; my $text = ""; my $list = 0; my $bullet = 0; my $listct = 0; my $title = "no T:html key!"; while (my $line = ) { next if ($line =~ /^#/); if ($line =~ /^Q:(.+)$/) { # Line includes the question key. $key = $1; $key =~ s/\s/_/; push @keys, $key if (not exists $answers{$key}); $current = "q"; } elsif ($key eq "" && $line =~ /^A:(.+)$/) { # Line includes the answer key. # NOTE: The answers themselves also start with "A:", which is # why we need to check whether we already have a key, above. $key = $1; $key =~ s/\s/_/; push @keys, $key if (not exists $questions{$key}); $current = "a"; } elsif ($key eq "" && $line =~ /^T:(.+)$/) { $key = $1; $current = "t"; } elsif ($line =~ /^%%%%/) { $_ = $text; s/&/&/g; s//>/g; s/"/"/g; if ($current eq "a") { $_ = $text; # Transform * lists into proper lists. s|\n+\* |\n
  • |g; # Separate paragraphs. s/\n\n/\n

    /g; s|((?:

  • [^<]+)+)|\n|g; # Hyperlink URLs. s{http(?:|s)://[^\s\),]+}{$&}g; # Highlight commands. s|'([^\s']+)'|$1|g; # Replace *emphasis* by italics. s|\*(.*)\*|$1|g; # Also use italics for mentioned txt/png files. s{[^\s]+\.(?:txt|png)}{$&}g; $answers{$key} = $_; } $questions{$key} = $_ if ($current eq "q"); $title = $_ if ($current eq "t" && $key eq "html"); $text = ""; $key = ""; $listct = 0; } else { $text .= $line; } } close FILE; open (OUTFILE, ">$outfile") or die "Cannot write to file '$outfile'.\n"; # Print the header. print OUTFILE < $title

    $title

      END # Print all questions, and link them to their answer. my $count = 0; foreach my $k (@keys) { if (not exists $questions{$k}) { print STDERR "No question for key '$k'. Skip key.\n"; next; } if (not exists $answers{$k}) { print STDERR "No answer for key '$k'. Skip key.\n"; next; } $count++; my $question = $questions{$k}; my $answer = $answers{$k}; # Prefix question with "Q#". chomp $question; $question = "Q$count".". $question"; $questions{$k} = $question; print OUTFILE "
    • $question\n"; } print OUTFILE <

      END # Print all question/answer pairs. foreach my $k (@keys) { next if (not (exists $questions{$k}) || not (exists $answers{$k})); my $question = $questions{$k}; my $answer = $answers{$k}; print OUTFILE <$question
      $answer
      END } print OUTFILE <

      Back to top

      END