From: Trans Date: 2008-08-14T04:51:05+09:00 Subject: Re: script for wrapping and unwrapping text? On Aug 13, 2:02 pm, Daniel Choi wrote: > 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. 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.