| 1 | package Text::Restructured::Writer::LibXML; | ||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use XML::LibXML; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | $Text::Restructured::Writer::LibXML::VERSION='0.01'; | |||||||||||||
| 7 | |||||||||||||||
| 8 | =head1 NAME | ||||||||||||||
| 9 | |||||||||||||||
| 10 | Text::Restructured::Writer::LibXML | ||||||||||||||
| 11 | |||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||
| 13 | |||||||||||||||
| 14 | use Text::Restructured; | ||||||||||||||
| 15 | use Text::Restructured::Writer::LibXML; | ||||||||||||||
| 16 | |||||||||||||||
| 17 | my $parser=Text::Restructured->new($opts,'gino'); | ||||||||||||||
| 18 | my $dudom=$parser->Parse($input,$filename); | ||||||||||||||
| 19 | my $xdoc=Text::Restructured::Writer::LibXML->new->ProcessDOM($dudom); | ||||||||||||||
| 20 | |||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||
| 22 | |||||||||||||||
| 23 | This module implements a "Writer" for L<Text::Restructured>, that | ||||||||||||||
| 24 | instead of returning a string, returns a L<XML::LibXML> DOM. | ||||||||||||||
| 25 | |||||||||||||||
| 26 | The DOM will have non-namespaced elements according to the docutils | ||||||||||||||
| 27 | vocabulary, and namespcaed elements according to the MathML | ||||||||||||||
| 28 | vocabulary. | ||||||||||||||
| 29 | |||||||||||||||
| 30 | This is probably the fastest way to transform a | ||||||||||||||
| 31 | L<Text::Restructured::DOM> structure into a proper XML DOM. | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =head1 METHODS | ||||||||||||||
| 34 | |||||||||||||||
| 35 | =head2 C<new> | ||||||||||||||
| 36 | |||||||||||||||
| 37 | Returns a new object. | ||||||||||||||
| 38 | |||||||||||||||
| 39 | =cut | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 0 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 42 | 0 | my ($class)=@_; | |||||||||||||
| 43 | 0 | return bless {},$class; | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | |||||||||||||||
| 46 | =head2 I<xml_dom>C<= ProcessDOM(>I<docutils_dom>C<)> | ||||||||||||||
| 47 | |||||||||||||||
| 48 | Given an object of type L<Text::Restructured::DOM>, processes it | ||||||||||||||
| 49 | recursively and builds an XML DOM into a new document. Returns the | ||||||||||||||
| 50 | document, or dies trying. | ||||||||||||||
| 51 | |||||||||||||||
| 52 | =cut | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 0 | ▶ | sub ProcessDOM { | ||||||||||||
|
Subroutine
ProcessDOM: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 55 | 0 | my ($self,$dudom)=@_; | |||||||||||||
| 56 | 0 | my $xdoc=XML::LibXML->createDocument(); | |||||||||||||
| 57 | 0 | $xdoc->setDocumentElement(_docutils2xml($dudom,$xdoc)); | |||||||||||||
| 58 | 0 | return $xdoc; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 1 | my $MATHML='http://www.w3.org/1998/Math/MathML'; | |||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | ▶ | sub _mathml2xml { | ||||||||||||
|
Subroutine
_mathml2xml: not called CC=5 SCAR=34.0
| |||||||||||||||
| 64 | 0 | my ($mnode,$xdoc)=@_; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 0 | ▶ | if ($mnode->isText) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | 0 | return $xdoc->createTextNode($mnode->nodeValue); | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | |||||||||||||||
| 71 | 0 | my @children=map {_mathml2xml($_,$xdoc)} | |||||||||||||
| 72 | $mnode->childNodes(); | ||||||||||||||
| 73 | |||||||||||||||
| 74 | 0 | my $elem=$xdoc->createElementNS($MATHML,$mnode->nodeName); | |||||||||||||
| 75 | 0 | for my $attname ($mnode->attributeList) { | |||||||||||||
| 76 | 0 | ▶ | next if $attname eq 'xmlns'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 0 | $elem->setAttribute($attname, | |||||||||||||
| 78 | $mnode->attribute($attname)) | ||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 0 | $elem->appendChild($_) for @children; | |||||||||||||
| 82 | |||||||||||||||
| 83 | 0 | return $elem; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 0 | ▶ | sub _docutils2xml { | ||||||||||||
|
Subroutine
_docutils2xml: not called CC=10 SCAR=47.0
| |||||||||||||||
| 87 | 0 | my ($dunode,$xdoc)=@_; | |||||||||||||
| 88 | |||||||||||||||
| 89 | 0 | ▶ | if ($dunode->{tag} eq '#PCDATA') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | 0 | ▶ | return $xdoc->createTextNode($dunode->{text} || ''); | ||||||||||||
|
Condition0%0/2
$dunode->{'text'} || ''
MC/DC0%0/1
$dunode->{'text'} || ''
$dunode->{'text'}
Truth table0%0/2
A
A= $dunode->{'text'}
| |||||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 0 | ▶ | if ($dunode->{tag} eq 'mathml') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | 0 | return _mathml2xml($dunode->{attr}{mathml},$xdoc); | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 0 | my @children=map {_docutils2xml($_,$xdoc)} | |||||||||||||
| 98 | 0 | ▶ | @{ $dunode->{content} || [] }; | ||||||||||||
|
Condition0%0/2
$dunode->{'content'} || []
MC/DC0%0/1
$dunode->{'content'} || []
$dunode->{'content'}
Truth table0%0/2
A
A= $dunode->{'content'}
| |||||||||||||||
| 99 | |||||||||||||||
| 100 | 0 | my $elem=$xdoc->createElement($dunode->{tag}); | |||||||||||||
| 101 | |||||||||||||||
| 102 | 0 | ▶ | if (defined $dunode->{attr}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | ▶ | while (my ($attname,$attval)=each %{$dunode->{attr}}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | 0 | ▶ | if (! defined $attval) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 105 | 0 | $attval=''; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | elsif (ref($attval) eq 'ARRAY') { | ||||||||||||||
| 108 | 0 | $attval=join ' ',@$attval; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | 0 | $elem->setAttribute($attname,$attval); | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | } | ||||||||||||||
| 113 | 0 | $elem->appendChild($_) for @children; | |||||||||||||
| 114 | |||||||||||||||
| 115 | 0 | return $elem; | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | 1 | 1; | |||||||||||||