#!/usr/bin/perl
# makems;
# Last updated: 2005-05-22
$version = '1.4';
$copyyear = '2005';
=head1 Roqet MakeMS v1.4
A Perl script to convert text files (plain or line-wrapped at 80
characters) into manuscript for further desktop publishing modification
by concatenating the lines (unless the lines do not contain text (as in
a paragraph page)) and reformatting them into Rich Text Format (RTF),
which can be read by Microsoft Word, OpenOffice and many other
word-processors. It also does a wordcount, and prepends the name,
address and wordcount to the manuscript.
=head1 Install
Roqet MakeMS requires 2 additional Perl modules to
be installed on your system, XML::Simple and RTF::Writer
Module Install;
linux/unix:
perl -MCPAN -e shell
cpan> install XML::Simple
cpan> install RTF::Writer
cpan> exit
windows (9x, NT, 2000 + XP):
ppm
PPM> install XML::Simple
PPM> install RTF::Writer
PPM> exit
=head1 Usage
As per the makems.xml example, create an xml file that contains the
configuration of the RTF file (author name, file names etc.). The name
of the xml file should reflect the name of the file you wish to create,
e.g. myfile.xml will create myfile.rtf.
Once the xml file has been created, run:
perl makems.pl fullpathname/filename.xml
=head2 makems.xml example
C:\roqetman\lang\perl\rtf\testfile.txt
roqetman's Test Manuscript
roqetman
Georgia
\fs15
\fs15\f2 roqetman's test MS, p.
Georgia
\sl480\fs20
=head1 Author
roqet - http://www.roqet.org
=head1 Copyright
MakeMS Perl script copyright 2005 roqet - http://www.roqet.org
Deploy et al can be distributed and modified under the terms of the
GNU General Public License: http://www.gnu.org/copyleft/gpl.html
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 of the License, 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.
Updates and other scripts and software available at:
http://www.roqet.org/software.html
~~~
'l'
-
=cut
require 5.005;
use XML::Simple;
use RTF::Writer 1.01;
use File::Basename;
#
# read input parameters
#
if ($ARGV[0] eq '') {die "Parameter XML file not entered!\n to run: perl makems.pl filename.xml\n";}
$xmlname = $ARGV[0];
$paramfile = XMLin("$xmlname");
print "Roqet MakeMS v$version\n";
print "Copyright $copyyear roqet \n";
open TEXTIN, $paramfile->{input_file} or die "error opening " . $paramfile->{input_file} . ": $!\n";
@textfile = ;
$wordcount = 0;
foreach $line (@textfile) {
chomp($line);
if ($line ne " "){
# splits the words in a line delimited by one or more whitespace
@words = split (/ +/, $line);
# increment wordcount.
$wordcount += @words;
}
}
undef @textfile;
close TEXTIN;
open IN, $paramfile->{input_file} or die "error opening " . $paramfile->{input_file} . ": $!\n";
($name, $path, $suffix) = fileparse($ARGV[0], '\..*');
$outfile = $path . $name . '.rtf';
my $rtf = RTF::Writer->new_to_file($outfile);
#
# Manuscript Initial Stuff
#
$tempname = $paramfile->{input_file};
# convert all "\" in pathname to "\'5c"
$tempname =~ s/\\/\\\'5c/g;
$rtf->prolog( 'title' => $paramfile->{title},
'author' => $paramfile->{author},
'doccomm' => 'Converted from text file: ' .
$tempname . ' to RTF using Roqet MakeMS v' .
$version . ' (Copyright ' . $copyyear . ' roqet) on ' . draftdate(),
'fonts' => $paramfile->{font} );
if ($paramfile->{page_number_detail} eq '.') { $page_number_detail = ' '; }
else { $page_number_detail = $paramfile->{page_number_detail}; }
$rtf->number_pages(\$page_number_detail);
#
# Manuscript Header
#
$header = $paramfile->{author_details};
$outdate = draftdate();
$header =~ s/\(\*author\*\)/$paramfile->{author}/g;
$header =~ s/\(\*draftdate\*\)/$outdate/g;
$header =~ s/\(\*wordcount\*\)/$wordcount/g;
$rtf->paragraph( \'\fs15', $header );
#
# Manuscript Details
#
my $line = '';
while () {
my $inline = $_;
chomp($inline);
if ($inline eq '') {
$paradetail = $paramfile->{paragraph_detail};
$rtf->paragraph( \$paradetail, "$line" );
$line = '';
}
else {
$line =~ s/\n/ /g;
$line = $line . $_;
}
}
close IN;
$rtf->close;
print 'Conversion of ' . $paramfile->{input_file} . ' to ' . $outfile
. " complete.\n";
###############
# #
# subroutines #
# #
###############
sub draftdate {
#
# returns date & time for the current draft
#
($dayofweek, $day, $month, $year, $hour, $minute, $second) = (localtime)[6,3,4,5,2,1,0];
$date = sprintf("%04d-%02d-%02d", $year+1900, $month+1, $day);
$time = sprintf("%02d:%02d:%02d", $hour, $minute, $second );
return("$date $time");
}
exit;