Subversion Repositories DevTools

Rev

Rev 5542 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
361 dpurdie 1
package JatsDocTools::Tree::HTML;
2
 
3
use strict;
4
use warnings;
5
 
6
use Cwd qw(cwd);
7
use File::Basename qw(dirname);
8
use File::Path qw(mkpath);
9
use Pod::Find qw(pod_find);
6177 dpurdie 10
use File::Spec::Functions;
361 dpurdie 11
 
12
use JatsDocTools::Pod qw(pod2html pod2html_remove_cache_files);
13
 
14
sub _relative_path {
15
    my($path, $prefix) = @_;
16
    $path =~ s,\\,/,g if $^O eq "MSWin32";
17
    $path =~ s,/\z,, unless $path =~ m,^([A-Za-z]:)?/\z,;
18
 
19
    if (defined $prefix && length $prefix) {
20
    $prefix =~ s,\\,/,g if $^O eq "MSWin32";
21
        $prefix =~ s,/\z,, unless $prefix =~ m,^([A-Za-z]:)?/\z,;
22
 
23
    my @path_parts   = split('/', $path);
24
    my @prefix_parts = split('/', $prefix);
25
    return $path if @path_parts < @prefix_parts;
26
 
27
    while (@prefix_parts) {
28
        my $path_part   = shift(@path_parts);
29
        my $prefix_part = shift(@prefix_parts);
30
        if ($^O eq "MSWin32") {
31
        $_ = lc for $path_part, $prefix_part;
32
        }
33
        return $path unless $path_part eq $prefix_part;
34
    }
35
 
36
    $path = join('/', @path_parts) || ".";
37
    }
38
    return $path;
39
}
40
 
41
sub Update {
42
    my %args = @_;
43
 
44
    my $prefix  = $args{prefix}  || croak("Required prefix argument missing");
45
    my $htmldir = $args{htmldir} || croak("Required htmldir argument missing");
46
    my $podpath = $args{podpath} || croak("Required podpath argument missing");
47
    my $cacheDir = $args{cacheDir} || croak("Required cacheDir argument missing");
48
    my $index = $args{index} || 0;
49
 
50
    my $starting_cwd = cwd();
51
    unless (chdir($prefix)) {
52
        warn "Can't chdir to root of target installation: $!\n";
53
        return;
54
    }
55
 
56
    print "Building HTML tree at $htmldir, cwd is $prefix\n" if $args{verbose};
57
 
58
    pod2html_remove_cache_files($cacheDir)
59
        if ( $args{force} );
60
 
6177 dpurdie 61
    # Need absolule paths for pod_find.
62
    #   If in a symlink directory, then relative paths get confused
63
    my @podpathAbs = map { catdir($prefix, $_) } @$podpath;
64
    my %pods = pod_find(@podpathAbs);
361 dpurdie 65
    @$podpath = map { _relative_path($_, $prefix) } @$podpath;
66
 
67
    foreach my $key (sort keys %pods) {
68
 
69
        my $in_file = _relative_path($key, $prefix);
70
        if ( $in_file =~ m~TOOLS/LIB~ )
71
        {
4779 dpurdie 72
            unless ( isaJatsKeyPod($in_file) ) {
73
                print "Ignoring $in_file\n" if $args{verbose};
74
                next;
75
            }
361 dpurdie 76
        }
77
 
78
        my $out_file = "$htmldir/$in_file";
79
        $out_file =~ s/\.[a-z]+\z|\z/.html/i;
80
        if ($args{force} || !-e $out_file || (stat $in_file)[9] > (stat $out_file)[9]) {
81
            print "Making $out_file from $in_file => $pods{$key}\n"
82
                if $args{verbose};
83
 
84
            unlink($out_file);
85
            my $out_dir = dirname($out_file);
86
            mkpath($out_dir);
87
            my $depth = $in_file =~ tr,/,,;
88
            pod2html(infile  => $in_file,
89
                     outfile => $out_file,
90
                     depth   => $depth,
91
                     podroot => ".",
92
                     podpath => $podpath,
93
                     cacheDir => $cacheDir,
94
                     index   => $index,
95
                     );
96
        }
97
        else {
98
            print "Skipping $out_file\n" if $args{verbose};
99
        }
100
    }
101
 
102
#    pod2html_remove_cache_files($cacheDir);
103
    chdir($starting_cwd) or die "Can't chdir back to '$starting_cwd': $!";
104
}
105
 
4779 dpurdie 106
#-------------------------------------------------------------------------------
107
# Function        : isaJatsKeyPod  
108
#
109
# Description     : Test a specified file to see if its a Jats Key Documentation file
110
#                   Used to filter in POD inn cases where it might outherwise be excluded
111
#
112
#                   The test is simple
113
#                   If the file contains a POD section and that section contains the keyword
114
#                       =for htmltoc
115
#                   Then it is assumed that this file conatins JATS POD
116
#
117
# Inputs          : $fname      - Path to file to examine
118
#
119
# Returns         : True        - Include this file
120
#
121
 
122
sub isaJatsKeyPod
123
{
124
    my ($fname) = @_;
125
    my $podSeen;
126
    my $jatsKeyPod = 0;
127
 
128
    open (my $file, '<', $fname) || return 0;
129
    while (<$file>) 
130
    {
131
        if ( ! $podSeen) {
132
            if (m~^=pod~) {
133
                $podSeen = 1;
134
            }
135
            next;
136
        }
137
 
138
        if (m~^=head~) {
139
            last;
140
        }
141
 
142
        if ( m~^=for\s+htmltoc~ )
143
        {
144
            $jatsKeyPod = 1;
145
            last;
146
        }
147
    }
148
 
149
    close $file;
150
    return $jatsKeyPod;
151
}
152
 
361 dpurdie 153
1;