text wrap

I

ishamid

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
 
I

ishamid

Hi Paul,

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
 
D

Daniel Finnie

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
 
W

William James

ishamid said:
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+/)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top