OK, presuming you want some constructive feedback here we go. First welcome to Perl - the worlds funnest language - it is ideal for tasks like what you are trying to do here. As this is relates to your code I will just mention that this task has already been done and is available via a variety of perl modules. Who cares. So it's an old wheel. You have to start somewhere and this is an interesting task.

Now to your code:

First you don't use -w and use strict which can save all sorts of headaches when your 100 liner here develops into a 100,000 line frontpage rival. For info on lots of perl godies including why you should be using strict and warnings see the links in New Monks

Now you have some typos like the lack of a $ before $bgcolor in your body tag so the user can specify any color they want but will always get "bgcolor" - I'm not sure what this renders as (probably white) but you should have picked this up during testing. When testing you need to test several options, null strings.... and see it all works.

Your user interface is sub optimal. This script was designed to run with a command line argument of the file to process - try it without. Whoops - it is now prompting the user for input with the while (<>) You can escape this with the EOF character ctl-Z in WIN32 ?ctl-D (I think) in unix.

Next your program logic flow is sub optimal. You continually prompt the user for input so you should write a sub.

Also on the logic area there are far better ways to test that a user has entered one option from a list of colors - see the code that follows.

Next your understanding of how HTML is rendered seems lacking. You need to escape literal chars like < as &lt; > as &gt; and & as &amp; like I just did here so they render as expected. Aditionally one or more spaces are rendered by HTML as a single space. Thus if you want to preserve multiple spaces and tabs as big spaces you need to escape them as &nbsp;

Next if you want to preserve the line formating of a textfile you need to use <BR> a lot as newlines in HTML are not rendered as newlines in the browser. I have shown one way to do this for fun but the easiest is to use the <PRE> </PRE> tags to wrap your text in so the formatting is preserved - you do get monospace font though.

Finally it seems you have yet to discover herepages to print out HTML. They are great. Also print is slow. Lots of prints are slower so it is often a good idea to lump everything you want to print into a string and then print the lot with a single print $string; command.

Logically the output should go to a file rather than the screen. I will leave this as an exercise for you.

Here is my version of your code with heaps of comments

#!/usr/bin/perl -w use strict; # delacre our vars my($file,$title,$bgcolor,$text,$head,$body,$mail); my @colors = qw( white black red green blue purple yellow aqua gray silver fuchsia lime maroon navy olive); push @colors, ''; # add no color option # define a prompting subroutine that takes two arguments # the first argument is the prompt text, the second is a # a message to print if the user does not supply any input # the second argument is optional so we can allow no input sub prompt { # get the first argument to the sub from @_ my $prompt = shift @_; # get the second argument if this is not supplied then # the shift is false so the default value assigned is '' # note that although the shift does not specify @_ # this is the 'default argument' so it is exactly # the same as the shift @_ above, just shorter! my $blank_msg = shift || ''; PROMPT: print $prompt; chomp (my $input = <>); unless ($input) { if ($blank_msg) { print "$blank_msg\n"; goto PROMPT; } } return $input; } # look for a command line argument $file = shift @ARGV || ''; # prompt for a file if no command line argument supplied $file = prompt('Please enter a text file to process: ', 'Please enter +a filename!') unless $file; # check that file exists and is a text file until (-e $file and -T $file) { print "File '$file' does not exist, or is not a text file!\n"; $file = prompt('Please enter a file to process: ', 'Please enter a + filename!'); } # now get the user input and validate it $title = prompt('Enter the title to use for your web page: ','Please e +nter a title!'); $bgcolor = prompt('Enter the background color: '); # now we use grep to ensure a valid color (as defined in our # @colors array has been entered. the '' we added to @colors # via the push @color,'' means a null string is OK unless (grep{$_ eq $bgcolor}@colors) { print "The color options are @colors or enter for none!\n"; $bgcolor = prompt('Enter the background color: '); } $text = prompt('Enter the text color: '); unless (grep{$_ eq $text}@colors) { print "The color options are @colors or enter for none!\n"; $text = prompt('Enter the text color: '); } $head = prompt('Enter a heading (enter for none): '); $mail = prompt('Enter your email address (enter for none): '); # now assemble the body and heading tags $body = "<BODY"; $body .= " BGCOLOR=\"$bgcolor\"" if $bgcolor; $body .= " TEXT=\"$text\"" if $text; $body .= ">\n\n"; $body .= "<H1>$head</H1>\n\n" if $head; # add the text file nicely formatted. we format on the basis # that a blank line is a blank line and a series of data lines # represents a paragraph. we maintain the newline structure # of paragraphs by adding <BR>. the var $last_line is used # as a flag so we know what to do my $last_line = ''; open (TEXT, "<$file") or die "Unable to open $file: $!"; # iterate over the lines in our textfile while (<TEXT>) { chomp; $_ = '' if m/^\s*$/; # now escape the special chars and convert spaces and tabs s/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g; s/( {2,})/'&nbsp;' x length $1/eg; s/(\t)/'&nbsp;' x 4/eg; # now add the line to $body with the required HTML formatting if ($_ eq '') { $body .= "<BR>\n"; } elsif ($last_line) { $body .= "<BR>$_\n"; } else { $body .= "<P>$_\n"; } $last_line = $_; } close TEXT; # print out our HTML using a herepage. In this herepage we # do not need to escape " but variables like $title etc are # correctly interpolated. talk about saving headaches # we print everything until we find the token END_HTML on # a line by itself print <<END_HTML; <HTML> <HEAD> <TITLE>$title</TITLE> </HEAD> $body <HR> <A HREF="mailto:$mail">Email: $mail</A> </BODY> </HTML> END_HTML

Welcome to Perl, I hope you don't take these comments to much to heart and gain some useful knowledge.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: text2html by tachyon
in thread text2html by AlphasGhost

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.