Okay, this is working. I had indeed forgotten to do the square brackets, but I'd also somehow removed the OnClick handler from the button. Duh. It should be functional now.

Nate or somebody set this up live on E2: Wharfinger's Linebreaker. Yay! Them guys rule.

We've got a problem with people abusing <pre> tags when they node poetry and lyrics. It's painful to read, and it often breaks E2's page formatting. I asked somebody about this, and he told me that it was too much work to add a <br> tag at the end of each line. He was noding Paradise Lost or some godawful endless thing, so I didn't blame him. That kind of thing should be automated, right? Indeed it should.

Okay, now it is. Just go to Wharfinger's Linebreaker and enjoy.

I've tested this and found it good in (Win32) with the following browsers: Mozilla M17, Netscape 4, and IE 4.


9/26/00: Changed indent-fixing search regex so it would stop ignoring the first line.

<!-- CUT HERE -->
    <title>E2 Break-Tagger</title>

    <!--  wharfinger  9/11/00                                    -->
    <!--  This "code", such as it is, is in the public domain.   -->

    <!--  We give the user a text-edit widget, and the user can  -->
    <!--  copy/paste a writeup into it.  The user then clicks    -->
    <!--  "submit", and we add <br> tags to the ends of all the  -->
    <!--  lines and select the whole text in the edit.  The      -->
    <!--  user can then very conveniently copy and paste the     -->
    <!--  break-tagged text back into, like, whatever.           -->

    <script language="JavaScript">
        function do_replace( widget ) {
            widget.value = add_br( widget.value, 
                                   document.breaktagger.fixtabs.checked );

            return false;   //  Even if this was invoked by a "submit" button,
                            //  don't submit.

        function add_br( str, fix_tabs ) {
            //  Props to dbrown for rephrasing the regular expressions below; 
            //  the second is more pleasing than what I had, and both of them 
            //  avoid E2 Square Bracket Hell.

            //  (\r\n|\r|\n):  Allow for Windows/DOS/UNIX/Mac newline madness.  
            if ( fix_tabs )  
                str = str.replace( /(\r\n|\r|\n|^)(\t| )+/g, '$1<dd>' );

            //  We remove all old break tags, provided they're at the ends of 
            //  lines.  We also remove all trailing whitespace, while we're at
            //  it.  I'm tidy that way.
            return str.replace( /(<br>|\t| )*(\r\n|\r|\n)/g, '<br>$2' );

    <p><font color="#a00000"><b>
    This is not going to work, because you don't have JavaScript.
    You may not have it at all, or you may just have it disabled.  It comes 
    to the same thing either way.

<form name="breaktagger">
    <textarea name="edit" cols="80" rows="20"></textarea>

    <input type="button" name="submit" value="Add Break Tags"
        OnClick="return do_replace( document.breaktagger.edit )"></input>

    <input type="checkbox" name="fixtabs" value="0">
        Replace indenting with <tt>&lt;dd&gt;</tt> tag</input>

<!-- CUT HERE -->

sockpuppet must be ironic or just not very experienced with JavaScript if he's impressed with the above :), but he's got a real good point. However, I wasn't aiming this at people who can do it in emacs, vi, or some other text editor -- nor perl, either. They're probably doing it already; I've been adding <br> tags for months now with a simple sed command (sed "s/$/<br>/g") called from Edit Plus.

Incidentally, I've written a Win32 command line utility that can read from, and write to, the Windows clipboard: If you can modify that Win32 perl command line to work on redirected text, that might make things even easier. Source or binary available on request. The source is a bit slobby; I may clean it up and node the thing.

I also considered writing a Win32 system tray utility, which would sit there quietly until you double click on it: When that happened, it would take whatever was in the clipboard and add break tags just like this thing. But that would have taken longer to write, and it'd be platform dependent, so I went with this deal instead.

sockpuppet #2: Ahh, the irony (or lack thereof) is more clear now. :)

I think what's nifty here is not merely the 'l33t skillz thing, but the vast array of different solutions to the same problem, and the strengths of each...

# -- cut here

#   Sed script to format code for www.everything2.org
#   Angle brackets must be replaced, naturally; 
#   Ampersands must be replaced because it could be HTML "code", in which 
#       case we want to reproduce the text of literal character entities 
#       rather than let the browser display the entity; 
#   Square brackets are replaced because E2 treats square-bracketed text 
#       as a directive to create a link.
#   GNU sed version 3.02 doesn't seem inclined to treat the '#' characters 
#       in the regexen below as comments; I can't say about your version, 
#       though.
#   wharfinger  9/20/00


# -- cut here

I am in awe of wharfinger's JavaScript acumen, but this is easier to do in an editor or from the command line.

In vi:

: (to get into edit mode)
1,$ s/$/<br>/g
:wq  (to write results and quit)


(go to the top line of the file)
M-x replace-regexp (return) $ (return) <br> (return)

Using perl from the command line:

perl -pi.bak -e 's/\n/<br>\n/' my.txt  (unix shell)
perl -pi.bak -e"s/\n/<br>\n/" my.txt  (Windows cmd shell - note double quote, lack of space after -e)

You can omit the ".bak" if you're willing to risk not having a backup when you modify your file in place. If you don't have perl, why not? Unless you're reading this on your WAP phone, it's probably available for your computing platform.

Update: Ah yes, forgot to mention this: if you want to use stdin/stdout, just omit the -i (and the .bak if you're using it), thus:
perl -p -e 's/\n/<br>\n/' <my.txt >mybr.txt  (unix)
perl -p -e"s/\n/<br>\n/" <my.txt >mybr.txt  (Windows)

You could also make a small script that would accept stdin and act as a filter, such as:

#!/usr/bin/perl (or wherever you've put it)

while(<>) {

Perl is fun!

Oh, and in answer to wharfinger above: I'm partly in ironic mode (since the script on inspection is completely understandable to even a JavaScript novice like myself), but partly impressed that you would think to whip up such a thing up in JavaScript so that anyone, even the poor perl-less folk, can do it. It's lovely--really. So on the whole it was intended as a compliment. And indeed I am a JS-dummy: what I do for a living is very non-webbish, so everything I learned about HTML and (now) JavaScript I owe to y'all here at E2. Thanks!

In BBEdit on the Macintosh:

  • Hit Command+F for Find...
  • Tick the box beside "Use Grep"
  • Enter "$" in the "Search for..." box
  • Enter "<br>" in the "Replace With..." box
  • Hit the "Replace All" button

Et voila! I count eight keystrokes/mouse clicks in total, making this easily as efficient as vi/command line jiggery pokery, if not quite as satisfying.

Addendum: to replace tabs with <dd>s, simply repeat the steps above, substituting "\t" for "$" and <dd> for <br>.

Log in or register to write something here or to contact authors.