cvs commit: www manage_news.pl

lizardo at linuxfromscratch.org lizardo at linuxfromscratch.org
Fri Sep 19 13:53:07 PDT 2003


lizardo     03/09/19 14:53:07

  Modified:    .        manage_news.pl
  Log:
  manage_news.pl: Code cleanup; added more verbosity for error messages.
  
  Revision  Changes    Path
  1.12      +65 -57    www/manage_news.pl
  
  Index: manage_news.pl
  ===================================================================
  RCS file: /home/cvsroot/www/manage_news.pl,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- manage_news.pl	19 Sep 2003 19:22:55 -0000	1.11
  +++ manage_news.pl	19 Sep 2003 20:53:07 -0000	1.12
  @@ -17,7 +17,7 @@
   # along with this program; if not, write to the Free Software
   # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   
  -# $Id: manage_news.pl,v 1.11 2003/09/19 19:22:55 lizardo Exp $
  +# $Id: manage_news.pl,v 1.12 2003/09/19 20:53:07 lizardo Exp $
   
   use warnings;
   use strict;
  @@ -49,70 +49,33 @@
   pod2usage(1) if $help;
   pod2usage(-exitstatus => 0, -verbose => 2) if $man;
   
  -sub Get_header {
  -    my ($header, $name) = @_;
  -    # Extract the field from the header
  -    my $hdr_content = $header->get($name);
  -    # Die if the field is not found
  -    if (!defined($hdr_content)) {
  -        die "Could not find header field " . $name . "\n" .
  -        "Header contents:\n" . $header->as_string . "\n";
  -    }
  -    $hdr_content =~ s/^\s*//;
  -    $hdr_content =~ s/\s*$//;
  -    $hdr_content =~ s/\n$//;
  -    return $hdr_content;
  -}
  -
  -# Translate the MIME data to HTML
  -sub mime2html {
  -    my ($sect, $mime_part) = @_;
  -
  -    my ($year, $month, undef) = split /\//, Get_header($mime_part->head, "date");
  -    my $archive_url;
  -    if ($sect =~ /^general$/) {
  -        $archive_url = "http://linuxfromscratch.org/news/".$year."/".$month.".html";
  -    }
  -    else {
  -        $archive_url = "http://linuxfromscratch.org/news/".$sect."/".$year."/".$month.".html";
  -    }
  -    my $news_id;
  -    if (defined($mime_part->head->get("id"))) {
  -        $news_id = lc(Get_header($mime_part->head, "id"));
  -    }
  -    else {
  -        # News item ID, created from the news title
  -        $news_id = lc(Get_header($mime_part->head, "title"));
  -        $news_id =~ s/\W+//g;
  -    }
  -    # Return the XHTML code
  -    return "\t<h3 id=\"$news_id\"><a href=\"$archive_url#$news_id\">" .
  -    Get_header($mime_part->head, "title") . "</a></h3>\n" .
  -    "\t\t<h4>" . Get_header($mime_part->head, "author") . " - " .
  -    Get_header($mime_part->head, "date") . "</h4>\n" .
  -    $mime_part->bodyhandle->as_string . "\n";
  -}
  -
   my $parser = new MIME::Parser;
   # These options disable the use of temporary files
   $parser->output_to_core(1);
   $parser->tmp_to_core(1);
   $parser->use_inner_files(1);
   
  +# Text buffer, used to store parsed HTML data
   my $buffer = "";
  +
  +# parsed MIME::Entity object
   my $entity;
  +# Filename of the input file, or <STDIN> in case of standard input
  +my $source;
   
   # Read the input file if one is given; otherwise, read from STDIN
   if ($infile) {
  +    $source = $infile;
       eval { $entity = $parser->parse_open($infile) } or pod2usage("$0: $@");
   }
   else {
  +    $source = "<STDIN>";
       $entity = $parser->parse(\*STDIN);
   }
   
   my $base_url = "";
   my $section = lc(Get_header($entity->head, "section"));
  -die "Invalid section name: " . $section . "\n" unless $section =~ /^[\w-]+$/;
  +die "$0\: $source\: Invalid section name: " . $section . "\n" unless $section =~ /^[\w-]+$/;
   
   if ($archive_under) {
       my %archive_files = ();
  @@ -120,7 +83,7 @@
           # Date field must be in YYYY/MM/DD format
           # It also avoids possible cross-site scripting
           if (Get_header($part->head, "date") !~ /^\d{4}\/\d{2}\/\d{2}$/) {
  -            die "Invalid date: " . Get_header($part->head, "date") . "\n" .
  +            die "$0\: $source\: Invalid date: " . Get_header($part->head, "date") . "\n" .
               "News header:\n---\n" . $part->head->as_string . "\n---\n";
           }
           # Split year and month, and store the destination archive location
  @@ -131,11 +94,11 @@
       foreach my $archive_file (keys %archive_files) {
           # Create the destination archive location
           my (undef, $dir, undef) = File::Spec->splitpath($archive_file);
  -        eval { mkpath($dir) unless -d $dir } or die "Could not create " . $dir . ": $@\n";
  +        eval { mkpath($dir) unless -d $dir } or die "$0\: Could not create " . $dir . ": $@\n";
           # If a top template is given, prepend it to the output
           if ($top) {
  -            open(NEWS, ">$archive_file") or die "Could not open $archive_file" . ": $!\n";
  -            open(TOP, $top) or die "Could not open " . $top . ": $!\n";
  +            open(NEWS, ">$archive_file") or die "$0\: Could not open $archive_file" . ": $!\n";
  +            open(TOP, $top) or die "$0\: Could not open " . $top . ": $!\n";
               print NEWS <TOP>;
               close TOP;
               close NEWS;
  @@ -157,7 +120,7 @@
           $html_p->attr_encoded(1);
           my ($year, $month, undef) = split /\//, Get_header($part->head, "date");
           my $archive_file = $archive_under."/".$year."/".$month.".html";
  -        open(NEWS, ">>$archive_file") or die "Could not open $archive_file" . ": $!\n";
  +        open(NEWS, ">>$archive_file") or die "$0\: Could not open $archive_file" . ": $!\n";
           $html_p->parse(mime2html($section, $part));
           $html_p->eof;
           print NEWS $buffer . "\n\n";
  @@ -167,8 +130,8 @@
       foreach my $archive_file (keys %archive_files) {
           # If a bottom template is given, append it to the output
           if ($bottom) {
  -            open(NEWS, ">>$archive_file") or die "Could not open $archive_file" . ": $!\n";
  -            open(BOTTOM, $bottom) or die "Could not open " . $bottom . ": $!\n";
  +            open(NEWS, ">>$archive_file") or die "$0\: Could not open $archive_file" . ": $!\n";
  +            open(BOTTOM, $bottom) or die "$0\: Could not open " . $bottom . ": $!\n";
               print NEWS <BOTTOM>;
               close BOTTOM;
               close NEWS;
  @@ -188,13 +151,11 @@
   }
   
   if ($parser->results->errors) {
  -    print STDERR "Errors: " . $parser->results->errors . "\n";
  -    print STDERR "- " . $_ foreach ($parser->results->errors);
  +    print STDERR "$0\: $source\: " . $_ foreach ($parser->results->errors);
   }
   
   if ($parser->results->warnings) {
  -    print STDERR "Warnings: " . $parser->results->warnings . "\n";
  -    print STDERR "- " . $_ foreach ($parser->results->warnings);
  +    print STDERR "$0\: $source\: " . $_ foreach ($parser->results->warnings);
   }
   
   ##########################
  @@ -232,6 +193,53 @@
       $text =~ s/</\</g;
       $text =~ s/>/\>/g;
       $buffer .= $text;
  +}
  +
  +######################
  +# General subroutines
  +
  +sub Get_header {
  +    my ($header, $name) = @_;
  +    # Extract the field from the header
  +    my $hdr_content = $header->get($name);
  +    # Die if the field is not found
  +    if (!defined($hdr_content)) {
  +        die "$0\: $source\: Could not find header field " . $name . "\n" .
  +        "Header contents:\n" . $header->as_string . "\n";
  +    }
  +    $hdr_content =~ s/^\s*//;
  +    $hdr_content =~ s/\s*$//;
  +    $hdr_content =~ s/\n$//;
  +    return $hdr_content;
  +}
  +
  +# Translate the MIME data to HTML
  +sub mime2html {
  +    my ($sect, $mime_part) = @_;
  +
  +    my ($year, $month, undef) = split /\//, Get_header($mime_part->head, "date");
  +    my $archive_url;
  +    if ($sect =~ /^general$/) {
  +        $archive_url = "http://linuxfromscratch.org/news/".$year."/".$month.".html";
  +    }
  +    else {
  +        $archive_url = "http://linuxfromscratch.org/news/".$sect."/".$year."/".$month.".html";
  +    }
  +    my $news_id;
  +    if (defined($mime_part->head->get("id"))) {
  +        $news_id = lc(Get_header($mime_part->head, "id"));
  +    }
  +    else {
  +        # News item ID, created from the news title
  +        $news_id = lc(Get_header($mime_part->head, "title"));
  +        $news_id =~ s/\W+//g;
  +    }
  +    # Return the XHTML code
  +    return "\t<h3 id=\"$news_id\"><a href=\"$archive_url#$news_id\">" .
  +    Get_header($mime_part->head, "title") . "</a></h3>\n" .
  +    "\t\t<h4>" . Get_header($mime_part->head, "author") . " - " .
  +    Get_header($mime_part->head, "date") . "</h4>\n" .
  +    $mime_part->bodyhandle->as_string . "\n";
   }
   
   __END__
  
  
  



More information about the website mailing list