Skip to content

Commit

Permalink
Add f:resource() for adding resources from XSLT
Browse files Browse the repository at this point in the history
  • Loading branch information
xworld21 committed Aug 6, 2023
1 parent 65d23bc commit ba18668
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/LaTeXML/Common/XML/XSLT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@ sub transform {
my ($self, $document, %params) = @_;
return $$self{stylesheet}->transform($document, %params); }

sub register_function {
my ($self, $uri, $name, $subref) = @_;
return $$self{stylesheet}->register_function($uri, $name, $subref); }

#======================================================================
1;
8 changes: 8 additions & 0 deletions lib/LaTeXML/Post/XSLT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ sub process {
return unless $$self{stylesheet};
# # Set up the Stylesheet parameters; making pathname parameters relative to document
my %params = %{ $$self{parameters} };
# set up XSLT extension function f:_resource(src, mimetype)
$$self{stylesheet}->register_function('http://dlmf.nist.gov/LaTeXML/functions', '_resource', sub {
# values can be scalars or XML::LibXML::NodeList
my ($src, $mimetype) = @_;
return Warn('malformed', '', $self, 'f:_resource() requires at least one argument') unless defined $src;
# string interpolation converts node lists to the concatenation of all the string values
return $self->copyResource($doc, "$src", defined $mimetype ? ("$mimetype") : ());
});
# Deal with any resources embedded within the document
if (my @resnodes = $doc->findnodes('//ltx:resource[@src]')) {
if ($$self{noresources}) {
Expand Down
22 changes: 22 additions & 0 deletions lib/LaTeXML/resources/XSLT/LaTeXML-common.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@
<func:result><xsl:value-of select="$url"/></func:result>
</func:function>

<!-- Return the destination url of a resource: f:resource(src, mimetype?)
When using latexmlpost, f:resource() is translated to a call to
LaTeXML::Post::XSLT::copyResource, which (if $src is not a URL) copies
$src to the destination directory and returns the location of the copy
relative to the destination directory. $mimetype, if present, is used to
search for $src in the LaTeXML distribution as well.
With other XSLT processors, f:resource returns $src unchanged.
-->
<func:function name="f:resource">
<xsl:param name="src"/>
<xsl:param name="mimetype"/>
<xsl:choose>
<!-- latexmlpost exports copyResource as 'f:_resource' -->
<xsl:when test="function-available('f:_resource')">
<func:result select="f:_resource($src,$type)"/>
</xsl:when>
<xsl:otherwise>
<func:result select="$src"/>
</xsl:otherwise>
</xsl:choose>
</func:function>

<func:function name="f:class-pref">
<xsl:param name="prefix"/>
<xsl:param name="string"/>
Expand Down

0 comments on commit ba18668

Please sign in to comment.