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