Script for wrapping and unwrapping text?

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

···

On Aug 13, 2:02 pm, Daniel Choi <dhc...@gmail.com> 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.

  #
  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.

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

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

(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.)

···

On Aug 13, 2008, at 10:02 AM, 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.