#!/usr/bin/perl -w
# vim: set sw=4 ts=4 si et:
# Author: Guido Socher, guido@bearix.oche.de
# Patch for -t option by Floris.Lambrechts@linuxfocus.org
# This software ist distributed under the conditions of the
# GNU public license.
use strict;
use vars qw($opt_h $opt_l $opt_t $opt_i);
use Getopt::Std;
#
my $l=0;
# we read all the themes into ram:
my %themesptr;
my %issueptr;
my @issuenames;
my $theme;
my $tname;
my $ver="version 0.7.1";
# small change to be able to link to .shtml files instead of .html if not available
my $bg;
my $i;
my @line;
my %templates;
my $articlefile;
my $iarticlefile;
my $abstract;
my $title;
my $today;
my $lang="en";
# LinuxFocus got messy over the initiative to change the article location
# from MonthYear to articles directory.
# This tells us per issue where things are. The syntax is
# 'issue_name'=>'directory'
# issue_name is the term that comes after the "month:" in the defintion file.
#
my %whereiswhat=(
'en'=>{"November1997"=>'November1997',"January1998"=>'January1998',"March1998"=>'March1998',"May1998"=>'May1998','July1998'=>'articles','September1998'=>'articles','October1998'=>'articles','November1998'=>'articles','December1998'=>'articles','January1999'=>'articles','Next'=>'../WorkSpace/Nextissue','July1999'=>'July1999','September1999'=>'September1999','November1999'=>'November1999','January2000'=>'January2000','March2000'=>'March2000','May2000'=>'May2000'},
'cn'=>{"November1997"=>'November1997',"January1998"=>'January1998',"March1998"=>'March1998',"May1998"=>'May1998','July1998'=>'July1998','September1998'=>'September1998','October1998'=>'October1998','November1998'=>'November1998','December1998'=>'December1998','January1999'=>'January1999','Next'=>'../WorkSpace/Nextissue','July1999'=>'July1999','September1999'=>'September1999','November1999'=>'November1999','May2000'=>'May2000'},
'de'=>{"November1997"=>'November1997',"January1998"=>'January1998',"March1998"=>'March1998',"May1998"=>'May1998','July1998'=>'July1998','September1998'=>'September1998','October1998'=>'October1998','November1998'=>'November1998','December1998'=>'December1998','January1999'=>'January1999','Next'=>'../WorkSpace/Nextissue','July1999'=>'July1999','September1999'=>'September1999','November1999'=>'November1999','May2000'=>'May2000'},
'fr'=>{"November1997"=>'November1997',"January1998"=>'January1998',"March1998"=>'March1998',"May1998"=>'May1998','July1998'=>'July1998','September1998'=>'September1998','October1998'=>'October1998','November1998'=>'November1998','December1998'=>'articles','January1999'=>'articles','Next'=>'../WorkSpace/Nextissue','July1999'=>'July1999','September1999'=>'September1999','November1999'=>'November1999','May2000'=>'May2000'},
'nl'=>{"November1997"=>'November1997',"January1998"=>'January1998',"March1998"=>'March1998',"May1998"=>'May1998','July1998'=>'articles','September1998'=>'articles','October1998'=>'articles','November1998'=>'articles','December1998'=>'articles','January1999'=>'articles','Next'=>'../WorkSpace/Nextissue','July1999'=>'articles','September1999'=>'September1999','November1999'=>'November1999','January2000'=>'January2000','March2000'=>'March2000','May2000'=>'May2000'},
'es'=>{"November1997"=>'November1997',"January1998"=>'January1998',"March1998"=>'March1998',"May1998"=>'May1998','July1998'=>'July1998','September1998'=>'articles','October1998'=>'articles','November1998'=>'articles','December1998'=>'articles','January1999'=>'articles','Next'=>'../WorkSpace/Nextissue','July1999'=>'July1999','September1999'=>'September1999','November1999'=>'November1999','May2000'=>'May2000'}
);
#
my %validcat=('Forum'=>1,'Applications'=>1,'Hardware'=>1,'Webdesign'=>1,
'System Administration'=>1,'Software Development'=>1,'Graphics'=>1,
'UNIX Basics'=>1,'Kernel Corner'=>1,'Interviews'=>1,'Community'=>1,
'Games'=>1
);
my %abbrevcat=('appl'=>'Applications','hw'=>'Hardware','www'=>'Webdesign',
'sysadm'=>'System Administration','sdev'=>'Software Development',
'grx'=>'Graphics',
'ubx'=>'UNIX Basics','kernel'=>'Kernel Corner');
#
&getopts("l:hti:")||exit 1;
&help if ($opt_h);
&help if (scalar(@ARGV) < 1);
die "ERROR: can not read file $ARGV[0]\n" unless (-r "$ARGV[0]");
$today=&today;
if ($opt_l){
if ($whereiswhat{$opt_l}){
$lang=$opt_l;
}else{
die "ERROR: no language extension $opt_l defined, yet. Please add it\n";
}
}
&readtemplates();
&readthemestxt($ARGV[0]);
unless (-d "Themes"){
print STDERR "making directory Themes...\n";
mkdir "Themes",0755 || die "ERROR: could not mkdir Themes\n";
}
#----------
print STDERR "writing ./Themes/index.html...\n";
open(OUT,">Themes/index.html")||die "ERROR: can not write to Themes/index.html\n";
&printtemplate("__head_$lang");
&printtemplate("__img_th_$lang");
&printtemplate("__index_$lang");
&printtemplate("__foot_th_$lang");
close OUT;
#----------
foreach $theme (keys %validcat){
$tname=$theme;
print STDERR "writing ./Themes/$tname.html...\n";
$tname=~s/ /_/g;
open(OUT,">Themes/$tname.html")|| die "ERROR: can not write to Themes/$tname.html\n";
&printtemplate("__head_$lang");
&printtemplate("__img_th_$lang");
&printtemplate("__${tname}_$lang");
if ($themesptr{$theme}){
foreach $articlefile (sort keys %{$themesptr{$theme}}){
$title=$themesptr{$theme}{$articlefile}{'title'};
$abstract=$themesptr{$theme}{$articlefile}{'short_abstract'};
if ($themesptr{$theme}{$articlefile}{'status'} eq "translated"){
if (!-e "Themes/$articlefile") {
$articlefile =~ s/html/shtml/;
}
print OUT "
$title\n";
print OUT "
$abstract\n";
}else{
print OUT "$title [Engelse versie]\n";
print OUT "
$abstract\n";
}
}
}else{
print OUT "Sorry, no article available in this category.\n";
}
&printtemplate("__foot_th_$lang");
}
#----------
print STDERR "writing ./issues.html...\n";
open(OUT,">issues.html")||die "ERROR: can not write to issues.html\n";
&printtemplate("__head_$lang");
&printtemplate("__img_i_$lang");
&printtemplate("__issue_$lang");
#----------
foreach $i (@issuenames){
print OUT "$i
\n\n";
foreach $iarticlefile (sort keys %{$issueptr{$i}}){
$title=$issueptr{$i}{$iarticlefile}{'title'};
$abstract=$issueptr{$i}{$iarticlefile}{'short_abstract'};
if ($issueptr{$i}{$iarticlefile}{'status'} eq "translated"){
print OUT "- $title\n";
print OUT "
$abstract \n";
}else{
print OUT "- $title [Engelse versie]\n";
print OUT "
$abstract \n";
}
}
print OUT "
\n
\n";
}
if ($opt_t){ # you want to make an index of all translated articles
print STDERR "writing ./vertaald.html...\n";
open(OUT,">vertaald.html")||die "ERROR: can not write to vertaald.html\n";
&printtemplate("__head_$lang");
&printtemplate("__img_i_$lang");
&printtemplate("__translated_$lang");
#----------
foreach $i (@issuenames){
print OUT "$i
\n\n";
foreach $iarticlefile (sort keys %{$issueptr{$i}}){
$title=$issueptr{$i}{$iarticlefile}{'title'};
$abstract=$issueptr{$i}{$iarticlefile}{'short_abstract'};
if ($issueptr{$i}{$iarticlefile}{'status'} eq "translated"){
print OUT "- $title\n";
print OUT "
$abstract \n";
}
}
print OUT "
\n
\n";
}
if ($opt_i){ # you want to include an HTML section at the end of
# the file translated.html. The HTML is in the file
# $opt_i
open(INCLUDE,"$opt_i")||die "ERROR: can not read the file $opt_i\n";
print OUT "\n\n\n
\n";
while(){
print OUT "$_";
}
close INCLUDE;
print OUT "\n";
}
}
&printtemplate("__foot_i_$lang");
#------------------
# read in the database:
#--------------
sub readthemestxt($){
my $file=shift;
my $dir;
my $endir;
my $iname;
open(FF,"$file")||die "ERROR: can not read $file\n";
while(){
$l++;
next if (/^#/); # ignore comment
next unless (/\w/); # ignore empty line
chomp;
s/\s+/ /g; # manny space to 1 space
s/^ //g; # leading space
@line=split(/\+\+/);
if (/^month:/){
# set the directory for English and the selected language
$line[0]=~ s/^month://;
$line[0]=~s/ +//g;
die "ERROR: update the \%whereiswhat variable in this program or download a new version. The location of $line[0] at line $l in $file is not defined for the English language\n" unless ($whereiswhat{'en'}{$line[0]});
die "ERROR: update the \%whereiswhat variable in this program or download a new version. The location of $line[0] at line $l in $file is not defined for the English language\n" unless ($whereiswhat{$lang}{$line[0]});
$dir=$whereiswhat{$lang}{$line[0]};
$endir=$whereiswhat{'en'}{$line[0]};
die "ERROR: issue name missing at line $l\n" unless($line[1]=~/\w/);
$line[1]=~s/ +/ /g;
$iname=$line[1];
push(@issuenames,$iname);
next;
}
if (scalar(@line) < 3){
print STDERR "ERROR: line $l does not look correct, ignored\n";
next;
}
unless ($line[2] && $line[2]=~/\w\w/){
print STDERR "ERROR: line $l does not have an article title\n";
next;
}
unless(/^\d/ || $line[1]=~ /\w\w\w/){
print STDERR "ERROR: line $l does not start with a digit and at the same time no full path to non-english page is given\n";
next;
}
for $i (0..4){
# take care of empty fields:
if (defined $line[$i] && $line[$i] =~ /\w/){
$line[$i] =~ s/\s//g if ($i == 0);
$line[$i] =~ s/^ //; # we have max one space
$line[$i] =~ s/ $//;
}else{
$line[$i] = " ";
}
}
$line[3]=$abbrevcat{$line[3]} if ($abbrevcat{$line[3]});
unless($validcat{$line[3]}){
print STDERR "ERROR: line $l, no category or invalid category, ignoring line\n";
next;
}
if ($line[1] =~/x/i){
# it's translated
$articlefile="../" .$dir . "/article". $line[0] .".html";
$iarticlefile=$dir . "/article". $line[0] .".html";
# store by category, articlefile
$themesptr{$line[3]}{$articlefile}{'status'}="translated";
$issueptr{$iname}{$iarticlefile}{'status'}="translated";
}else{
# not translated
$articlefile="../" .$endir . "/article". $line[0] .".html";
$iarticlefile=$endir . "/article". $line[0] .".html";
# store by category, articlefile
$themesptr{$line[3]}{$articlefile}{'status'}="english";
$issueptr{$iname}{$iarticlefile}{'status'}="english";
}
$themesptr{$line[3]}{$articlefile}{'title'}=$line[2];
$themesptr{$line[3]}{$articlefile}{'short_abstract'}=$line[4];
$issueptr{$iname}{$iarticlefile}{'title'}=$line[2];
$issueptr{$iname}{$iarticlefile}{'short_abstract'}=$line[4];
}
close FF;
}
#--------------
sub today{
my @ltime = localtime;
#return a date in yyyy-mm-dd format
my $today;
$today = sprintf("%04d-%02d-%02d",1900 + $ltime[5],$ltime[4] + 1,$ltime[3]);
$today;
}
#--------------
sub printtemplate($){
my $name=shift;
if (defined $templates{$name}){
foreach (@{$templates{$name}}){
print OUT;
}
}else{
# try english
print STDERR "Warning, no template $name, trying english version\n";
$name=~s/_[a-z]+$/_en/;
if (defined $templates{$name}){
foreach (@{$templates{$name}}){
print OUT;
}
}else{
die "ERROR: No such template $name\n";
}
}
}
#--------------
sub readtemplates(){
my $templatename="nix";
#read and print any text between "^__ xxx" and the next __
while(){
next if (/^#/);
if (/^(__\w+)/){
$templatename=$1;
next;
}
s/\$date/$today/o;
push(@{$templates{$templatename}},$_);
}
}
#-------------
sub help{
print "lfthemes -- generate a linuxfocus subject index page (themes)
USAGE: lfthemes [-h][-t][-i include.txtl][-l cn|de|en|es|nl|fr] themes_definition_file
OPTIONS: -h this help text
-l select a language for the output. Supported are de, en, fr, nl
and cn. You need to translate the provided lfthemes_lang
file to your language and send it to guido.socher\@linuxfocus.org.
-t generate also a a file translated.html (contains the same as the
file, but only for transtlated articles)
-i include the file 'include.txt' at the end of translated.html
EXAMPLE: lfthemes -l es -ti include.txt spanish_themse_def.txt
This generates in the current directory the subdirectory Themes
where it stores all the web-pages. lfthemes generates also the file
issues.html in the current directory. All Images are linked to
../../common/images/
The latest version of this program is at
http://www.linuxfocus.org/developer/Guido/
This program was written by Guido Socher
$ver
\n";
exit;
}
__END__
__head_en
LinuxFocus Index
__img_th_en
__img_i_en
__index_en
LinuxFocusThemes Index
|
This is the index of the first free multi-lingual web magazine for Linux sorted by subject.
|
__foot_i_en