3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-27 13:12:42 +01:00
pbot/applets/gen_cfacts.pl
Pragmatic Software 3d97dc2c33 Rename "modules" to "applets"
"Applet" is a much better name for the external command-line
scripts and programs that can be loaded as PBot commands. They
will no longer be confused with Perl modules.

https://en.wikipedia.org/wiki/Applet
2021-11-19 18:05:50 -08:00

80 lines
2.2 KiB
Perl
Vendored

#!/usr/bin/env perl
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
# quick and dirty
use warnings;
use strict;
use HTML::Entities;
my $STD = 'n1570.html';
my $text;
{
local $/ = undef;
open my $fh, "<", $STD or die "Could not open $STD: $!";
$text = <$fh>;
close $fh;
}
my $cfact_regex = qr/
(
\s+\S+\s+which\s+is.*?
|\s+\S+\s+which\s+expand.*?
|(?:\-\-\s+|\s+|<pre>\s*\d*\s*(EXAMPLE\s*|NOTE\s*)?)An?\s+[^.]+describes.*?
|(?:\-\-\s+|\s+|<pre>\s*\d*\s*(EXAMPLE\s*|NOTE\s*))An?\s+[^.]+is.*?
|(?:\-\-\s+|\s+|<pre>\s*\d*\s*)[^.]+shall.*?
|(?:\-\-\s+|\s+|<pre>\s*\d*\s*)If.*?
|(?:\-\-\s+|\s+|<pre>\s*\d*\s*)[^.]+is\s+named.*?
|(?:\-\-\s+|\s+|<pre>\s*\d*\s*)[^.]+is\s+known.*?
|(?:\-\-\s+|\s+|<pre>\s*\d*\s*)[^.]+are\s+known.*?
|(?:\-\-\s+|\s+|<pre>\s*\d*\s*)[^.]+is\s+called.*?
|(?:\-\-\s+|\s+|<pre>\s*\d*\s*)[^.]+are\s+called.*?
|(?:\-\-\s+|\s+|<pre>\s*\d*\s*)When.*?
|(?:\-\-\s+|\s+|<pre>\s*\d*\s*)The\s+\S+\s+function.*?
)
(?:\.(?!(\d|h))|<\/pre>)
/msx;
my @sections;
while ($text =~ /^<h3>(.*?)<\/h3>/mg) {
my $section = $1;
$section =~ s/[\[\]]//g;
unshift @sections, [pos $text, $section];
}
while ($text =~ /$cfact_regex/gms) {
my $fact = $1;
next unless length $fact;
$fact =~ s/[\n\r]/ /g;
$fact =~ s/ +/ /g;
$fact =~ s/^\.\s*//;
$fact =~ s/^\s*--\s*//;
$fact =~ s/^\d+\s*//;
$fact =~ s/- ([a-z])/-$1/g;
$fact =~ s/\s+\././g;
$fact =~ s/^\s*<pre>\s*\d*\s*//;
$fact =~ s/^\s*EXAMPLE\s*//;
$fact =~ s/^\s*NOTE\s*//;
$fact =~ s/^\s+//;
$fact =~ s/\s+$//;
my $section = '';
foreach my $s (@sections) {
if (pos $text >= $s->[0]) {
$section = "[$s->[1]] ";
last;
}
}
$fact = decode_entities($fact);
$fact =~ s/[a-z;,.]\K\d+\)//g; # remove footnote markers
print "$section$fact.\n";
}