genorg.pl
changeset 9 58b72fea234c
parent 8 ee9b57cbbefc
child 10 a81440e9cf9b
equal deleted inserted replaced
8:ee9b57cbbefc 9:58b72fea234c
    37 my $no_name_dir = "c";
    37 my $no_name_dir = "c";
    38 my $cache_file = "genorg-cache/cache.xml";
    38 my $cache_file = "genorg-cache/cache.xml";
    39 my $domain = "p.bauherren.ovh";
    39 my $domain = "p.bauherren.ovh";
    40 my $max_rss = 30;
    40 my $max_rss = 30;
    41 # 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.
    42 my $max_cat = 7;
    42 my $max_cat = 10;
    43 my $main_title= "";
       
    44 my $blog_title = "Pranshu's blog";
    43 my $blog_title = "Pranshu's blog";
       
    44 my ($home, $about) = ("home.html", "about.html");
       
    45 my $etc_files;
       
    46 my $nav_id = "nav";
    45 
    47 
    46 my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<);
    48 my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<);
    47 my $template = <<"END";
    49 my $template = <<"END";
    48 #+title: Test document
    50 #+title: Test document
    49 #+subtitle: This is the subtitle
    51 #+subtitle: This is the subtitle
    80 
    82 
    81 my $custom_code = "(list 1)";
    83 my $custom_code = "(list 1)";
    82 
    84 
    83 my $navbar = <<"END" =~ s/\n\s+//gr;
    85 my $navbar = <<"END" =~ s/\n\s+//gr;
    84 <ul>
    86 <ul>
    85   <li> Home
    87   <li> <a href="LINK">Home</a> </li>
    86   <li> Catagories
    88   <li> <a href="LINK">Catagories</a> </li>
    87   <li> About
    89   <li> <a href="LINK">About</a>  </li>
    88 </ul>
    90 </ul>
    89 END
    91 END
    90 
    92 
    91 my $elisp_code = <<"END" =~ s/\n\s+//gr;
    93 my $elisp_code = <<"END" =~ s/\n\s+//gr;
    92 (let ((ls '(LIST)))
    94 (let ((ls '(LIST)))
    96      (with-current-buffer b
    98      (with-current-buffer b
    97        (when (and (not (string-match "^ " (buffer-name b)))
    99        (when (and (not (string-match "^ " (buffer-name b)))
    98 		  (eq major-mode 'org-mode))
   100 		  (eq major-mode 'org-mode))
    99 	 (org-export-to-file 'html (pop ls)))))
   101 	 (org-export-to-file 'html (pop ls)))))
   100    (buffer-list))
   102    (buffer-list))
   101    (kill-emacs)
   103    (kill-emacs))
   102 )
       
   103 END
   104 END
   104 
   105 
   105 
   106 
   106 my ($h_intro, $h_c_intro, $h_cat_li, $h_chap, $h_en_cat, $h_en, $h_end)
   107 my ($h_intro, $h_c_intro, $h_cat_li, $h_chap, $h_en_cat, $h_en, $h_end)
   107   = split /SPLIT/ ,<<"END" =~ s/\n\s+//gr;
   108   = split /SPLIT/ ,<<"END" =~ s/\n\s+//gr;
   175   (
   176   (
   176    'emacs-path' => \$emacs,
   177    'emacs-path' => \$emacs,
   177    css => \$art_css,
   178    css => \$art_css,
   178    indir => \$dir,
   179    indir => \$dir,
   179    outdir => \$outdir,
   180    outdir => \$outdir,
   180    no_name_dir => \$no_name_dir
   181    no_name_dir => \$no_name_dir,
       
   182    etc_files => \$etc_files
   181   );
   183   );
   182 
   184 
   183 
   185 
   184 sub read_config ($path) {
   186 sub read_config ($path) {
   185   open my $fh, '<', $path or die "Couldn't open config file\n";
   187   open my $fh, '<', $path or die "Couldn't open config file\n";
   214   close $fh;
   216   close $fh;
   215 }
   217 }
   216 
   218 
   217 # Reletive file from the perspect of file1, to file2
   219 # Reletive file from the perspect of file1, to file2
   218 sub prel_path ($from, $to) {
   220 sub prel_path ($from, $to) {
   219   return '.' if $from eq $to;
   221   return $to =~ s/^.//r if $from eq $to;
   220   my \(@f1, @f2) = map [m{/[^/]+}g], ($from, $to);
   222   my \(@f1, @f2) = map [m{/[^/]+}g], ($from, $to);
   221   # return substr($f1[$#f1], 1) if $from =~ $to;
   223   # return substr($f1[$#f1], 1) if $from =~ $to;
   222   while (@f1 && @f2 && $f1[0] =~ $f2[0]) {
   224   while (@f1 && @f2 && $f1[0] =~ $f2[0]) {
   223     shift @f1;
   225     shift @f1;
   224     shift @f2;
   226     shift @f2;
   229 }
   231 }
   230 
   232 
   231 my @org_exps;
   233 my @org_exps;
   232 my @files_to_make;
   234 my @files_to_make;
   233 my @files_to_move;
   235 my @files_to_move;
       
   236 my @navify_files;
   234 
   237 
   235 sub wanted {
   238 sub wanted {
   236   my $n_path = $outdir . prel_path ($dir,  "$File::Find::name/");
   239   my $n_path = $outdir . prel_path ($dir,  "$File::Find::name/");
   237   # We need to mirror the direcotry structure
   240   # We need to mirror the direcotry structure
   238   if (-d) {
   241   if (-d) {
   243   if (/\.org$/) {
   246   if (/\.org$/) {
   244     push @org_exps, substr $n_path, length($outdir);
   247     push @org_exps, substr $n_path, length($outdir);
   245   } else {
   248   } else {
   246     push @files_to_move, $File::Find::name
   249     push @files_to_move, $File::Find::name
   247   }
   250   }
       
   251   
   248 }
   252 }
   249 
   253 
   250 my @modified_files;
   254 my @modified_files;
   251 
   255 
   252 # Export @org_exps
   256 # Export @org_exps
   286 my %catags;
   290 my %catags;
   287 my @arts;
   291 my @arts;
   288 my $cache_dom;
   292 my $cache_dom;
   289 
   293 
   290 sub main {
   294 sub main {
   291     $outdir =~ s!/*$!/!;
   295     s!/*$!/! for ($outdir, $dir);
   292     -d $cdir or help_and_bye;
   296     -d $cdir or help_and_bye;
   293     mkdir $outdir;
   297     mkdir $outdir;
   294     chdir $cdir or die "Couldn't access $cdir\n";
   298     chdir $cdir or die "Couldn't access $cdir\n";
   295     read_config $config_file;
   299     read_config $config_file;
       
   300     my @navify_files = map {"$dir$_"} ($about, $home, $etc_files ? split(/[ \t]+/, $etc_files) : ());
   296     -d $dir or die "$dir doesn't exist \n";
   301     -d $dir or die "$dir doesn't exist \n";
   297     find (\&wanted, $dir);
   302     find (\&wanted, $dir);
   298     for (@files_to_move) {
   303     for my $f (@files_to_move) {
   299 	copy $_, s#$dir/?#$outdir#er;
   304       my $dest = $f =~ s#$dir/?#$outdir#er;
       
   305       if (grep(m#^$f$#, @navify_files)) {
       
   306 	# YAY we have to sub navbar and copy whoop whoop
       
   307 	open my $file_tc, '<', $f or die "file no open, grrr!";
       
   308 	open my $file_out, '>', $dest;
       
   309 	say $dest;
       
   310 	local $/;
       
   311 	print $file_out ((<$file_tc>) =~ s/NAV/nav_up("$dest" =~ s%.+?\/%%r,1)/er);
       
   312 	map \&close, ($file_tc, $file_out);
       
   313       } else {
       
   314 	copy $f, $dest;
       
   315       }
   300     }
   316     }
   301     mkdir for @files_to_make;
   317     mkdir for @files_to_make;
   302     values %{conv_files()};
   318     values %{conv_files()};
   303     $cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file;
   319     $cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file;
   304     chdir $outdir;
   320     chdir $outdir;
   388     } ((["←", $page - 1]) x!! ($page - 1),
   404     } ((["←", $page - 1]) x!! ($page - 1),
   389        @_,
   405        @_,
   390        (["→", $page + 1]) x ($page != $_[$#_]->[1]))) . '</ul>'
   406        (["→", $page + 1]) x ($page != $_[$#_]->[1]))) . '</ul>'
   391      }
   407      }
   392 
   408 
   393 sub move_on_if_neccasary ($fh, $art_num, $total_articles, $path) {
   409 sub move_on_if_neccasary ($fh, $art_num, $total_articles, $path, $title, $rp) {
   394   return $fh if $art_num == 0 || $art_num % $max_cat;
   410   return $fh if $art_num == 0 || $art_num % $max_cat;
   395   my $cpage = $art_num / $max_cat;
   411   my $cpage = $art_num / $max_cat;
   396   print $fh panigation($cpage, ceil($total_articles / $max_cat), $path);
   412   print $fh panigation($cpage, ceil($total_articles / $max_cat), $path);
   397   open $fh, '>', $path . '/' . ($cpage + 1).'.html';
   413   open $fh, '>', $path . '/' . ($cpage + 1).'.html';
       
   414   # ($h_intro . $h_c_intro, $blog_title, "a/c");
       
   415   print $fh cssbs($h_intro, $title, $rp);
   398   $fh;
   416   $fh;
   399 }
   417 }
   400 
   418 
   401 sub panigation ($page, $no_pages, $dir) {
   419 sub panigation ($page, $no_pages, $dir) {
   402   if ($no_pages < 9) {
   420   if ($no_pages < 9) {
   426   for (@arts) {
   444   for (@arts) {
   427     return $_ if $_->{catag} eq $cat && $_->{title} eq $id
   445     return $_ if $_->{catag} eq $cat && $_->{title} eq $id
   428   }
   446   }
   429 }
   447 }
   430 
   448 
       
   449 sub nav_up {
       
   450   my ($path) = @_;
       
   451   my @links = ($home, 'c/1.html', $about);
       
   452   my $nav = shift ? $navbar : "<div class\"$nav_id\">$navbar</div>";
       
   453   $nav =~ s#LINK#prel_path("/$path", "/". shift @links)#ger;
       
   454 }
       
   455 
   431 sub cssbs($s,$t,$p){
   456 sub cssbs($s,$t,$p){
   432   $s =~ s|CSS|prel_path("/$p","/$cat_css")|er
   457   $s =~ s|CSS|prel_path("/$p","/$cat_css")|er
   433     =~ s/TITLE|BTIT/$t/ger
   458     =~ s/TITLE|BTIT/$t/ger
   434     =~ s/NAV/$navbar/er;
   459     =~ s/NAV/nav_up($p)/er;
       
   460 }
       
   461 
       
   462 sub modify_art ($file, $dom) {
       
   463   # I guess we can rely on navbar not changed, if it did then user
       
   464   # would need to call with -d option
       
   465   return $dom unless grep /^$file$/, @modified_files;
       
   466   # We add css, navbar
       
   467   for ($dom->findnodes('/html/body')) {
       
   468     # my @cds = $_->childNodes;
       
   469     my $nav_el = $dom->createElement('div');
       
   470     $nav_el->{id} = $nav_id;
       
   471     $nav_el->appendWellBalancedChunk(nav_up($file));
       
   472     $_->insertBefore($nav_el, $_->childNodes->[0])
       
   473   }
       
   474   for ($dom->findnodes('/html/head')) {
       
   475     my $link = $dom->createElement('link');
       
   476     $link->{rel} = "stylesheet";
       
   477     $link->{href} = prel_path("/$file", "/$art_css");
       
   478     $_->appendChild($link);
       
   479   }
       
   480   $dom;
   435 }
   481 }
   436 
   482 
   437 sub finalise_html {
   483 sub finalise_html {
   438   mkdir $no_name_dir;
   484   mkdir $no_name_dir;
   439   open my $fh, '>', "$no_name_dir/1.html";
   485   open my $fh, '>', "$no_name_dir/1.html";
   472     if ($n_mrss < $max_rss) {
   518     if ($n_mrss < $max_rss) {
   473       print $mrss $rentry;
   519       print $mrss $rentry;
   474       ++$n_mrss
   520       ++$n_mrss
   475     }
   521     }
   476     # ($fh, $art_num, $total_articles, $path)
   522     # ($fh, $art_num, $total_articles, $path)
   477     $fh = move_on_if_neccasary $fh, $n_fh, $total_articles, $no_name_dir;
   523     $fh = move_on_if_neccasary
       
   524       $fh, $n_fh, $total_articles, $no_name_dir, "All articles", "a/c";
   478     $n_fh++;
   525     $n_fh++;
   479     print $fh entry $art, 1;
   526     print $fh entry $art, 1;
   480     my $ncat = \$n_cat{$catag};
   527     my $ncat = \$n_cat{$catag};
   481     ++$$ncat;
   528     ++$$ncat;
   482     my $cfh = \$c_files{$catag};
   529     my $cfh = \$c_files{$catag};
   483     $$cfh = move_on_if_neccasary $$cfh, $$ncat, $catags{$catag}, "$no_name_dir/$catag";
   530     $$cfh = move_on_if_neccasary
       
   531       $$cfh, $$ncat, $catags{$catag}, "$no_name_dir/$catag", $catag, "a/c/b";
   484     print {$$cfh} entry $art;
   532     print {$$cfh} entry $art;
   485     my $a_file = $art->{file};
   533     my $a_file = $art->{file};
   486     if (1 or grep /^$a_file$/, @modified_files) {
   534     if (1 or grep /^$a_file$/, @modified_files) {
   487       my $doom = linkify($art);
   535       my $doom = modify_art($a_file, linkify($art));
   488       open my $h_file, '>', $a_file;
   536       open my $h_file, '>', $a_file;
   489       print $h_file $doom->toStringHTML;
   537       print $h_file $doom->toStringHTML;
   490       close $h_file;
   538       close $h_file;
   491     }
   539     }
   492   }
   540   }
   578       my $p = $_->parentNode;
   626       my $p = $_->parentNode;
   579       $p->parentNode->removeChild($p);
   627       $p->parentNode->removeChild($p);
   580     }
   628     }
   581   }
   629   }
   582   # Title
   630   # Title
   583   say "HELLO";
       
   584   for ($dom->findnodes('/html/head/title')) {
   631   for ($dom->findnodes('/html/head/title')) {
   585     say "HERE";
       
   586     $data{utitle}  = $_->to_literal;
   632     $data{utitle}  = $_->to_literal;
   587   }
   633   }
   588   # keywords/catogry
   634   # keywords/catogry
   589   for my $node ($dom->findnodes('/html/head/meta')) {
   635   for my $node ($dom->findnodes('/html/head/meta')) {
   590     (my $cont = $node->getAttribute('content')) || next;
   636     (my $cont = $node->getAttribute('content')) || next;