| 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 toProcess { | ||
| 31 | my ($self, $doc) = @_; | ||
| 32 | return $$doc{document}; } # Presumably can print anything? | ||
| 33 | |||
| 34 | sub process { | ||
| 35 | my ($self, $doc, $root) = @_; | ||
| 36 | |||
| 37 | my $xmldoc = $doc->getDocument; | ||
| 38 | $doc->getDocument->removeInternalSubset if $$self{omit_doctype}; | ||
| 39 | |||
| 40 | $root->removeAttribute('xml:id') | ||
| 41 | if isElementNode($root) && ($root->hasAttribute('xml:id') || '') eq 'TEMPORARY_DOCUMENT_ID'; | ||
| 42 | |||
| 43 | # Note that this will NOT RE-format a document read in from xml, | ||
| 44 | # (providing no_blanks is false; which it should be, since it is dangerous. | ||
| 45 | # it can also remove significant spaces between elements!) | ||
| 46 | my $serialized = ($xmldoc->documentElement # Is some sort of DOM? | ||
| 47 | ? ($$self{is_html} ? $xmldoc->toStringHTML : $xmldoc->toString(1)) | ||
| 48 | : $xmldoc->textContent); | ||
| 49 | # NOTE that we are serializing the XML::LibXML::Document whose toString | ||
| 50 | # has ALREADY encoded (in this case to utf8), so NO encode is needed! | ||
| 51 | if (my $destination = $doc->getDestination) { | ||
| 52 | my $destdir = $doc->getDestinationDirectory; | ||
| 53 | pathname_mkdir($destdir) | ||
| 54 | or return Fatal('I/O', $destdir, undef, "Couldn't create directory '$destdir'", | ||
| 55 | "Response was: $!"); | ||
| 56 | my $OUT; | ||
| 57 | open($OUT, '>', $destination) | ||
| 58 | or return Fatal('I/O', $destdir, undef, "Couldn't write '$destination'", "Response was: $!"); | ||
| 59 | print $OUT $serialized; | ||
| 60 | close($OUT); } | ||
| 61 | else { | ||
| 62 | print $serialized; } | ||
| 63 | return $doc; } | ||
| 64 | |||
| 65 | # ================================================================================ | ||
| 66 | 1; |