2007-05-20 22:44:44 +02:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
2023-02-21 06:31:52 +01:00
|
|
|
# SPDX-FileCopyrightText: 2005-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $match = 1;
|
2007-05-20 22:44:44 +02:00
|
|
|
my $matches = 0;
|
2020-02-15 23:38:32 +01:00
|
|
|
my $found = 0;
|
2007-05-20 22:44:44 +02:00
|
|
|
|
|
|
|
print "Usage: faq [match #] <search regex>\n" and exit 0 if not defined $ARGV[0];
|
|
|
|
|
|
|
|
my $query = join(".*?", @ARGV);
|
|
|
|
$query =~ s/\s+/.*?/g;
|
|
|
|
|
|
|
|
$query =~ s/\+/\\+/g;
|
|
|
|
$query =~ s/[^\.]\*/\\*/g;
|
|
|
|
$query =~ s/^\*/\\*/g;
|
|
|
|
$query =~ s/\[/\\[/g;
|
|
|
|
$query =~ s/\]/\\]/g;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($query =~ /^(\d+)\.\*\?/) {
|
2020-02-15 23:38:32 +01:00
|
|
|
$match = $1;
|
|
|
|
$query =~ s/^\d+\.\*\?//;
|
2007-05-20 22:44:44 +02:00
|
|
|
}
|
|
|
|
|
2014-04-22 17:10:10 +02:00
|
|
|
open(FILE, "< cfaq-questions.html") or print "Can't open cfaq-questions.html: $!" and exit 1;
|
|
|
|
my @contents = <FILE>;
|
|
|
|
close(FILE);
|
|
|
|
|
|
|
|
my ($heading, $question_full, $question_link, $question_number, $question_text, $result);
|
|
|
|
|
|
|
|
foreach my $line (@contents) {
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($line =~ m/^<H4>(.*?)<\/H4>/) {
|
|
|
|
$heading = $1;
|
|
|
|
next;
|
|
|
|
}
|
2014-04-22 17:10:10 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($line =~ m/<p><a href="(.*?)" rel=subdocument>(.*?)<\/a>/) {
|
|
|
|
($question_link, $question_number) = ($1, $2);
|
|
|
|
|
|
|
|
if (defined $question_full) {
|
|
|
|
if ($question_full =~ m/$query/i) {
|
|
|
|
$matches++;
|
|
|
|
$found = 1;
|
|
|
|
if ($match == $matches) {
|
|
|
|
$question_text =~ s/\s+/ /g;
|
|
|
|
$result = $question_text;
|
|
|
|
}
|
|
|
|
}
|
2014-04-22 17:10:10 +02:00
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
|
|
|
|
$question_full = "$question_number $question_link ";
|
|
|
|
$question_text = "http://c-faq.com/$question_link - $heading, $question_number: ";
|
|
|
|
next;
|
2007-05-20 22:44:44 +02:00
|
|
|
}
|
2014-04-22 17:10:10 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (defined $question_full) {
|
|
|
|
$line =~ s/[\n\r]/ /g;
|
|
|
|
$line =~ s/(<pre>|<\/pre>|<TT>|<\/TT>|<\/a>|<br>)//g;
|
|
|
|
$line =~ s/<a href=".*?">//g;
|
|
|
|
$line =~ s/ / /g;
|
|
|
|
$line =~ s/&/&/g;
|
|
|
|
$line =~ s/</</g;
|
|
|
|
$line =~ s/>/>/g;
|
|
|
|
|
|
|
|
$question_full .= $line;
|
|
|
|
$question_text .= $line;
|
|
|
|
}
|
2007-05-20 22:44:44 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($found == 1) {
|
2020-02-15 23:38:32 +01:00
|
|
|
print "But there are $matches results...\n" and exit if ($match > $matches);
|
2007-05-20 22:44:44 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
print "$matches results, displaying #$match: " if ($matches > 1);
|
2007-05-20 22:44:44 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
print "$result\n";
|
2007-05-20 22:44:44 +02:00
|
|
|
} else {
|
2020-02-15 23:38:32 +01:00
|
|
|
$query =~ s/\.\*\?/ /g;
|
|
|
|
print "No FAQs match $query\n";
|
2007-05-20 22:44:44 +02:00
|
|
|
}
|