# Script for wrapping and unwrapping text?

**URL:** <https://www.ruby-forum.com/t/script-for-wrapping-and-unwrapping-text/145764>\
**Category:** Ruby\
**Created:** [August 13, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/script-for-wrapping-and-unwrapping-text/145764 "2008-08-13T20:05:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcarneiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e36b37/32.png) [@dcarneiro](https://www.ruby-forum.com/u/dcarneiro)\
**Post date:** [August 13, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/script-for-wrapping-and-unwrapping-text/145764/1 "2008-08-13T20:05:46Z")

</div>

I’m interested in storing, say, wiki pages as flat files that can be  
edited with Vim or Emacs and also processed with line-based unix text  
tools and version control systems like Git. All these tools work much  
better with linebreaks after every line of text. For example, diffs  
are much easier to deal with when a text file has line breaks after  
every line of text.

But it’s awkward to edit these same files through a textarea on a  
webpage with all the line breaks. Is there a good Ruby tool or script  
that can convert a text file written in, say Markdown or Asciidoc,  
into a version that removes the line breaks for editing in a browser  
textarea and then reinserts them on the update?

The problem is complicated a bit by the fact that you might want to  
preserve line breaks in some parts of a Markdown or Asciidoc document,  
like where you have a bulleted list. But you would want to wrap and  
unwrap flowing text in a paragraph or in an item of a list.

---

<div class="post-metadata">

**Author:** ![dcarneiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e36b37/32.png) [@dcarneiro](https://www.ruby-forum.com/u/dcarneiro)\
**Post date:** [August 13, 2008, 9:56pm UTC](https://www.ruby-forum.com/t/script-for-wrapping-and-unwrapping-text/145764/2 "2008-08-13T21:56:00Z")

</div>

On Aug 13, 2:02 pm, Daniel C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> into a version that removes the line breaks for editing in a browser  
> textarea and then reinserts them on the update?
> 
> The problem is complicated a bit by the fact that you might want to  
> preserve line breaks in some parts of a Markdown or Asciidoc document,  
> like where you have a bulleted list. But you would want to wrap and  
> unwrap flowing text in a paragraph or in an item of a list.

Well, here’s something I’ve used for part of what you are asking for:

class String

# 

def unfold\_paragraphs  
blank = false  
text = ‘’  
split(/\n/).each do |line|  
if /\S/ !~ line  
text \<\< “\n\n”  
blank = true  
else  
if /^(\s+|[\*])/ =~ line  
text \<\< (line.rstrip + “\n”)  
else  
text \<\< (line.rstrip + " ")  
end  
blank = false  
end  
end  
return text  
end

end

T.

---

<div class="post-metadata">

**Author:** ![dcarneiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e36b37/32.png) [@dcarneiro](https://www.ruby-forum.com/u/dcarneiro)\
**Post date:** [August 14, 2008, 1:11am UTC](https://www.ruby-forum.com/t/script-for-wrapping-and-unwrapping-text/145764/3 "2008-08-14T01:11:38Z")

</div>

I was working on something similar to this last night - it may be able  
to help you.

My requirements are slightly different - I wanted something that would  
wrap text to a given terminal size for a IM/IRC client I am writing -  
my requirements are that wrapped text ‘look’ good (i.e. not break  
words unless absolutely necessary, not leave mostly-blank lines due to  
a really long ‘word’ [URL/links/code strings/whatever], and not screw  
with existing whitespace if you’re pasting code or something into a  
chat).

I wrote a little library in the process that ‘intelligently’ splits a  
string into an array of words, allowing me to include Enumerable into  
String. Called it StringRay (I know, retarded… sorry), you can check  
it out here: [GitHub - ELLIOTTCABLE/stringray: String += Enumerable](http://github.com/elliottcable/stringray)

The actual wrapping code is available here - you’ll need to gem  
install stringray and include it into class String to use this:

> <https://github.com/ELLIOTTCABLE/rat/blob/master/lib/rat/core_ext/string.rb#L28-80>

(That’s a direct link - if you’re reading this in the archives, the  
line number references have probably changed. The link may no longer  
be applicable either.)
