genorg.pl
changeset 8 ee9b57cbbefc
parent 7 f00ed34eca17
child 9 58b72fea234c
equal deleted inserted replaced
7:f00ed34eca17 8:ee9b57cbbefc
    17 # Fix up CLI interface
    17 # Fix up CLI interface
    18 # Add option ro set cache to 0 through command line
    18 # Add option ro set cache to 0 through command line
    19 # Print required elisp code it is in $req_config
    19 # Print required elisp code it is in $req_config
    20 # Add config option for $max_rss and $max_cat
    20 # Add config option for $max_rss and $max_cat
    21 # Sort cataogirs by entires
    21 # Sort cataogirs by entires
       
    22 # Document link to file x from file x no work
    22 # Variable for map_css
    23 # Variable for map_css
    23 # Remove the desc
    24 # Remove the desc
    24 # Domain thing as well
    25 # Domain thing as well
    25 # 2 articles 1 day better sorting
    26 # 2 articles 1 day better sorting
    26 # cataogry linking
    27 # cataogry linking
    29 
    30 
    30 my $config_file = "genorg-conf";
    31 my $config_file = "genorg-conf";
    31 my $dir = "blog";
    32 my $dir = "blog";
    32 my $outdir = "out/";
    33 my $outdir = "out/";
    33 my $emacs = "emacs";
    34 my $emacs = "emacs";
    34 my $css = "style.css";
    35 my $art_css = "astyle.css";
    35 my $map_css ="other.css";
    36 my $cat_css ="cstyle.css";
    36 my $no_name_dir = "c";
    37 my $no_name_dir = "c";
    37 my $cache_file = "genorg-cache/cache.xml";
    38 my $cache_file = "genorg-cache/cache.xml";
    38 my $domain = "p.bauherren.ovh";
    39 my $domain = "p.bauherren.ovh";
    39 my $max_rss = 30;
    40 my $max_rss = 30;
    40 # The first page has one less article.  Think of it as a feature.
    41 # The first page has one less article.  Think of it as a feature.
    41 my $max_cat = 7;
    42 my $max_cat = 7;
       
    43 my $main_title= "";
       
    44 my $blog_title = "Pranshu's blog";
    42 
    45 
    43 my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<);
    46 my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<);
    44 my $template = <<"END";
    47 my $template = <<"END";
    45 #+title: Test document
    48 #+title: Test document
    46 #+subtitle: This is the subtitle
    49 #+subtitle: This is the subtitle
    75 			 :store nil)
    78 			 :store nil)
    76 END
    79 END
    77 
    80 
    78 my $custom_code = "(list 1)";
    81 my $custom_code = "(list 1)";
    79 
    82 
       
    83 my $navbar = <<"END" =~ s/\n\s+//gr;
       
    84 <ul>
       
    85   <li> Home
       
    86   <li> Catagories
       
    87   <li> About
       
    88 </ul>
       
    89 END
       
    90 
    80 my $elisp_code = <<"END" =~ s/\n\s+//gr;
    91 my $elisp_code = <<"END" =~ s/\n\s+//gr;
    81 (let ((ls '(LIST)))
    92 (let ((ls '(LIST)))
    82   (require 'org)
    93   (require 'org)
    83   (mapc
    94   (mapc
    84    (lambda (b)
    95    (lambda (b)
    98 <html lang="en">
   109 <html lang="en">
    99   <head>
   110   <head>
   100     <meta charset="UTF-8">
   111     <meta charset="UTF-8">
   101     <meta name="viewport" content="width=device-width, initial-scale=1.0">
   112     <meta name="viewport" content="width=device-width, initial-scale=1.0">
   102     <title>TITLE</title>
   113     <title>TITLE</title>
   103     <link rel="stylesheet" href="style.css">
   114     <link rel="stylesheet" href="CSS">
   104   </head>
   115   </head>
       
   116   <body>
       
   117     NAV
       
   118     <h1> BTIT </h1>
   105 SPLIT
   119 SPLIT
   106   <body>
       
   107     <h1> Pranshu's Blog </h1>
       
   108     <ul id="cataogries">
   120     <ul id="cataogries">
   109 SPLIT 
   121 SPLIT 
   110       <li> <a href="LINK">  CATAG (NUMBER)</a>
   122       <li> <a href="LINK">  CATAG (NUMBER)</a>
   111 SPLIT
   123 SPLIT
   112     </ul>
   124     </ul>
   113     <div>
   125     <div>
   114       <h2> All articles </h2> <hr>
   126       <h2> All articles <a id="rss" href="rss.xml">(rss)</span> </h2> <hr>
   115 SPLIT
   127 SPLIT
   116    <a href="LINK">
   128    <a href="LINK">
   117      <strong> TITLE </strong> <span>DATE</span>
   129      <strong> TITLE </strong> <span>DATE</span> <i>CATAG</i>
   118      <p>DESCREPTION</p>
       
   119      <i>CATAG</i>
       
   120      <hr>
   130      <hr>
   121   </a>
   131   </a>
   122 SPLIT
   132 SPLIT
   123    <a href="LINK">
   133    <a href="LINK">
   124      <strong> TITLE </strong> <span>DATE</span>
   134      <strong> TITLE </strong> <span>DATE</span>
   125      <p>DESCREPTION</p>
       
   126      <hr>
   135      <hr>
   127    </a>
   136    </a>
   128 SPLIT
   137 SPLIT
   129     </div>
   138     </div>
   130   </body>
   139   </body>
   163 END
   172 END
   164   
   173   
   165 my %config_vars =
   174 my %config_vars =
   166   (
   175   (
   167    'emacs-path' => \$emacs,
   176    'emacs-path' => \$emacs,
   168    css => \$css,
   177    css => \$art_css,
   169    indir => \$dir,
   178    indir => \$dir,
   170    outdir => \$outdir,
   179    outdir => \$outdir,
   171    no_name_dir => \$no_name_dir
   180    no_name_dir => \$no_name_dir
   172   );
   181   );
   173 
   182 
   277 my %catags;
   286 my %catags;
   278 my @arts;
   287 my @arts;
   279 my $cache_dom;
   288 my $cache_dom;
   280 
   289 
   281 sub main {
   290 sub main {
   282   -d $cdir or help_and_bye;
   291     $outdir =~ s!/*$!/!;
   283   mkdir $outdir;
   292     -d $cdir or help_and_bye;
   284   chdir $cdir or die "Couldn't access $cdir\n";
   293     mkdir $outdir;
   285   read_config $config_file;
   294     chdir $cdir or die "Couldn't access $cdir\n";
   286   -d $dir or die "$dir doesn't exist \n";
   295     read_config $config_file;
   287   find (\&wanted, $dir);
   296     -d $dir or die "$dir doesn't exist \n";
   288   for (@files_to_move) {
   297     find (\&wanted, $dir);
   289     copy $_, s#$dir/?#$outdir#er;
   298     for (@files_to_move) {
   290   }
   299 	copy $_, s#$dir/?#$outdir#er;
   291   mkdir for @files_to_make;
   300     }
   292   values %{conv_files()};
   301     mkdir for @files_to_make;
   293   $cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file;
   302     values %{conv_files()};
   294   chdir $outdir;
   303     $cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file;
   295   @arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps;
   304     chdir $outdir;
   296   finalise_html();
   305     @arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps;
   297   chdir '..';
   306     finalise_html();
   298   populate_cache();
   307     chdir '..';
       
   308     populate_cache();
   299 }
   309 }
   300 
   310 
   301 sub populate_cache {
   311 sub populate_cache {
   302   my $dom = XML::LibXML::Document->new('1.0', 'UTF-8');
   312   my $dom = XML::LibXML::Document->new('1.0', 'UTF-8');
   303   my $root = $dom->createElement('root');
   313   my $root = $dom->createElement('root');
   304   my %c_els;
   314   my %c_els;
   305   for my $art (@arts) {
   315   for my $art (@arts) {
   306     # We need the: related, file, date
   316     # We need the: related, file, date
   307     my ($art_el, $date, $title, $ca, $related) =
   317     my ($art_el, $date, $title, $ca, $related, $utitle) =
   308       map {$dom->createElement($_)} ("art", "date", "title", "catag", "related");
   318       map {$dom->createElement($_)} ("art", "date", "title", "catag", "related", "utitle");
   309     $art_el->{path} = $art->{file};
   319     $art_el->{path} = $art->{file};
   310     $ca->appendText($art->{catag});
   320     $ca->appendText($art->{catag});
   311     $title->appendText($art->{title});
   321     $title->appendText($art->{title});
   312     $date->appendText($art->{date}->text_easy);
   322     $date->appendText($art->{date}->text_easy);
       
   323     $utitle->appendText($art->{utitle});
   313     my @ll = @{$art->{links}};
   324     my @ll = @{$art->{links}};
   314     if (@ll) {
   325     if (@ll) {
   315       my $links = $dom->createElement('links');
   326       my $links = $dom->createElement('links');
   316       for (@ll) {
   327       for (@ll) {
   317 	my $link = $dom->createElement("link");
   328 	my $link = $dom->createElement("link");
   327 	my ($catag, $artt) = @{$_};
   338 	my ($catag, $artt) = @{$_};
   328 	my $rel_str .= "\"$catag\"" . ($artt && "->\"$artt\"");
   339 	my $rel_str .= "\"$catag\"" . ($artt && "->\"$artt\"");
   329 	$related->appendText("\"$catag\"" . ($artt && "->\"$artt\" "));
   340 	$related->appendText("\"$catag\"" . ($artt && "->\"$artt\" "));
   330       }
   341       }
   331     }
   342     }
   332     $art_el->appendChild($_) for ($date, $ca, $related, $title);
   343     $art_el->appendChild($_) for ($date, $ca, $related, $title, $utitle);
   333     $root->appendChild($art_el);
   344     $root->appendChild($art_el);
   334   }
   345   }
   335   $root->appendChild($_) for values %c_els;
   346   $root->appendChild($_) for values %c_els;
   336   $dom->setDocumentElement($root);
   347   $dom->setDocumentElement($root);
   337   open my $fh, '>' , $cache_file;
   348   open my $fh, '>' , $cache_file;
   343   my ($data, $catagory) = @_;
   354   my ($data, $catagory) = @_;
   344   my $str = defined $catagory
   355   my $str = defined $catagory
   345     ? ($h_en_cat =~ s/CATAG/$data->{catag}/er) : $h_en;
   356     ? ($h_en_cat =~ s/CATAG/$data->{catag}/er) : $h_en;
   346   my $prel_path = "/$no_name_dir/" . (defined $catagory ? "c" : "c/b");
   357   my $prel_path = "/$no_name_dir/" . (defined $catagory ? "c" : "c/b");
   347   $str =~ s/DATE/$data->{date}->fmt/er
   358   $str =~ s/DATE/$data->{date}->fmt/er
   348     =~ s/TITLE/$data->{title}/er
   359     =~ s/TITLE/$data->{utitle}/er
   349     =~ s|LINK|prel_path($prel_path, '/'. $data->{file})|er
   360     =~ s|LINK|prel_path($prel_path, '/'. $data->{file})|er
   350   }
   361   }
   351 
   362 
   352 sub rss_en ($data) {
   363 sub rss_en ($data) {
   353   my $thing = <<"END" =~ s/\n\s+//gr;
   364   my $thing = <<"END" =~ s/\n\s+//gr;
   355     <title>TITLE</title>
   366     <title>TITLE</title>
   356     <link>LINK</link>
   367     <link>LINK</link>
   357     <description>DESC</description>
   368     <description>DESC</description>
   358   </item>
   369   </item>
   359 END
   370 END
   360   $thing =~ s|LINK|"$domain/" . $data->{file}|er
   371   $thing 
   361     =~ s/TITLE/$data->{title}/er;
   372     =~ s|LINK|"$domain/" . $data->{file}|er
       
   373       =~ s/TITLE/$data->{utitle}/er;
   362 }
   374 }
   363 
   375 
   364 sub min ($x, $y) {
   376 sub min ($x, $y) {
   365   ($x, $y)[$x > $y]
   377   ($x, $y)[$x > $y]
   366 }
   378 }
   368 # @_ has to be in format of [STRING, LINK]
   380 # @_ has to be in format of [STRING, LINK]
   369 sub list2paginaiton {
   381 sub list2paginaiton {
   370   my $page = shift;
   382   my $page = shift;
   371   '<ul>' . join ("", map {
   383   '<ul>' . join ("", map {
   372     my ($str, $file) = @{$_};
   384     my ($str, $file) = @{$_};
   373     $str = "---$str" if $file && ($file == $page);
   385     $str = "<span class=\"psel\">$str</span>" if $file && ($file == $page);
   374     defined $file ?
   386     defined $file ?
   375       "<li><a href=\"$file.html\">$str</a>" : $str
   387       "<li><a href=\"$file.html\">$str</a>" : $str
   376     } ((["←", $page - 1]) x!! ($page - 1),
   388     } ((["←", $page - 1]) x!! ($page - 1),
   377        @_,
   389        @_,
   378        (["→", $page + 1]) x ($page != $_[$#_]->[1]))) . '</ul>'
   390        (["→", $page + 1]) x ($page != $_[$#_]->[1]))) . '</ul>'
   414   for (@arts) {
   426   for (@arts) {
   415     return $_ if $_->{catag} eq $cat && $_->{title} eq $id
   427     return $_ if $_->{catag} eq $cat && $_->{title} eq $id
   416   }
   428   }
   417 }
   429 }
   418 
   430 
       
   431 sub cssbs($s,$t,$p){
       
   432   $s =~ s|CSS|prel_path("/$p","/$cat_css")|er
       
   433     =~ s/TITLE|BTIT/$t/ger
       
   434     =~ s/NAV/$navbar/er;
       
   435 }
       
   436 
   419 sub finalise_html {
   437 sub finalise_html {
   420   mkdir $no_name_dir;
   438   mkdir $no_name_dir;
   421   open my $fh, '>', "$no_name_dir/1.html";
   439   open my $fh, '>', "$no_name_dir/1.html";
   422   open my $mrss, '>', "$no_name_dir/rss.xml"; # The master rss file
   440   open my $mrss, '>', "$no_name_dir/rss.xml"; # The master rss file
   423   print $mrss $rss_entry;
   441   print $mrss $rss_entry;
   424   print $fh $h_intro . $h_c_intro;
   442   print $fh cssbs($h_intro . $h_c_intro, $blog_title, "a/c");
   425   my (%c_files, %r_files);	# Cataogry files and rss
   443   my (%c_files, %r_files);	# Cataogry files and rss
   426   while (my ($cat, $num) = each %catags) {
   444   
       
   445   for my $cat (sort { $catags{$b} <=> $catags{$a} } keys(%catags)) {
       
   446     my $num = $catags{$cat};
   427     # ($h_intro, $h_c_intro, $h_cat_li, $h_chap, $h_en_cat, $h_en, $h_end)
   447     # ($h_intro, $h_c_intro, $h_cat_li, $h_chap, $h_en_cat, $h_en, $h_end)
   428     print $fh $h_cat_li =~ s/NUMBER/$num/re
   448     print $fh $h_cat_li =~ s/NUMBER/$num/re
   429       =~ s/CATAG/$cat/er
   449       =~ s/CATAG/$cat/er
   430       =~ s=LINK="$cat/1.html"=er;
   450       =~ s=LINK="$cat/1.html"=er;
   431     mkdir "$no_name_dir/$cat";
   451     mkdir "$no_name_dir/$cat";
   432     open $c_files{$cat}, '>', "$no_name_dir/$cat/1.html";
   452     open $c_files{$cat}, '>', "$no_name_dir/$cat/1.html";
   433     open $r_files{$cat}, '>', "$no_name_dir/$cat/rss.xml";
   453     open $r_files{$cat}, '>', "$no_name_dir/$cat/rss.xml";
   434     print {$r_files{$cat}} $rss_entry;
   454     print {$r_files{$cat}} $rss_entry;
   435     print {$c_files{$cat}} $h_intro;
   455     print {$c_files{$cat}}
       
   456       cssbs($h_intro, "$cat  <a id=\"rss\" href=\"rss.xml\">(rss)</span> ", "a/b/c");
   436   }
   457   }
   437   print $fh $h_chap;
   458   print $fh $h_chap;
   438   # :-)
   459   # :-)
   439   my $total_articles = () =  map {(1)x$_} values %catags;
   460   my $total_articles = () =  map {(1)x$_} values %catags;
   440   # Now we iterate through the articles and add their index
   461   # Now we iterate through the articles and add their index
   441   my ($n_fh, $n_mrss, %n_rss) = (0,0);
   462   my ($n_fh, $n_mrss, %n_rss) = (0,0);
   442   my %n_cat;
   463   my %n_cat;
   443   for my $art (@arts) {
   464   for my $art (@arts) {
       
   465     # TODO do we need $title?
   444     my ($catag, $title) = @{$art}{qw(catag title)};
   466     my ($catag, $title) = @{$art}{qw(catag title)};
   445     my $rentry = rss_en $art;
   467     my $rentry = rss_en $art;
   446     if (($n_rss{$catag} // 0) < $max_rss) {
   468     if (($n_rss{$catag} // 0) < $max_rss) {
   447       print {$r_files{$catag}} $rentry;
   469       print {$r_files{$catag}} $rentry;
   448       ++$n_rss{$catag};
   470       ++$n_rss{$catag};
   459     ++$$ncat;
   481     ++$$ncat;
   460     my $cfh = \$c_files{$catag};
   482     my $cfh = \$c_files{$catag};
   461     $$cfh = move_on_if_neccasary $$cfh, $$ncat, $catags{$catag}, "$no_name_dir/$catag";
   483     $$cfh = move_on_if_neccasary $$cfh, $$ncat, $catags{$catag}, "$no_name_dir/$catag";
   462     print {$$cfh} entry $art;
   484     print {$$cfh} entry $art;
   463     my $a_file = $art->{file};
   485     my $a_file = $art->{file};
   464     # say $a_file;
       
   465     if (1 or grep /^$a_file$/, @modified_files) {
   486     if (1 or grep /^$a_file$/, @modified_files) {
   466       my $doom = linkify($art);
   487       my $doom = linkify($art);
   467       open my $h_file, '>', $a_file;
   488       open my $h_file, '>', $a_file;
   468       print $h_file $doom->toStringHTML;
   489       print $h_file $doom->toStringHTML;
   469       close $h_file;
   490       close $h_file;
   500       $_->appendText($link_text);
   521       $_->appendText($link_text);
   501     }
   522     }
   502     push @links, $key;
   523     push @links, $key;
   503     my $l = get_art(@{$key})->{file} or die "Link not found in $art->{filename}\n";
   524     my $l = get_art(@{$key})->{file} or die "Link not found in $art->{filename}\n";
   504     $_->{href} = prel_path("/".$art->{file}, "/$l");
   525     $_->{href} = prel_path("/".$art->{file}, "/$l");
   505     print $_;
       
   506   }
   526   }
   507   $art->{links} = \@links;
   527   $art->{links} = \@links;
   508   return $bom
   528   return $bom
   509 }
   529 }
   510 
   530 
   514 sub getcache ($fn) {
   534 sub getcache ($fn) {
   515   $cache_dom || return 0;
   535   $cache_dom || return 0;
   516   for my $d ($cache_dom->findnodes("/root/art[\@path=\"$fn\"]")) {
   536   for my $d ($cache_dom->findnodes("/root/art[\@path=\"$fn\"]")) {
   517     # TODO related
   537     # TODO related
   518     my %dome = ('file' => $fn);
   538     my %dome = ('file' => $fn);
   519     (@dome{'title', 'catag'}, my $date) =
   539     (@dome{'title', 'catag', 'utitle'}, my $date) =
   520       map
   540       map
   521       { $d->getChildrenByTagName($_)->[0]->to_literal }
   541       { $d->getChildrenByTagName($_)->[0]->to_literal }
   522       qw[title catag date];
   542       qw[title catag  utitle date];
   523     for ($d->getChildrenByTagName("links")) {
   543     for ($d->getChildrenByTagName("links")) {
   524       my @ll = map [$_->{catag}, $_->to_literal],
   544       my @ll = map [$_->{catag}, $_->to_literal],
   525 	($_->getChildrenByTagName('link'));
   545 	($_->getChildrenByTagName('link'));
   526       $dome{links} = \@ll;
   546       $dome{links} = \@ll;
   527     }
   547     }
   531   }
   551   }
   532   return 0;
   552   return 0;
   533 }
   553 }
   534 
   554 
   535 sub html_fixup ($filename) {
   555 sub html_fixup ($filename) {
   536   # for (getcache($filename)) {
   556   if ($cache && !grep /^$filename$/, @modified_files) {
   537   # $_ && return $_;
       
   538   # }
       
   539   unless (!$cache && grep /^$filename$/, @modified_files) {
       
   540     for (getcache($filename)) {
   557     for (getcache($filename)) {
   541       return $_ if $_
   558       return $_ if $_
   542     }
   559     }
   543   }
   560   }
   544   my $dom =
   561   my $dom =
   545     XML::LibXML->load_html(location  => $filename, recover   => 1);
   562     XML::LibXML->load_html(location  => $filename);
   546   my %data = ('file', $filename);
   563   my %data = ('file', $filename);
   547 
   564 
   548   # We don't need style or table of contents heading (if exists)
   565   # We don't need style or table of contents heading (if exists)
   549   for my $xpath ('/html/head/style', '/html/body/div/div[@id="table-of-contents"]/h2') {
   566   for my $xpath ('/html/head/style', '/html/body/div/div[@id="table-of-contents"]/h2') {
   550     $_->parentNode->removeChild($_) for $dom->findnodes($xpath) 
   567     $_->parentNode->removeChild($_) for $dom->findnodes($xpath) 
   560       $data{date} = $date;
   577       $data{date} = $date;
   561       my $p = $_->parentNode;
   578       my $p = $_->parentNode;
   562       $p->parentNode->removeChild($p);
   579       $p->parentNode->removeChild($p);
   563     }
   580     }
   564   }
   581   }
   565   # Title\
   582   # Title
       
   583   say "HELLO";
   566   for ($dom->findnodes('/html/head/title')) {
   584   for ($dom->findnodes('/html/head/title')) {
   567     $data{title}  = $_->to_literal;
   585     say "HERE";
       
   586     $data{utitle}  = $_->to_literal;
   568   }
   587   }
   569   # keywords/catogry
   588   # keywords/catogry
   570   for my $node ($dom->findnodes('/html/head/meta')) {
   589   for my $node ($dom->findnodes('/html/head/meta')) {
   571     (my $cont = $node->getAttribute('content')) || next;
   590     (my $cont = $node->getAttribute('content')) || next;
   572     for ($node->getAttribute('name')) {
   591     for ($node->getAttribute('name')) {