genorg.pl
changeset 7 f00ed34eca17
parent 6 c576e88fef13
child 8 ee9b57cbbefc
--- a/genorg.pl	Sun Dec 15 23:46:24 2024 +1000
+++ b/genorg.pl	Mon Dec 16 22:51:52 2024 +1000
@@ -12,6 +12,7 @@
 use experimental qw(declared_refs);
 use Data::Dumper;
 use POSIX;
+# !! Add seperatio between title and identifier
 
 # Fix up CLI interface
 # Add option ro set cache to 0 through command line
@@ -21,8 +22,9 @@
 # Variable for map_css
 # Remove the desc
 # Domain thing as well
-# Load custom filpe for htmlize and all that
 # 2 articles 1 day better sorting
+# cataogry linking
+# reinforce slash ending for $oudir
 my $cache = 1;
 
 my $config_file = "genorg-conf";
@@ -219,6 +221,7 @@
 
 my @org_exps;
 my @files_to_make;
+my @files_to_move;
 
 sub wanted {
   my $n_path = $outdir . prel_path ($dir,  "$File::Find::name/");
@@ -231,7 +234,7 @@
   if (/\.org$/) {
     push @org_exps, substr $n_path, length($outdir);
   } else {
-    copy $_, $n_path;
+    push @files_to_move, $File::Find::name
   }
 }
 
@@ -282,6 +285,9 @@
   read_config $config_file;
   -d $dir or die "$dir doesn't exist \n";
   find (\&wanted, $dir);
+  for (@files_to_move) {
+    copy $_, s#$dir/?#$outdir#er;
+  }
   mkdir for @files_to_make;
   values %{conv_files()};
   $cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file;
@@ -304,6 +310,17 @@
     $ca->appendText($art->{catag});
     $title->appendText($art->{title});
     $date->appendText($art->{date}->text_easy);
+    my @ll = @{$art->{links}};
+    if (@ll) {
+      my $links = $dom->createElement('links');
+      for (@ll) {
+	my $link = $dom->createElement("link");
+	$link->{catag} = $_->[0];
+	$link->appendText($_->[1]);
+	$links->appendChild($link);
+      }
+      $art_el->appendChild($links);
+    }
     my $rel_str;
     for my $thing ($art->{related}) {
       for (@{$thing}) {
@@ -393,8 +410,10 @@
 	}
 }
 
-sub linkify ($bom) {
-  $bom
+sub get_art($cat, $id) {
+  for (@arts) {
+    return $_ if $_->{catag} eq $cat && $_->{title} eq $id
+  }
 }
 
 sub finalise_html {
@@ -443,10 +462,10 @@
     print {$$cfh} entry $art;
     my $a_file = $art->{file};
     # say $a_file;
-    if (grep /^$a_file$/, @modified_files) {
+    if (1 or grep /^$a_file$/, @modified_files) {
+      my $doom = linkify($art);
       open my $h_file, '>', $a_file;
-      my $doom = linkify $art->{dom};
-      print $h_file $doom->toString;
+      print $h_file $doom->toStringHTML;
       close $h_file;
     }
   }
@@ -455,15 +474,40 @@
   close $fh;
   for (values %c_files) {
     print $_ $h_end;
-    close;
+    close $_;
   }
   print $mrss '</channel> </rss>';
   for (values %r_files) {
     print $_ '</channel> </rss>';;
-    close;
+    close $_;
   }
 }
 
+sub linkify ($art) {
+  # if $art->{dom} is undefined, we must sadly parse again
+  my @links;
+  my @olinks = @{$art->{links} //[]};
+  my $bom = $art->{dom} // XML::LibXML->load_html(location => $art->{file});
+  for ($bom->findnodes('//a[@class="rakim"]')) {
+    my $key;
+    my $link_text;
+    if (@olinks) {
+      $key = pop @olinks;
+    } else {
+      ($key) = parse_keywords($_->to_literal);
+      $link_text = $_->{href};
+      $_->removeChild($_->firstChild());
+      $_->appendText($link_text);
+    }
+    push @links, $key;
+    my $l = get_art(@{$key})->{file} or die "Link not found in $art->{filename}\n";
+    $_->{href} = prel_path("/".$art->{file}, "/$l");
+    print $_;
+  }
+  $art->{links} = \@links;
+  return $bom
+}
+
 # I don't want to prepopulate, as it is not known which ones are
 # usless and outdated, and it would be a waste of cycles parsing
 # something to know it is not needed
@@ -476,6 +520,11 @@
       map
       { $d->getChildrenByTagName($_)->[0]->to_literal }
       qw[title catag date];
+    for ($d->getChildrenByTagName("links")) {
+      my @ll = map [$_->{catag}, $_->to_literal],
+	($_->getChildrenByTagName('link'));
+      $dome{links} = \@ll;
+    }
     $catags{$dome{catag}}++;
     $dome{date} = PDate->new(split /-/, $date);
     return \%dome;
@@ -487,7 +536,7 @@
   # for (getcache($filename)) {
   # $_ && return $_;
   # }
-  unless ($cache && grep /^$filename$/, @modified_files) {
+  unless (!$cache && grep /^$filename$/, @modified_files) {
     for (getcache($filename)) {
       return $_ if $_
     }