[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

script for wrapping and unwrapping text?

Daniel Choi

8/13/2008 6:01:00 PM


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.

2 Answers

Trans

8/13/2008 7:51:00 PM

0



On Aug 13, 2:02=A0pm, 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.

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

class String

#
def unfold_paragraphs
blank =3D false
text =3D ''
split(/\n/).each do |line|
if /\S/ !~ line
text << "\n\n"
blank =3D true
else
if /^(\s+|[*])/ =3D~ line
text << (line.rstrip + "\n")
else
text << (line.rstrip + " ")
end
blank =3D false
end
end
return text
end

end

T.

elliottcable

8/13/2008 11:08:00 PM

0

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: http://github.com/elliottcable...

The actual wrapping code is available here - you'll need to gem
install stringray and include it into class String to use this:
http://github.com/elliottcable/rat/tree/master/lib/rat/core_ext/string...

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