#!/usr/bin/perl -Wall #Open file called "novel", do some formatting, output result into #"formattednovel, clobbering any existing formattednovel files. Create #a chapter index called index.html. #################################################################### #formatting rules for the raw text: #title: title must be given as "Title: " - program will insert #a tag for css chapter formatting #chapters: each chapter is given as "Chapter: <chaptertitle>" - #program will insert a chapter number and a tag for css chapter #formatting #newline: an empty line indicates a new line, indented #paragraph: leave two empty lines #################################################################### $filename = "novel"; $outfilename = "formattednovel.html"; $indexfilename = "index.html"; open (FILE, $filename) or die("Couldn't open file '$filename'.\n"); open (OUTFILE, ">$outfilename") or die ("Couldn't open output file '$outfilename'.\n"); print(OUTFILE "<html>\n<body>\n"); open (INDEXFILE, ">$indexfilename") or die ("Couldn't open index output file '$indexfilename'.\n"); print(INDEXFILE "<html>\n<body>\n"); #css formatting $cssformatting = "<style type=\"text/css\"> <!-- body { margin-left: 2cm; margin-right: 4cm; width: 15cm; color: black; background: white; line-height: 150%; } a:link {color: maroon} a:visited {color: navy} div.title, div.chapter { font-family: \"arial\", sans-serif; } div.title { font-size: 150%; } div.chapter { font-size: 120%; } --> </style>"; print(OUTFILE $cssformatting); print(INDEXFILE $cssformatting); #initialize the index file for chapter listing print(INDEXFILE "<html>\n<body>\n"); #print out date $datestring = `date "+%a %b %e, %H:%M"`; print(INDEXFILE "<p align=\"right\">Last modified: $datestring</p>"); #Now go through the text and inset break and paragraph tags as well as #paragraph indentation. If we have one empty line, indent for a quote #if we have two empty lines in a row, it's a paragraph. $newlines = 0; #Start of paragraph is true if it's the first line after an empty #line. $startofparagraph = 0; #use automatic chapter numbering $chapternumber = 0; while(<FILE>) { #check for newline and start of paragraph and set flags if (/^\n/) { $newlines++; } else { #paragraph line if ($newlines > 0) { $startofparagraph = 1; } $newlines = 0; } #now add break tags, paragraph tags, and indents as necessary if ($newlines == 1) { $_ =~ s/\n/<br>\n/; } if ($newlines == 2) { $_ =~ s/\n/<p>\n/; } if ($startofparagraph == 1) { if (!/Chapter/ && !/Title/) { $_ = "        \n".$_; $startofparagraph = 0; } elsif (/^\s*Chapter:/) { $chapternumber++; $_ =~ s/Chapter:\s*(.*)/<a name=\"$chapternumber\"><div class="chapter">Chapter $chapternumber: $1<\/div>/; print(INDEXFILE "<div class=\"chapter\">Chapter $chapternumber: <a href=\"$outfilename#$chapternumber\">$1</a></div>\n<p>"); } elsif (/^\s*Title:/) { $_ =~ s/Title:\s*(.*)/<div class="title">$1<\/div>/; print(INDEXFILE "<div class=\"title\">$1</div>\n<p>"); } } #write each line from the input file, edited or not, to the output file print (OUTFILE "$_"); } close (FILE); print(OUTFILE "</body>\n</html>\n"); close (OUTFILE); print(INDEXFILE "</body>\n</html>\n"); close (INDEXFILE); print ("Done. $chapternumber chapters found.");