3
0
mirror of https://github.com/pragma-/pbot.git synced 2025-01-05 17:42:34 +01:00
pbot/applets/gencstd.pl

671 lines
22 KiB
Perl
Raw Normal View History

2021-02-07 23:37:12 +01:00
#!/usr/bin/env perl
2021-07-11 00:00:22 +02:00
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
License project under MPL2 This patch adds the file LICENSE which is the verbatim copy of the Mozilla Public License Version 2.0 as retreived from https://www.mozilla.org/media/MPL/2.0/index.815ca599c9df.txt on 2017-03-05. This patch also places license headers for the MPL2 type A variant of the license header in the following files: PBot/AntiFlood.pm PBot/BanTracker.pm PBot/BlackList.pm PBot/BotAdminCommands.pm PBot/BotAdmins.pm PBot/ChanOpCommands.pm PBot/ChanOps.pm PBot/Channels.pm PBot/Commands.pm PBot/DualIndexHashObject.pm PBot/EventDispatcher.pm PBot/FactoidCommands.pm PBot/FactoidModuleLauncher.pm PBot/Factoids.pm PBot/HashObject.pm PBot/IRCHandlers.pm PBot/IgnoreList.pm PBot/IgnoreListCommands.pm PBot/Interpreter.pm PBot/LagChecker.pm PBot/Logger.pm PBot/MessageHistory.pm PBot/MessageHistory_SQLite.pm PBot/NickList.pm PBot/PBot.pm PBot/Plugins.pm PBot/Plugins/AntiAway.pm PBot/Plugins/AntiKickAutoRejoin.pm PBot/Plugins/AntiRepeat.pm PBot/Plugins/AntiTwitter.pm PBot/Plugins/AutoRejoin.pm PBot/Plugins/Counter.pm PBot/Plugins/Quotegrabs.pm PBot/Plugins/Quotegrabs/Quotegrabs_Hashtable.pm PBot/Plugins/Quotegrabs/Quotegrabs_SQLite.pm PBot/Plugins/UrlTitles.pm PBot/Plugins/_Example.pm PBot/Refresher.pm PBot/Registerable.pm PBot/Registry.pm PBot/RegistryCommands.pm PBot/SQLiteLogger.pm PBot/SQLiteLoggerLayer.pm PBot/SelectHandler.pm PBot/StdinReader.pm PBot/Timer.pm PBot/Utils/ParseDate.pm PBot/VERSION.pm build/update-version.pl modules/acronym.pl modules/ago.pl modules/c11std.pl modules/c2english.pl modules/c2english/CGrammar.pm modules/c2english/c2eng.pl modules/c99std.pl modules/cdecl.pl modules/cfaq.pl modules/cjeopardy/IRCColors.pm modules/cjeopardy/QStatskeeper.pm modules/cjeopardy/Scorekeeper.pm modules/cjeopardy/cjeopardy.pl modules/cjeopardy/cjeopardy_answer.pl modules/cjeopardy/cjeopardy_filter.pl modules/cjeopardy/cjeopardy_hint.pl modules/cjeopardy/cjeopardy_qstats.pl modules/cjeopardy/cjeopardy_scores.pl modules/cjeopardy/cjeopardy_show.pl modules/codepad.pl modules/compiler_block.pl modules/compiler_client.pl modules/compiler_vm/Diff.pm modules/compiler_vm/cc modules/compiler_vm/compiler_client.pl modules/compiler_vm/compiler_server.pl modules/compiler_vm/compiler_server_vbox_win32.pl modules/compiler_vm/compiler_server_watchdog.pl modules/compiler_vm/compiler_vm_client.pl modules/compiler_vm/compiler_vm_server.pl modules/compiler_vm/compiler_watchdog.pl modules/compiler_vm/languages/_c_base.pm modules/compiler_vm/languages/_default.pm modules/compiler_vm/languages/bash.pm modules/compiler_vm/languages/bc.pm modules/compiler_vm/languages/bf.pm modules/compiler_vm/languages/c11.pm modules/compiler_vm/languages/c89.pm modules/compiler_vm/languages/c99.pm modules/compiler_vm/languages/clang.pm modules/compiler_vm/languages/clang11.pm modules/compiler_vm/languages/clang89.pm modules/compiler_vm/languages/clang99.pm modules/compiler_vm/languages/clangpp.pm modules/compiler_vm/languages/clisp.pm modules/compiler_vm/languages/cpp.pm modules/compiler_vm/languages/freebasic.pm modules/compiler_vm/languages/go.pm modules/compiler_vm/languages/haskell.pm modules/compiler_vm/languages/java.pm modules/compiler_vm/languages/javascript.pm modules/compiler_vm/languages/ksh.pm modules/compiler_vm/languages/lua.pm modules/compiler_vm/languages/perl.pm modules/compiler_vm/languages/python.pm modules/compiler_vm/languages/python3.pm modules/compiler_vm/languages/qbasic.pm modules/compiler_vm/languages/scheme.pm modules/compiler_vm/languages/server/_c_base.pm modules/compiler_vm/languages/server/_default.pm modules/compiler_vm/languages/server/c11.pm modules/compiler_vm/languages/server/c89.pm modules/compiler_vm/languages/server/c99.pm modules/compiler_vm/languages/server/clang.pm modules/compiler_vm/languages/server/clang11.pm modules/compiler_vm/languages/server/clang89.pm modules/compiler_vm/languages/server/clang99.pm modules/compiler_vm/languages/server/cpp.pm modules/compiler_vm/languages/server/freebasic.pm modules/compiler_vm/languages/server/haskell.pm modules/compiler_vm/languages/server/java.pm modules/compiler_vm/languages/server/qbasic.pm modules/compiler_vm/languages/server/tendra.pm modules/compiler_vm/languages/sh.pm modules/compiler_vm/languages/tendra.pm modules/compliment modules/cstd.pl modules/define.pl modules/dice_roll.pl modules/excuse.sh modules/expand_macros.pl modules/fnord.pl modules/funnyish_quote.pl modules/g.pl modules/gdefine.pl modules/gen_cfacts.pl modules/gencstd.pl modules/get_title.pl modules/getcfact.pl modules/google.pl modules/gspy.pl modules/gtop10.pl modules/gtop15.pl modules/headlines.pl modules/horoscope modules/horrorscope modules/ideone.pl modules/insult.pl modules/love_quote.pl modules/man.pl modules/map.pl modules/math.pl modules/prototype.pl modules/qalc.pl modules/random_quote.pl modules/seen.pl modules/urban modules/weather.pl modules/wikipedia.pl pbot.pl pbot.sh It is highly recommended that this list of files is reviewed to ensure that all files are the copyright of the sole maintainer of the repository. If any files with license headers contain the intellectual property of anyone else, it is recommended that a request is made to revise this patch or that the explicit permission of the co-author is gained to allow for the license of the work to be changed. I (Tomasz Kramkowski), the contributor, take no responsibility for any legal action taken against the maintainer of this repository for incorrectly claiming copyright to any work not owned by the maintainer of this repository.
2017-03-05 22:33:31 +01:00
2019-06-26 18:34:19 +02:00
# ugly and hacked together
2024-12-14 23:58:10 +01:00
# Instructions:
#
# Step 1: convert file.pdf to file.in:
# n1256: pdftotext -layout -y 75 -H 650 -W 1000 n1256.pdf n1256.in
# n1570: pdftotext -layout -y 80 -H 650 -W 1000 n1570.pdf n1570.in
# n3047: pdftotext -layout -y 75 -H 700 -W 1000 n3047.pdf n3047.in
# n3220: pdftotext -layout -y 80 -H 700 -W 1000 n3220.pdf n3220.in
# n3301: pdftotext -layout -y 80 -H 700 -W 1000 n3301.pdf n3301.in
#
# Step 2: manually edit file.in as follows. Compare with existing n3047.in
# for guidance.
#
# a) Add ABSTRACT., CONTENTS., INTRO., FOREWORD. BIBLIO. section headers
# indented to column 5 (4 spaces indentation).
#
# b) Delete any leftover INTERNATIONAL STANDARD headers/footers.
#
# c) Edit CONTENTS. section to add ~~ in front of every line so they
# are not parsed as sections. I use the following vim macro:
#
# qq
# i
# ~~
# <ESC>
# j
# 0
# q
# 300@q (subtract first ToC line number from last line number to
# determine how many lines to mask . Or just add a few
# more 10@q until all table of contents lines are masked)
#
# d) Strip page numbers from CONTENTS. I use the following vim macro:
#
# qq
# / \. \.
# D
# q
# 50@q (repeat until done)
#
# Then go back to top of ToC and:
#
# qq
# /\s\+\d\+$
# D
# q
# 10@q (repeat until done)
#
# e) Delete M section identifiers from Bibliography.
#
# f) Delete Index section at bottom after Bibliography.
#
# h) Add Z. indented to 4 spaces as last line to mark final section.
#
# Step 3: run ./gencstd.pl -d file.in (this validates the data of file.in)
#
# Step 4: when an error about mismatched sections/footnotes occurs,
# manually edit the file.in to fix the error.
#
# The debug output will show you the last section/paragrah that was
# successfully added. Look in the contents to see which section/paragraph
# was slurped up. Fix that section/paragraph.
#
# 99% of the time the fix is to simply adjust indentation to exactly 4
# spaces for the section/footnote identifier.
#
# Rarely there will be a numerical literal or a section reference at the
# beginning of the line that belongs to the paragraph's contents but it's
# being parsed as a section/paragraph identifier. In this case, put a ~~
# at the beginning of the line to mask the literal/reference.
#
# If there's an invalid footnote difference, ensure the footnote is attached
# to a word and not at the beginning of a line.
#
# Return to step 3.
#
# Step 5: run ./gencstd.pl -t file.in > file.out
# (this is for the `cstd` bot cmd)
#
# Step 6: run ./gencstd.pl -h file.in > file.html
# (this is the HTML for the website)
#
# Step 7: Update docs, website, commands, etc.
# * doc/Applets.md (###c99, ###c11, ###c23, etc)
# * upload file.html to website
# * update applets/cstd.pl
# * add new bot command if necessary:
# factadd #c c2y <info about c2y>
# factset #c c2y action_with_args /call cstd -std=C2Y
use warnings;
use strict;
use HTML::Entities;
use Data::Dumper;
my $debug = 100;
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
2024-12-14 23:58:10 +01:00
my $USAGE = "Usage: $0 <-d|-t|-h> <input file>";
my $input = "@ARGV";
if (not length $input) {
2024-12-14 23:58:10 +01:00
print STDERR "$USAGE\n";
exit 1;
}
# too lazy to use getopt at the moment
$input =~ s/^(-[^ ]+)\s+//;
my $mode = $1;
if ($mode ne '-t' && $mode ne '-h' && $mode ne '-d') {
print STDERR "Missing -d, -t or -h. $USAGE\n";
exit 1;
}
2020-02-15 23:38:32 +01:00
open FH, "<:encoding(UTF-8)", $input or die "Could not open $input: $!";
my @contents = <FH>;
close FH;
my $text = join '', @contents;
$text =~ s/\r//g;
my $section_title;
my $this_section = '';
my %sections;
my @last_section_number;
my @section_number;
my $last_section = '';
my @footnotes;
2020-02-15 23:38:32 +01:00
my $footnote = 0;
my $last_footnote = 0;
gen_data();
2024-12-14 23:58:10 +01:00
if ($mode eq '-d') {
exit 0;
} elsif ($mode eq '-t') {
gen_txt();
} elsif ($mode eq '-h') {
gen_html();
} else {
print STDERR "Invalid mode `$mode`.\n";
exit 1;
}
sub gen_data {
while ($text =~ m/^\f?\s{0,5}([0-9A-Z]+\.[0-9\.]*)/msg) {
$last_section = $this_section;
$this_section = $1;
@last_section_number = @section_number;
@section_number = split /\./, $this_section;
print STDERR "----------------------------------\n" if $debug;
print STDERR "Processing section [$this_section]\n" if $debug;
validate_section_difference();
2020-02-15 23:38:32 +01:00
my $section_text;
if ($text =~ m/(.*?)^(?=\f?\s{0,4}[0-9A-Z]+\.)/msg) {
$section_text = $1;
} else {
2020-02-15 23:38:32 +01:00
print STDERR "No section text, end of file marker found.\n";
last;
}
2020-02-15 23:38:32 +01:00
if ($section_text =~ m/(.*?)$/msg) {
if (length $1) {
$section_title = $1;
$section_title =~ s/^\s+//;
$section_title =~ s/\s+$//;
print STDERR "+++ set new section title: [$section_title]\n" if $debug;
} else {
print STDERR "--- no length for section title\n" if $debug;
}
} else {
print STDERR "--- no new section title\n" if $debug;
2020-02-15 23:38:32 +01:00
}
2020-02-15 23:38:32 +01:00
$sections{$this_section}{title} = $section_title;
($section_text) = $section_text =~ m/\s*(.*)/msg;
2020-02-15 23:38:32 +01:00
print STDERR "+++ $this_section [$section_title]\n" if $debug >= 2;
print STDERR "+++ section text: [$section_text]\n" if $debug >= 2;
if (not $section_text =~ m/^(?=\d+\s)/msg) {
print STDERR "??? no paragraphs in section\n" if $debug;
$section_text =~ s/~~//msg;
$section_text =~ s/ZZZ//msg;
$sections{$this_section}{text} = $section_text;
} else {
2020-02-15 23:38:32 +01:00
my $last_p = 0;
my $p = 0;
print STDERR "+++ getting paragraphs for $this_section\n" if $debug;
my $pretext;
if ($section_text =~ m/^(?!\f?\d+\s)/) {
($pretext) = $section_text =~ m/^(.*?)^(?=\f?\d+\s)/ms;
print STDERR "pretext captured: [$pretext]\n";
}
while ($section_text =~ m/^\f?(\d+)\s(.*?)^(?=\f?\d)/msgc or $section_text =~ m/^\f?(\d+)\s(.*)/msg) {
2020-02-15 23:38:32 +01:00
$last_p = $p;
$p = $1;
my $t = $2;
if (length $pretext) {
$t = "$pretext $t";
$pretext = '';
}
2020-02-15 23:38:32 +01:00
print STDERR "paragraph $p: [$t]\n" if $debug >= 3;
if ($p - $last_p != 1) {
die "Paragraph diff invalid" unless ($input eq 'n1570.in' && $this_section =~ /^(?:K.3.9.(?:2|3))/);
}
2020-02-15 23:38:32 +01:00
# check for footnotes
my @new_footnotes;
while ($t =~ m/^\s*(\d+)\)\s*(.*?)$/mgc) {
$footnote = $1;
my $footnote_text = "$2\n";
print STDERR "processing 1st footnote $footnote [last: $last_footnote]\n" if $debug;
print STDERR "footnote text [$footnote_text]\n" if $debug;
2020-02-15 23:38:32 +01:00
if ($last_footnote - $footnote != -1) {
2020-02-15 23:38:32 +01:00
die "Footnote diff invalid";
}
2020-02-15 23:38:32 +01:00
$last_footnote = $footnote;
push @new_footnotes, $footnote;
2020-02-15 23:38:32 +01:00
print STDERR "footnote $footnote text: [$footnote_text]\n" if $debug >= 4;
2020-02-15 23:38:32 +01:00
while ($t =~ m/^(.*?)$/mgc) {
2020-02-15 23:38:32 +01:00
my $line = $1;
print STDERR "processing [$line]\n" if $debug;
if ($line =~ m/^\f/mg) {
print STDERR "end of footnote $footnote\n";
last;
}
if (not length $line or $line =~ m/^\s+$/) {
print STDERR "skipping empty line\n";
next;
}
if ($line =~ m/^\s*(\d+)\)\s*(.*?)$/mg) {
print STDERR "----------------\n" if $debug >= 1;
print STDERR "+++ added footnote $footnote: [$footnote_text]\n" if $debug >= 1;
2020-02-15 23:38:32 +01:00
$footnotes[$footnote] = $footnote_text;
print STDERR "----------------\n" if $debug >= 1;
$footnote = $1;
$footnote_text = "$2\n";
print STDERR "processing 2nd footnote $footnote [last: $last_footnote]\n" if $debug;
2020-02-15 23:38:32 +01:00
if ($last_footnote - $footnote != -1) {
die "Footnote diff invalid";
}
2020-02-15 23:38:32 +01:00
$last_footnote = $footnote;
push @new_footnotes, $footnote;
2020-02-15 23:38:32 +01:00
print STDERR "footnote $footnote text: [$footnote_text]\n" if $debug >= 4;
2020-02-15 23:38:32 +01:00
next;
}
if (not length $line or $line =~ m/^\s+$/) {
print STDERR "footnote $footnote: skipping empty line\n";
} else {
$footnote_text .= "$line\n";
print STDERR "footnote $footnote text: appending [$line]\n" if $debug >= 3;
2020-02-15 23:38:32 +01:00
}
}
print STDERR "----------------\n" if $debug >= 1;
print STDERR "+++ added footnote $footnote: [$footnote_text]\n" if $debug >= 1;
2020-02-15 23:38:32 +01:00
$footnotes[$footnote] = $footnote_text;
print STDERR "----------------\n" if $debug >= 1;
}
# strip footnotes from section text
foreach my $fn (@new_footnotes) {
my $sub = quotemeta $footnotes[$fn];
$sub =~ s/(\\ )+/\\s*/g;
#print STDERR "subbing out [$footnote) $sub]\n";
$t =~ s/^\s*$fn\)\s*$sub//ms;
}
$t =~ s/\f//g;
$t =~ s/~~//msg;
$t =~ s/ZZZ//msg;
2020-02-15 23:38:32 +01:00
$sections{$this_section . "p$p"}{text} = "$p $t";
print STDERR "+++ added ${this_section}p$p:\n$p $t\n" if $debug;
}
print STDERR "+++ paragraphs done\n" if $debug;
}
}
}
sub bysection {
2020-02-15 23:38:32 +01:00
my $inverse = 1;
my ($a1, $p1) = split /p/, $a;
my ($b1, $p2) = split /p/, $b;
$p1 //= 0;
$p2 //= 0;
2020-02-15 23:38:32 +01:00
my @k1 = split /\./, $a1;
my @k2 = split /\./, $b1;
my @r;
if ($#k2 > $#k1) {
my @tk = @k1;
@k1 = @k2;
@k2 = @tk;
2020-02-15 23:38:32 +01:00
my $tp = $p1;
$p1 = $p2;
$p2 = $tp;
2020-02-15 23:38:32 +01:00
$inverse = -1;
} else {
$inverse = 1;
}
2020-02-15 23:38:32 +01:00
my $i = 0;
for (; $i < $#k1 + 1; $i++) {
if (not defined $k2[$i]) { $r[$i] = 1; }
else {
if ($i == 0) { $r[$i] = $k1[$i] cmp $k2[$i]; }
else { $r[$i] = $k1[$i] <=> $k2[$i]; }
}
}
2020-02-15 23:38:32 +01:00
$r[$i] = ($p1 <=> $p2);
my $ret = 0;
foreach my $rv (@r) {
if ($rv != 0) {
$ret = $rv;
last;
}
}
return $ret * $inverse;
}
sub gen_txt {
2020-02-15 23:38:32 +01:00
my $footer = "";
my $paren = 0;
my $section_head;
my $section_title;
foreach my $this_section (sort bysection keys %sections) {
print STDERR "writing section $this_section\n" if $debug;
if (not $this_section =~ m/p/) {
2022-08-12 22:02:00 +02:00
print "$this_section $sections{$this_section}{title}\n";
2020-02-15 23:38:32 +01:00
$section_head = $this_section;
$section_title = $sections{$this_section}{title};
}
2020-02-15 23:38:32 +01:00
my $section_text = $sections{$this_section}{text};
2020-02-15 23:38:32 +01:00
while ($section_text =~ m/^(.*?)$/msg) {
my $line = $1;
print STDERR "paren reset, line [$line]\n" if $debug >= 8;
my $number = "";
while ($line =~ m/(.)/g) {
my $c = $1;
if ($c =~ m/[0-9]/) { $number .= $c; }
elsif ($c eq ' ') { $number = ""; }
elsif ($c eq '(') {
$paren++;
print STDERR "got $paren (\n" if $debug >= 8;
} elsif ($c eq ')') {
$paren--;
print STDERR "got $paren )\n" if $debug >= 8;
if ($paren == -1) {
if (length $number and defined $footnotes[$number]) {
print STDERR "Got footnote $number here!\n" if $debug;
2022-08-12 22:02:00 +02:00
$footer .= "\nFootnote.$number) $footnotes[$number]\n";
2020-02-15 23:38:32 +01:00
}
$paren = 0;
}
} else {
$number = "";
}
}
}
2020-02-15 23:38:32 +01:00
print "$section_text\n";
2020-02-15 23:38:32 +01:00
if (length $footer) {
print $footer;
$footer = "";
}
}
}
sub make_link {
my ($text) = @_;
if (exists $sections{$text}) {
return "<a href='#$text'>$text</a>";
} else {
return $text;
}
}
sub linkify {
my ($text) = @_;
$text =~ s/\b((?:[A-Z]|[1-9])\.(?:\.?[0-9]+)*)\b/make_link($1)/ge;
return $text;
}
sub gen_html {
2020-02-15 23:38:32 +01:00
print "<html>\n<body>\n";
foreach my $section (qw/ABSTRACT. CONTENTS. FOREWORD. INTRO./) {
foreach my $paragraph (sort bysection keys %sections) {
if ($paragraph =~ m/^$section/) {
write_html_section($paragraph);
delete $sections{$paragraph};
}
}
delete $sections{$section};
}
foreach my $section (sort bysection keys %sections) {
2024-12-14 23:58:10 +01:00
next if $section eq 'BIBLIO.';
write_html_section($section);
}
2024-12-14 23:58:10 +01:00
foreach my $section (qw/BIBLIO./) {
foreach my $paragraph (sort bysection keys %sections) {
if ($paragraph =~ m/^$section/) {
write_html_section($paragraph);
}
}
}
print "\n</body>\n</html>\n";
}
sub write_html_section {
my ($this_section) = @_;
2020-02-15 23:38:32 +01:00
my $footer = "";
my $paren = 0;
print STDERR "writing section [$this_section]\n" if $debug;
print "<a name='", encode_entities($this_section), "'></a>\n";
if (not $this_section =~ m/p/) {
print "<hr>\n<h3>", encode_entities($this_section), " [", encode_entities($sections{$this_section}{title}), "]</h3>\n";
}
my $section_text = $sections{$this_section}{text};
next if not length $section_text;
$section_text = encode_entities $section_text;
while ($section_text =~ m/^(.*?)$/msg) {
my $line = $1;
print STDERR "paren reset, line [$line]\n" if $debug >= 8;
my $number = "";
while ($line =~ m/(.)/g) {
my $c = $1;
if ($c =~ m/[0-9]/) { $number .= $c; }
elsif ($c eq ' ') { $number = ""; }
elsif ($c eq '(') {
$paren++;
print STDERR "got $paren (\n" if $debug >= 8;
} elsif ($c eq ')') {
$paren--;
print STDERR "got $paren )\n" if $debug >= 8;
if ($paren == -1) {
if (length $number and defined $footnotes[$number]) {
print STDERR "Got footnote $number here!\n" if $debug;
$section_text =~ s/$number\)/<a href='#FOOTNOTE.$number'><sup>[$number]<\/sup><\/a>/;
$footer .= "<a name='FOOTNOTE.$number'>\n<pre><i><b>Footnote $number)</b> ".encode_entities($footnotes[$number])."</i></pre>\n</a>\n";
}
$paren = 0;
}
} else {
$number = "";
}
2020-02-15 23:38:32 +01:00
}
}
2020-02-15 23:38:32 +01:00
$section_text = linkify($section_text);
$footer = linkify($footer);
if ($this_section eq 'CONTENTS.') {
$section_text =~ s/Annex ([A-Z])/<a href='#$1.'>Annex $1<\/a>/mg;
$section_text =~ s/^(\d+\.)/<a href='#$1'>$1<\/a>/mg;
$section_text =~ s/^Foreword/<a href='#FOREWORD.'>Foreword<\/a>/mg;
$section_text =~ s/^Introduction/<a href='#INTRO.'>Introduction<\/a>/mg;
}
2020-02-15 23:38:32 +01:00
print "<pre>", $section_text, "</pre>\n";
2020-02-15 23:38:32 +01:00
if (length $footer) {
print $footer;
$footer = '';
}
}
2020-02-15 23:38:32 +01:00
# this mess of code verifies that two given section numbers are within 1 unit of distance of each other
# this ensures that no sections were skipped due to misparses
sub validate_section_difference {
2024-12-14 23:58:10 +01:00
if (@last_section_number && $last_section_number[0] !~ /(?:ABSTRACT|CONTENTS|FOREWORD|INTRO|BIBLIO)/) {
my $fail = 0;
my $skip = 0;
2020-02-15 23:38:32 +01:00
print STDERR "comparing last section ", join('.', @last_section_number), " vs ", join('.', @section_number), "\n";
2024-12-14 23:58:10 +01:00
return if "@section_number" eq 'BIBLIO';
if (@section_number > @last_section_number) {
if (@section_number - @last_section_number != 1) {
$fail = 1;
print STDERR "size difference too great\n";
}
unless ($fail) {
if ($section_number[0] =~ /^[A-Z]+$/) {
if ($last_section_number[0] =~ /^[A-Z]+$/) {
for (my $i = 0; $i < @last_section_number; $i++) {
if ($section_number[$i] ne $last_section_number[$i]) {
$fail = 1;
print STDERR "digits different\n";
last;
}
}
} else {
print STDERR "disregarding section namespace change from number to alphabet\n";
$skip = 1;
2020-02-15 23:38:32 +01:00
}
} else {
for (my $i = 0; $i < @last_section_number; $i++) {
if ($section_number[$i] ne $last_section_number[$i]) {
$fail = 1;
print STDERR "digits different\n";
last;
}
}
2020-02-15 23:38:32 +01:00
}
}
if (!$skip && ($fail || $section_number[$#section_number] != 1)) {
print STDERR "difference too great ", join('.', @last_section_number), " vs ", join('.', @section_number), "\n";
die;
}
} elsif (@last_section_number > @section_number) {
if ($section_number[0] =~ /^[A-Z]+$/) {
if ($last_section_number[0] =~ /^[A-Z]+$/) {
if ($section_number[0] ne $last_section_number[0]) {
if (ord($section_number[0]) - ord($last_section_number[0]) != 1) {
$fail = 1;
print STDERR "letter difference too great\n";
} else {
$skip = 1;
print STDERR "letter difference good\n";
}
}
unless ($fail) {
for (my $i = 1; $i < @section_number - 1; $i++) {
if ($section_number[$i] != $last_section_number[$i]) {
if ($section_number[$i] - $last_section_number[$i] != 1) {
print STDERR "digit difference too great\n";
$fail = 1;
}
last;
}
}
}
} else {
print STDERR "disregarding section namespace change from number to alphabet\n";
$skip = 1;
}
} else {
for (my $i = 0; $i < @section_number - 1; $i++) {
if ($section_number[$i] != $last_section_number[$i]) {
if ($section_number[$i] - $last_section_number[$i] != 1) {
print STDERR "digit difference too great\n";
$fail = 1;
}
last;
}
}
}
if (!$skip && ($fail || $section_number[$#section_number] - $last_section_number[$#section_number] != 1)) {
print STDERR "difference too great ", join('.', @last_section_number), " vs ", join('.', @section_number), "\n";
die;
}
} else {
my @rev_last = reverse @last_section_number;
my @rev_curr = reverse @section_number;
if ($rev_curr[$#rev_curr] =~ /^[A-Z]+$/) {
if ($rev_last[$#rev_last] =~ /^[A-Z]+$/) {
if ($rev_curr[$#rev_curr] ne $rev_last[$#rev_last]) {
if (ord($rev_curr[$#rev_curr]) - ord($rev_last[$#rev_last]) != 1) {
$fail = 1;
print STDERR "letter difference too great\n";
}
}
for (my $i = 1; $i < @rev_curr; $i++) {
if ($rev_curr[$i] != $rev_last[$i]) {
if ($rev_curr[$i] - $rev_last[$i] > 1) {
$fail = 1;
}
last;
}
}
} else {
print STDERR "disregarding section namespace change from number to alphabet\n";
$skip = 1;
}
} else {
for (my $i = 0; $i < @rev_curr; $i++) {
if ($rev_curr[$i] != $rev_last[$i]) {
if ($rev_curr[$i] - $rev_last[$i] > 1) {
$fail = 1;
}
last;
}
}
}
if (!$skip && $fail) {
print STDERR "difference too great ", join('.', @last_section_number), " vs ", join('.', @section_number), "\n";
die;
}
2020-02-15 23:38:32 +01:00
}
}
}