Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
(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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2021-12-03 20:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (30 votes). Check out past polls.

    Notices?