xref: /aoo41x/main/solenv/bin/convertlinks.pl (revision 841807c9)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21#
22# convertlinks - a perl script to make hrefs to
23# http://api.openoffice.org/common/ref relativ.
24#
25
26use File::Find;
27
28# for the convenience of &wanted calls, including -eval statements:
29use vars qw/*name *dir/;
30*name   = *File::Find::name;
31*dir    = *File::Find::dir;
32@files = ();
33
34if($#ARGV == 1)
35{
36	$pattern = "www";
37} else
38{
39	$pattern = $ARGV[2];
40}
41
42find(\&wanted, "$ARGV[0]");
43
44$return = 1;
45
46foreach $i (@files)
47{
48	next if( $i->{directory} =~ /.*common((\/|\\)ref(.*))/ ||
49		 $i->{directory} =~ /.*cpp((\/|\\)ref(.*))/ ||
50		 $i->{directory} =~ /.*java((\/|\\)ref(.*))/ );
51
52	open ( FILEIN, $i->{filename} ) || die "could not open $i->{filename} for reading";
53
54	$relPath = ".";
55	$relToSource = ".";
56	if( $i->{directory} =~ /.*$pattern((\/|\\)(.*))/ )
57	{
58		$relPath = $3;
59		$relPath =~ s#\w+#\.\.#go;
60		if($pattern eq "examples")
61		{
62			$relPath = "\.\.\/$relPath";
63		}
64		if($pattern eq "www")
65		{
66			$relToSource = "\.\.\/$relPath";
67		} else
68		{
69			$relToSource = $relPath;
70		}
71	} else
72	{
73		if($pattern eq "examples")
74		{
75			$relPath = "\.\.";
76		}
77		if($pattern eq "www")
78		{
79			$relToSource = "\.\.";
80		} else
81		{
82			$relToSource = $relPath;
83		}
84	}
85
86	@lines = <FILEIN>;
87	close( FILEIN );
88	open( FILEOUT, ">$i->{filename}.tmp" ) || die "could not open $i->{filename} for writing";
89	foreach $_ (@lines)
90	{
91		# change the refenreces to the index in dependency of UDK or ODK
92		if("$ARGV[1]" eq "udk_" | "$ARGV[1]" eq "odk_")
93		{
94			s#((\")(index.html\"))#$2$ARGV[1]$3#go;
95			s#((\/|\")(faq.html\"))#$2$ARGV[1]$3#go;
96			s#((\/|\")(bylaws.html\"))#$2$ARGV[1]$3#go;
97		}
98
99		s#((http:\/\/api\.openoffice\.org\/)(common\/ref[^\"]+))#$relPath\/$3#go;
100		s#((http:\/\/api\.openoffice\.org\/unbranded-source\/)(.*)(examples\/examples.html))#$relToSource\/$4#go;
101
102		if($pattern eq "examples")
103		{
104			# change the links for the C++/Java examples in the ODK
105			s#((http:\/\/api\.openoffice\.org\/source\/browse\/api\/odk\/examples\/)(java\/*))#$3#go;
106			s#((http:\/\/api\.openoffice\.org\/source\/browse\/api\/odk\/examples\/)(cpp\/*))#$3#go;
107			s#((http:\/\/api\.openoffice\.org\/source\/browse\/api\/odk\/examples\/)(basic\/*))#$3#go;
108			s#((http:\/\/api\.openoffice\.org\/source\/browse\/api\/odk\/examples\/)(OLE\/*))#$3#go;
109
110			# change link api specific stuff
111			s#((http:\/\/api\.openoffice\.org\/)(design_guide.html))#$relPath\/www\/$3#go;
112			s#(http:\/\/api\.openoffice\.org\/index.html)#$relPath\/www\/odk_index.html#go;
113
114			# change the links for the C++ examples in the UDK
115			s#((http:\/\/udk\.openoffice\.org\/source\/browse\/udk\/product\/examples\/)(cpp\/*))#$3#go;
116
117			# change the links to udk.openoffice.org to relativ links
118			s#(http:\/\/udk\.openoffice\.org\/index.html)#$relPath\/www\/udk_index.html#go;
119			s#((http:\/\/udk\.openoffice\.org)(\/*))#$relPath\/www$3#go;
120
121			# change the link to tutorial
122			s#((http:\/\/api\.openoffice\.org\/)(basic\/man\/tutorial\/tutorial.pdf))#$relPath\/www\/$3#go;
123		}
124		print FILEOUT $_;
125	}
126	close FILEOUT;
127	chmod 0666, $i->{filename};
128	rename "$i->{filename}.tmp", $i->{filename} || die "could not rename $i->{filename}.tmp to $i->{filename}";
129	$return = 0;
130}
131
132exit $return;
133
134sub wanted {
135    %file = (
136	     directory => $dir,
137	     filename  => $name
138	     );
139    push @files, {%file} if /^.*\.html\z/s;
140}
141