Subversion Repositories DevTools

Rev

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