1: A simple program that takes a text file and then converts it to  html pls leave feed back im new so dont burn me to much
   2: 
   3: 
   4: 
   5: #!/usr/bin/perl
   6: 
   7: # webulid sipel text to html conversion
   8: #no links boldface blank space no crap
   9: 
  10: $title = '';
  11: $bgcolor = '';
  12: $text = '';
  13: $head = '';
  14: $mail = '';
  15: 
  16: print "enter the title to use for your web page: ";
  17: chomp($title = <STDIN>);
  18: 
  19: foreach $color ('backround', 'text') {
  20:     $in = '';
  21:     while () {
  22:         print "enter the $color color )? for options): ";
  23:         chomp($in = <stdin>);
  24:         $in = lc $in;
  25:         
  26:         if ($in eq '?') {     #print help
  27:             print "One of: \nwhite, black, red, green, blue,\n";
  28:             print "orange, purple, yellow aqua, gray,\n";
  29:             print "silver, fuchsia, lime, maroon,navy,\n";
  30:             print "olive, or return for none\n";
  31:             next;
  32:         } elsif ($in eq '' or
  33:                  $in eq 'white' or
  34:                  $in eq 'black' or
  35:                  $in eq 'red' or
  36:                  $in eq 'blue' or
  37:                  $in eq 'green' or
  38:                  $in eq 'orange' or
  39:                  $in eq 'purple' or
  40:                  $in eq 'yellow' or
  41:                  $in eq 'aqua' or
  42:                  $in eq 'gray' or
  43:                  $in eq 'silver' or
  44:                  $in eq 'fuchisa' or
  45:                  $in eq 'lime' or
  46:                  $in eq 'maroon' or
  47:                  $in eq 'navy' or
  48:                  $in eq 'olive') { last; }
  49:         else {
  50:            print "that's not a color.\n";
  51:         }
  52:     }
  53:     
  54:     if ($color eq 'backround') {
  55:         $bgcolor = $in;
  56:     } else {
  57:         $text = $in;
  58:     }
  59: }
  60: 
  61: print "enter a heading: ";
  62: chomp($head = <STDIN>);
  63: 
  64: print "enter your email address: ";
  65: chomp($mail = <STDIN>);
  66: 
  67: print '*' x 30;
  68: 
  69: print "\n<HTML>\n<HEAD>\n<TITLE>$title</TITLE>\n";
  70: print "</HEAD>\n<BODY";
  71: if ($bgcolor ne '') {print qq( BGCOLOR="bgcolor"); }
  72: if ($text ne '') { print qq( TEXT="$text"); }
  73: print">\n";
  74: print"<H1>$head</h1>\n<p>";
  75: 
  76: while (<>) {
  77:       if ($_ eq "\n") {
  78:           print "<p>\n";
  79:       } else {
  80:         print $_;
  81:       }
  82: }
  83: 
  84: print qq(<HR\n<ADDRESS><A HREF="mailto:$mail</A></ADDRESS>\n);
  85: print "</BODY>\n</HTML>\n";
  86: 
  87: 
  88: 
  89: 
  90: 
  91: 
  92: 
  93: 
  94: 

Replies are listed 'Best First'.
Re: text2html
by tachyon (Chancellor) on Jul 09, 2001 at 20:14 UTC

    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

Re: text2html
by Chady (Priest) on Jul 09, 2001 at 10:45 UTC

    Umm... feedback? well...

    For first, you don't use Strict or -w, then you go on to declare your variables in an awkward way. Did you try to do my $title; instead of $title = '' ?

    next, the elsif is ugly, and do you need to re-print the line each time I type it?

    the while loop is unbreakable, how can someone exit that? and you might want to redirect STDOUT to a file, so one can save what he is writing quickly.

    That's my 2 cents for it.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: text2html
by Anonymous Monk on Jul 09, 2001 at 22:38 UTC
    Line 22 uses <stdin> while all other lines use <STDIN> you should be consistent and use <STDIN> everywhere. You could have made a hash or array of colors to make the checking and printing of the colors easier and more expandable in the future (adding a new color would be done in just one place rather than two). The program isn't very useful as you can neither redirect the output to a file (because there is non-html being written to stdout) nor redirect the input from another file (because the user needs to answer a few questions before it's time to read the file). The first problem can be solved by writing your questions to STDERR and the generated html to STDOUT which will allow someone to yourprogram > file.html and still see the questions on their screen. The second problem can be solved by allowing someone to specify a filename to read from on the commandline such as yourprogram -f textfile > file.html or a simpler version of yourprogram textfile > file.html