lib/LaTeXML/Post/Writer.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 73 CC 7 SCAR 25.9
CC
7
Cov
0%
process
30.0
new
12.0
BEGIN
2.0
CRAP 13.3
1 # /=====================================================================\ #
2 # | LaTeXML::Post::Writer | #
3 # | Write file to output | #
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::Writer;
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 use Encode;
21  
22 sub new {
23 my ($class, %options) = @_;
24 my $self = $class->SUPER::new(%options);
25 $$self{format} = ($options{format} || 'xml');
26 $$self{omit_doctype} = 1 if $options{omit_doctype};
27 $$self{is_html} = 1 if $options{is_html};
28 return $self; }
29  
30 sub process {
31 my ($self, $doc, $root) = @_;
32  
33 my $xmldoc = $doc->getDocument;
34 $doc->getDocument->removeInternalSubset if $$self{omit_doctype};
35  
36 $root->removeAttribute('xml:id')
37 if ($root->getAttribute('xml:id') || '') eq 'TEMPORARY_DOCUMENT_ID';
38  
39 # Note that this will NOT RE-format a document read in from xml,
40 # (providing no_blanks is false; which it should be, since it is dangerous.
41 # it can also remove significant spaces between elements!)
42 my $serialized = ($$self{is_html} ? $xmldoc->toStringHTML : $xmldoc->toString(1));
43 # NOTE that we are serializing the XML::LibXML::Document whose toString
44 # has ALREADY encoded (in this case to utf8), so NO encode is needed!
45 if (my $destination = $doc->getDestination) {
46 my $destdir = $doc->getDestinationDirectory;
47 pathname_mkdir($destdir)
48 or return Fatal('I/O', $destdir, undef, "Couldn't create directory '$destdir'",
49 "Response was: $!");
50 my $OUT;
51 open($OUT, '>', $destination)
52 or return Fatal('I/O', $destdir, undef, "Couldn't write '$destination'", "Response was: $!");
53 print $OUT $serialized;
54 close($OUT); }
55 else {
56 print $serialized; }
57 return $doc; }
58  
59 # ================================================================================
60 1;
61