genorg.pl
changeset 7 f00ed34eca17
parent 6 c576e88fef13
child 8 ee9b57cbbefc
equal deleted inserted replaced
6:c576e88fef13 7:f00ed34eca17
    10 use File::Copy qw(copy move);
    10 use File::Copy qw(copy move);
    11 use Cwd;
    11 use Cwd;
    12 use experimental qw(declared_refs);
    12 use experimental qw(declared_refs);
    13 use Data::Dumper;
    13 use Data::Dumper;
    14 use POSIX;
    14 use POSIX;
       
    15 # !! Add seperatio between title and identifier
    15 
    16 
    16 # Fix up CLI interface
    17 # Fix up CLI interface
    17 # Add option ro set cache to 0 through command line
    18 # Add option ro set cache to 0 through command line
    18 # Print required elisp code it is in $req_config
    19 # Print required elisp code it is in $req_config
    19 # Add config option for $max_rss and $max_cat
    20 # Add config option for $max_rss and $max_cat
    20 # Sort cataogirs by entires
    21 # Sort cataogirs by entires
    21 # Variable for map_css
    22 # Variable for map_css
    22 # Remove the desc
    23 # Remove the desc
    23 # Domain thing as well
    24 # Domain thing as well
    24 # Load custom filpe for htmlize and all that
       
    25 # 2 articles 1 day better sorting
    25 # 2 articles 1 day better sorting
       
    26 # cataogry linking
       
    27 # reinforce slash ending for $oudir
    26 my $cache = 1;
    28 my $cache = 1;
    27 
    29 
    28 my $config_file = "genorg-conf";
    30 my $config_file = "genorg-conf";
    29 my $dir = "blog";
    31 my $dir = "blog";
    30 my $outdir = "out/";
    32 my $outdir = "out/";
   217   "../" x ($p > 0 ? $p : 0) . substr("@f2", 1);
   219   "../" x ($p > 0 ? $p : 0) . substr("@f2", 1);
   218 }
   220 }
   219 
   221 
   220 my @org_exps;
   222 my @org_exps;
   221 my @files_to_make;
   223 my @files_to_make;
       
   224 my @files_to_move;
   222 
   225 
   223 sub wanted {
   226 sub wanted {
   224   my $n_path = $outdir . prel_path ($dir,  "$File::Find::name/");
   227   my $n_path = $outdir . prel_path ($dir,  "$File::Find::name/");
   225   # We need to mirror the direcotry structure
   228   # We need to mirror the direcotry structure
   226   if (-d) {
   229   if (-d) {
   229     return;
   232     return;
   230   }
   233   }
   231   if (/\.org$/) {
   234   if (/\.org$/) {
   232     push @org_exps, substr $n_path, length($outdir);
   235     push @org_exps, substr $n_path, length($outdir);
   233   } else {
   236   } else {
   234     copy $_, $n_path;
   237     push @files_to_move, $File::Find::name
   235   }
   238   }
   236 }
   239 }
   237 
   240 
   238 my @modified_files;
   241 my @modified_files;
   239 
   242 
   280   mkdir $outdir;
   283   mkdir $outdir;
   281   chdir $cdir or die "Couldn't access $cdir\n";
   284   chdir $cdir or die "Couldn't access $cdir\n";
   282   read_config $config_file;
   285   read_config $config_file;
   283   -d $dir or die "$dir doesn't exist \n";
   286   -d $dir or die "$dir doesn't exist \n";
   284   find (\&wanted, $dir);
   287   find (\&wanted, $dir);
       
   288   for (@files_to_move) {
       
   289     copy $_, s#$dir/?#$outdir#er;
       
   290   }
   285   mkdir for @files_to_make;
   291   mkdir for @files_to_make;
   286   values %{conv_files()};
   292   values %{conv_files()};
   287   $cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file;
   293   $cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file;
   288   chdir $outdir;
   294   chdir $outdir;
   289   @arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps;
   295   @arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps;
   302       map {$dom->createElement($_)} ("art", "date", "title", "catag", "related");
   308       map {$dom->createElement($_)} ("art", "date", "title", "catag", "related");
   303     $art_el->{path} = $art->{file};
   309     $art_el->{path} = $art->{file};
   304     $ca->appendText($art->{catag});
   310     $ca->appendText($art->{catag});
   305     $title->appendText($art->{title});
   311     $title->appendText($art->{title});
   306     $date->appendText($art->{date}->text_easy);
   312     $date->appendText($art->{date}->text_easy);
       
   313     my @ll = @{$art->{links}};
       
   314     if (@ll) {
       
   315       my $links = $dom->createElement('links');
       
   316       for (@ll) {
       
   317 	my $link = $dom->createElement("link");
       
   318 	$link->{catag} = $_->[0];
       
   319 	$link->appendText($_->[1]);
       
   320 	$links->appendChild($link);
       
   321       }
       
   322       $art_el->appendChild($links);
       
   323     }
   307     my $rel_str;
   324     my $rel_str;
   308     for my $thing ($art->{related}) {
   325     for my $thing ($art->{related}) {
   309       for (@{$thing}) {
   326       for (@{$thing}) {
   310 	my ($catag, $artt) = @{$_};
   327 	my ($catag, $artt) = @{$_};
   311 	my $rel_str .= "\"$catag\"" . ($artt && "->\"$artt\"");
   328 	my $rel_str .= "\"$catag\"" . ($artt && "->\"$artt\"");
   391 	   ["<li>..."],
   408 	   ["<li>..."],
   392 	   (map [$_, $_], (($no_pages - 5)..$no_pages)))
   409 	   (map [$_, $_], (($no_pages - 5)..$no_pages)))
   393 	}
   410 	}
   394 }
   411 }
   395 
   412 
   396 sub linkify ($bom) {
   413 sub get_art($cat, $id) {
   397   $bom
   414   for (@arts) {
       
   415     return $_ if $_->{catag} eq $cat && $_->{title} eq $id
       
   416   }
   398 }
   417 }
   399 
   418 
   400 sub finalise_html {
   419 sub finalise_html {
   401   mkdir $no_name_dir;
   420   mkdir $no_name_dir;
   402   open my $fh, '>', "$no_name_dir/1.html";
   421   open my $fh, '>', "$no_name_dir/1.html";
   441     my $cfh = \$c_files{$catag};
   460     my $cfh = \$c_files{$catag};
   442     $$cfh = move_on_if_neccasary $$cfh, $$ncat, $catags{$catag}, "$no_name_dir/$catag";
   461     $$cfh = move_on_if_neccasary $$cfh, $$ncat, $catags{$catag}, "$no_name_dir/$catag";
   443     print {$$cfh} entry $art;
   462     print {$$cfh} entry $art;
   444     my $a_file = $art->{file};
   463     my $a_file = $art->{file};
   445     # say $a_file;
   464     # say $a_file;
   446     if (grep /^$a_file$/, @modified_files) {
   465     if (1 or grep /^$a_file$/, @modified_files) {
       
   466       my $doom = linkify($art);
   447       open my $h_file, '>', $a_file;
   467       open my $h_file, '>', $a_file;
   448       my $doom = linkify $art->{dom};
   468       print $h_file $doom->toStringHTML;
   449       print $h_file $doom->toString;
       
   450       close $h_file;
   469       close $h_file;
   451     }
   470     }
   452   }
   471   }
   453   print $fh panigation ((ceil $n_fh / $max_cat) x 2, $no_name_dir) if $max_cat < $total_articles;
   472   print $fh panigation ((ceil $n_fh / $max_cat) x 2, $no_name_dir) if $max_cat < $total_articles;
   454   print $fh $h_end;
   473   print $fh $h_end;
   455   close $fh;
   474   close $fh;
   456   for (values %c_files) {
   475   for (values %c_files) {
   457     print $_ $h_end;
   476     print $_ $h_end;
   458     close;
   477     close $_;
   459   }
   478   }
   460   print $mrss '</channel> </rss>';
   479   print $mrss '</channel> </rss>';
   461   for (values %r_files) {
   480   for (values %r_files) {
   462     print $_ '</channel> </rss>';;
   481     print $_ '</channel> </rss>';;
   463     close;
   482     close $_;
   464   }
   483   }
       
   484 }
       
   485 
       
   486 sub linkify ($art) {
       
   487   # if $art->{dom} is undefined, we must sadly parse again
       
   488   my @links;
       
   489   my @olinks = @{$art->{links} //[]};
       
   490   my $bom = $art->{dom} // XML::LibXML->load_html(location => $art->{file});
       
   491   for ($bom->findnodes('//a[@class="rakim"]')) {
       
   492     my $key;
       
   493     my $link_text;
       
   494     if (@olinks) {
       
   495       $key = pop @olinks;
       
   496     } else {
       
   497       ($key) = parse_keywords($_->to_literal);
       
   498       $link_text = $_->{href};
       
   499       $_->removeChild($_->firstChild());
       
   500       $_->appendText($link_text);
       
   501     }
       
   502     push @links, $key;
       
   503     my $l = get_art(@{$key})->{file} or die "Link not found in $art->{filename}\n";
       
   504     $_->{href} = prel_path("/".$art->{file}, "/$l");
       
   505     print $_;
       
   506   }
       
   507   $art->{links} = \@links;
       
   508   return $bom
   465 }
   509 }
   466 
   510 
   467 # I don't want to prepopulate, as it is not known which ones are
   511 # I don't want to prepopulate, as it is not known which ones are
   468 # usless and outdated, and it would be a waste of cycles parsing
   512 # usless and outdated, and it would be a waste of cycles parsing
   469 # something to know it is not needed
   513 # something to know it is not needed
   474     my %dome = ('file' => $fn);
   518     my %dome = ('file' => $fn);
   475     (@dome{'title', 'catag'}, my $date) =
   519     (@dome{'title', 'catag'}, my $date) =
   476       map
   520       map
   477       { $d->getChildrenByTagName($_)->[0]->to_literal }
   521       { $d->getChildrenByTagName($_)->[0]->to_literal }
   478       qw[title catag date];
   522       qw[title catag date];
       
   523     for ($d->getChildrenByTagName("links")) {
       
   524       my @ll = map [$_->{catag}, $_->to_literal],
       
   525 	($_->getChildrenByTagName('link'));
       
   526       $dome{links} = \@ll;
       
   527     }
   479     $catags{$dome{catag}}++;
   528     $catags{$dome{catag}}++;
   480     $dome{date} = PDate->new(split /-/, $date);
   529     $dome{date} = PDate->new(split /-/, $date);
   481     return \%dome;
   530     return \%dome;
   482   }
   531   }
   483   return 0;
   532   return 0;
   485 
   534 
   486 sub html_fixup ($filename) {
   535 sub html_fixup ($filename) {
   487   # for (getcache($filename)) {
   536   # for (getcache($filename)) {
   488   # $_ && return $_;
   537   # $_ && return $_;
   489   # }
   538   # }
   490   unless ($cache && grep /^$filename$/, @modified_files) {
   539   unless (!$cache && grep /^$filename$/, @modified_files) {
   491     for (getcache($filename)) {
   540     for (getcache($filename)) {
   492       return $_ if $_
   541       return $_ if $_
   493     }
   542     }
   494   }
   543   }
   495   my $dom =
   544   my $dom =