Everything2
Near Matches
Ignore Exact
Full Text
Everything2

E2 Break Tag Utility

created by wharfinger

(thing) by wharfinger (6.4 y) (print)   ?   (I like it!) 1 C! Mon Sep 11 2000 at 17:20:56

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.

Revisions:

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


<!-- CUT HERE -->
<html>
<head>
    <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 );
            widget.select();
            widget.focus();

            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' );
        }
    //-->
    </script>
</head>

<body>
<noscript>
    <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.
    </b></font></p>
</noscript>

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

    <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>
</form>

</body>
</html>
<!-- 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

s/&/\&amp;/g
s/\[/\&#91;/g
s/\]/\&#93;/g
s/</\&lt;/g
s/>/\&gt;/g

# -- cut here


(thing) by sockpuppet (7.2 mon) (print)   ?   (I like it!) 1 C! Mon Sep 11 2000 at 18:20:42

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)

In EMACS:

(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(<>) {
     s/\n/<br>\n/;
     print;
}

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!


(thing) by ryano (1.9 wk) (print)   ?   (I like it!) 1 C! Tue Sep 12 2000 at 9:48:45

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

printable version
chaos

my first perl program l33t sp34k BBEdit EditPlus
JavaScript Nuclear war is not dangerous It's How I Spell Ireland grep
Script Paradise Lost E2 September 11, 2001 book l33t
Macintosh square brackets // SED
Talkin' all that Jazz ROT13 Everything2 in the Pages of Cool What some stuffy Victorians had to say about poetry
Everything University Americans don't speak English E2 AC/DC
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Just another sprinkling of indeterminacy
Summit in Savannah
Life of a government employee
Angostura bitters
Pinyin
What Can I Point To?
What to do if a big dog attacks you
aircraft
September 11, 2001 - III
Excerpts from a letter to President Pierce from Chief Seattle
fingernail
What is Everything2?
Devo
hippopotamus
New Writeups
a8ksh4
regret(idea)
Heisenberg
Editor Log: July 2008(log)
sam512
halfway homes, catacombs, twilight zones(fiction)
Timeshredder
The Texas UFO Crash of 1897(event)
Heitah
The Dark Knight(review)
ignis_glaciesque
Uppsala(place)
ignis_glaciesque
diffusion of responsibility(idea)
TheOrientalAfrican
The Soft Meadow of my Childhood(event)
BookReader
The Dragon Slayers(fiction)
kohlcass
religiously fashionable(review)
Pavlovna
waulking song(thing)
tentative
Stick Man(poetry)
Ereneta
The Fight with the Snapping Turtle: Or, the American St. George(poetry)
sitaraika
Fog and fire(personal)
MonoliTheory
She sobs in response(fiction)
E2 is a by-product of the existence of The Everything Development Company