sub
new {
my
(
$class
,
%options
) =
@_
;
my
$self
=
$class
->SUPER::new(
%options
);
my
$stylesheet
=
$options
{stylesheet};
Error(
'expected'
,
'stylesheet'
,
undef
,
"No stylesheet specified!"
)
unless
$stylesheet
;
if
(!
ref
$stylesheet
) {
$$self
{name} .=
'[using '
.
$stylesheet
.
']'
;
my
$pathname
= pathname_find(
$stylesheet
,
types
=> [
'xsl'
],
installation_subdir
=>
'resources/XSLT'
,
paths
=>
$$self
{searchpaths} || [
'.'
]);
Error(
'missing-file'
,
$stylesheet
,
undef
,
"No stylesheet '$stylesheet' found!"
)
unless
$pathname
&& -f
$pathname
;
$stylesheet
=
$pathname
; }
$stylesheet
=
$stylesheet
&& LaTeXML::Common::XML::XSLT->new(
$stylesheet
);
if
((!
ref
$stylesheet
) || !(
$stylesheet
->can(
'transform'
))) {
Error(
'expected'
,
'stylesheet'
,
undef
,
"Stylesheet '$stylesheet' is not a usable stylesheet!"
); }
XML::LibXSLT->max_depth(1000);
$$self
{stylesheet} =
$stylesheet
;
my
%params
= ();
%params
= %{
$options
{parameters} }
if
$options
{parameters};
$$self
{parameters} = {
%params
};
$$self
{noresources} =
$options
{noresources};
$$self
{resource_directory} =
$options
{resource_directory};
return
$self
; }
sub
process {
my
(
$self
,
$doc
,
$root
) =
@_
;
return
unless
$$self
{stylesheet};
my
%params
= %{
$$self
{parameters} };
if
(
my
@resnodes
=
$doc
->findnodes(
'//ltx:resource[@src]'
)) {
if
(
$$self
{noresources}) {
$doc
->removeNodes(
@resnodes
); }
else
{
foreach
my
$node
(
@resnodes
) {
my
$src
=
$node
->getAttribute(
'src'
);
my
$path
=
$self
->copyResource(
$doc
,
$src
,
$node
->getAttribute(
'type'
));
$node
->setAttribute(
src
=> pathname_to_url(
$path
))
unless
$path
eq
$src
; } } }
if
(
my
$css
=
$params
{CSS}) {
$params
{CSS} =
'"'
.
join
(
'|'
,
map
{
$self
->copyResource(
$doc
,
$_
,
'text/css'
) }
@$css
) .
'"'
; }
if
(
my
$js
=
$params
{JAVASCRIPT}) {
$params
{JAVASCRIPT}
=
'"'
.
join
(
'|'
,
map
{
$self
->copyResource(
$doc
,
$_
,
'text/javascript'
) }
@$js
) .
'"'
; }
if
(
my
$icon
=
$params
{ICON}) {
$params
{ICON} =
'"'
.
$self
->copyResource(
$doc
,
$icon
,
undef
) .
'"'
; }
my
$newdoc
=
$doc
->new(
$$self
{stylesheet}->transform(
$doc
->getDocument,
%params
));
return
$newdoc
; }
my
$RESOURCE_INFO
= {
'text/css'
=> {
extension
=>
'css'
,
subdir
=>
'resources/CSS'
},
'text/javascript'
=> {
extension
=>
'js'
,
subdir
=>
'resources/javascript'
}
};
sub
copyResource {
my
(
$self
,
$doc
,
$reqsrc
,
$type
) =
@_
;
my
$ext
=
$type
&&
$$RESOURCE_INFO
{
$type
}{extension};
my
$resdir
=
$type
&&
$$RESOURCE_INFO
{
$type
}{subdir};
my
@searchpaths
= (
$doc
->getSearchPaths, (
$$self
{searchpaths} ? @{
$$self
{searchpaths} } : ()));
if
(pathname_is_url(
$reqsrc
)) {
return
$reqsrc
; }
elsif
(
my
$path
= pathname_find(
$reqsrc
, (
$ext
? (
types
=> [
$ext
]) : ()),
paths
=> [
@searchpaths
],
(
$resdir
? (
installation_subdir
=>
$resdir
) : ()))) {
my
$dest
;
my
(
$dir
,
$name
,
$ex
) = pathname_split(
$path
);
if
(
my
$rd
=
$$self
{resource_directory}) {
$dest
= pathname_absolute(pathname_make(
dir
=>
$rd
,
name
=>
$name
,
type
=>
$ex
),
$doc
->getSiteDirectory); }
else
{
my
$relpath
= pathname_relative(
$reqsrc
,
$doc
->getSourceDirectory);
$dest
= pathname_absolute(
$relpath
,
$doc
->getSiteDirectory);
if
(!pathname_is_contained(
$dest
,
$doc
->getSiteDirectory)) {
$dest
= pathname_make(
dir
=>
$doc
->getSiteDirectory,
name
=>
$name
,
type
=>
$ex
); } }
pathname_copy(
$path
,
$dest
)
unless
$path
eq
$dest
;
return
pathname_relative(
$dest
,
$doc
->getDestinationDirectory); }
else
{
Warn(
'missing_file'
,
$reqsrc
,
$self
,
"Couldn't find resource file $reqsrc in paths "
.
join
(',',
@searchpaths
));
return
$reqsrc
; } }
1;