Anonymous avatar Anonymous committed 4fdcd98

Added the first test to convert to Qantor-XML.

Comments (0)

Files changed (5)

Text-Qantor/MANIFEST

 README
 t/00-load.t
 t/01-convert-to-xsl-fo.t
+t/02-convert-to-qantor-xml.t
 t/data/to-xsl-fo/input/several-paras.qant
 t/data/to-xsl-fo/input/three-paras.qant
 t/data/to-xsl-fo/input/with-bold.qant
+t/data/to-xsl-fo/output-qantor-xml/placeholder.txt
 t/data/to-xsl-fo/output-xsl-fo/placeholder.txt
+t/data/to-xsl-fo/qantor-xml/three-paras.xml
 t/data/to-xsl-fo/xsl-fo/several-paras.fo
 t/data/to-xsl-fo/xsl-fo/three-paras.fo
 t/data/to-xsl-fo/xsl-fo/with-bold.fo

Text-Qantor/lib/Text/Qantor.pm

 has '_xml_out' => (is => "rw", isa => "XML::Writer");
 
 my $fo_ns = "http://www.w3.org/1999/XSL/Format";
+my $xlink_ns = "http://www.w3.org/1999/xlink";
+my $qantor_xml_ns = "http://web-cpan.berlios.de/Qantor/qantor-xml/";
+my $xml_ns = "http://www.w3.org/XML/1998/namespace";
 
 =head1 NAME
 
     return;
 }
 
+sub _write_xml_para
+{
+    my $self = shift;
+    my $p = shift;
+
+    my $writer = $self->_xml_out();
+
+    my $text_nodes = $p->{'Para_Text_Wrapper'};
+
+    foreach my $t_n (@$text_nodes)
+    {
+        if (exists($t_n->{'Plain_Para_Text'}))
+        {
+            $writer->characters($t_n->{'Plain_Para_Text'});
+        }
+        elsif (exists($t_n->{'Macro_Para_Text'}))
+        {
+            my $macro_call = $t_n->{'Macro_Para_Text'};
+            $writer->startTag([$qantor_xml_ns, "b"]);
+            $self->_write_xml_para(
+                $macro_call->{'Raw_Para'}
+            );
+            $writer->endTag(); # inline font-weight=bold
+        }
+    }
+
+    return;
+}
+
+=head2 $qantor->convert_input_to_xml({in_fh => \*STDIN, out_fh => \*STDOUT})
+
+Converts the input from the C<in_fh> filehandle to Qantor-XML and outputs it 
+to C<out_fh>.
+
+=cut
+
+sub convert_input_to_xml
+{
+    my ($self, $args) = @_;
+
+    my $in_fh = $args->{in_fh};
+    my $out_fh = $args->{out_fh};
+
+    my $writer = XML::Writer->new(
+        NAMESPACES => 1,
+        OUTPUT => $out_fh,
+        PREFIX_MAP =>
+        {
+            $qantor_xml_ns => '',
+            $xlink_ns => "xlink",
+            $xml_ns => "xml",
+        },
+        NEWLINES => 1,
+        ENCODING => "utf-8",
+    );
+
+    $self->_xml_out($writer);
+
+    $writer->xmlDecl("utf-8");
+
+    $writer->startTag("doc",
+        version => "0.1",
+        [$xml_ns, "id"] => "index",
+        [$xml_ns, "lang"] => "en",
+    );
+    $writer->startTag("body");
+
+    my $text;
+
+    {
+        local $/;
+        $text = <$in_fh>;
+    }
+
+    my $parser = Text::Qantor::Parser->new();
+
+    my $doc_tree = $parser->parse(
+        {
+            text => $text,
+        }
+    );
+
+    foreach my $p (@{$doc_tree->{Text}->{Raw_Para}})
+    {
+        $writer->startTag("p");
+        $self->_write_xml_para($p);
+        $writer->endTag(); # p
+    }
+
+    
+
+    $writer->endTag(); # body
+    $writer->endTag(); # doc
+
+    return;
+}
+
 sub _lexer
 {
     my $self = shift;

Text-Qantor/t/02-convert-to-qantor-xml.t

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 1;
+
+use lib "./t/lib";
+
+use Test::XML::Ordered qw(is_xml_ordered);
+
+use IO::String;
+
+use Text::Qantor;
+
+# TEST:$num_files=1
+my @files =
+(
+    qw(
+        t/data/to-xsl-fo/input/three-paras.qant
+    )
+);
+
+#        t/data/to-xsl-fo/input/several-paras.qant
+#        t/data/to-xsl-fo/input/with-bold.qant
+
+sub read_file
+{
+    my $path = shift;
+
+    open my $in, "<", $path;
+    binmode $in, ":utf8";
+    my $contents;
+    {
+        local $/;
+        $contents = <$in>
+    }
+    close($in);
+    return $contents;
+}
+
+
+
+foreach my $input_file (@files)
+{
+    if ($input_file !~ m{\At/data/to-xsl-fo/input/([^\.]+)\.qant\z})
+    {
+        die "File is not the correct format.";
+    }
+
+    my $base = $1;
+
+    my $expected_file = "t/data/to-xsl-fo/qantor-xml/$base.xml";
+
+    my $qantor = Text::Qantor->new();
+
+    open my $input_file_fh, "<", $input_file;
+
+    my $got_file = "t/data/to-xsl-fo/output-qantor-xml/$base.xml";
+    open my $got_output_fh, ">", $got_file;
+
+    $qantor->convert_input_to_xml(
+        {
+            in_fh => $input_file_fh,
+            out_fh => $got_output_fh,
+        },
+    );
+
+    close($input_file_fh);
+    close($got_output_fh);
+
+    # Now let's compare the XMLs.
+    # TEST*$num_files
+    is_xml_ordered(
+        [ location => $got_file ],
+        [ location => $expected_file ],
+        "'$input_file' generated good output"
+    );
+}
Add a comment to this file

Text-Qantor/t/data/to-xsl-fo/output-qantor-xml/placeholder.txt

Empty file added.

Text-Qantor/t/data/to-xsl-fo/qantor-xml/three-paras.xml

+<?xml version='1.0' encoding='utf-8'?>
+<doc xmlns="http://web-cpan.berlios.de/Qantor/qantor-xml/" xmlns:xlink="http://www.w3.org/1999/xlink" version="0.1" xml:id="index" xml:lang="en">
+<body>
+<p>
+First paragraph - foo is better.
+</p>
+<p>
+Second paragraph - bar is worse.
+</p>
+<p>
+Third paragraph - quux for the win!
+</p>
+</body>
+</doc>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.