genorg.pl
changeset 3 bf4ae6f1dbbf
parent 2 c4ca65113229
child 4 c98c7c32ab46
--- a/genorg.pl	Fri Dec 13 23:15:34 2024 +1000
+++ b/genorg.pl	Sat Dec 14 16:22:55 2024 +1000
@@ -19,7 +19,7 @@
 # Sort cataogirs by entires
 # Variable for map_css
 # Domain thing as well
-# Dicmuemnt how init.el is required
+# Load custom filpe for htmlize and all that
 my $cache = 1;
 
 my $config_file = "genorg-conf";
@@ -29,10 +29,11 @@
 my $css = "style.css";
 my $map_css ="other.css";
 my $no_name_dir = "c";
+my $cache_file = ".genorg/cache.xml";
 my $domain = "p.bauherren.ovh";
 my $max_rss = 30;
 # The first page has one less article.  Think of it as a feature.
-my $max_cat = 1;
+my $max_cat = 10;
 
 my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<);
 my $template = <<"END";
@@ -221,6 +222,7 @@
   }
 }
 
+my @modified_files;
 
 # Export @org_exps
 sub conv_files {
@@ -232,6 +234,7 @@
     s/org$/html/ for ($to, $rel_file);
     unless ($cache && -f $to && (stat($to))[9] > (stat($file))[9]) {
       $changed_files{$file} = $to;
+      push @modified_files, $rel_file;
     }
   }
   %changed_files // exit;
@@ -256,6 +259,7 @@
 
 my %catags;
 my @arts;
+my $cache_dom;
 
 sub main {
   -d $cdir or help_and_bye;
@@ -266,9 +270,23 @@
   find (\&wanted, $dir);
   mkdir for @files_to_make;
   values %{conv_files()};
+  # my $cache_dom = XML::LibXML->load_xml(location => $cache_dom) if $cache && -f $cache_file;
   chdir $outdir;
   @arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps;
-  finalise_html();
+  chdir '..';
+  populate_cache();
+  # finalise_html();
+}
+
+sub populate_cache {
+  my $dom = XML::LibXML::Document->new('1.0', 'UTF-8');
+  my $title = $dom->createElement('title');
+  for my $art (@arts) {
+    print Dumper($art);
+  }
+  open my $fh, '>' ,$cache_file;
+  print $fh $dom->toString;
+  close $fh;
 }
 
 sub entry {
@@ -304,6 +322,7 @@
   my $page = shift;
   '<ul>' . join ("", map {
     my ($str, $file) = @{$_};
+    $str = "---$str" if $file && ($file == $page);
     defined $file ?
       "<li><a href=\"$file.html\">$str</a>" : $str
     } ((["←", $page - 1]) x!! ($page - 1),
@@ -311,33 +330,40 @@
        (["→", $page + 1]) x ($page != $_[$#_]->[1]))) . '</ul>'
      }
 
-sub panigation ($page, $no_pages, $dir) {
-  say "HERE";
-  if ($no_pages < 10) {
-    say "There";
-    list2paginaiton ($page, (map [$_ == $page ? "---$_" : $_, $_], (1..$no_pages)))
-    } else {
-      list2paginaiton
-	(# (["←", $page - 1]) x!! ($page - 1) ,
-	 [1, 1],
-	 ["<li>..."],
-	 (map [$_ == $page ? "---$_" : $_, $_], (($page-2)..($page+2))),
-	 ["<li>..."],
-	 [($no_pages) x 2],
-	 # (["→", $page + 1]) x ($page != $no_pages)
-       )  
-      }
-}
-
 sub move_on_if_neccasary ($fh, $art_num, $total_articles, $path) {
   return $fh if $art_num == 0 || $art_num % $max_cat;
   my $cpage = $art_num / $max_cat;
-  # saypani "\$cpage is $cpage is $art_num / $max_cat";
   print $fh panigation($cpage, int 0.6 +  $total_articles / $max_cat, $path);
   open $fh, '>', $path . '/' . ($cpage + 1).'.html';
   $fh;
 }
 
+sub panigation ($page, $no_pages, $dir) {
+  say ($no_pages - 4);
+  if ($no_pages < 9) {
+    list2paginaiton ($page, (map [$_, $_], (1..$no_pages)))
+  } elsif (4 < $page <= ($no_pages - 4)) {
+    say "HERE";
+    list2paginaiton
+      ($page,
+       [1, 1],
+       ["<li>..."],
+       (map [$_, $_], (($page-2)..($page+2))),
+       ["<li>..."],
+       [($no_pages) x 2])  
+    } elsif ($page < 6) {
+      list2paginaiton $page,
+	((map [$_, $_], (1..5)),
+	 ["<li>..."],
+	 [($no_pages) x 2])
+      } else {
+	list2paginaiton $page,
+	  ([1,1],
+	   ["<li>..."],
+	   (map [$_, $_], (($no_pages - 5)..$no_pages)))
+	}
+}
+
 sub finalise_html {
   mkdir $no_name_dir;
   open my $fh, '>', "$no_name_dir/1.html";
@@ -348,7 +374,8 @@
   while (my ($cat, $num) = each %catags) {
     # ($h_intro, $h_c_intro, $h_cat_li, $h_chap, $h_en_cat, $h_en, $h_end)
     print $fh $h_cat_li =~ s/NUMBER/$num/re
-      =~ s/CATAG/$cat/er;
+      =~ s/CATAG/$cat/er
+      =~ s=LINK="$cat/1.html"=er;
     mkdir "$no_name_dir/$cat";
     open $c_files{$cat}, '>', "$no_name_dir/$cat/1.html";
     open $r_files{$cat}, '>', "$no_name_dir/$cat/rss.xml";
@@ -372,15 +399,22 @@
       print $mrss $rentry;
       ++$n_mrss
     }
-    # my $ncat = \$n_cat{$catag};
     # ($fh, $art_num, $total_articles, $path)
     $fh = move_on_if_neccasary $fh, $n_fh, $total_articles, $no_name_dir;
     $n_fh++;
     print $fh entry $art, 1;
-    print {$c_files{$catag}} entry $art;
-    # say "$catag -> $title($n_cat{$catag})";
-    # say ($n_cat{$catag} // 1);
-    # ++$$ncat
+    my $ncat = \$n_cat{$catag};
+    ++$$ncat;
+    my $cfh = \$c_files{$catag};
+    $$cfh = move_on_if_neccasary $$cfh, $$ncat, $catags{$catag}, "$no_name_dir/$catag";
+    print {$$cfh} entry $art;
+    my $a_file = $art->{file};
+    # say $a_file;
+    if (grep /^$a_file$/, @modified_files) {
+      open my $h_file, '>', $a_file;
+      # print $h_file $art->{dom}->toString;
+      close $h_file;
+    }
   }
   print $fh panigation (($n_fh / $max_cat) x 2, $no_name_dir) if $max_cat < $total_articles;
   print $fh $h_end;
@@ -396,7 +430,16 @@
   }
 }
 
+sub getcache {
+  
+}
+
 sub html_fixup ($filename) {
+  # I don't want to prepopulate, as it is not known which ones are
+  # usless and outdated
+  for (getcache($filename)) {
+    $_ && return $_;
+  }
   my $dom =
     XML::LibXML->load_html(location  => $filename, recover   => 1);
   my %data = ('file', $filename);
@@ -443,11 +486,8 @@
       $node->parentNode->removeChild($node);
     }
   }
-  # $data{dom} = $dom;
+  $data{dom} = $dom;
   \%data;
-  
-  # print $newfh $dom->toString;
-  # close $newfh;
 }
 
 sub parse_keywords ($str) {