Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
We don't bite newbies here... much

Simple tool for making HTML blockquotes from text selections

by Intrepid (Deacon)
on Dec 14, 2003 at 10:57 UTC ( #314621=sourcecode: print w/ replies, xml ) Need Help??

Category: Related Scripts
Author/Contact Info Soren / somian / Intrepid check here

This is "pmnodecomp-blockquoter", and if you can say that 5 times and still chew salt-water taffy afterwards you get a prize. It's a helper for working in a Xterm shell and composing node text for posts here on Perlmonks. If you are replying to an existing node and wish to cite a brief part of what a previous poster has written, you may like to do so using a style that embraces the quote text in <blockquote> tags. This makes it visually stand out as being text attributable to another poster than yourself.

Related Links:

A cryptographically signed copy of the latest release of the program may also be obtained at the site:


Now does what theorbtwo's reply and bart's reply wanted it to do. Also fixed a few other flaws. We now have a plugin-ready script that users can invent their own transmutations for.

Also the example shown below ought to be something like this instead (but see later at Second Update for an even better plan):

Then I switch to a text term (Xterm) and type:
$ pmnodecomp-blockquote <<'NOPOL8N'
which waits for me to paste the X cutbuffer / clipboard selection at the prompt.
Thanks to merlyn for the righteous slap that dispels brain-fevered idiocy like a bucket of icewater on a pair of amorous mutts.

There's nothing automagickal or sophisticated about it, it's a very simple tool. It is for when you don't care to fire up a text editor just for a quick-ish reply node, but you want to efficiently create a blockquote as part of your contribution. It's for a very lazy approach, made especially for those of us who's typing skills are not world-class ;-).

This was also an excuse for me to beat my head against the strangeness that is Perl's format and write functionality until I understood it well enough to write something useful employing them.

Users of a *NIX-like ('sh'-like) shell will get the best use out of this.

The next section is superceded by later refinements and corrections. Left for historical thread context.

I like the style of workflow in which I make an X-Windows selection of the text I wish to quote from the Perlmonks node (in my WWW browser). Then I switch to a text term (Xterm) and type:

$ cat <<'DONE' | pmnodecomp-blockquote
which waits for me to paste the X cutbuffer / clipboard selection at the prompt. Shell veterans may know other ways (TMTOWTDI) of getting the desired text poured into STDIN and then issuing the EOF (generally <CTRL-D> on *NIX, IAMNM). Anyway that's the general idea.

A completely empty newline must be present for paragraphs to be handled as separate from the preceeding one. This could be construed as a limitation ;-).

    Have fun --
Soren / somian / Intrepid

Second Update

After doing some testing I can recommend a refined "workflow" scheme for those who might be left in a second or two of puzzlement about how to use this tool otherwise. If you have the Linux/*nix tool xclip(1) on your system you can use it nicely like so:

  1. Make your selection with mouse in the source application/document window.
  2. Switch to your Xterm-like tool and issue the following command, i.e. :
    $ xclip -selection p -o | \ pmnodecomp-blockquoter | \ xclip -fi -selection c
    The -f switch to the last invocation of xclip will let that tool echo the output processed text to your terminal, acting as a preview so that you can see what was done. Eliminate it if you want silent operation.
  3. Switch to the Perlmonks node you wish to compose in your browser, place text cursor in the input box and issue CTRL-V or whatever maps to "paste" in your X environment. The blockquoted text now on the X clipboard will appear at your cursor position.

I realize these instructions will seem like huge overkill to many of the experts on the site, but we all had to learn somewhere, and I didn't want to leave novices to X or computers completely up the creek w/o a periscope. Adapting these instructions for another kind of environment that still has a usable shell (DOS would not fall under that category) is left as an exercise to the readers :-)

#!/usr/bin/env perl
eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

# $Author$ Last modified: 14 Dec 2003 15:30:18
# Suggested name: "pmnodecomp-blockquoter" - shorten if wished.
# Or better yet make a symlink or alias.
use strict;
    use vars qw! $plugin !;
    $plugin =
       (eval    { # Place your own home-brewed filter module below.
              # This is only a suggestive example / default.
         require PMntextFilter;
           # import  PMnTextFilter "quazzle wrench bangbar";
        }) ? 'extern' : 'intern';

use warnings;
use vars qw! $DeBug $acq $outfm $rcnt $pictstr !,
         qw! $amps $ltgt $sqbr !;
use subs qw! bbegin bendin newblk filtermeta !;

$DeBug = 0;
use constant rCNT => 66; # arbitrary length for output lines.
format EBLK =

format BBLK =
format NBLK =

if    ($plugin eq 'extern')
    $amps = sub { PMntextFilter::amps(@_) };
    $ltgt = sub { PMntextFilter::ltgt(@_) };
    $sqbr = sub { PMntextFilter::sqbr(@_) };
    $amps = sub
    my $amper = '&amp;';
    my $para = shift;
    $para =~ s% \& %$amper%oxgm;
    return $para;
    $ltgt = sub
    my ($lt,$gt) = ( '&lt;','&gt;' );
    my $para = shift;
    $para =~ s% \< %$lt%oxgm;
    $para =~ s% \> %$gt%oxgm;
    print STDERR "\n$acq\n" if $DeBug;
    return $para;
    $sqbr = sub
    my ($sqbro,$sqbrc) =
                  ( '&#91;','&#93;' );
    my $para = shift;
    $para =~ s% \[ %$sqbro%oxgm;
    $para =~ s% \] %$sqbrc%oxgm;
    print STDERR "\n$acq\n" if $DeBug;
    return $para;

$pictstr = q[^] . q[<] x rCNT;
$outfm = qq[format STDOUT = ]
        .qq[\n        $pictstr\n]
        . q[$acq] . qq[\n.\n];
eval $outfm;
   die "Bad perl format!: $@" if $@;
my $splurt = sub { local $/ = qq[]; $acq = <STDIN> };
my $pc = 0;
while (&$splurt and $pc++ < 10000)
    filtermeta  $acq;
    write while $acq;
    newblk unless eof(STDIN);

exit 0;

sub filtermeta
{ # Do what you want to here. Given is a minimal default.
sub newblk
    local $~ = "NBLK";
sub bbegin
    local $~ = "BBLK";
sub bendin
    local $~ = "EBLK";

Comment on Simple tool for making HTML blockquotes from text selections
Download Code
Re: Simple tool for making HTML blockquotes from text selections
by bart (Canon) on Dec 14, 2003 at 11:40 UTC
    It doesn't escape HTML characters, it simply passes along the entered text, rewrapped, with extra <blockquote> tags around it. When I mentioned this in the Chatterbox, you appeared to not give a damn.

    Update: Appearances can be desceptive, perhaps the author's tiredness could have played a role too &mdash after all, it was the middle of the night for him at the time; but from later conversations it appears to mainly have been a misunderstanding between us. Glad that has been cleared up.

•Re: Simple tool for making HTML blockquotes from text selections
by merlyn (Sage) on Dec 14, 2003 at 14:35 UTC
Re: Simple tool for making HTML blockquotes from text selections
by theorbtwo (Prior) on Dec 14, 2003 at 16:19 UTC

    The problem with this is that it does not quote < and & properly. This isn't normaly a problem, because PM allows you to use < many places, escaping it itself, and browsers will display &foo; as such if "foo" is not a known escape. (It's important to note that the former is an explicit action of PM, to allow people to right $x<42, <g> and other similar things without additional effort, whereas the later is a normal part of rendering HTML, so even when a UA recives an entity that it does not know, the user has a chance to guess at what it means from the mnemonic name.

    However, try writing a block quote of &amp; <b>this should not be bold</b>

    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: Simple tool for making HTML blockquotes from text selections
by zengargoyle (Deacon) on Dec 14, 2003 at 22:15 UTC

    if you're on a unixy box, take a look for a program called 'xclip'.

    $ xclip -o | pmnodecomp-blockquote

    it should take care of all of that ^D or <<EOF nonsense.

    plus it's a good example of why 'useless use of cat' is a good thing (IMHO). using useless cat allows you to easily replace the useless cat by a usefull program changing only what comes before the pipe.

      Thanks for the suggestion, zengargoyle, and also to TVSET (replies that follow). I wanted to avoid giving the impression that I had appropriated your idea w/o so much as a nod in your direction (in Updates above, in root node) -- in fact I came up with and reposted much the same workflow plan independently before revisiting the node and seeing your reply. Nonetheless thanks very much; obviously great minds ... or some kind of minds ... think alike. ;-)

          Soren / somian / Intrepid

      use PerlMonk::Tye qw(:wisely);
Re: Simple tool for making HTML blockquotes from text selections
by TVSET (Chaplain) on Dec 14, 2003 at 23:11 UTC

      ++ for the handy link. and sorta on the same subject the MozEX extension for Firebird will add a 'Edit Textarea' to the right-click context menu for textareas. mine is configured for VIM. when you exit your editor just click in the textarea again to update the text. this is my favorite extension ever.

      there might be similar things for other browsers.

        there might be similar things for other browsers

        Yup. w3m (console browser like lynx, or more likely links) allows you to configure the editor to be used for text areas.

Re: Simple tool for making HTML blockquotes from text selections
by exussum0 (Vicar) on Dec 15, 2003 at 05:04 UTC
    The only way I'd modify this, is to take all the input in at once and shoot it back out after it's done.

    Otherwise, if I'm working from stdin, I get this:

            sadlfjsafjlsdf sadfkljsdlafjlsadf sadfljsadfljsadfjlksaf

    Play that funky music white boy..

Back to Code Catacombs

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://314621]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-04-17 10:22 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (444 votes), past polls