genorg.pl
changeset 5 24fc5a406138
parent 4 c98c7c32ab46
child 6 c576e88fef13
equal deleted inserted replaced
4:c98c7c32ab46 5:24fc5a406138
     9 use File::Find;
     9 use File::Find;
    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 
    15 
    15 # Fix up CLI interface
    16 # Fix up CLI interface
    16 # Add option ro set cache to 0 through command line
    17 # Add option ro set cache to 0 through command line
    17 # Print required elisp code it is in $req_config
    18 # Print required elisp code it is in $req_config
    18 # Add config option for $max_rss and $max_cat
    19 # Add config option for $max_rss and $max_cat
    33 my $no_name_dir = "c";
    34 my $no_name_dir = "c";
    34 my $cache_file = "genorg-cache/cache.xml";
    35 my $cache_file = "genorg-cache/cache.xml";
    35 my $domain = "p.bauherren.ovh";
    36 my $domain = "p.bauherren.ovh";
    36 my $max_rss = 30;
    37 my $max_rss = 30;
    37 # The first page has one less article.  Think of it as a feature.
    38 # The first page has one less article.  Think of it as a feature.
    38 my $max_cat = 10;
    39 my $max_cat = 7;
    39 
    40 
    40 my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<);
    41 my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<);
    41 my $template = <<"END";
    42 my $template = <<"END";
    42 #+title: Test document
    43 #+title: Test document
    43 #+subtitle: This is the subtitle
    44 #+subtitle: This is the subtitle
    69 			 #'(lambda (link desc _ _)
    70 			 #'(lambda (link desc _ _)
    70 			     (format "<genorg desc=\"%s\" link=\"%s\"/>" desc link))
    71 			     (format "<genorg desc=\"%s\" link=\"%s\"/>" desc link))
    71 			 :store nil))
    72 			 :store nil))
    72 END
    73 END
    73 
    74 
       
    75 my $custom_code = "(require 'ob-dot) (setq org-confirm-babel-evaluate nil)";
       
    76 
    74 my $elisp_code = <<"END" =~ s/\n\s+//gr;
    77 my $elisp_code = <<"END" =~ s/\n\s+//gr;
    75 (let ((ls '(LIST)))
    78 (let ((ls '(LIST)))
    76   (require 'org)
    79   (require 'org)
       
    80   $custom_code
    77   (mapc
    81   (mapc
    78    (lambda (b)
    82    (lambda (b)
    79      (with-current-buffer b
    83      (with-current-buffer b
    80        (when (and (not (string-match "^ " (buffer-name b)))
    84        (when (and (not (string-match "^ " (buffer-name b)))
    81 		  (eq major-mode 'org-mode))
    85 		  (eq major-mode 'org-mode))
   270   read_config $config_file;
   274   read_config $config_file;
   271   -d $dir or die "$dir doesn't exist \n";
   275   -d $dir or die "$dir doesn't exist \n";
   272   find (\&wanted, $dir);
   276   find (\&wanted, $dir);
   273   mkdir for @files_to_make;
   277   mkdir for @files_to_make;
   274   values %{conv_files()};
   278   values %{conv_files()};
   275   my $cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file;
   279   $cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file;
   276   chdir $outdir;
   280   chdir $outdir;
   277   @arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps;
   281   @arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps;
       
   282   finalise_html();
   278   chdir '..';
   283   chdir '..';
   279   populate_cache();
   284   populate_cache();
   280   # finalise_html();
       
   281 }
   285 }
   282 
   286 
   283 sub populate_cache {
   287 sub populate_cache {
   284   my $dom = XML::LibXML::Document->new('1.0', 'UTF-8');
   288   my $dom = XML::LibXML::Document->new('1.0', 'UTF-8');
   285   my $root = $dom->createElement('root');
   289   my $root = $dom->createElement('root');
   298 	my ($catag, $artt) = @{$_};
   302 	my ($catag, $artt) = @{$_};
   299 	my $rel_str .= "\"$catag\"" . ($artt && "->\"$artt\"");
   303 	my $rel_str .= "\"$catag\"" . ($artt && "->\"$artt\"");
   300 	$related->appendText("\"$catag\"" . ($artt && "->\"$artt\" "));
   304 	$related->appendText("\"$catag\"" . ($artt && "->\"$artt\" "));
   301       }
   305       }
   302     }
   306     }
   303     $art_el->appendChild($_) for ($date, $ca, $related);
   307     $art_el->appendChild($_) for ($date, $ca, $related, $title);
   304     $root->appendChild($art_el);
   308     $root->appendChild($art_el);
   305   }
   309   }
   306   $root->appendChild($_) for values %c_els;
   310   $root->appendChild($_) for values %c_els;
   307   $dom->setDocumentElement($root);
   311   $dom->setDocumentElement($root);
   308   open my $fh, '>' ,$cache_file;
   312   open my $fh, '>' , $cache_file;
   309   print $fh ($dom->toString(1) =~ s/(.+)/$1\n<!-- Nicley formated :() -->/r);
   313   print $fh ($dom->toString(1) =~ s/(.+)/$1\n<!-- Nicley formated :() -->/r);
   310   close $fh;
   314   close $fh;
   311 }
   315 }
   312 
   316 
   313 sub entry {
   317 sub entry {
   315   my $str = defined $catagory
   319   my $str = defined $catagory
   316     ? ($h_en_cat =~ s/CATAG/$data->{catag}/er) : $h_en;
   320     ? ($h_en_cat =~ s/CATAG/$data->{catag}/er) : $h_en;
   317   my $prel_path = "/$no_name_dir/" . (defined $catagory ? "c" : "c/b");
   321   my $prel_path = "/$no_name_dir/" . (defined $catagory ? "c" : "c/b");
   318   $str =~ s/DATE/$data->{date}->fmt/er
   322   $str =~ s/DATE/$data->{date}->fmt/er
   319     =~ s/TITLE/$data->{title}/er
   323     =~ s/TITLE/$data->{title}/er
   320     =~ s/DESCREPTION/$data->{desc}/er
       
   321     =~ s|LINK|prel_path($prel_path, '/'. $data->{file})|er
   324     =~ s|LINK|prel_path($prel_path, '/'. $data->{file})|er
   322   }
   325   }
   323 
   326 
   324 sub rss_en ($data) {
   327 sub rss_en ($data) {
   325   my $thing = <<"END" =~ s/\n\s+//gr;
   328   my $thing = <<"END" =~ s/\n\s+//gr;
   326   <item>
   329  <item>
   327     <title>TITLE</title>
   330     <title>TITLE</title>
   328     <link>LINK</link>
   331     <link>LINK</link>
   329     <description>DESC</description>
   332     <description>DESC</description>
   330   </item>
   333   </item>
   331 END
   334 END
   332   $thing =~ s|LINK|"$domain/" . $data->{file}|er
   335   $thing =~ s|LINK|"$domain/" . $data->{file}|er
   333     =~ s/TITLE/$data->{title}/er
   336     =~ s/TITLE/$data->{title}/er;
   334     =~ s/DESC/$data->{desc}/er;
       
   335 }
   337 }
   336 
   338 
   337 sub min ($x, $y) {
   339 sub min ($x, $y) {
   338   ($x, $y)[$x > $y]
   340   ($x, $y)[$x > $y]
   339 }
   341 }
   352      }
   354      }
   353 
   355 
   354 sub move_on_if_neccasary ($fh, $art_num, $total_articles, $path) {
   356 sub move_on_if_neccasary ($fh, $art_num, $total_articles, $path) {
   355   return $fh if $art_num == 0 || $art_num % $max_cat;
   357   return $fh if $art_num == 0 || $art_num % $max_cat;
   356   my $cpage = $art_num / $max_cat;
   358   my $cpage = $art_num / $max_cat;
   357   print $fh panigation($cpage, int 0.6 +  $total_articles / $max_cat, $path);
   359   print $fh panigation($cpage, ceil($total_articles / $max_cat), $path);
   358   open $fh, '>', $path . '/' . ($cpage + 1).'.html';
   360   open $fh, '>', $path . '/' . ($cpage + 1).'.html';
   359   $fh;
   361   $fh;
   360 }
   362 }
   361 
   363 
   362 sub panigation ($page, $no_pages, $dir) {
   364 sub panigation ($page, $no_pages, $dir) {
   363   say ($no_pages - 4);
       
   364   if ($no_pages < 9) {
   365   if ($no_pages < 9) {
   365     list2paginaiton ($page, (map [$_, $_], (1..$no_pages)))
   366     list2paginaiton ($page, (map [$_, $_], (1..$no_pages)))
   366   } elsif (4 < $page <= ($no_pages - 4)) {
   367   } elsif (4 < $page <= ($no_pages - 4)) {
   367     say "HERE";
       
   368     list2paginaiton
   368     list2paginaiton
   369       ($page,
   369       ($page,
   370        [1, 1],
   370        [1, 1],
   371        ["<li>..."],
   371        ["<li>..."],
   372        (map [$_, $_], (($page-2)..($page+2))),
   372        (map [$_, $_], (($page-2)..($page+2))),
   383 	   ["<li>..."],
   383 	   ["<li>..."],
   384 	   (map [$_, $_], (($no_pages - 5)..$no_pages)))
   384 	   (map [$_, $_], (($no_pages - 5)..$no_pages)))
   385 	}
   385 	}
   386 }
   386 }
   387 
   387 
       
   388 sub linkify ($bom) {
       
   389   $bom
       
   390 }
       
   391 
   388 sub finalise_html {
   392 sub finalise_html {
   389   mkdir $no_name_dir;
   393   mkdir $no_name_dir;
   390   open my $fh, '>', "$no_name_dir/1.html";
   394   open my $fh, '>', "$no_name_dir/1.html";
   391   open my $mrss, '>', "$no_name_dir/rss.xml"; # The master rss file
   395   open my $mrss, '>', "$no_name_dir/rss.xml"; # The master rss file
   392   print $mrss $rss_entry;
   396   print $mrss $rss_entry;
   431     print {$$cfh} entry $art;
   435     print {$$cfh} entry $art;
   432     my $a_file = $art->{file};
   436     my $a_file = $art->{file};
   433     # say $a_file;
   437     # say $a_file;
   434     if (grep /^$a_file$/, @modified_files) {
   438     if (grep /^$a_file$/, @modified_files) {
   435       open my $h_file, '>', $a_file;
   439       open my $h_file, '>', $a_file;
   436       # print $h_file $art->{dom}->toString;
   440       my $doom = linkify $art->{dom};
       
   441       print $h_file $doom->toString;
   437       close $h_file;
   442       close $h_file;
   438     }
   443     }
   439   }
   444   }
   440   print $fh panigation (($n_fh / $max_cat) x 2, $no_name_dir) if $max_cat < $total_articles;
   445   print $fh panigation ((ceil $n_fh / $max_cat) x 2, $no_name_dir) if $max_cat < $total_articles;
   441   print $fh $h_end;
   446   print $fh $h_end;
   442   close $fh;
   447   close $fh;
   443   for (values %c_files) {
   448   for (values %c_files) {
   444     print $_ $h_end;
   449     print $_ $h_end;
   445     close;
   450     close;
   452 }
   457 }
   453 
   458 
   454 # I don't want to prepopulate, as it is not known which ones are
   459 # I don't want to prepopulate, as it is not known which ones are
   455 # usless and outdated, and it would be a waste of cycles parsing
   460 # usless and outdated, and it would be a waste of cycles parsing
   456 # something to know it is not needed
   461 # something to know it is not needed
   457 sub getcache {
   462 sub getcache ($fn) {
   458   $cache_dom || return 0;
   463   $cache_dom || return 0;
       
   464   for my $d ($cache_dom->findnodes("/root/art[\@path=\"$fn\"]")) {
       
   465     # TODO related
       
   466     my %dome = ('file' => $fn);
       
   467     (@dome{'title', 'catag'}, my $date) =
       
   468       map
       
   469       { $d->getChildrenByTagName($_)->[0]->to_literal }
       
   470       qw[title catag date];
       
   471     $catags{$dome{catag}}++;
       
   472     $dome{date} = PDate->new(split /-/, $date);
       
   473     return \%dome;
       
   474   }
   459   return 0;
   475   return 0;
   460 }
   476 }
   461 
   477 
   462 sub html_fixup ($filename) {
   478 sub html_fixup ($filename) {
   463    # for (getcache($filename)) {
   479   # for (getcache($filename)) {
   464     # $_ && return $_;
   480   # $_ && return $_;
   465   # }
   481   # }
   466   unless (grep /^$filename$/, @modified_files) {
   482   unless ($cache && grep /^$filename$/, @modified_files) {
   467     for (getcache($filename)) {
   483     for (getcache($filename)) {
   468       return $_ if $_
   484       return $_ if $_
   469     }
   485     }
   470   }
   486   }
   471   my $dom =
   487   my $dom =
   495   }
   511   }
   496   # keywords/catogry
   512   # keywords/catogry
   497   for my $node ($dom->findnodes('/html/head/meta')) {
   513   for my $node ($dom->findnodes('/html/head/meta')) {
   498     (my $cont = $node->getAttribute('content')) || next;
   514     (my $cont = $node->getAttribute('content')) || next;
   499     for ($node->getAttribute('name')) {
   515     for ($node->getAttribute('name')) {
   500       if (/description/) {
   516       if (/keywords/) {
   501 	$data{desc} = $cont;
       
   502       } elsif (/keywords/) {
       
   503 	my ($key, $rest) = parse_keywords($cont);
   517 	my ($key, $rest) = parse_keywords($cont);
   504 	@data{qw[catag title]} = @{$key};
   518 	@data{qw[catag title]} = @{$key};
   505 	$catags{$data{catag}}++;
   519 	$catags{$data{catag}}++;
   506 	$rest =~ s/\s+\|\s+//;
   520 	$rest =~ s/\s+\|\s+//;
   507 	while ($rest) {
   521 	while ($rest) {
   558       qw(January Febuary March April May June July August September November October December);
   572       qw(January Febuary March April May June July August September November October December);
   559     my $n = $self->{day};
   573     my $n = $self->{day};
   560     if ($n == 1) { $n = '1st' }
   574     if ($n == 1) { $n = '1st' }
   561     elsif (($n - 2) % 10 == 0) { $n = "${n}nd" }
   575     elsif (($n - 2) % 10 == 0) { $n = "${n}nd" }
   562     elsif (($n - 3) % 10 == 0) { $n = "${n}rd" }
   576     elsif (($n - 3) % 10 == 0) { $n = "${n}rd" }
   563     else { $n = "${n}st" }
   577     else { $n = "${n}th" }
   564     $months[$self->{month} - 1] . " $n, " . $self->{year}
   578     $months[$self->{month} - 1] . " $n, " . $self->{year}
   565   }
   579   }
   566     
   580     
   567   sub short_fmt {
   581   sub short_fmt {
   568     my $self = shift;
   582     my $self = shift;