1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#!/usr/bin/perl
#
# Copyright (c) 2001 Tony Sideris
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#
####################################################
## Simple text -> html translator
## Run with no args or --help for usage
##
## by Tony Sideris 1/22/02
####################################################
use strict;
my $LIST_TAG = "ul"; ## HTML list type
&usage && exit unless @ARGV;
## Process cmdline args
while (@ARGV) {
$_ = shift @ARGV;
if (/--help/) { usage(); }
elsif (/--ordered/) { $LIST_TAG = "ol"; }
elsif (/--para/) { para(); }
elsif (/--list/) { list(); }
}
# ----------------------------------------------------
# Convert to paragraphs
sub para
{
$/ = "";
while (<>) {
s/[\n \t]+$//m; ## Cleanup the end of the paragraph
print "\n<p>" . translate($_) . "</p>\n";
}
}
# ----------------------------------------------------
# Convert to a HTML list
sub list
{
my $level = -1;
## Read the input line by line, count the tabs
## at the start of each line, if the tab count
## is greater that the previous tab-cout then
## start a new branch in the list, if the
## tab-count is less than the previous, then
## close all open branches.
while (<>) {
/(\t*)(.+)/;
my $newlen = length($1);
if ($newlen > $level) { print "\t$1<$LIST_TAG>\n"; }
elsif ($newlen < $level) {
my $diff = $level - $newlen;
print "\t\t$1";
print "</$LIST_TAG>" x $diff;
print "\n";
}
print "\t$1<li>" . translate($2) . "\n";
$level = $newlen;
}
## Close all open branches
print "\t" . "</$LIST_TAG>" x ++$level . "\n";
}
# ----------------------------------------------------
# Translate HTML special characters and make
# auto hyperlinks in content.
sub translate
{
$_ = shift;
s/&/&/g; ## Must be first!
s/\([cC]\)/©/g;
s/\</</g;
s/\>/>/g;
s/"/"/g;
s,\b((?:http|ftp|mailto):(?://)?[\S]+)\b,<a href='\1'>\1</a>,gm;
return $_;
}
# ----------------------------------------------------
# Display script usage
sub usage
{
print <<FOO;
textfmt usage:
textfmt [options] <operation> [file]
operations:
--para adds <p> tags to blank-line delimited paragraphs of text
--list creates a list from hierarchly indented text (<ul> by default)
--help displays this message and quits
options:
--ordered makes --list create a <ol> list
Reads stdin if no input file is given, all output
goes to stdout, redirect as needed...
FOO
return 1;
}
|