cvs commit: www/scripts manage_news.pl

lizardo at linuxfromscratch.org lizardo at linuxfromscratch.org
Sat Nov 29 11:20:56 PST 2003


lizardo     03/11/29 12:20:56

  Modified:    scripts  manage_news.pl
  Log:
  manage_news.pl: Added "$relative_urls" option. Improved "truncate_text" subroutine.
  
  Revision  Changes    Path
  1.25      +36 -13    www/scripts/manage_news.pl
  
  Index: manage_news.pl
  ===================================================================
  RCS file: /home/cvsroot/www/scripts/manage_news.pl,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- manage_news.pl	28 Nov 2003 16:26:57 -0000	1.24
  +++ manage_news.pl	29 Nov 2003 19:20:56 -0000	1.25
  @@ -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.24 2003/11/28 16:26:57 lizardo Exp $
  +# $Id: manage_news.pl,v 1.25 2003/11/29 19:20:56 lizardo Exp $
   
   use strict;
   use warnings;
  @@ -34,7 +34,12 @@
   #######################
   # Global configuration
   
  -my $lfs_url = "/";       # LFS main URL. Set to "/" to make relative URLs
  +# LFS main URL. All URLs are made relative to this URL, so do not change it
  +# unless the LFS URL has changed
  +my $lfs_url = "http://www.linuxfromscratch.org/";
  +
  +# Use relative URLs?
  +my $relative_urls = 1;
   
   # Publishing
   my $show_content = 1;    # How many items to show fully
  @@ -91,7 +96,7 @@
   if ($section =~ /^general$/) { $section = "" }
   else { $section .= "/" };
   
  -my $change_urls = sub { return $_[0] };
  +my $base_url = "";
   
   if ($archive_under) {
       # News items' dates (%dates->year->month)
  @@ -110,10 +115,10 @@
       # Create index.html files
       foreach my $year (keys %{$dates}) {
           my $dir = $archive_under . "/" . $section . $year;
  +        $base_url = $lfs_url . "news/" . $section . $year . "/";
           eval { mkpath($dir) unless -d $dir } or die "$0\: Could not create " . $dir . ": $@\n";
           open (INDEX, ">$dir/index.html") or die "$0\: Could not open $dir/index.html: $!\n";
           if ($top) {
  -            $change_urls = sub { return $_[0] };
               $html_p->parse_file($top) or die "$0\: Could not parse " . $top . ": $!\n";
               $html_p->eof;
               print INDEX $buffer;
  @@ -126,7 +131,6 @@
           }
           print INDEX "</ul>\n";
           if ($bottom) {
  -            $change_urls = sub { return $_[0] };
               $html_p->parse_file($bottom) or die "$0\: Could not parse " . $bottom . ": $!\n";
               $html_p->eof;
               print INDEX $buffer;
  @@ -138,9 +142,9 @@
       foreach my $year (keys %{$dates}) {
           foreach my $month (keys %{$dates->{$year}}) {
               my $archive_file = $archive_under . "/" . $section . $year . "/" . $month . ".html";
  +            $base_url = $lfs_url . "news/" . $section . $year . "/";
               open(NEWS, ">$archive_file") or die "$0\: Could not open $archive_file" . ": $!\n";
               if ($top) {
  -                $change_urls = sub { return $_[0] };
                   $html_p->parse_file($top) or die "$0\: Could not parse " . $top . ": $!\n";
                   $html_p->eof;
                   print NEWS $buffer;
  @@ -151,8 +155,8 @@
       }
       foreach my $part ($mime_entity->parts) {
           my $archive_file = $archive_under . "/" . $section . isodate2any($part->head, '%Y/%m') . ".html";
  +        $base_url = $lfs_url . "news/" . $section . isodate2any($part->head, '%Y') . "/";
           open(NEWS, ">>$archive_file") or die "$0\: Could not open $archive_file" . ": $!\n";
  -        $change_urls = sub { local $URI::ABS_REMOTE_LEADING_DOTS = 1; return URI->new($_[0])->abs($lfs_url . $section) };
           $html_p->parse(mime2html($part));
           $html_p->eof;
           print NEWS $buffer . "\n\n";
  @@ -162,9 +166,9 @@
       foreach my $year (keys %{$dates}) {
           foreach my $month (keys %{$dates->{$year}}) {
               my $archive_file = $archive_under . "/" . $section . $year . "/" . $month . ".html";
  +            $base_url = $lfs_url . "news/" . $section . $year . "/";
               open(NEWS, ">>$archive_file") or die "$0\: Could not open $archive_file" . ": $!\n";
               if ($bottom) {
  -                $change_urls = sub { return $_[0] };
                   $html_p->parse_file($bottom) or die "$0\: Could not parse " . $bottom . ": $!\n";
                   $html_p->eof;
                   print NEWS $buffer;
  @@ -176,6 +180,8 @@
   }
   else {
       print "</div>\n<div id=\"generalnews\"><h2>General news</h2>\n" unless $section;
  +    # FIXME Workaround for "no section for general news" problem
  +    $base_url = $section ? $lfs_url . $section : $lfs_url . "dummy_section/";
       foreach my $part (splice(@{[$mime_entity->parts]}, 0, $show_content)) {
           print mime2html($part);
       }
  @@ -207,10 +213,10 @@
       my ($tag, $attrs) = @_;
   
       if ($$attrs{"href"}) {
  -        $$attrs{"href"} = &$change_urls($$attrs{"href"}) unless $$attrs{"href"} =~ /^#/;
  +        $$attrs{"href"} = change_urls($$attrs{"href"}) unless $$attrs{"href"} =~ /^#/;
       }
  -    elsif ($tag eq "img" and $$attrs{"src"}) {
  -        $$attrs{"src"} = &$change_urls($$attrs{"src"});
  +    if ($$attrs{"src"}) {
  +        $$attrs{"src"} = change_urls($$attrs{"src"});
       }
       $buffer .= "<$tag";
       $buffer .= " $_=\"$$attrs{$_}\"" foreach (keys %$attrs);
  @@ -281,7 +287,7 @@
           $news_id = lc(Get_header($mime_part->head, "title"));
           $news_id =~ s/\W+//g;
       }
  -    my $item_url = $lfs_url . "news/" . $section . isodate2any($mime_part->head, '%Y/%m') . ".html";
  +    my $item_url = change_urls("/news/" . $section . isodate2any($mime_part->head, '%Y/%m') . ".html");
       my $title = Get_header($mime_part->head, "title");
       my $author = Get_header($mime_part->head, "author");
       my $date = Get_header($mime_part->head, "date");
  @@ -305,7 +311,7 @@
           $news_id = lc(Get_header($mime_part->head, "title"));
           $news_id =~ s/\W+//g;
       }
  -    my $item_url = $lfs_url . "news/" . $section . isodate2any($mime_part->head, '%Y/%m') . ".html";
  +    my $item_url = change_urls("/news/" . $section . isodate2any($mime_part->head, '%Y/%m') . ".html");
       my $title = Get_header($mime_part->head, "title");
       my $summary = truncate_text($mime_part->bodyhandle->as_string, $summary_length);
   
  @@ -317,7 +323,24 @@
   
       $text =~ s/<[^>]+>//g;
       $text =~ s/^\s+//;
  +    $text =~ s/\n/ /g;
  +    $text =~ s/\s{2,}/ /g;
       return substr(encode_entities($text), 0, $length) . "...";
  +}
  +
  +# Make URLs relative or absolute (see $relative_urls)
  +sub change_urls {
  +    my ($url) = @_;
  +
  +    $base_url = $lfs_url unless $base_url;
  +
  +    # "scheme" returns undef if $url is relative
  +    if ($relative_urls and !defined(URI->new($url)->scheme)) {
  +        return URI->new($url)->abs($base_url)->rel($base_url);
  +    }
  +    else {
  +        return URI->new($url)->abs($base_url);
  +    }
   }
   
   {
  
  
  



More information about the website mailing list