Anonymous avatar Anonymous committed f80edd3

Add another source option in ToHTML - 'string_ref'.

Comments (0)

Files changed (2)

perl/modules/XML-Grammar-Fiction/lib/XML/Grammar/Fiction/ToHTML.pm

     return 0;
 }
 
-=head2 $converter->translate_to_html({source => {file => $filename}, output => "string" })
+=head2 translate_to_html
 
-Does the actual conversion. $filename is the filename to translate (currently
-the only available source). 
+=over 4
+
+=item * my $xhtml_source = $converter->translate_to_html({source => {file => $filename}, output => "string" })
+
+=item * my $xhtml_source = $converter->translate_to_html({source => {string_ref => \$buffer}, output => "string" })
+
+=item * my $xhtml_dom = $converter->translate_to_html({source => {file => $filename}, output => "xml" })
+
+=back
+
+Does the actual conversion. The C<'source'> argument points to a hash-ref with
+keys and values for the source. If C<'file'> is specified there it points to the
+filename to translate (currently the only available source). If 
+C<'string_ref'> is specified it points to a reference to a string, with the
+contents of the source XML.
 
 The C<'output'> key specifies the return value. A value of C<'string'> returns 
 the XML as a string, and a value of C<'xml'> returns the XML as an 
     my $self = shift;
     my $args = shift;
 
+    my $source = $args->{source};
+
     return
-        $self->_xml_parser()->parse_file($args->{source}->{file})
+        exists($source->{'string_ref'})
+            ? $self->_xml_parser()->parse_string(${$source->{'string_ref'}}) 
+            : $self->_xml_parser()->parse_file($source->{'file'})
         ;
 }
 

perl/modules/XML-Grammar-Fiction/t/to-xhtml.t

 use strict;
 use warnings;
 
-use Test::More tests => 30;
+use Test::More tests => 32;
 
 use File::Spec;
 
 
         my $format_hash = $converters{$format_id};
         my $m = $format_hash->{method};
+
+        my $xml_fn = "t/data/xml/$fn.xml";
+
         my $text = $format_hash->{'obj'}->$m(
             {
-                source => { file => "t/data/xml/$fn.xml", },
+                source => { file => $xml_fn, },
                 output => "string",
             }
         );
+        if ($format_id eq "xhtml")
+        {
+            my $file_contents = load_xml($xml_fn);
+            my $from_string_text =
+                $format_hash->{'obj'}->$m(
+                {
+                    source => { string_ref => \$file_contents, },
+                    output => "string",
+                }
+            );
+
+            # TEST*$num_texts
+            is ($from_string_text, $text, "From-string-ref text is OK.")
+        }
         my $parser = XML::LibXML->new();
 
         $parser->load_ext_dtd(0);
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.