text wrap

Discussion in 'Ruby' started by ishamid, Dec 9, 2006.

  1. ishamid

    ishamid Guest

    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
     
    ishamid, Dec 9, 2006
    #1
    1. Advertising

  2. ishamid

    ishamid Guest

    Hi Paul,

    On Dec 9, 4:31 pm, Paul Lutus <> 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
     
    ishamid, Dec 10, 2006
    #2
    1. Advertising

  3. 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
    >
    >
    >
     
    Daniel Finnie, Dec 10, 2006
    #3
  4. 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+/)
     
    William James, Dec 10, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sara T.

    WRAP text in <TD>

    Sara T., Jul 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    645
    Sara T.
    Jul 26, 2003
  2. Dan
    Replies:
    2
    Views:
    2,091
    Tommy
    Mar 2, 2004
  3. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    717
    Chronic Philharmonic
    May 10, 2008
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    254
    Art Werschulz
    Sep 22, 2003
  5. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    257
    Anno Siegel
    Sep 25, 2003
Loading...

Share This Page