Merge branch 'BZ20643' into 'master'
[kohadocs.git] / mappings.pl
1 use Modern::Perl;
2 use File::Slurp;
3
4 my @files = @ARGV;
5 my ( $mapping_anchors, $mapping_titles );
6
7 for my $file ( @files ) {
8     my $content = read_file( $file );
9     for my $line ( split "\n", $content ) {
10         chomp $line;
11         if ( $line =~ m|^`(?<title>.*)<#(?<old_anchor>.*)>`__$| ) {
12             my $old_anchor = $+{old_anchor};
13             my $title = $+{title};
14             my $new_anchor = lc $title;
15             $new_anchor =~ s|\s+$||;
16             $new_anchor =~ s|\s|-|g;
17             $new_anchor .= '-label';
18             $title =~ s|\s+$||;
19             say "$title + $old_anchor = $new_anchor";
20             $mapping_anchors->{$old_anchor} = { new_anchor => $new_anchor, title => $title };
21             $mapping_titles->{$title} = { new_anchor => $new_anchor, old_anchor => $old_anchor };
22         }
23     }
24 }
25
26 #   -`Currencies and Exchange Rates <#currexchangeadmin>`__
27 #   +.. _currexchangeadmin:
28 #   +
29 #   +Currencies and Exchange Rates
30 #
31 #   -      -  Currencies are assigned in the `Currencies & Exchange
32 #   -         Rates <#currexchangeadmin>`__ admin area
33 #   +      -  Currencies are assigned in the :ref:`Currencies & Exchange
34 #   +         Rates <currexchangeadmin>` admin area
35
36 for my $file ( @files ) {
37     my $content = read_file( $file );
38     for my $old_anchor ( keys %$mapping_anchors ) {
39         my $new_anchor = $mapping_anchors->{$old_anchor}{new_anchor};
40         my $title = $mapping_anchors->{$old_anchor}{title};
41         $content =~ s|\n`([^`]+)\s<#$old_anchor>`__\n([\Q~=-'^\E]+)\n|\n.. _$new_anchor:\n\n$1\n$2\n|g; # Header titles, add a reference label (same as would have been the default one)
42         $content =~ s|\n`$title`\n([~=-^']+)\n|\n.. _$new_anchor:\n\n$title\n$1\n|g; # Header titles that have already been fixed
43         $content =~ s|`$title\s<#$old_anchor>`__|:ref:`$title`|g; #If link title is the same as the header title, remove the header title
44         $content =~ s|`([^`]+)\s<#$old_anchor>`__|:ref:`$1 <$new_anchor>`|g; # If link is different that header title
45     }
46     write_file($file, $content);
47 }
48