r341 - scripts/trunk

lizardo at linuxfromscratch.org lizardo at linuxfromscratch.org
Mon Jun 27 23:26:14 PDT 2005


Author: lizardo
Date: 2005-06-28 00:26:14 -0600 (Tue, 28 Jun 2005)
New Revision: 341

Added:
   scripts/trunk/manage_news2.pl
   scripts/trunk/sample_news.txt
Log:
[www2] Committed very initial version of manage_news2.pl (and a sample news item to play with :).


Added: scripts/trunk/manage_news2.pl
===================================================================
--- scripts/trunk/manage_news2.pl	2005-06-26 23:07:11 UTC (rev 340)
+++ scripts/trunk/manage_news2.pl	2005-06-28 06:26:14 UTC (rev 341)
@@ -0,0 +1,82 @@
+#!/usr/bin/perl
+# manage_news2.pl - Script for news management
+
+# Copyright (C) 2005 Anderson Lizardo
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+
+# TODO: Parse the output through a htmltidy-like module?
+
+use strict;
+use warnings;
+
+die "Usage: $0 <news_file>\n" if @ARGV ne 1;
+
+use File::Copy;
+use File::Temp qw(tempfile);
+use HTML::TextToHTML;
+use Text::Header;
+use Tie::File;
+
+# Extract the header (Title, Author and Date) from a news item file and return
+# a hash with this data. Note: The given file is modified by tie, so it should
+# copied before passing to extract_header().
+sub extract_header {
+   my $filename = shift;
+   my @news_array;
+   my @news_header = ();
+
+   tie @news_array, 'Tie::File', $filename
+      or die "Could not tie $filename: $!\n";
+   while (@news_array) {
+      my $hdr_line = shift @news_array;
+      last if $hdr_line =~ /^\s*$/;
+      push(@news_header, $hdr_line);
+   }
+   untie @news_array;
+   my %news_header = unheader(@news_header);
+   return \%news_header;
+}
+
+# Convert a title to an "id" field.
+sub title2id {
+   my $title = shift;
+
+   my $id = lc($title);
+   $id =~ s/\W+//g;
+
+   return $id;
+}
+
+my $news_file = shift;
+
+# Extract the header
+my ($tmp_fh, $tmp_filename) = tempfile(UNLINK => 1);
+copy($news_file, $tmp_filename)
+   or die "Could not copy $news_file to $tmp_filename: $!\n";
+my $hdr = extract_header($tmp_filename);
+my %news_hdr = %$hdr;
+
+# Print header info in HTML format
+printf("<h3 id=\"%s\">%s</h3>\n", title2id($news_hdr{"title"}),
+   $news_hdr{"title"});
+printf("<h4>%s - %s</h4>\n", $news_hdr{"author"}, $news_hdr{"date"});
+
+# Parse the news item content and convert it to HTML
+my $conv = new HTML::TextToHTML(extract => 1,
+   xhtml => 1,
+   use_preformat_marker => 1,
+);
+$conv->txt2html(infile => [$tmp_filename]);


Property changes on: scripts/trunk/manage_news2.pl
___________________________________________________________________
Name: svn:executable
   + *

Added: scripts/trunk/sample_news.txt
===================================================================
--- scripts/trunk/sample_news.txt	2005-06-26 23:07:11 UTC (rev 340)
+++ scripts/trunk/sample_news.txt	2005-06-28 06:26:14 UTC (rev 341)
@@ -0,0 +1,22 @@
+Title: Sample News
+Author: Anderson Lizardo
+Date: 2005/06/28
+
+This is a *test*.
+
+Can _*tags*_ be mixed? Hm, *no* (we can create custom tags using dictionaries,
+though; See
+http://search.cpan.org/~rubykat/txt2html-2.41/lib/HTML/TextToHTML.pm)
+
+<PRE>
+   Testing code block. (start/end tags can be changed)
+   Testing code block.
+   Testing code block.
+</PRE>
+
+List test:
+
+* Item 1
+* Item 2
+  1. Item 3
+   2. Item 4




More information about the website mailing list