Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Removing Preformatted Word Wrapping

by abitkin (Monk)
on Oct 12, 2004 at 14:48 UTC ( [id://398512]=perlquestion: print w/replies, xml ) Need Help??

abitkin has asked for the wisdom of the Perl Monks concerning the following question:

Okay, I'm posting this without working on it myself yet. It mainly is a challenge for those who have time to work on it now, and I'll hack away at it after work. (In about 7 hours or so.)

So the idea is this, I use fortune for a number of things; however there is an annoyance. The people who wrote the fortunes put newlines in for wrapping and such. Normally this wouldn't be a problem, if I was using fortune in a terminal, but in this case, it's a resizable window. So newlines within a quote should be removed, but others should stay.

So, to make this easier to explain I ran my fortune command a number of times and seperated the quotes by a line of "=". I then made a copy of it and corrected it by hand. The goal is to have a perl script do it for me.

Original Text:

Bender: Stay away from our women. You got metal fever, baby, metal fe +ver! =================================================================== Kids, kids, kids. As far as Daddy's concerned, you're both potential murderers. -- Homer Simpson Who Shot Mr. Burns? (Part 2) =================================================================== Wh ... what's going on? Wh ... wha ... why am I on a Japanese box? -- Homer Simpson In Marge We Trust =================================================================== Who spread garbage all over Flanders's yard before I got a chance to? -- Homer Simpson Two Dozen and One Greyhounds =================================================================== THIS IS NOT A CLUE...OR IS IT? THIS IS NOT A CLUE...OR IS IT? THIS IS NOT A CLUE...OR IS IT? THIS IS NOT A CLUE...OR IS IT? Bart Simpson on chalkboard in episode 2F16 =================================================================== "It was nice of you to let me reattach your arm." --Zoidber =================================================================== Calculon: An Oscar, you say? That would get me out of this festering rats' nest called television once and for all. =================================================================== Dasher, Dancer ... Prancer ... Nixon, Comet, Cupid ... Donna Dixon. -- Homer Simpson Simpsons Roasting on an Open Fire =================================================================== Homer: I suppose you want to probe me. Well, you might as well get it over with. Kang: Stop! We have reached the limits of what rectal probing can teach us. Treehouse of Horror VII Famous Original Ray's Superior Court =================================================================== It's okay, Marge. I've learned my lesson. A mountain of sugar is too much for one man. It's clear now why God portions it out in those tiny packets, and why he lives on a plantation in Hawaii. -- Homer Simpson Lisa's Rival =================================================================== Bender: "Tell the Donbot I'm quitting organized crime. From now on I'l +l stick to the regular kind." =================================================================== "Hurry up! I wanna see the moon." -Fry "Relax. It's open 'till nine." -Leela =================================================================== I WILL NOT SCREAM FOR ICE CREAM I WILL NOT SCREAM FOR ICE CREAM I WILL NOT SCREAM FOR ICE CREAM I WILL NOT SCREAM FOR ICE CREAM Bart Simpson on chalkboard in episode AABF03 =================================================================== Calculon: I'm programmed to be very busy. =================================================================== "I don't care how many eyes a man has... as long as it's less than fi +ve." -Leela

Fixed Text:

Bender: Stay away from our women. You got metal fever, baby, metal fe +ver! =================================================================== Kids, kids, kids. As far as Daddy's concerned, you're both potential +murderers. -- Homer Simpson Who Shot Mr. Burns? (Part 2) =================================================================== Wh ... what's going on? Wh ... wha ... why am I on a Japanese box? -- Homer Simpson In Marge We Trust =================================================================== Who spread garbage all over Flanders's yard before I got a chance to? -- Homer Simpson Two Dozen and One Greyhounds =================================================================== THIS IS NOT A CLUE...OR IS IT? THIS IS NOT A CLUE...OR IS IT? THIS IS NOT A CLUE...OR IS IT? THIS IS NOT A CLUE...OR IS IT? Bart Simpson on chalkboard in episode 2F16 =================================================================== "It was nice of you to let me reattach your arm." --Zoidber =================================================================== Calculon: An Oscar, you say? That would get me out of this festering +rats' nest called television once and for all. =================================================================== Dasher, Dancer ... Prancer ... Nixon, Comet, Cupid ... Donna Dixon. -- Homer Simpson Simpsons Roasting on an Open Fire =================================================================== Homer: I suppose you want to probe me. Well, you might as well get + it over with. Kang: Stop! We have reached the limits of what rectal probing can +teach us. Treehouse of Horror VII Famous Original Ray's Superior Court =================================================================== It's okay, Marge. I've learned my lesson. A mountain of sugar is too + much for one man. It's clear now why God portions it out in those t +iny packets, and why he lives on a plantation in Hawaii. -- Homer Simpson Lisa's Rival =================================================================== Bender: "Tell the Donbot I'm quitting organized crime. From now on I'l +l stick to the regular kind." =================================================================== "Hurry up! I wanna see the moon." -Fry "Relax. It's open 'till nine." -Leela =================================================================== I WILL NOT SCREAM FOR ICE CREAM I WILL NOT SCREAM FOR ICE CREAM I WILL NOT SCREAM FOR ICE CREAM I WILL NOT SCREAM FOR ICE CREAM Bart Simpson on chalkboard in episode AABF03 =================================================================== Calculon: I'm programmed to be very busy. =================================================================== "I don't care how many eyes a man has... as long as it's less than fi +ve." -Leela

Something to remember, not only spaces are used, but also tabs, so be aware of that.

There are really only a couple of tasks here:

  • figure out if the line break at the end of the current line is there for wrapping
  • If it is, make sure by removing it, there still is a space in between words
  • Clean up any tabs or other whitespace from the start of the line added by removing the newline

==
Kwyjibo. A big, dumb, balding North American ape. With no chin.

Replies are listed 'Best First'.
Re: Removing Preformatted Word Wrapping
by ikegami (Patriarch) on Oct 12, 2004 at 15:00 UTC

    I see at least three conditions for wrapping:

    Name: bla bla bla bla -> Name: bla bla bla bla -- bla bla bla bla -> -- bla bla bla bla bla bla bla bla -> bla bla bla bla

    That's it for now; I must be off.

      Ok, I see that I'm doing more than I should, but so be it :)

      Output:

      ======================================== Bender: Stay away from our women. You got metal fever, baby, metal fever! ======================================== Kids, kids, kids. As far as Daddy's concerned, you're both potential murderers. -- Homer Simpson Who Shot Mr. Burns? (Part 2) ======================================== Wh ... what's going on? Wh ... wha ... why am I on a Japanese box? -- Homer Simpson In Marge We Trust ======================================== Who spread garbage all over Flanders's yard before I got a chance to? -- Homer Simpson Two Dozen and One Greyhounds ======================================== THIS IS NOT A CLUE...OR IS IT? THIS IS NOT A CLUE...OR IS IT? THIS IS NOT A CLUE...OR IS IT? THIS IS NOT A CLUE...OR IS IT? Bart Simpson on chalkboard in episode 2F16 ======================================== "It was nice of you to let me reattach your arm." --Zoidber ======================================== Calculon: An Oscar, you say? That would get me out of this festering rats' nest called television once and for all. ======================================== Dasher, Dancer ... Prancer ... Nixon, Comet, Cupid ... Donna Dixon. -- Homer Simpson Simpsons Roasting on an Open Fire ======================================== Homer: I suppose you want to probe me. Well, you might as well get it over with. Kang: Stop! We have reached the limits of what rectal probing can teach us. Treehouse of Horror VII Famous Original Ray's Superior Court ======================================== It's okay, Marge. I've learned my lesson. A mountain of sugar is too much for one man. It's clear now why God portions it out in those tiny packets, and why he lives on a plantation in Hawaii. -- Homer Simpson Lisa's Rival ======================================== Bender: "Tell the Donbot I'm quitting organized crime. From now on I'll stick to the regular kind." ======================================== "Hurry up! I wanna see the moon." -Fry "Relax. It's open 'till nine." -Leela ======================================== I WILL NOT SCREAM FOR ICE CREAM I WILL NOT SCREAM FOR ICE CREAM I WILL NOT SCREAM FOR ICE CREAM I WILL NOT SCREAM FOR ICE CREAM Bart Simpson on chalkboard in episode AABF03 ======================================== Calculon: I'm programmed to be very busy. ======================================== "I don't care how many eyes a man has... as long as it's less than five." -Leela ========================================

        *Clap clap* That is worth applauding. My line of thinking was to check if the newline was a result of the next word being of a length that would cause a wrap.

        I have to say this is impressive, aligning and everything. Definite ++.


        ==
        Kwyjibo. A big, dumb, balding North American ape. With no chin.
Re: Removing Preformatted Word Wrapping
by ccn (Vicar) on Oct 12, 2004 at 15:03 UTC

    >perl -e 'print join"="x40 ."\n",map~~`fortune`,1..pop' 30

Re: Removing Preformatted Word Wrapping
by ccn (Vicar) on Oct 13, 2004 at 07:31 UTC

    Please try the following:

    #!/usr/bin/perl -w use strict; $_ = join '=' x 79 . "\n", map {scalar `fortune`} 1 .. $ARGV[0] || 1; s{\n} { my ($a, $b) = ($`, $'); ($a =~ /[a-z] *$/ && $b =~ /^ *[a-z]/) ? ' ' : "\n"; }ge; s/(?<=[\w.,]) +/ /g; print;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://398512]
Approved by muntfish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found