1. Shlomi Fish
  2. Fortune-XML

Commits

shl...@52c325ad-5fd0-0310-8a0f-c43feede02cc  committed ae8105d

Squash spaces in the text output of <work, <author, etc..

It caused that such tags with newlines will render improperly in the
http://www.shlomifish.org/ fortunes.

  • Participants
  • Parent commits 8b7a192
  • Branches default

Comments (0)

Files changed (5)

File XML-Grammar-Fortune/module/MANIFEST

View file
 t/data/text-results/irc-conversation-1.txt
 t/data/text-results/irc-conversation-3-with-join-unjoin.txt
 t/data/text-results/quote-fort-sample-10-with-hyperlink.txt
+t/data/text-results/quote-fort-sample-11-with-work-elem-spanning-several-lines.txt
 t/data/text-results/quote-fort-sample-2-with-brs.txt
 t/data/text-results/quote-fort-sample-3-more-than-one-para.txt
 t/data/text-results/quote-fort-sample-4-ul.txt
 t/data/xml/irc-conversation-4-several-convos.xml
 t/data/xml/irc-convos-and-raw-fortunes-1.xml
 t/data/xml/quote-fort-sample-10-with-hyperlink.xml
+t/data/xml/quote-fort-sample-11-with-work-elem-spanning-several-lines.xml
 t/data/xml/quote-fort-sample-1.xml
 t/data/xml/quote-fort-sample-2-with-brs.xml
 t/data/xml/quote-fort-sample-3-more-than-one-para.xml

File XML-Grammar-Fortune/module/lib/XML/Grammar/Fortune/ToText.pm

View file
     {
         my $name = $field_node->localname();
 
+        my $value = $field_node->textContent();
+
+        # Squash whitespace including newlines into a single space.
+        $value =~ s{\s+}{ }g;
+        
+        # Remove leading and trailing space - it is not desirable here
+        # because we want it formatted consistently.
+        $value =~ s{\A\s+}{};
+        $value =~ s{\s+\z}{};
+
         if ($name eq "author")
         {
-            $self->_out((" " x 4) . "-- " . $field_node->textContent() . "\n");
+            $self->_out((" " x 4) . "-- " . $value . "\n");
         }
         elsif (($name eq "work") || ($name eq "tagline"))
         {
             }
             $self->_out(
                   (" " x 4) . "-- "
-                . $field_node->textContent()
+                . $value
                 . $url
                 . "\n"
             );

File XML-Grammar-Fortune/module/t/data/text-results/quote-fort-sample-11-with-work-elem-spanning-several-lines.txt

View file
+The [American](http://www.usa.tld/) Lottery.
+
+* It's best [SFed](http://www.shlomifish.org/) out.
+
+* It's so good.
+
+* It's a mess.
+
+    -- Nobody
+    -- Shlomi Fish's Works ( http://www.shlomifish.org/ )

File XML-Grammar-Fortune/module/t/data/xml/quote-fort-sample-11-with-work-elem-spanning-several-lines.xml

View file
+<?xml version="1.0" encoding="utf-8"?>
+<collection>
+    <head />
+    <list>
+        <fortune id="the-american-lottery">
+            <meta>
+                <title>The American Lottery</title>
+            </meta>
+            <quote>
+                <body>
+                    <p>The <a href="http://www.usa.tld/">American</a> Lottery.</p>
+<ul>
+<li>It's best <a href="http://www.shlomifish.org/">SFed</a> out.</li>
+<li>It's so good.</li>
+<li>It's a mess.</li>
+</ul>
+                </body>
+            <info>
+                <author>Nobody</author>
+                <work href="http://www.shlomifish.org/">Shlomi
+                Fish's Works</work>
+            </info>
+            </quote>
+        </fortune>
+    </list>
+</collection>

File XML-Grammar-Fortune/module/t/to-text.t

View file
 use strict;
 use warnings;
 
-use Test::More tests => 16;
+use Test::More tests => 17;
 use Test::Differences;
 
 use File::Spec;
 
 use XML::Grammar::Fortune::ToText;
 
-# TEST:$num_texts=16
+# TEST:$num_texts=17
 
 my @tests = (qw(
         raw-fort-empty-info-1
         quote-fort-sample-8-with-em-and-strong
         quote-fort-sample-9-with-blockquote
         quote-fort-sample-10-with-hyperlink
+        quote-fort-sample-11-with-work-elem-spanning-several-lines
         screenplay-fort-sample-1
         screenplay-fort-sample-2-long-line
         screenplay-fort-sample-3-with-inlinedesc