faq.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
  2. # Niklas Volbers <mithrandir42@web.de>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. # This module creates a list of all questions on the page, e.g.
  17. # for a faq. It does so by recognizing all lines that begin with
  18. # a Q: as a question.
  19. # Additionally, lines starting with Q: and A: are rendered using
  20. # the css classes div.question and div.answer.
  21. use strict;
  22. use v5.10;
  23. AddModuleDescription('faq.pl', 'FAQ Extension');
  24. our ($q, $bol, @MyRules);
  25. our ($FaqHeaderText, $FaqQuestionText, $FaqAnswerText);
  26. $FaqHeaderText = "Questions on this page:" unless $FaqHeaderText;
  27. $FaqQuestionText = "Question: " unless $FaqQuestionText;
  28. $FaqAnswerText = "Answer: " unless $FaqAnswerText;
  29. push(@MyRules, \&FaqRule);
  30. sub FaqRule {
  31. if ($bol && m/\GQ: (.+)/cg) {
  32. return $q->a({name=>'FAQ_' . UrlEncode($1)},'')
  33. . $q->div({class=>'question'}, $FaqQuestionText . $1);
  34. } elsif ($bol && m/\GA:[ \t]*/cg) {
  35. return CloseHtmlEnvironments()
  36. . AddHtmlEnvironment('div', "class='answer'") . $FaqAnswerText;
  37. }
  38. return;
  39. }
  40. *OldFaqGetHeader = \&GetHeader;
  41. *GetHeader = \&NewFaqGetHeader;
  42. sub NewFaqGetHeader {
  43. my ($id) = @_;
  44. my $result = OldFaqGetHeader(@_);
  45. # append FAQ to header
  46. $result .= FaqHeadings($id) if $id;
  47. return $result;
  48. }
  49. sub FaqHeadings {
  50. my $page = GetPageContent(shift);
  51. # ignore all the stuff that gets processed anyway by usemod.pl and
  52. # creole.pl -- if we're not going to hook into ordinary parsing like
  53. # toc.pl does, this will always be incomplete.
  54. $page =~ s/<nowiki>(.*\n)*<\/nowiki>//gi;
  55. $page =~ s/<pre>(.*\n)*<\/pre>//gi;
  56. $page =~ s/<code>(.*\n)*<\/code>//gi;
  57. $page =~ s/\{\{\{[ \t]*\n(.*?)\n\}\}\}[ \t]*(\n|$)//gs;
  58. my $Headings = '';
  59. foreach my $line (grep(/^Q:[ \t]*(.*?)$/, split(/\n/, $page))) {
  60. next unless $line =~ /^Q:[ \t]*(.*?)$/;
  61. next unless $1;
  62. my $link = 'FAQ_' . UrlEncode($1);
  63. $Headings .= $q->li($q->a({href=>'#' . $link}, $1));
  64. }
  65. return $q->div({class=>'faq'}, $FaqHeaderText . $q->ol($Headings)) if $Headings;
  66. }