summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/nxhtml/html-wtoc/PerlLib/PathSubs.pm
blob: e95b8d5f707334234c6fc6643418f45bf2a35690 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Copyright 2006 Lennart Borgman, http://OurComments.org/. All rights
# reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

package PathSubs;

#####################################################
###     This package contains general path handling
###     routines and some win32 specific dito.
### The latter should ev be moved to a new module!
#####################################################
use strict;

use File::Spec;

### Absolute path names

sub is_abs_path ($) {
   my $path = shift;
   return 0 if $path eq "";
   return 1 if File::Spec->file_name_is_absolute($path);
   #return 1 if substr($path, 1, 1) eq ":";     # MSWin32
   #return 1 if substr($path, 0, 1) eq "/";
   return 1 if $path =~ /^https?:/i;
   return 1 if $path =~ /^file:/i;
   return 1 if $path =~ /^javascript:/i;
   return 1 if $path =~ /^mailto:/i;
}
sub is_abs_netpath($) {
   my $path = shift;
   return 1 if $path =~ /^https?:/i;
   # New
   return 1 if $path =~ /^ftp:/i;
   return 1 if $path =~ /^mailto:/i;
}


sub uniq_file($) {
        my $fname = shift;
        $fname =~ s!^\s+|\s+$!!g;
        return "" if ($fname eq "");
        $fname = File::Spec->rel2abs($fname);
        if (!File::Spec->file_name_is_absolute($fname)) {
            die "File name is not absolute: $fname";
        }
        #print STDERR "uniq_file($fname)\n";
        $fname =~ tr!\\!/!;
        if (-e $fname)  {
                #print STDERR "exists $fname\n";
                ### There is an error in 522, compensate for this!
                #die substr($fname, -1);
                if (substr($fname, -1) eq "/") { chop $fname; }
                #print STDERR "exists $fname\n";
                ### Translate ..
                if (substr($fname, 1, 1) eq ":") {
                    my $ffname = Win32::GetFullPathName($fname);
                    ### Get case
                    my $lfname = Win32::GetLongPathName($ffname);
                    #print STDERR "lexists $lfname\n";
                    $fname = $lfname if ($lfname ne "");
                }
        } else {
                #print STDERR "NOT exists $fname\n";
                if (substr($fname, -1) eq "/") { chop $fname; }
                my $head = "";
                if (substr($fname, 0, 2) eq "//") {
                        $head = "//";
                        $fname = substr($fname, 2);
                }
                my @fname = split("/", $fname);
                my $tail = pop @fname;
                $fname = uniq_dir($head . join("/", @fname)) . $tail;
        }
        if (substr($fname, 1, 1) eq ":") {
                $fname = uc(substr($fname, 0, 1)) . substr($fname, 1);
                #print STDERR "fname $fname\n";
        }
        $fname =~ tr!\\!/!;
                #print STDERR "fname ($fname)\n";
        return $fname;
}
sub uniq_dir($) {
        my $dir = shift;
        my $uq_dir = uniq_file($dir);
        if (substr($uq_dir, -1) ne "/") { $uq_dir .= "/"; }
        return $uq_dir;
}



### Relative paths
sub _get_link_root($) {
        my $lnk = shift;
        if ($lnk =~ m!^(/|ftp://[^/]*|https?://[^/]*|[a-z]:/)!i) {
                return $1;
        } else {
                return "";
        }
}

sub resolve_dotdot($) {
        my $orig_url = shift;
        my $root = _get_link_root($orig_url);
        return $orig_url if length($root) == length($orig_url);
        my $url = substr($orig_url, length($root));
        if (substr($root, -1) eq "/") {
                chop $root;
                $url = "/$url";
        }
        #die "$root\n$url";
        my $iPosSearch = 2;
        #print "url=$url\n";
        while ((my $iPos = index($url, "/../", $iPosSearch)) > -1) {
                my $sLeft = substr($url, 0, $iPos);
                if (substr($sLeft, -2) eq "..") {
                        $iPosSearch += 3;
                        next;
                }
                my $sRight = substr($url, $iPos+3);
                #print "url=$url\n";
                #print "iPos=$iPos\n";
                #print "sLeft=$sLeft\n";
                $sLeft =~ s!/[^/]*$!!;
                #print "sLeft=$sLeft\n";
                #print "sRight=$sRight\n";
                $url = $sLeft . $sRight;
                #print "\t***url=$url\n";
                #print "url=$url\n";
        }
        if (index($url, "../") > -1) {
                return $orig_url;
        }
        return $root . $url;
}

sub mk_relative_link($$;$) {
    my $from = shift;
    my $to   = shift;
    my $norm = shift;
    if ($norm) {
        $from = uniq_file($from);
        $to   = uniq_file($to);
    }
    if (-e $from) {
        $from = uniq_file($from);
    } else {
        $from = resolve_dotdot($from);
    }
    if (-e $to) {
        $to   = uniq_file($to);
    } else {
        $to = resolve_dotdot($to);
    }
    my $root_from = _get_link_root($from);
    my $root_to   = _get_link_root($to  );
    if ($root_from ne $root_to) {
        return $to;
    }
    my @from = split "/", $from;
    my @to   = split "/", $to;
    while (@to) {
        last if ($to[0] ne $from[0]);
        shift @to;
        shift @from;
    }
    if (@to == 1 && @from == 1) {
        if (length($to[0]) > length($from[0])) {
                        if (substr($to[0], 0, length($from[0])+1) eq ($from[0] . "#")) {
                                return substr($to[0], length($from[0]));
                        }
        }
    }
    my $rl;
    for (1..$#from) { $rl .= "../"; }
    $rl .= join("/", @to);

    return $rl;
}



sub mk_absolute_link($$) {
        my $from   = shift;
        my $rel_to = shift;
        my $abs = $from;
        $abs =~ s![^/]*$!!;
        $abs .= $rel_to;
        if (!is_abs_netpath($abs)) { $abs = uniq_file($abs); }
        $abs;
}


1;