diff options
author | dakkar <dakkar@luxion> | 2007-07-17 15:27:31 +0000 |
---|---|---|
committer | dakkar <dakkar@luxion> | 2007-07-17 15:27:31 +0000 |
commit | 6baf5b52e18811f2b44d35a5dcc1a51d6071ce8b (patch) | |
tree | 9ec28059c24dd540ab0e5ef6a83a684fc32ed00c | |
parent | r2492@narval: dakkar | 2007-07-17 10:42:14 +0200 (diff) | |
download | Text-Restructured-Writer-LibXML-6baf5b52e18811f2b44d35a5dcc1a51d6071ce8b.tar.gz Text-Restructured-Writer-LibXML-6baf5b52e18811f2b44d35a5dcc1a51d6071ce8b.tar.bz2 Text-Restructured-Writer-LibXML-6baf5b52e18811f2b44d35a5dcc1a51d6071ce8b.zip |
r2493@narval: dakkar | 2007-07-17 10:42:29 +0200
importo il modulo ReST-XML
git-svn-id: svn://luxion/repos/Text-Restructured-Writer-LibXML@257 fcb26f47-9200-0410-b104-b98ab5b095f3
-rw-r--r-- | .cvsignore | 0 | ||||
-rw-r--r-- | Build.PL | 31 | ||||
-rw-r--r-- | Changes | 5 | ||||
-rw-r--r-- | MANIFEST | 10 | ||||
-rw-r--r-- | README | 6 | ||||
-rw-r--r-- | lib/Text/Restructured/Writer/LibXML.pm | 118 | ||||
-rw-r--r-- | t/00-load.t | 7 | ||||
-rw-r--r-- | t/00-pod-coverage.t | 7 | ||||
-rw-r--r-- | t/00-pod.t | 7 | ||||
-rw-r--r-- | t/00-prereqs.t | 6 | ||||
-rw-r--r-- | t/01-basic.t | 79 |
11 files changed, 276 insertions, 0 deletions
diff --git a/.cvsignore b/.cvsignore new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/.cvsignore diff --git a/Build.PL b/Build.PL new file mode 100644 index 0000000..9999a86 --- /dev/null +++ b/Build.PL @@ -0,0 +1,31 @@ + +use strict; +use warnings; +use Module::Build; + +my $builder = Module::Build->new( + module_name => 'Text::Restructured::Writer::LibXML', + license => 'perl', + dist_name => 'Text-Restructured-Writer-LibXML', + dist_abstract => <<'END_ABSTRACT', +This module implements a "Writer" for Text::Restructured, that instead +of returning a string, returns a LibXML DOM. +END_ABSTRACT + dist_author => 'Gianni Ceccarelli <dakkar@thenautilus.net>', + dist_version_from => 'lib/Text/Restructured/Writer/LibXML.pm', + requires => { + 'Text::Restructured' => 0.003031, + 'XML::LibXML' => 1.61, + }, + build_requires => { + 'Test::More' => 0, + 'XML::LibXML::XPathContext' => 1.61, + }, + add_to_cleanup => [ 'Text-Restructured-Writer-LibXML-*' ], + create_makefile_pl => 'traditional', + create_packlist => 1, + recursive_test_files=> 1, + sign => 1, +); + +$builder->create_build_script(); @@ -0,0 +1,5 @@ +Revision history for Text-Restructured-Writer-LibXML + +0.01 2007-07-16 + First version. + diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..7c1b0b5 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,10 @@ +Build.PL +Changes +MANIFEST +META.yml # Will be created by "make dist" +README +lib/Text/Restructured/Writer/LibXML.pm +t/00-load.t +t/00-pod-coverage.t +t/00-pod.t +t/00-prereqs.t @@ -0,0 +1,6 @@ +This module implements a "Writer" for Text::Restructured, that instead +of returning a string, returns a LibXML DOM. + +The DOM will have non-namespaced elements according to the docutils +vocabulary, and namespcaed elements according to the MathML +vocabulary. diff --git a/lib/Text/Restructured/Writer/LibXML.pm b/lib/Text/Restructured/Writer/LibXML.pm new file mode 100644 index 0000000..99ef206 --- /dev/null +++ b/lib/Text/Restructured/Writer/LibXML.pm @@ -0,0 +1,118 @@ +package Text::Restructured::Writer::LibXML; +use strict; +use warnings; +use XML::LibXML; + +$Text::Restructured::Writer::LibXML::VERSION='0.01'; + +=head1 NAME + +Text::Restructured::Writer::LibXML + +=head1 SYNOPSIS + + use Text::Restructured; + use Text::Restructured::Writer::LibXML; + + my $parser=Text::Restructured->new($opts,'gino'); + my $dudom=$parser->Parse($input,$filename); + my $xdoc=Text::Restructured::Writer::LibXML->new->ProcessDOM($dudom); + +=head1 DESCRIPTION + +This module implements a "Writer" for L<Text::Restructured>, that +instead of returning a string, returns a L<XML::LibXML> DOM. + +The DOM will have non-namespaced elements according to the docutils +vocabulary, and namespcaed elements according to the MathML +vocabulary. + +This is probably the fastest way to transform a +L<Text::Restructured::DOM> structure into a proper XML DOM. + +=head1 METHODS + +=head2 C<new> + +Returns a new object. + +=cut + +sub new { + my ($class)=@_; + return bless {},$class; +} + +=head2 I<xml_dom>C<= ProcessDOM(>I<docutils_dom>C<)> + +Given an object of type L<Text::Restructured::DOM>, processes it +recursively and builds an XML DOM into a new document. Returns the +document, or dies trying. + +=cut + +sub ProcessDOM { + my ($self,$dudom)=@_; + my $xdoc=XML::LibXML->createDocument(); + $xdoc->setDocumentElement(_docutils2xml($dudom,$xdoc)); + return $xdoc; +} + +my $MATHML='http://www.w3.org/1998/Math/MathML'; + +sub _mathml2xml { + my ($mnode,$xdoc)=@_; + + if ($mnode->isText) { + return $xdoc->createTextNode($mnode->nodeValue); + } + + + my @children=map {_mathml2xml($_,$xdoc)} + $mnode->childNodes(); + + my $elem=$xdoc->createElementNS($MATHML,$mnode->nodeName); + for my $attname ($mnode->attributeList) { + next if $attname eq 'xmlns'; + $elem->setAttribute($attname, + $mnode->attribute($attname)) + } + + $elem->appendChild($_) for @children; + + return $elem; +} + +sub _docutils2xml { + my ($dunode,$xdoc)=@_; + + if ($dunode->{tag} eq '#PCDATA') { + return $xdoc->createTextNode($dunode->{text} || ''); + } + + if ($dunode->{tag} eq 'mathml') { + return _mathml2xml($dunode->{attr}{mathml},$xdoc); + } + + my @children=map {_docutils2xml($_,$xdoc)} + @{ $dunode->{content} || [] }; + + my $elem=$xdoc->createElement($dunode->{tag}); + + if (defined $dunode->{attr}) { + while (my ($attname,$attval)=each %{$dunode->{attr}}) { + if (! defined $attval) { + $attval=''; + } + elsif (ref($attval) eq 'ARRAY') { + $attval=join ' ',@$attval; + } + $elem->setAttribute($attname,$attval); + } + } + $elem->appendChild($_) for @children; + + return $elem; +} + +1; diff --git a/t/00-load.t b/t/00-load.t new file mode 100644 index 0000000..f152344 --- /dev/null +++ b/t/00-load.t @@ -0,0 +1,7 @@ +use Test::More tests => 1; + +BEGIN { + use_ok('Text::Restructured::Writer::LibXML'); +} + +diag( "Testing Text::Restructured::Writer::LibXML $Text::Restructured::Writer::LibXML::VERSION" ); diff --git a/t/00-pod-coverage.t b/t/00-pod-coverage.t new file mode 100644 index 0000000..011042e --- /dev/null +++ b/t/00-pod-coverage.t @@ -0,0 +1,7 @@ +use Test::More; +plan skip_all => 'Set DEVEL_TESTS to run these tests' + unless $ENV{DEVEL_TESTS}; +eval "use Test::Pod::Coverage 1.04"; +plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" + if $@; +all_pod_coverage_ok(); diff --git a/t/00-pod.t b/t/00-pod.t new file mode 100644 index 0000000..3681990 --- /dev/null +++ b/t/00-pod.t @@ -0,0 +1,7 @@ +use Test::More; +plan skip_all => 'Set DEVEL_TESTS to run these tests' + unless $ENV{DEVEL_TESTS}; +eval "use Test::Pod 1.14"; +plan skip_all => "Test::Pod 1.14 required for testing POD" + if $@; +all_pod_files_ok(); diff --git a/t/00-prereqs.t b/t/00-prereqs.t new file mode 100644 index 0000000..5bfc2c6 --- /dev/null +++ b/t/00-prereqs.t @@ -0,0 +1,6 @@ +use Test::More; +plan skip_all => 'Set DEVEL_TESTS to run these tests' + unless $ENV{DEVEL_TESTS}; +eval "use Test::Prereq::Build"; +plan skip_all => "Test::Prereq::Build required to test dependencies" if $@; +prereq_ok(); diff --git a/t/01-basic.t b/t/01-basic.t new file mode 100644 index 0000000..94ba201 --- /dev/null +++ b/t/01-basic.t @@ -0,0 +1,79 @@ +use Test::More tests => 10; +use strict; +use warnings; +use Text::Restructured; +use Text::Restructured::Writer::LibXML; +use XML::LibXML::XPathContext; + +my $input=<<'EOF'; +====== + Test +====== +:Author: dakkar + +paragrafo + +link_ + +.. _link: /gino/pino/ + +mathml: :mathml:`x^2` + +.. mathml:: + + (x^2)/(y^2) + +pre:: + + gino pino + rino + +normale + +EOF + +my $opts={ + D => { + generator => 0, + 'source-link' => 0, + 'time' => 0, + }, +}; +my $parser=Text::Restructured->new($opts,'gino'); +my $dudom=$parser->Parse($input,'--inline--'); +my $xdoc=Text::Restructured::Writer::LibXML->new->ProcessDOM($dudom); + +my $ctx=XML::LibXML::XPathContext->new($xdoc); +$ctx->registerNs('m','http://www.w3.org/1998/Math/MathML'); +sub xis { + my ($expr,$val,$comment)=@_; + is $ctx->findvalue($expr), + $val, + $comment||''; +} + +=begin comment + +<document source="--inline--" names="test" ids="test" title="Test"><title>Test +</title><docinfo><author>dakkar +</author></docinfo><paragraph>paragrafo +</paragraph><paragraph><reference refuri="/gino/pino/" name="link">link</reference></paragraph><target refuri="/gino/pino/" names="link" ids="link"/><paragraph>mathml: <math xmlns="http://www.w3.org/1998/Math/MathML" title="x^2"><mstyle><msup><mi>x</mi><mn>2</mn></msup></mstyle></math></paragraph><paragraph><math xmlns="http://www.w3.org/1998/Math/MathML" title=" (x^2)/(y^2)"><mstyle displaystyle="true"><mfrac><mrow><msup><mi>x</mi><mn>2</mn></msup></mrow><mrow><msup><mi>y</mi><mn>2</mn></msup></mrow></mfrac></mstyle></math></paragraph><paragraph>pre: +</paragraph><literal_block xml:space="preserve"> gino pino +rino +</literal_block><paragraph>normale +</paragraph></document> + +=end comment + +=cut + +xis '/document/@source','--inline--','Source name'; +xis '/document/@title','Test','Title'; +xis '/document/docinfo/author',"dakkar\n",'Author'; +xis 'count(/document/paragraph)',6,'Paragraphs'; +xis '/document/paragraph[1]',"paragrafo\n",'para 1'; +xis '/document/paragraph[2]/reference/@refuri','/gino/pino/','Ref'; +xis '/document/paragraph[3]/m:math','x2','MathML 1'; +xis '/document/paragraph[4]/m:math','x2y2','MathML 2'; +xis 'name(/document/paragraph[5]/following-sibling::*[1])','literal_block','Literal'; +xis '/document/literal_block'," gino pino\nrino\n",'Literal content'; |