#!/usr/bin/perl ########################################### # af -- autoformat bulleted text # Mike Schilli, 2002 (m@perlmeister.com) ########################################### use warnings; use strict; our $VERSION = "1.00"; our $CVSVERSION = '$Revision: 1.4 $'; use Text::Wrap qw(fill $columns $huge); $columns = 70; $huge = 'overflow'; # Slurp in whole paragraphs local $/ = ""; my @MODE = (); my @TAIL = (); ################################################## sub start { ################################################## my($newmode, $head, $tail) = @_; if(@MODE and "$MODE[-1] $newmode" =~ /(list)(\d+) \1(\d+)/) { if($2 < $3) { push @MODE, $newmode; push @TAIL, $tail; print $head; } else { while(@MODE and $MODE[-1] ne $newmode) { print scalar pop @TAIL; pop @MODE; } } return; } while(@MODE and $MODE[-1] ne $newmode) { print scalar pop @TAIL; pop @MODE; } print $head; @MODE = ($newmode); @TAIL = ($tail); } while(<>) { if(/^[^*\s-]/) { # Text at the beginning of a line: # It's a text paragraph. start('text', "", "\n\n"); print fill("", "", "$_"); } elsif(/^\s/) { # Indented code start('code', "", ""); print; } elsif(/^\s*(\*{1,2})/) { # Head1/2 my $i = length($1); s/^\s*\*+\s+//; start("head$i", "=head$i ", "\n\n"); s/\s+/ /sg; print $_; } elsif(/^\s*(-+)/) { # Bullet point my $i = length($1); s/^\s+//gm; s/^\s*-+\s*//; start("list$i", "=over 4\n\n", "=back\n\n"); print "=item *\n\n"; print fill("", "", "$_"), "\n\n"; } else { print "Unrecognized chunk: '$_'\n"; } } print reverse @TAIL; __DATA__ * Head1 ** Head21 - item1 is so long you won't believe it. Really, really, really long. Indeed very long. So long, it won't even fit on one line. - item2 is so long you won't believe it. Indeed very long. -- sub bullet list - another list item A Text paragraph, so long, you won't believe it. Really, really, really long. Indeed very long. # Indented code print(); ** Head22 __END__ =head1 NAME af - Autoformat ascii text for POD =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS af [file] =head1 DESCRIPTION C transforms ASCII text to POD. It understands three different types of formatted ASCII text: =over 4 =item * Regular paragraphs of text. These begin at the beginning of a line without any leading whitespace and continue until an empty line or the end of file is encountered. These paragraphs are trimmed to a line length of 70 using a word-bound linebreak algorithm. If a line can't be broken (e.g. because if consists of just one word), it just overflows. This makes sure long URLs etc. are not chopped. =item * Indented code. These listings aren't formatted at all, they're handed over to POD I. =item * Headlines. These start with a C<*>: * GNUs -- how to tame them =item * Sub-Headlines. These start with a C<**>: ** Approaching GNUs =item * Bullet lists. Items in the list just start with a C<->: - Grab the GNU by the horns =item * Sub-Bullet lists. If one bullet point contains a sub-bullet list, just use two C<-->s: -- Grab the left horn =back It's important that every marked sections is within a separate paragraph. C reads its input paragraph by paragraph and determines for each of them what kind of markup it is by looking at the first couple of characters. =head1 LEGALESE Copyright 2002 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR 2001, Mike Schilli