(Updated, again, 11/26/03—Thanks Roy and Coruscate.)

A while ago on /., while viewing at -1, I witnessed one, um, "condemned" post that utilized a rather bizarre style of markup; one where bold tags were randomly inserted around letters, creating an eye-catching/unique/annoying effect. I've tried to recreate that using Perl, and this script is the result.

Run the script with -h for usage:

#!/usr/bin/perl -w use 5.004; use strict; use Getopt::Std; srand($$ ^ time); # get the options: my %opts; usage() unless (getopts('t:i:o:h', \%opts)); usage() if ($opts{'h'} || !$opts{'t'}); # the opening and closing tags we'll insert into the text: my $opening_tag = "<$opts{'t'}>"; my $closing_tag = "</$opts{'t'}>"; # open the input file for reading, or, if no file was specified, get s +ome text # from the console: my $text_from_console; if ($opts{'i'}) { open(INFILE, "< $opts{'i'}") || die "Couldn't open file ($opts{'i'}) to read text: $!"; } else { print "Enter some text: "; $text_from_console = <STDIN>; } # now randomly insert the specified HTML tag into the text: my $tagged_text = ''; my $unclosed_tag = 0; while (my $line = $opts{'i'} ? <INFILE> : $text_from_console) { my $char; while (length($char = substr($line, 0, 1, ''))) { if ($char !~ /\s/ # skip spaces. and int rand 2) # 0 or 1. { # add the opening tag unless the previous character is # within a tag that has yet to be closed: $tagged_text .= $opening_tag unless ($unclosed_tag); $tagged_text .= $char; $unclosed_tag = 1; } else { # Either this is a space or a non-space character that # was rejected by rand(). Regardless, close the last tag # if it's still open: if ($unclosed_tag) { $tagged_text .= $closing_tag; $unclosed_tag = 0; } $tagged_text .= $char; } } # break if we got text from the console instead of from a file, si +nce # there's nothing left: last unless ($opts{'i'}); } close INFILE if ($opts{'i'}); if (my $output_file = $opts{'o'} || $opts{'i'}) { # print the text with tags inserted into it to a file: open(OUTFILE, "> $output_file") || die "Couldn't file ($output_file) to save text: $!"; print OUTFILE $tagged_text; close OUTFILE; print "Saved text to $output_file.\n"; } else { print "\n$tagged_text"; } sub usage { print <<'END_OF_USAGE'; This script randomly inserts a specified HTML tag into some text. Usage: $ taginserter [FLAGS | OPTIONS...] Options: -t The name of the tag you want inserted (e.g., "b" for <b> (bol +d), "i" for <i> (italic), etc.). -i (Optional.) The file which taginserter will read the text you + want tags inserted into from. If this isn't supplied, then you'll +be prompted to enter some text. -o (Optional.) The file where you want the text outputted to aft +er the tags have been inserted. If you don't supply this, then t +he filename you supplied to the -i option will be used instead. +If you didn't supply a filename to -i, then the text will be pri +nted out to the console. Flags: -h Displays this message. END_OF_USAGE exit; }

For example, running the PM copyright notice through the script with -t set to "b" converts this:

This page brought to you by the kind folks at The Everything Development Company and maintained by Tim Vroom. Perl Monks somehow became entangled with Yet Another Society. Beefy Boxes and Bandwidth Generously Provided by pair Networks

to this:

This page brought to you by the kind folks at The Everything Development Company and maintained by Tim Vroom. Perl Monks somehow became entangled with Yet Another Society. Beefy Boxes and Bandwidth Generously Provided by pair Networks


In reply to CUFP: Random tag insertion by William G. Davis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.