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