| 1 |
#!/usr/bin/env perl -w |
| 2 |
|
| 3 |
######################################################################### |
| 4 |
# Written and (C) by Jérôme Lelong <[email protected]> # |
| 5 |
# # |
| 6 |
# This program is free software; you can redistribute it and/or modify # |
| 7 |
# it under the terms of the GNU General Public License as published by # |
| 8 |
# the Free Software Foundation; either version 3 of the License, or # |
| 9 |
# (at your option) any later version. # |
| 10 |
# # |
| 11 |
# This program is distributed in the hope that it will be useful, # |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 14 |
# GNU General Public License for more details. # |
| 15 |
# # |
| 16 |
# You should have received a copy of the GNU General Public License # |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. # |
| 18 |
######################################################################### |
| 19 |
|
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
use File::Basename; |
| 23 |
use Cwd; |
| 24 |
use Getopt::Long; |
| 25 |
# recognised math environments |
| 26 |
my @have_star_modes = qw( equation eqnarray align multline table gather); |
| 27 |
my $have_star_mode = join('|', @have_star_modes); |
| 28 |
$have_star_mode = "($have_star_mode)"; |
| 29 |
|
| 30 |
|
| 31 |
my $TEX = 'pdflatex'; |
| 32 |
my $TEXOPTIONS = ' -interaction nonstopmode '; |
| 33 |
my $USERTEXOPTIONS = ''; |
| 34 |
my $RUNQUIET = 0; |
| 35 |
my $RUNDEBUG = 0; |
| 36 |
my $PARSEONLY = 0; |
| 37 |
|
| 38 |
# Create a hash with three keys "str", "line", "file" and returns a |
| 39 |
# reference to it |
| 40 |
# |
| 41 |
# Input: 3 args |
| 42 |
# a string |
| 43 |
# a line number |
| 44 |
# a filename |
| 45 |
sub new_ref { |
| 46 |
my ($str, $line, $file) = @_; |
| 47 |
my $ref = {}; |
| 48 |
$ref->{str} = $str; |
| 49 |
$ref->{line} = $line; |
| 50 |
$ref->{file} = $file; |
| 51 |
return $ref; |
| 52 |
} |
| 53 |
|
| 54 |
# Create a hash with six keys "str", "begin", "end", "star", |
| 55 |
# "label_line" and "label" and returns a reference to it. |
| 56 |
# |
| 57 |
# Input : 7 args |
| 58 |
# a string (the env name) |
| 59 |
# line of beginning |
| 60 |
# line of end |
| 61 |
# has the env a star? |
| 62 |
# line on which the label appears |
| 63 |
# value of the label |
| 64 |
# file name |
| 65 |
sub new_math_env { |
| 66 |
my ($str, $begin, $end, $star, $label_line, $label, $file) = @_; |
| 67 |
my $this = {}; |
| 68 |
$this->{str} = $str; |
| 69 |
$this->{begin} = $begin; |
| 70 |
$this->{end} = $end; |
| 71 |
$this->{star} = $star; |
| 72 |
$this->{label_line} = $label_line; |
| 73 |
$this->{label} = $label; |
| 74 |
$this->{file} = $file; |
| 75 |
return $this; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
# Parse .chk file and looks for the environments defined by |
| 80 |
# @have_star_modes. The first and last line of the environment are stored |
| 81 |
# along with the name of the environment. One last variable is |
| 82 |
# used to remember if the star version was used. |
| 83 |
# |
| 84 |
# Input: |
| 85 |
# a chk file content |
| 86 |
# entries |
| 87 |
# refs |
| 88 |
# labels |
| 89 |
# the last 3 args are passed as references and are modified |
| 90 |
sub chk_parse { |
| 91 |
my ($txt, $entries, $refs, $labels, $citations, $bibcites) = @_; |
| 92 |
my ($str, $begin, $end, $star, $labelled_env, $label, $file, $entry, $l); |
| 93 |
|
| 94 |
while (defined($l = shift(@$txt))) { |
| 95 |
if ($l =~ m/^label (.+) line (\d+) file (.+)\n$/o) { |
| 96 |
push(@$labels, new_ref($1, $2, $3)); |
| 97 |
} |
| 98 |
elsif ($l =~ m/^ref (.+) line (\d+) file (.+)$/o) { |
| 99 |
push(@$refs, new_ref($1, $2, $3)); |
| 100 |
} |
| 101 |
elsif ($l =~ m/^citation (.+) line (\d+) file (.+)$/o) { |
| 102 |
foreach my $c (split(/\s*,\s*/, $1)) { |
| 103 |
${$citations}{$c} = 1; |
| 104 |
} |
| 105 |
} |
| 106 |
elsif ($l =~ m/^bibcite (.+) line (\d+) file (.+)$/o) { |
| 107 |
push(@$bibcites, $1); |
| 108 |
} |
| 109 |
elsif ($l =~ m/^begin\{$have_star_mode(\**)\} line (\d+) file (.+)$/o) { |
| 110 |
$str = $1; |
| 111 |
$star = 1; |
| 112 |
$star = 0 unless ($2); |
| 113 |
$begin = $3; |
| 114 |
$file = $4; |
| 115 |
$label = ""; |
| 116 |
$labelled_env = 0; |
| 117 |
while (defined($l = shift(@$txt))) { |
| 118 |
if ($l =~ m/^label (.+) line (\d+) file (.+)$/o) { |
| 119 |
push(@$labels, new_ref($1, $2, $3)); |
| 120 |
$label = $1; |
| 121 |
$labelled_env = $2; |
| 122 |
} |
| 123 |
elsif ($l =~ m/^ref (.+) line (\d+) file (.+)$/o) { |
| 124 |
push(@$refs, new_ref($1, $2, $3)); |
| 125 |
} |
| 126 |
elsif ($l =~ m/^end\{$str\*{$star}\} line (\d+) file (.+)$/) { |
| 127 |
$end = $1; |
| 128 |
$entry = new_math_env($str, $begin, $end, $star, $labelled_env, |
| 129 |
$label, $file); |
| 130 |
push(@$entries, $entry); |
| 131 |
last; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
# Find environments with both labels and stars |
| 140 |
# Input : list of entries |
| 141 |
sub star_label { |
| 142 |
my ($entries) = @_; |
| 143 |
print "************************************\n"; |
| 144 |
print "** Labels in starred environments **\n"; |
| 145 |
print "************************************\n"; |
| 146 |
foreach my $e (@$entries) { |
| 147 |
printf("-- %-20s line %4d: remove label %s \n", |
| 148 |
$e->{file}, $e->{label_line}, $e->{label}) |
| 149 |
if (($e->{star} == 1) && ($e->{label_line} > 0)); |
| 150 |
|
| 151 |
printf("-- %-20s line %4d: consider using a STAR environment\n", |
| 152 |
$e->{file}, $e->{begin}) |
| 153 |
if (($e->{star} == 0) && ($e->{label_line} == 0)); |
| 154 |
} |
| 155 |
print "\n"; |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
# Find labels without any corrsponding references. |
| 160 |
# references must be sorted |
| 161 |
sub unused_label { |
| 162 |
my ($labels, $refs) = @_; |
| 163 |
my $found; |
| 164 |
|
| 165 |
print "*******************\n"; |
| 166 |
print "** Unused Labels **\n"; |
| 167 |
print "*******************\n"; |
| 168 |
foreach my $l (@$labels) { |
| 169 |
$found = 0; |
| 170 |
foreach my $r (@$refs) { |
| 171 |
if ($r->{str} eq $l->{str}) { |
| 172 |
$found = 1; |
| 173 |
last; |
| 174 |
} |
| 175 |
elsif ($r->{str} ge $l->{str}) { |
| 176 |
last; |
| 177 |
} |
| 178 |
} |
| 179 |
printf("-- %-20s line %4d: remove label %s\n", |
| 180 |
$l->{file}, $l->{line}, $l->{str}) |
| 181 |
if (!$found); |
| 182 |
} |
| 183 |
print "\n"; |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
# Find bibitems without any corresponding \cite |
| 188 |
sub unused_cites { |
| 189 |
my ($bibcites, $citations) = @_; |
| 190 |
print "**********************************\n"; |
| 191 |
print "** Uncited Bibliography entries **\n"; |
| 192 |
print "**********************************\n"; |
| 193 |
foreach my $c (@$bibcites) { |
| 194 |
print("remove bibitem : $c\n") unless exists ${$citations}{$c}; |
| 195 |
} |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
# Find and remove duplicates in an |
| 200 |
# array of { str, line } entries. |
| 201 |
# Note that the array must be sorted according to str |
| 202 |
# Input : ref or label list |
| 203 |
# Return the corresponding list |
| 204 |
sub rm_duplicate { |
| 205 |
my ($array) = @_; |
| 206 |
my $prev = '___________not_a_true_label______'; |
| 207 |
my @uniq_array = |
| 208 |
grep { $_->{str} ne $prev && (($prev) = $_->{str}) } @$array; |
| 209 |
return \@uniq_array; |
| 210 |
} |
| 211 |
|
| 212 |
# Display all math envs with their characteristics |
| 213 |
# Input args |
| 214 |
# line of beginning |
| 215 |
# line of end |
| 216 |
# starred or not |
| 217 |
# label |
| 218 |
sub disp_msg { |
| 219 |
my ($entries) = @_; |
| 220 |
my @entries = @$entries; |
| 221 |
|
| 222 |
foreach my $e (@entries) { |
| 223 |
print("env $e->{str} \n"); |
| 224 |
print("\tbeginning : $e->{begin}\n\tend : $e->{end}\n"); |
| 225 |
print("\tstar environment\n") if ($e->{star} == 1); |
| 226 |
print("\tlabelled environement\n") if ($e->{label} > 0); |
| 227 |
print("\n"); |
| 228 |
} |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
sub parse { |
| 233 |
my ($chkfile) = @_; |
| 234 |
my @entries = (); |
| 235 |
my @refs = (); |
| 236 |
my @labels = (); |
| 237 |
my %citations = (); |
| 238 |
my @bibcites = (); |
| 239 |
my $FIC; |
| 240 |
|
| 241 |
# Read the whole file to an array because label and end commands |
| 242 |
# generally need to be swapped. |
| 243 |
open($FIC, '<', $chkfile) or die("open: $!"); |
| 244 |
my @txt = <$FIC>; |
| 245 |
close($FIC); |
| 246 |
|
| 247 |
chk_parse(\@txt, \@entries, \@refs, \@labels, \%citations, \@bibcites); |
| 248 |
|
| 249 |
@labels = sort { $a->{line} cmp $b->{line} } @labels; |
| 250 |
@refs = sort { $a->{str} cmp $b->{str} } @refs; |
| 251 |
my $uniq_refs = rm_duplicate(\@refs); |
| 252 |
star_label(\@entries); |
| 253 |
#disp_msg( \@entries ); |
| 254 |
unused_label(\@labels, $uniq_refs); |
| 255 |
unused_cites(\@bibcites, \%citations); |
| 256 |
} |
| 257 |
|
| 258 |
sub usage { |
| 259 |
print << 'EOT'; |
| 260 |
Usage: chklref [options] texfile |
| 261 |
|
| 262 |
Check unused labels and bibitems in a LaTeX file |
| 263 |
|
| 264 |
Options: |
| 265 |
--tex <compiler>, -t Specify the TeX compiler to be used. Default is `pdflatex`. |
| 266 |
--tex-options List of options to pass to the TeX compiler. It should be a quoted |
| 267 |
string of white space delimited options. Note that we always add |
| 268 |
`-interaction nonstopmode` on top of these options. |
| 269 |
--debug, -d Run in debug mode. Do not clean the generated `.chk` file. |
| 270 |
--quiet, -q Run in quiet mode. Do not print the output of the TeX compiler. |
| 271 |
--parse-only Do not run the LaTeX compiler but use the already existing `.chk` file. |
| 272 |
When this option is passed, the following other options are meaningless: |
| 273 |
`--tex`, `--tex-options`, `--quiet`, `--debug`. |
| 274 |
--version, -v Print the version of this script. |
| 275 |
--help,h Print this help. |
| 276 |
|
| 277 |
|
| 278 |
Report bugs to https://github.com/jlelong/chklref. `chklref` is known not to work with `cleveref`. If you would like to contribute to `chklref`, feel free to open a PR on https://github.com/jlelong/chklref. |
| 279 |
EOT |
| 280 |
exit(0) |
| 281 |
} |
| 282 |
|
| 283 |
sub version { |
| 284 |
print << 'EOT'; |
| 285 |
chklref 3.1.2 |
| 286 |
|
| 287 |
Copyright 2005-2019 Jerome Lelong <[email protected]>. |
| 288 |
This program comes with ABSOLUTELY NO WARRANTY. |
| 289 |
This is free software, and you are welcome to redistribute it under GPLv3. |
| 290 |
|
| 291 |
Written by Jerome Lelong <[email protected]> |
| 292 |
EOT |
| 293 |
exit(0) |
| 294 |
} |
| 295 |
|
| 296 |
sub texcompile { |
| 297 |
my ($texfile, $basetexfile) = @_; |
| 298 |
print "Running $TEX $TEXOPTIONS $USERTEXOPTIONS \"$texfile\" to collect labels references and environment declarations\n\n"; |
| 299 |
my $texoutput = `$TEX $TEXOPTIONS $USERTEXOPTIONS "$texfile" 2>&1`; |
| 300 |
print($texoutput) unless $RUNQUIET; |
| 301 |
unlink($basetexfile . '.chk'); |
| 302 |
$texoutput = `$TEX $TEXOPTIONS $USERTEXOPTIONS --jobname "$basetexfile" '\\RequirePackage{chklref}\\input' "{$texfile}" 2>&1`; |
| 303 |
print($texoutput) unless $RUNQUIET; |
| 304 |
} |
| 305 |
|
| 306 |
Getopt::Long::Configure ("bundling"); |
| 307 |
GetOptions ( |
| 308 |
'version|v!' => \&version, |
| 309 |
'quiet|q!' => \$RUNQUIET, |
| 310 |
'debug|d!' => \$RUNDEBUG, |
| 311 |
'tex|t=s' => \$TEX, |
| 312 |
'texoptions=s' => \$USERTEXOPTIONS, |
| 313 |
'parse-only' => \$PARSEONLY, |
| 314 |
'help|h' => \&usage |
| 315 |
) || usage(); |
| 316 |
|
| 317 |
if (@ARGV != 1) { |
| 318 |
usage(); |
| 319 |
exit 0; |
| 320 |
} |
| 321 |
|
| 322 |
my $texfile = $ARGV[0]; |
| 323 |
my $cwd = getcwd(); |
| 324 |
my $dirtexfile = dirname($texfile); |
| 325 |
my $basetexfile = basename($texfile, '.tex'); |
| 326 |
my $chkfile = $basetexfile . '.chk'; |
| 327 |
chdir($dirtexfile); |
| 328 |
if ($PARSEONLY) { |
| 329 |
parse($chkfile); |
| 330 |
} |
| 331 |
else { |
| 332 |
texcompile($texfile, $basetexfile); |
| 333 |
parse($chkfile); |
| 334 |
if (! $RUNDEBUG) { |
| 335 |
unlink($chkfile); |
| 336 |
} |
| 337 |
} |