lib/LaTeXML/Post/XSLT.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 158 CC 20 SCAR 39.2
CC
20
Cov
0%
process
72.0
copyResource
72.0
new
42.0
CRAP 50.6
1 # /=====================================================================\ #
2 # | LaTeXML::Post::XSLT | #
3 # | Postprocessor for XSL Transform | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12  
13 package LaTeXML::Post::XSLT;
14 use strict;
15 use warnings;
16 use LaTeXML::Util::Pathname;
17 use LaTeXML::Common::XML;
18 use LaTeXML::Post;
19 use base qw(LaTeXML::Post::Processor);
20  
21 # Useful Options:
22 # stylesheet : path to XSLT stylesheet.
23 # noresources: removes resource requests so CSS/Javascript/etc
24 # are not copied, and they are not included in XSLT.
25 # parameters : hash of parameters to pass to stylesheet.
26 # Among which:
27 # CSS is a '|' separated list of paths
28 # ICON a favicon
29 # resource_directory a directory under top-level to put resources (css, js, etc)
30 # searchpaths : list of paths to search
31 sub new {
32 my ($class, %options) = @_;
33 my $self = $class->SUPER::new(%options);
34 my $stylesheet = $options{stylesheet};
35 Error('expected', 'stylesheet', undef, "No stylesheet specified!") unless $stylesheet;
36 if (!ref $stylesheet) {
37 $$self{name} .= '[using ' . $stylesheet . ']';
38 my $pathname = pathname_find($stylesheet,
39 types => ['xsl'], installation_subdir => 'resources/XSLT',
40 paths => $$self{searchpaths} || ['.']);
41 Error('missing-file', $stylesheet, undef, "No stylesheet '$stylesheet' found!")
42 unless $pathname && -f $pathname;
43 $stylesheet = $pathname; }
44 $stylesheet = $stylesheet && LaTeXML::Common::XML::XSLT->new($stylesheet);
45 if ((!ref $stylesheet) || !($stylesheet->can('transform'))) {
46 Error('expected', 'stylesheet', undef, "Stylesheet '$stylesheet' is not a usable stylesheet!"); }
47 $$self{stylesheet} = $stylesheet;
48 my %params = ();
49 %params = %{ $options{parameters} } if $options{parameters};
50 $$self{parameters} = {%params};
51 $$self{noresources} = $options{noresources};
52 $$self{resource_directory} = $options{resource_directory}; # ???
53 return $self; }
54  
55 sub process {
56 my ($self, $doc, $root) = @_;
57 return unless $$self{stylesheet};
58 # # Set up the Stylesheet parameters; making pathname parameters relative to document
59 my %params = %{ $$self{parameters} };
60 # Deal with any resources embedded within the document
61 if (my @resnodes = $doc->findnodes('//ltx:resource[@src]')) {
62 if ($$self{noresources}) {
63 $doc->removeNodes(@resnodes); }
64 else {
65 foreach my $node (@resnodes) {
66 my $src = $node->getAttribute('src');
67 my $path = $self->copyResource($doc, $src, $node->getAttribute('type'));
68 $node->setAttribute(src => $path) unless $path eq $src; } } }
69 if (my $css = $params{CSS}) {
70 $params{CSS} = '"' . join('|', map { $self->copyResource($doc, $_, 'text/css') } @$css) . '"'; }
71 if (my $js = $params{JAVASCRIPT}) {
72 $params{JAVASCRIPT}
73 = '"' . join('|', map { $self->copyResource($doc, $_, 'text/javascript') } @$js) . '"'; }
74 if (my $icon = $params{ICON}) {
75 # Hmm.... what type? could be various image types
76 $params{ICON} = '"' . $self->copyResource($doc, $icon, undef) . '"'; }
77 my $newdoc = $doc->new($$self{stylesheet}->transform($doc->getDocument, %params));
78 return $newdoc; }
79  
80 my $RESOURCE_INFO = { # [CONSTANT]
81 'text/css' => { extension => 'css', subdir => 'resources/CSS' },
82 'text/javascript' => { extension => 'js', subdir => 'resources/javascript' }
83 };
84 # Copy a resource file, if found, and return the relative path to it
85 # (in case it has been adjusted to be local to the destination document)
86 sub copyResource {
87 my ($self, $doc, $reqsrc, $type) = @_;
88 my $ext = $type && $$RESOURCE_INFO{$type}{extension};
89 my $resdir = $type && $$RESOURCE_INFO{$type}{subdir};
90 my @searchpaths => $doc->getSearchPaths;
91 # If the $reqsrc is a URL, no need to copy the resource, or modify the path to it.
92 if (pathname_is_url($reqsrc)) {
93 return $reqsrc; }
94 # Else find the file somewhere; in user's source or in distribution
95 elsif (my $path = pathname_find($reqsrc, ($ext ? (types => [$ext]) : ()),
96 paths => [@searchpaths],
97 ($resdir ? (installation_subdir => $resdir) : ()))) {
98 my $dest;
99 my ($dir, $name, $ex) = pathname_split($path);
100 # If a resource directory has been specified,
101 if (my $rd = $$self{resource_directory}) {
102 # Put resources in resource_directory, relative to site_directory
103 $dest = pathname_absolute(pathname_make(dir => $rd, name => $name, type => $ex),
104 $doc->getSiteDirectory); }
105 # Otherise, make an attempt to preserve the relative path to the requested resource
106 # Ie. same path from dest doc to copied resource, as from original doc to source resource.
107 # Get the path relative to user's source, and simulate that path in the destination
108 else {
109 my $relpath = pathname_relative($reqsrc, $doc->getSourceDirectory);
110 $dest = pathname_absolute($relpath, $doc->getSiteDirectory);
111 # Now IFF that is a valid relative path WITHIN the site directory, we'll use it.
112 # Otherwise, we'll place at toplevel in the destination.
113 if (!pathname_is_contained($dest, $doc->getSiteDirectory)) {
114 $dest = pathname_make(dir => $doc->getSiteDirectory, name => $name, type => $ex); } }
115 # Now, copy (unless in same place! happens a lot during testing!!!!)
116 pathname_copy($path, $dest) unless $path eq $dest;
117 # and return the relative path from the dest doc to the resource
118 return pathname_relative($dest, $doc->getDestinationDirectory); }
119 else {
120 warn "Couldn't find resource file $reqsrc in paths " . join(',', @searchpaths) . "\n";
121 return $reqsrc; } }
122  
123 # ================================================================================
124 1;
125