#!/usr/bin/perl -w

# $Id: txt2lout,v 1.18 2000/02/01 21:08:34 root Exp $

# Copyright (c) 1999-2000 Mark Summerfield. All Rights Reserved.
# May be used/distributed under the GPL.

use strict ; 

use Getopt::Long ;
use Lout qw( txt2lout ) ;

use vars qw( $VERSION ) ;
$VERSION = '1.06' ;

my %Opt ;

# main

&process_args ;

if( $Opt{'outfile'} ne 'STDOUT' ) {
    open OUT, ">$Opt{'outfile'}" or die "Failed to open $Opt{'outfile'}: $!\n" ;
    select( OUT ) ;
}

&head if $Opt{'wrapper'} ;

my $verbatim = 0 ;

while( <> ) {
    if( $verbatim ) {
        if( /^=endlout/o ) {
            $verbatim = 0 ;
        }
        else {
            print ;
        }
    }
    elsif( /^=lout/o ) {
        $verbatim = 1 ;
    }
    elsif( /^\s*$/o ) {
        # Paragraphs separated by blank lines.
        print "\@PP\n$Opt{'extraspace'}" ;
    }
    else {
        print txt2lout( $_ ) ;
    }
}

&tail if $Opt{'wrapper'} ;

sub process_args {

    $Opt{'outfile'}      = 'STDOUT' ;
    $Opt{'wrapper'}      = 1 ;
    $Opt{'hyphen'}       = 0 ;
    $Opt{'smartquotes'}  = 1 ;
    $Opt{'superscripts'} = 1 ;
    $Opt{'extraspace'}   = 0 ;
    $Opt{'verbatim'}     = 1 ;
    $Opt{'wrapper'}      = 1 ;

    &help if defined $ARGV[0] and $ARGV[0] =~ /^(-h|--help)$/o ;

    Getopt::Long::config 'no_ignore_case' ;
    GetOptions( \%Opt,
        'hyphen|H=i',
        'extraspace|e=i',
        'outfile|o=s',
        'smartquotes|q=i',
        'superscripts|s=i',
        'wrapper|w=i',
        'verbatim|v=i',
        ) or &help ;

    Lout::set_option( -hyphen       => $Opt{'hyphen'} ) ;
    Lout::set_option( -smart_quotes => $Opt{'smartquotes'} ) ;
    Lout::set_option( -superscripts => $Opt{'superscripts'} ) ;
    Lout::set_option( -verbatim     => $Opt{'verbatim'} ) ;

    $Opt{'extraspace'} = $Opt{'extraspace'} ? "\n" : '' ;
}

sub help {
    print <<__EOT__ ;

txt2lout v $VERSION. Copyright (c) Mark Summerfield 1999-2000. 
All rights reserved. May be used/distributed under the GPL.

usage: txt2lout [options] <files>

options:
-h --help           Print this help screen and exit
-H --hyphen       b Turn space delimeted - into -- [$Opt{'hyphen'}]
-e --extraspace   b Print blank lines between paras [$Opt{'extraspace'}]
-o --outfile      s Write to file or STDOUT [$Opt{'outfile'}]
-q --smartquotes  b Smartquotes [$Opt{'smartquotes'}]
-s --superscripts b Superscripts for 1st, 2nd etc [$Opt{'superscripts'}]
-w --wrapper      b Wrap text in lout \@Begin..\@End [$Opt{'wrapper'}]
-v --verbatim     b Treat text between =lout and =endlout lines and
                    V<\@Verbatim lout> as literal lout [$Opt{'verbatim'}]

b = boolean 1 = true, 0 = false; i = integer; s = string e.g. filename
The -e option is useful for troff and tex spacing.

e.g. for use in an editor like vim:
    .,\$!txt2lout -w 0 -e 1
e.g. from the command line:
    txt2lout file.txt > file.lt ; lout -s file.lt > file.ps
__EOT__
    exit ;
}

sub head {
 
    my( $d, $m, $y ) = (localtime( time ))[3..5] ;
    $y += 1900 ; $m++ ; 
    $m = "0$m" if $m < 10 ; 
    $d = "0$d" if $d < 10 ;

    print <<__EOT__ ;
\@SysInclude { tbl }
\@SysInclude { doc }
# Created by txt2lout on $y/$m/$d.
\@Doc \@Text \@Begin
__EOT__
}

sub tail {
    print <<__EOT__ ;
\@End \@Text
__EOT__
}

__END__

=head1 NAME

txt2lout - convert plain text to lout

=head1 README

txt2lout - convert plain text to lout

=head1 PREREQUISITES

C<strict>

C<Getopt::Long>
C<Lout>

=head1 COREQUISITES

=head1 COPYRIGHT

Copyright (c) Mark Summerfield 1999. All Rights Reserved.
May be used/distributed under the GPL.
Email <summer@perlpress.com> with 'txt2lout' in the subject line.

=head1 OSNAMES

any

=head1 SCRIPT CATEGORIES

Lout
Text-processing

=cut
