-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenumber-topics.pl
executable file
·71 lines (56 loc) · 1.62 KB
/
renumber-topics.pl
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
#!/usr/bin/env perl
#
# Fix a toctree.rst file and change the file names of the referenced files so
# the topic numbers in the file names line up with what Sphinx is going to
# assign. This script will git mv the files to renae them and will edit the
# toctree.rst in place and git add it.
#
# Run with -u it will strip the numbering from the files which may be a good
# idea while you are moving things around to avoid constantly renaming things
# just because you insterted a file in the toc. Then at the end you can run it
# without -u to get numbers put back in.
#
use warnings;
use strict;
use File::Basename;
my $unnumber = 0;
if ($ARGV[0] =~ /--?u/) {
print "Unnumbering\n";
$unnumber = 1;
shift;
}
my $tocfile = shift || die "Need tocfile.";
my $dir = dirname($tocfile);
#open(my $in, $tocfile) or die "$!: Can't open $tocfile";
if ($dir =~ /Unit-?(\d+)-/) {
my $unit = $1;
my $n = 1;
my $in_toc;
our @ARGV = ($tocfile);
$^I = '';
while (<>) {
if (/^\.\. toctree\:/) {
$in_toc = 1;
} elsif ($in_toc) {
if (/^\S/) {
$in_toc = 0;
} elsif (/^(\s+)(?!:)(\S+)/) {
my $indent = $1;
my $oldname = $2;
my ($base) = $oldname =~ /^topic-(?:\d+-\d+-)?(.*)/g;
if ($base) {
my $newname = $unnumber ? "topic-$base" : "topic-$unit-$n-$base";
$n++;
$_ = "$indent$newname\n";
if ($newname ne $oldname) {
system("git mv $dir/$oldname $dir/$newname") == 0 or die $!;
}
}
}
}
print;
}
system("git add $tocfile") == 0 or die $!;
} else {
die "Can't figure out unit from $dir";
}