genorg.pl
changeset 3 bf4ae6f1dbbf
parent 2 c4ca65113229
child 4 c98c7c32ab46
equal deleted inserted replaced
2:c4ca65113229 3:bf4ae6f1dbbf
    17 # Print required elisp code it is in $req_config
    17 # Print required elisp code it is in $req_config
    18 # Add config option for $max_rss and $max_cat
    18 # Add config option for $max_rss and $max_cat
    19 # Sort cataogirs by entires
    19 # Sort cataogirs by entires
    20 # Variable for map_css
    20 # Variable for map_css
    21 # Domain thing as well
    21 # Domain thing as well
    22 # Dicmuemnt how init.el is required
    22 # Load custom filpe for htmlize and all that
    23 my $cache = 1;
    23 my $cache = 1;
    24 
    24 
    25 my $config_file = "genorg-conf";
    25 my $config_file = "genorg-conf";
    26 my $dir = "blog";
    26 my $dir = "blog";
    27 my $outdir = "out/";
    27 my $outdir = "out/";
    28 my $emacs = "emacs";
    28 my $emacs = "emacs";
    29 my $css = "style.css";
    29 my $css = "style.css";
    30 my $map_css ="other.css";
    30 my $map_css ="other.css";
    31 my $no_name_dir = "c";
    31 my $no_name_dir = "c";
       
    32 my $cache_file = ".genorg/cache.xml";
    32 my $domain = "p.bauherren.ovh";
    33 my $domain = "p.bauherren.ovh";
    33 my $max_rss = 30;
    34 my $max_rss = 30;
    34 # The first page has one less article.  Think of it as a feature.
    35 # The first page has one less article.  Think of it as a feature.
    35 my $max_cat = 1;
    36 my $max_cat = 10;
    36 
    37 
    37 my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<);
    38 my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<);
    38 my $template = <<"END";
    39 my $template = <<"END";
    39 #+title: Test document
    40 #+title: Test document
    40 #+subtitle: This is the subtitle
    41 #+subtitle: This is the subtitle
   219   } else {
   220   } else {
   220     copy $_, $n_path;
   221     copy $_, $n_path;
   221   }
   222   }
   222 }
   223 }
   223 
   224 
       
   225 my @modified_files;
   224 
   226 
   225 # Export @org_exps
   227 # Export @org_exps
   226 sub conv_files {
   228 sub conv_files {
   227   my %changed_files;
   229   my %changed_files;
   228   # Remove files that are already converted
   230   # Remove files that are already converted
   230     # Emacs need the full file path
   232     # Emacs need the full file path
   231     my ($file, $to) = map { getcwd . "/$_" . $rel_file} ($dir, $outdir);
   233     my ($file, $to) = map { getcwd . "/$_" . $rel_file} ($dir, $outdir);
   232     s/org$/html/ for ($to, $rel_file);
   234     s/org$/html/ for ($to, $rel_file);
   233     unless ($cache && -f $to && (stat($to))[9] > (stat($file))[9]) {
   235     unless ($cache && -f $to && (stat($to))[9] > (stat($file))[9]) {
   234       $changed_files{$file} = $to;
   236       $changed_files{$file} = $to;
       
   237       push @modified_files, $rel_file;
   235     }
   238     }
   236   }
   239   }
   237   %changed_files // exit;
   240   %changed_files // exit;
   238   $elisp_code =~ s/LIST/join '', map "\"$_\" ", values %changed_files /e;
   241   $elisp_code =~ s/LIST/join '', map "\"$_\" ", values %changed_files /e;
   239   open my $fh, '-|', ($emacs, '--batch', '~/.emacs.d/init.el', keys %changed_files, "--eval" , "$elisp_code");
   242   open my $fh, '-|', ($emacs, '--batch', '~/.emacs.d/init.el', keys %changed_files, "--eval" , "$elisp_code");
   254 $cdir || die "directory not provided\n";
   257 $cdir || die "directory not provided\n";
   255 main();
   258 main();
   256 
   259 
   257 my %catags;
   260 my %catags;
   258 my @arts;
   261 my @arts;
       
   262 my $cache_dom;
   259 
   263 
   260 sub main {
   264 sub main {
   261   -d $cdir or help_and_bye;
   265   -d $cdir or help_and_bye;
   262   mkdir $outdir;
   266   mkdir $outdir;
   263   chdir $cdir or die "Couldn't access $cdir\n";
   267   chdir $cdir or die "Couldn't access $cdir\n";
   264   read_config $config_file;
   268   read_config $config_file;
   265   -d $dir or die "$dir doesn't exist \n";
   269   -d $dir or die "$dir doesn't exist \n";
   266   find (\&wanted, $dir);
   270   find (\&wanted, $dir);
   267   mkdir for @files_to_make;
   271   mkdir for @files_to_make;
   268   values %{conv_files()};
   272   values %{conv_files()};
       
   273   # my $cache_dom = XML::LibXML->load_xml(location => $cache_dom) if $cache && -f $cache_file;
   269   chdir $outdir;
   274   chdir $outdir;
   270   @arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps;
   275   @arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps;
   271   finalise_html();
   276   chdir '..';
       
   277   populate_cache();
       
   278   # finalise_html();
       
   279 }
       
   280 
       
   281 sub populate_cache {
       
   282   my $dom = XML::LibXML::Document->new('1.0', 'UTF-8');
       
   283   my $title = $dom->createElement('title');
       
   284   for my $art (@arts) {
       
   285     print Dumper($art);
       
   286   }
       
   287   open my $fh, '>' ,$cache_file;
       
   288   print $fh $dom->toString;
       
   289   close $fh;
   272 }
   290 }
   273 
   291 
   274 sub entry {
   292 sub entry {
   275   my ($data, $catagory) = @_;
   293   my ($data, $catagory) = @_;
   276   my $str = defined $catagory
   294   my $str = defined $catagory
   302 # @_ has to be in format of [STRING, LINK]
   320 # @_ has to be in format of [STRING, LINK]
   303 sub list2paginaiton {
   321 sub list2paginaiton {
   304   my $page = shift;
   322   my $page = shift;
   305   '<ul>' . join ("", map {
   323   '<ul>' . join ("", map {
   306     my ($str, $file) = @{$_};
   324     my ($str, $file) = @{$_};
       
   325     $str = "---$str" if $file && ($file == $page);
   307     defined $file ?
   326     defined $file ?
   308       "<li><a href=\"$file.html\">$str</a>" : $str
   327       "<li><a href=\"$file.html\">$str</a>" : $str
   309     } ((["←", $page - 1]) x!! ($page - 1),
   328     } ((["←", $page - 1]) x!! ($page - 1),
   310        @_,
   329        @_,
   311        (["→", $page + 1]) x ($page != $_[$#_]->[1]))) . '</ul>'
   330        (["→", $page + 1]) x ($page != $_[$#_]->[1]))) . '</ul>'
   312      }
   331      }
   313 
   332 
   314 sub panigation ($page, $no_pages, $dir) {
       
   315   say "HERE";
       
   316   if ($no_pages < 10) {
       
   317     say "There";
       
   318     list2paginaiton ($page, (map [$_ == $page ? "---$_" : $_, $_], (1..$no_pages)))
       
   319     } else {
       
   320       list2paginaiton
       
   321 	(# (["←", $page - 1]) x!! ($page - 1) ,
       
   322 	 [1, 1],
       
   323 	 ["<li>..."],
       
   324 	 (map [$_ == $page ? "---$_" : $_, $_], (($page-2)..($page+2))),
       
   325 	 ["<li>..."],
       
   326 	 [($no_pages) x 2],
       
   327 	 # (["→", $page + 1]) x ($page != $no_pages)
       
   328        )  
       
   329       }
       
   330 }
       
   331 
       
   332 sub move_on_if_neccasary ($fh, $art_num, $total_articles, $path) {
   333 sub move_on_if_neccasary ($fh, $art_num, $total_articles, $path) {
   333   return $fh if $art_num == 0 || $art_num % $max_cat;
   334   return $fh if $art_num == 0 || $art_num % $max_cat;
   334   my $cpage = $art_num / $max_cat;
   335   my $cpage = $art_num / $max_cat;
   335   # saypani "\$cpage is $cpage is $art_num / $max_cat";
       
   336   print $fh panigation($cpage, int 0.6 +  $total_articles / $max_cat, $path);
   336   print $fh panigation($cpage, int 0.6 +  $total_articles / $max_cat, $path);
   337   open $fh, '>', $path . '/' . ($cpage + 1).'.html';
   337   open $fh, '>', $path . '/' . ($cpage + 1).'.html';
   338   $fh;
   338   $fh;
       
   339 }
       
   340 
       
   341 sub panigation ($page, $no_pages, $dir) {
       
   342   say ($no_pages - 4);
       
   343   if ($no_pages < 9) {
       
   344     list2paginaiton ($page, (map [$_, $_], (1..$no_pages)))
       
   345   } elsif (4 < $page <= ($no_pages - 4)) {
       
   346     say "HERE";
       
   347     list2paginaiton
       
   348       ($page,
       
   349        [1, 1],
       
   350        ["<li>..."],
       
   351        (map [$_, $_], (($page-2)..($page+2))),
       
   352        ["<li>..."],
       
   353        [($no_pages) x 2])  
       
   354     } elsif ($page < 6) {
       
   355       list2paginaiton $page,
       
   356 	((map [$_, $_], (1..5)),
       
   357 	 ["<li>..."],
       
   358 	 [($no_pages) x 2])
       
   359       } else {
       
   360 	list2paginaiton $page,
       
   361 	  ([1,1],
       
   362 	   ["<li>..."],
       
   363 	   (map [$_, $_], (($no_pages - 5)..$no_pages)))
       
   364 	}
   339 }
   365 }
   340 
   366 
   341 sub finalise_html {
   367 sub finalise_html {
   342   mkdir $no_name_dir;
   368   mkdir $no_name_dir;
   343   open my $fh, '>', "$no_name_dir/1.html";
   369   open my $fh, '>', "$no_name_dir/1.html";
   346   print $fh $h_intro . $h_c_intro;
   372   print $fh $h_intro . $h_c_intro;
   347   my (%c_files, %r_files);	# Cataogry files and rss
   373   my (%c_files, %r_files);	# Cataogry files and rss
   348   while (my ($cat, $num) = each %catags) {
   374   while (my ($cat, $num) = each %catags) {
   349     # ($h_intro, $h_c_intro, $h_cat_li, $h_chap, $h_en_cat, $h_en, $h_end)
   375     # ($h_intro, $h_c_intro, $h_cat_li, $h_chap, $h_en_cat, $h_en, $h_end)
   350     print $fh $h_cat_li =~ s/NUMBER/$num/re
   376     print $fh $h_cat_li =~ s/NUMBER/$num/re
   351       =~ s/CATAG/$cat/er;
   377       =~ s/CATAG/$cat/er
       
   378       =~ s=LINK="$cat/1.html"=er;
   352     mkdir "$no_name_dir/$cat";
   379     mkdir "$no_name_dir/$cat";
   353     open $c_files{$cat}, '>', "$no_name_dir/$cat/1.html";
   380     open $c_files{$cat}, '>', "$no_name_dir/$cat/1.html";
   354     open $r_files{$cat}, '>', "$no_name_dir/$cat/rss.xml";
   381     open $r_files{$cat}, '>', "$no_name_dir/$cat/rss.xml";
   355     print {$r_files{$cat}} $rss_entry;
   382     print {$r_files{$cat}} $rss_entry;
   356     print {$c_files{$cat}} $h_intro;
   383     print {$c_files{$cat}} $h_intro;
   370     }
   397     }
   371     if ($n_mrss < $max_rss) {
   398     if ($n_mrss < $max_rss) {
   372       print $mrss $rentry;
   399       print $mrss $rentry;
   373       ++$n_mrss
   400       ++$n_mrss
   374     }
   401     }
   375     # my $ncat = \$n_cat{$catag};
       
   376     # ($fh, $art_num, $total_articles, $path)
   402     # ($fh, $art_num, $total_articles, $path)
   377     $fh = move_on_if_neccasary $fh, $n_fh, $total_articles, $no_name_dir;
   403     $fh = move_on_if_neccasary $fh, $n_fh, $total_articles, $no_name_dir;
   378     $n_fh++;
   404     $n_fh++;
   379     print $fh entry $art, 1;
   405     print $fh entry $art, 1;
   380     print {$c_files{$catag}} entry $art;
   406     my $ncat = \$n_cat{$catag};
   381     # say "$catag -> $title($n_cat{$catag})";
   407     ++$$ncat;
   382     # say ($n_cat{$catag} // 1);
   408     my $cfh = \$c_files{$catag};
   383     # ++$$ncat
   409     $$cfh = move_on_if_neccasary $$cfh, $$ncat, $catags{$catag}, "$no_name_dir/$catag";
       
   410     print {$$cfh} entry $art;
       
   411     my $a_file = $art->{file};
       
   412     # say $a_file;
       
   413     if (grep /^$a_file$/, @modified_files) {
       
   414       open my $h_file, '>', $a_file;
       
   415       # print $h_file $art->{dom}->toString;
       
   416       close $h_file;
       
   417     }
   384   }
   418   }
   385   print $fh panigation (($n_fh / $max_cat) x 2, $no_name_dir) if $max_cat < $total_articles;
   419   print $fh panigation (($n_fh / $max_cat) x 2, $no_name_dir) if $max_cat < $total_articles;
   386   print $fh $h_end;
   420   print $fh $h_end;
   387   close $fh;
   421   close $fh;
   388   for (values %c_files) {
   422   for (values %c_files) {
   394     print $_ '</channel> </rss>';;
   428     print $_ '</channel> </rss>';;
   395     close;
   429     close;
   396   }
   430   }
   397 }
   431 }
   398 
   432 
       
   433 sub getcache {
       
   434   
       
   435 }
       
   436 
   399 sub html_fixup ($filename) {
   437 sub html_fixup ($filename) {
       
   438   # I don't want to prepopulate, as it is not known which ones are
       
   439   # usless and outdated
       
   440   for (getcache($filename)) {
       
   441     $_ && return $_;
       
   442   }
   400   my $dom =
   443   my $dom =
   401     XML::LibXML->load_html(location  => $filename, recover   => 1);
   444     XML::LibXML->load_html(location  => $filename, recover   => 1);
   402   my %data = ('file', $filename);
   445   my %data = ('file', $filename);
   403 
   446 
   404   # We don't need style or table of contents heading (if exists)
   447   # We don't need style or table of contents heading (if exists)
   441 	next;
   484 	next;
   442       }
   485       }
   443       $node->parentNode->removeChild($node);
   486       $node->parentNode->removeChild($node);
   444     }
   487     }
   445   }
   488   }
   446   # $data{dom} = $dom;
   489   $data{dom} = $dom;
   447   \%data;
   490   \%data;
   448   
       
   449   # print $newfh $dom->toString;
       
   450   # close $newfh;
       
   451 }
   491 }
   452 
   492 
   453 sub parse_keywords ($str) {
   493 sub parse_keywords ($str) {
   454   if ($str =~ s/\s*(?:"(.+?)"|(\w+))\s*->(?:\s*(?:"(.+?)"|(\w+)))//) {
   494   if ($str =~ s/\s*(?:"(.+?)"|(\w+))\s*->(?:\s*(?:"(.+?)"|(\w+)))//) {
   455     ([$1 // $2, $3 // $4], $str);
   495     ([$1 // $2, $3 // $4], $str);