summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/aomart/aom-fullref.pl
blob: 523307ab42cfd8694426c8a2efb6f0ce144ab329 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/perl
#
# Convert the refences to \fullref.  Usage:
# perl fullref.pl original.tex > converted.tex
#
#
#
# Author: Boris Veytsman
#
# Version: 1.0, 2025/01/08

=pod

=head1 NAME

aom-fullref.pl - confert references to aomart fullref commands

=head1 SYNOPSIS

aom-fullref.pl INPUT > OUTPUT


=head1 DESCRIPTION

Aomart class L<https://ctan.org/pkg/aomart> uses commands like
C<\fullref{Theorem}{ref}> to generate clickable reference to constructions
like ``Theorem 7'' or ``Equation 4''.  This script changes all C<\ref> and
C<\pageref> commands into C<\fullref> and C<\pfullref> commands suitable
for use with aomart.


=head1 AUTHOR

Boris Veytsman

=head1 COPYRIGHT AND LICENSE

This work is in public domain and licensed under CC0 1.0

=cut


use strict;

# List of patterns for 'equation' words
my @eqsynonyms = ('\S*equa\S*', 'relat\S*',  'item\S*',
		  'condition\S*',
		  '\S*propert\S*');

# First we skip preamble
while(<>) {
    print;
    last if (/\\begin\{document\}/);
}

# This is a trick to catch \ref being the first word on the line
my $prevline = "";

# Processing document
while (<>) {
    if ((/^[\s~]*\(?\\ref/) || (/^[\s~]*\\eqref/)) {
	chomp $prevline;
	$_ = "$prevline $_";
    } else {
	print $prevline;
    }
    
    # Now the meat of the substitutions
    # We add initial space, so all words have space before them.
    $_ = " $_";
    foreach my $synonym (@eqsynonyms) {
	s/(\s)($synonym)[\s~]*\(\\ref\{([^\}]+)\}\)/$1\\pfullref{$2}{$3}/gi;
	s/(\s)($synonym)[\s~]*\\eqref\{([^\}]+)\}/$1\\eqfullref{$2}{$3}/gi;
    }
    s/(\s)([^\s\\]\S*[^~\s\(\)\[\]])[\s~]*\[\\ref\{([^\}]+)\}\]/$1\\bfullref{$2}{$3}/g;
    s/(\s)([^\s\\]\S*[^~\s\(\)\[\]])[\s~]*\\ref\{([^\}]+)\}/$1\\fullref{$2}{$3}/g;
    s/(\s)([^\s\\]\S*[^~\s\(\)\[\]])[\s~]*\\pageref\{([^\}]+)\}/$1\\fullpageref[$2]{$3}/g;
    # Now delete the extra space
    s/^ //;
    $prevline=$_;
    if (/\\end\{document\}/) {
	print;
	last;
    }
}

# And processing whatever is left
while (<>) {
    print;
}

exit 0;