genorg.pl
changeset 2 c4ca65113229
parent 1 0b7be2e78d3b
child 3 bf4ae6f1dbbf
--- a/genorg.pl	Fri Dec 13 00:46:06 2024 +1000
+++ b/genorg.pl	Fri Dec 13 23:15:34 2024 +1000
@@ -295,38 +295,47 @@
     =~ s/DESC/$data->{desc}/er;
 }
 
-sub make_h_list {
-  my $rstr = '<ul>';
-  for (@_) {
-    my ($str, $page_no) = @{$_};
-    $rstr .= "<li> <a href=\"$page_no.html\"> $str </a>";
-  }
-  $rstr . "</ul>"
-}
-
 sub min ($x, $y) {
   ($x, $y)[$x > $y]
 }
 
-sub panigation ($total_arts, $page_no) {
-  if ($total_arts < 9) {
-    make_h_list ((['&laquo', $page_no - 1]) x!! ($page_no - 1),
-		 (map [$_ == $page_no ? "<span>$_</span>" : $_,  $_ ],
-		  (1..($total_arts + 1))),
-		 ['&raquo', $page_no])
-  }
+# @_ has to be in format of [STRING, LINK]
+sub list2paginaiton {
+  my $page = shift;
+  '<ul>' . join ("", map {
+    my ($str, $file) = @{$_};
+    defined $file ?
+      "<li><a href=\"$file.html\">$str</a>" : $str
+    } ((["←", $page - 1]) x!! ($page - 1),
+       @_,
+       (["→", $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($article, $cfh, $next_file, $total_arts) {
-  # say "Article is ".  $article;
-  my $page = 1 + int $article / $max_cat;
-  print $cfh panigation int($total_arts / $max_cat), $page;
-  print $cfh $h_end;
-  close $cfh;
-  open $cfh, '>', "$next_file$page.html";
-  say "$next_file$page.html";
-  print $cfh $h_intro;
-  $cfh
+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 finalise_html {
@@ -364,20 +373,16 @@
       ++$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++;
-    if ($n_fh % $max_cat == 0) {
-      say "$n_fh % $max_cat = " . $n_fh % $max_cat ;
-      say "\$n_fh is $n_fh";
-      $fh = move_on($n_fh, $fh, "$no_name_dir/", $total_articles);
-    }
     print $fh entry $art, 1;
     print {$c_files{$catag}} entry $art;
     # say "$catag -> $title($n_cat{$catag})";
     # say ($n_cat{$catag} // 1);
     # ++$$ncat
   }
-  # ($total_arts, $page_no)
-  print $fh panigation ((int $total_articles / $max_cat) x 2) if $total_articles >= $max_cat;
+  print $fh panigation (($n_fh / $max_cat) x 2, $no_name_dir) if $max_cat < $total_articles;
   print $fh $h_end;
   close $fh;
   for (values %c_files) {