LaTeXML
view release on metacpan or search on metacpan
lib/LaTeXML/Util/Pack.pm view on Meta::CPAN
my $imagesrc = $math->getAttribute('imagesrc') || $math->getAttribute('src');
if ($imagesrc && $imagesrc =~ /[.]svg$/) {
# Return the SVG directly
$math = LaTeXML::Common::XML::Parser->new()->parseFile($imagesrc);
$math = $math && $math->getDocumentElement;
}
# Copy over document namespace declarations:
# NOTE: This copies ALL the namespaces, not just the needed ones!
foreach ($doc->getDocumentElement->getNamespaces) {
$math->setNamespace($_->getData, $_->getLocalName, 0); } }
return $math; }
sub get_embeddable {
my ($doc) = @_;
return unless defined $doc;
my ($embeddable) = $doc->findnodes('//*[contains(@class,"ltx_document")]');
if ($embeddable) {
# Only one child? Then get it, must be a inline-compatible one!
while (($embeddable->nodeName eq 'div') && (scalar(@{ $embeddable->childNodes }) == 1) &&
($embeddable->getAttribute('class') =~ /^ltx_(page_(main|content)|document|para|header)$/) &&
(!defined $embeddable->getAttribute('style'))) {
if (defined $embeddable->firstChild) {
$embeddable = $embeddable->firstChild; }
else {
last; }
}
# Is the root a <p>? Make it a span then, if it has only math/text/spans - it should be inline
# For MathJax-like inline conversion mode
# TODO: Make sure we are schema-complete wrt nestable inline elements, and maybe find a smarter way to do this?
if (($embeddable->nodeName eq 'p') && ((@{ $embeddable->childNodes }) == (grep { $_->nodeName =~ /math|text|span/ } $embeddable->childNodes))) {
$embeddable->setNodeName('span');
$embeddable->setAttribute('class', 'text');
}
# Copy over document namespace declarations:
foreach ($doc->getDocumentElement->getNamespaces) {
$embeddable->setNamespace($_->getData, $_->getLocalName, 0);
}
# Also, copy RDFa attributes:
foreach my $rdfa_attr (qw(prefix property content resource about typeof rel rev datatype)) {
if (my $rdfa_value = $doc->getDocumentElement->getAttribute($rdfa_attr)) {
$embeddable->setAttribute($rdfa_attr, $rdfa_value); } }
}
return $embeddable || $doc; }
1;
__END__
=pod
=head1 NAME
C<LaTeXML::Util::Pack> - smart packing and unpacking of TeX archives
=head1 DESCRIPTION
This module provides an API and convenience methods for:
1. Unpacking Zip archives which contain a TeX manuscript.
2. Packing the files of a LaTeXML manuscript into a single archive
3. Extracting embeddable fragments, as well as single formulas from LaTeXML documents
All user-level methods are unconditionally exported by default.
=head2 METHODS
=over 4
=item C<< $main_tex_source = unpack_source($archive,$extraction_directory); >>
Unpacks a given $archive into the $extraction_directory. Next, perform a
heuristic analysis to determine, and return, the main file of the TeX manuscript.
If the main file cannot be determined, the $extraction_directory is removed and undef is returned.
In this regard, we implement a simplified form of the logic in
TeX::AutoTeX and particularly arXiv::FileGuess
=item C<< @packed_documents = pack_collection(collection=>\@documents, whatsout=>'math|fragment|archive', siteDirectory=>$path); >>
Packs a collection of documents using the packing method specified via the 'whatsout' option.
If 'fragment' or 'math' are chosen, each input document is transformed into
an embeddable fragment or a single formula, respectively.
If 'archive' is chose, all input documents are written into an archive in the specified 'siteDirectory'.
The name of the archive is provided by the 'destination' property of the first provided $document object.
Each document is expected to be a LaTeXML::Post::Document object.
=back
=head1 AUTHOR
Bruce Miller <bruce.miller@nist.gov>,
Deyan Ginev <deyan.ginev@nist.gov>
=head1 COPYRIGHT
Public domain software, produced as part of work done by the
United States Government & not subject to copyright in the US.
=cut
( run in 1.858 second using v1.01-cache-2.11-cpan-437f7b0c052 )