Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > text wrap

Reply
Thread Tools

text wrap

 
 
ishamid
Guest
Posts: n/a
 
      12-09-2006
Hi,

I have a ruby script that's doing what I need, and I would like to add
one feature. The script outputs a text file, and I would like Ruby to
take the final text file and wrap it at 67 characters, instead of doing
it manually in the editor.

Is there an available method or something that I can invoke? If so, can
you give me an example of its use (I'm still very much a novice!)

Best
Idris

 
Reply With Quote
 
 
 
 
ishamid
Guest
Posts: n/a
 
      12-10-2006
Hi Paul,

On Dec 9, 4:31 pm, Paul Lutus <(E-Mail Removed)> wrote:
> ishamid wrote:
> > Hi,

>
> > I have a ruby script that's doing what I need, and I would like to add
> > one feature. The script outputs a text file, and I would like Ruby to
> > take the final text file and wrap it at 67 characters, instead of doing
> > it manually in the editor.

>
> > Is there an available method or something that I can invoke? If so, can
> > you give me an example of its use (I'm still very much a novice!)


> Idris, I can offer various methods to break text up into individual lines,
> but I want to ask you to think this over before doing it.


Ok, see below...

> If you take a text document containing paragraphs (each a continuous string
> with no embedded linefeeds) and break the paragraphs into lines of specific
> lengths, you are throwing away information that is very difficult -- almost
> impossible in some cases -- to recover.


> Most environments can produce nicely formatted paragraphs for you, while you
> are editing the text and while printing it. It is generally preferred that
> the paragraph-to-line conversion take place at the time of display or
> printing, not before, and that the text be retained in its original form.


Perhaps my case is different. I am converting OOo xml to TeX for
further processing. Since TeX input is one-dimensional, TeX-the-engine
does not, akaik, care if the input is wrapped or not. I generally edit
my TeX-documents wrapped and editing a mile-long paragraph is a pain.
If I run the script and wrap from the editor then, when I run the
script again, I have to wrap from the editor again.

Does this case seem like an exception to your point or no?

>
> I am not unaware of the irony of posting this advice in a medium (Usenet)
> that breaks paragraphs into lines right away, before transmitting the
> message, and this trait is shared by the standard e-mail protocol. Both
> these behaviors (e.g. Usenet and e-mail breaking up paragraphs) are now
> widely recognized as mistakes, unfortunately they cannot really be
> corrected at this late date. But newer protocols, and all decent word
> processing document formats, do not do this to their content, for excellent
> reasons.
>
> Consider this. Let's say you break a document up into 67-character lines,
> permanently, and save it in that form. Later on, you discover you need to
> print the document with 80-character lines. You are out of luck -- the
> damage has been done.


But TeX does not care about line lengths in an editor; it just
processes a single paragraph as it's told, ignoring wrapping
completely.

I notice that David Kastrup posts here; he is an expert on TeX
text-editing and can correct me if I'm wrong.

> There are various schemes to rejoin broken lines into paragraphs once again,
> but all of them have corner cases where they fail. It is generally agreed
> to be better not to have broken the paragraphs in the first place.
>
> If, after reading this, you still want to break paragraphs into lines, post
> again, and someone will offer suggestions about how to proceed.


Do my reasons make more since now, or should I consider something else?

Thank you so much for the care you put into answering my question; it
is appreciated!

Best
Idris

 
Reply With Quote
 
 
 
 
Daniel Finnie
Guest
Posts: n/a
 
      12-10-2006
text = whatever you need to wrap

To print it:
0.step(text.length, 67) {|x| puts text[x, x+67]}

To put it in another variable:
wrapped = ""
0.step(text.length, 67) {|x| wrapped << "\n" << text[x, x+67]}

Dan

ishamid wrote:
> Hi,
>
> I have a ruby script that's doing what I need, and I would like to add
> one feature. The script outputs a text file, and I would like Ruby to
> take the final text file and wrap it at 67 characters, instead of doing
> it manually in the editor.
>
> Is there an available method or something that I can invoke? If so, can
> you give me an example of its use (I'm still very much a novice!)
>
> Best
> Idris
>
>
>


 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      12-10-2006

ishamid wrote:
> Hi,
>
> I have a ruby script that's doing what I need, and I would like to add
> one feature. The script outputs a text file, and I would like Ruby to
> take the final text file and wrap it at 67 characters, instead of doing
> it manually in the editor.
>
> Is there an available method or something that I can invoke? If so, can
> you give me an example of its use (I'm still very much a novice!)
>
> Best
> Idris


X = 10 # Width is 10 characters.
str =
"This\nis a test of the emergency broadcasting servicings I
asseverate"
p str.gsub(/\n/," ").scan(/\S.{0,#{X-2}}\S(?=\s|$)|\S+/)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To wrap or not to wrap? Aaron Fude Java 12 05-10-2008 06:33 PM
Wrap computer components in bubble wrap? Ickshka Computer Support 7 05-05-2006 05:54 PM
Text::Wrap::wrap difference Art Werschulz Perl Misc 1 09-25-2003 06:15 PM
Text::Wrap::wrap difference Art Werschulz Perl Misc 0 09-22-2003 02:36 PM
WRAP text in <TD> Sara T. ASP .Net 0 07-26-2003 09:05 AM



Advertisments