source: vendor/python/2.5/Doc/tools/node2label.pl

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 1.9 KB
Line 
1#! /usr/bin/env perl
2
3# On Cygwin, we actually have to generate a temporary file when doing
4# the inplace edit, or we'll get permission errors. Not sure who's
5# bug this is, except that it isn't ours. To deal with this, we
6# generate backups during the edit phase and remove them at the end.
7#
8use English;
9$INPLACE_EDIT = '.bak';
10
11# read the labels, then reverse the mappings
12require "labels.pl";
13
14%nodes = ();
15my $key;
16# sort so that we get a consistent assignment for nodes with multiple labels
17foreach $label (sort keys %external_labels) {
18 #
19 # If the label can't be used as a filename on non-Unix platforms,
20 # skip it. Such labels may be used internally within the documentation,
21 # but will never be used for filename generation.
22 #
23 if ($label =~ /^([-.a-zA-Z0-9]+)$/) {
24 $key = $external_labels{$label};
25 $key =~ s|^/||;
26 $nodes{$key} = $label;
27 }
28}
29
30# This adds the "internal" labels added for indexing. These labels will not
31# be used for file names.
32require "intlabels.pl";
33foreach $label (keys %internal_labels) {
34 $key = $internal_labels{$label};
35 $key =~ s|^/||;
36 if (defined($nodes{$key})) {
37 $nodes{$label} = $nodes{$key};
38 }
39}
40
41# collect labels that have been used
42%newnames = ();
43
44while (<>) {
45 # don't want to do one s/// per line per node
46 # so look for lines with hrefs, then do s/// on nodes present
47 if (/(HREF|href)=[\"\']node\d+\.html[\#\"\']/) {
48 @parts = split(/(HREF|href)\=[\"\']/);
49 shift @parts;
50 for $node (@parts) {
51 $node =~ s/[\#\"\'].*$//g;
52 chomp($node);
53 if (defined($nodes{$node})) {
54 $label = $nodes{$node};
55 if (s/(HREF|href)=([\"\'])$node([\#\"\'])/href=$2$label.html$3/g) {
56 s/(HREF|href)=([\"\'])$label.html/href=$2$label.html/g;
57 $newnames{$node} = "$label.html";
58 }
59 }
60 }
61 }
62 print;
63}
64
65foreach $oldname (keys %newnames) {
66 rename($oldname, $newnames{$oldname});
67}
68
69foreach $filename (glob('*.bak')) {
70 unlink($filename);
71}
Note: See TracBrowser for help on using the repository browser.