genorg.pl
changeset 14 31e45dd2a894
parent 13 da3b9ba1a7f0
child 15 c8e6e4e514f3
equal deleted inserted replaced
13:da3b9ba1a7f0 14:31e45dd2a894
    35 my $no_name_dir = "c";
    35 my $no_name_dir = "c";
    36 my $cache_file = "genorg-cache/cache.xml";
    36 my $cache_file = "genorg-cache/cache.xml";
    37 my $domain = "p.bauherren.ovh";
    37 my $domain = "p.bauherren.ovh";
    38 my $max_rss = 30;
    38 my $max_rss = 30;
    39 # The first page has one less article.  Think of it as a feature.
    39 # The first page has one less article.  Think of it as a feature.
    40 my $max_cat = 10;
    40 my $max_cat = 30;
    41 my $blog_title = "Pranshu's blog";
    41 my $blog_title = "Pranshu's blog";
    42 my ($home, $about) = ("home.html", "about.html");
    42 my ($home, $about) = ("home.html", "about.html");
    43 my $etc_files;
    43 my $etc_files;
    44 my $nav_id = "nav";
    44 my $nav_id = "nav";
       
    45 my $rdesc = "description";
    45 
    46 
    46 my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<);
    47 my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<);
    47 my $template = <<"END";
    48 my $template = <<"END";
    48 #+title: Test document
    49 #+title: Test document
    49 #+subtitle: This is the subtitle
    50 #+subtitle: This is the subtitle
       
    51 #+date: IMPORANT
    50 #+author: $username
    52 #+author: $username
    51 #+keywords: tag thing | related
    53 #+keywords: tag->thing | rel->a
    52 #+options: html-link-use-abs-url:nil html-postamble:auto
    54 #+options: html-link-use-abs-url:nil html-postamble:auto
    53 #+options: html-preamble:t html-scripts:nil html-style:t
    55 #+options: html-preamble:t html-scripts:nil html-style:t
    54 #+options: html5-fancy:nil tex:t
    56 #+options: html5-fancy:nil tex:t
    55 #+options: tex:mathjax
       
    56 #+html_doctype: html5
    57 #+html_doctype: html5
    57 #+html_container: div
       
    58 #+html_content_class: content
       
    59 #+html_link_home:
       
    60 #+html_link_up:
       
    61 #+html_mathjax:
       
    62 #+html_equation_reference_format: \eqref{%s}
       
    63 #+html_head:
       
    64 #+html_head_extra:
       
    65 #+infojs_opt:
       
    66 #+creator: <a href="https://www.gnu.org/software/emacs/">Emacs</a> 31.0.50 (<a href="https://orgmode.org">Org</a> mode 9.7.11)
       
    67 #+latex_header:
       
    68 END
    58 END
    69 
    59 
    70 my $req_config = <<"END";
    60 my $req_config = <<"END";
    71 (require 'org)
    61 (require 'org)
    72 (require 'ob)
    62 (require 'ob)
   151 
   141 
   152 $config_file follow the syntax of:
   142 $config_file follow the syntax of:
   153 VAR VALUE
   143 VAR VALUE
   154 where VAR can be:
   144 where VAR can be:
   155  emacs-path : The value should be path of emacs. 'emacs' by default.
   145  emacs-path : The value should be path of emacs. 'emacs' by default.
   156  css        : The path of the css file to use.
   146  acss       : The path of the css file to use for articles.
       
   147  ccss       : Path of css to use for cataogry pages.
   157  in_dir     : The directory that contains the files.
   148  in_dir     : The directory that contains the files.
   158  out_dir    : the output directory.
   149  out_dir    : the output directory.
   159  no_name_dir: The directory for generate files.  This means in_dir/no_name_dir
   150  no_name_dir: The directory for generate files.  This means in_dir/no_name_dir
   160               cannot exist.
   151               cannot exist.
   161  etc_files  : Space sperated list of files to be navved up
   152  etc_files  : Space sperated list of files to be navved up
   162  cache      : xml cache file
   153  cache      : xml cache file
   163  home       : home file
   154  home       : home file
   164  about      : about file
   155  about      : about file
   165  domain     : domain (for rss)
   156  domain     : domain (for rss)
   166  title      : title of the blog
   157  title      : title of the blog
       
   158  max_rss    : max_rss entries
       
   159  max_cat    : max catagory entries before next page
   167 
   160 
   168 As soon as config file comes accross '>>---', the rest of the file
   161 As soon as config file comes accross '>>---', the rest of the file
   169 is now the elisp code used in emacs startup.
   162 is now the elisp code used in emacs startup.
   170 END
   163 END
   171 
   164 
   172 
   165 
   173 my $rss_entry = <<"END" =~ s/\n\s+//gr;
   166 my $rss_entry = <<"END" =~ s/\n\s+//gr;
   174 <?xml version=\"1.0\" encoding=\"UTF-8\" ?>
   167 <?xml version=\"1.0\" encoding=\"UTF-8\" ?>
   175 <rss version=\"2.0\">
   168 <rss version=\"2.0\">
   176  <channel>
   169  <channel>
   177   <title>W3Schools Home Page</title>
   170   <title>TITLE</title>
   178   <link>https://www.w3schools.com</link>
   171   <link>BLINK</link>
   179   <description>Free web building tutorials</description>"
   172    <description>RDESC</description>"
   180 END
   173 END
   181   
   174   
   182 my %config_vars =
   175 my %config_vars =
   183   (
   176   (
   184    'emacs-path' => \$emacs,
   177    'emacs-path' => \$emacs,
   185    css => \$art_css,
   178    acss => \$art_css,
       
   179    ccss => \$cat_css,
   186    indir => \$dir,
   180    indir => \$dir,
   187    outdir => \$outdir,
   181    outdir => \$outdir,
   188    no_name_dir => \$no_name_dir,
   182    no_name_dir => \$no_name_dir,
   189    etc_files => \$etc_files,
   183    etc_files => \$etc_files,
   190    cache => \$cache_file,
   184    cache => \$cache_file,
   191    home => \$home,
   185    home => \$home,
   192    about => \$about,
   186    about => \$about,
   193    domain => \$domain,
   187    domain => \$domain,
   194    title => \&blog_title
   188    title => \$blog_title,
       
   189    rdesc => \$rdesc,
       
   190    max_rss => \$max_rss,
       
   191    max_cat => \$max_cat
   195   );
   192   );
   196 
   193 
   197 
   194 
   198 sub read_config ($path) {
   195 sub read_config ($path) {
   199   open my $fh, '<', $path or die "Couldn't open config file\n";
   196   open my $fh, '<', $path or die "Couldn't open config file\n";
   204       $custom_code = <$fh>;
   201       $custom_code = <$fh>;
   205       return;
   202       return;
   206     }
   203     }
   207     chomp;
   204     chomp;
   208     my ($directive, $rest) = split /\s+/, $_, 2;
   205     my ($directive, $rest) = split /\s+/, $_, 2;
   209     if (exists($config_vars{$directive})) {
   206     if (exists $config_vars{$directive}) {
   210       ${$config_vars{$directive}} = $rest;
   207       ${$config_vars{$directive}} = $rest;
   211     } else {
   208     } else {
   212       print "Unknown variable $directive\n";
   209       die "Unknown variable $directive\n";
   213     }
   210     }
   214   }
   211   }
   215   close $fh;
   212   close $fh;
   216 }
   213 }
   217 
   214 
   219   print $help;
   216   print $help;
   220   exit;
   217   exit;
   221 }
   218 }
   222 
   219 
   223 sub template ($file) {
   220 sub template ($file) {
   224   $file // die "-t needs an argument.\n";
   221   $file // do {print $template; exit(1)};
   225   $file =~ s/\.org$//;
   222   $file =~ s/\.org$//;
   226   open my $fh, '>', $file . ".org" or die $file . " couldn't be opened\n";
   223   open my $fh, '>', $file . ".org" or die $file . " couldn't be opened\n";
   227   print $fh $template;
   224   print $fh $template;
   228   close $fh;
   225   close $fh;
   229 }
   226 }
   230 
   227 
   231 # Reletive file from the perspect of file1, to file2
   228 # Reletive file from the perspect of file1, to file2
   232 sub prel_path ($from, $to) {
   229 sub prel_path ($from, $to) {
   233   return $to =~ s/^.//r if $from eq $to;
   230   return $to if $from eq $to;
   234   my \(@f1, @f2) = map [m{/[^/]+}g], ($from, $to);
   231   my \(@f1, @f2) = map [m{/[^/]+}g], ($from, $to);
   235   # return substr($f1[$#f1], 1) if $from =~ $to;
   232   # return substr($f1[$#f1], 1) if $from =~ $to;
   236   while (@f1 && @f2 && $f1[0] =~ $f2[0]) {
   233   while (@f1 && @f2 && $f1[0] =~ $f2[0]) {
   237     shift @f1;
   234     shift @f1;
   238     shift @f2;
   235     shift @f2;
   302 my %catags;
   299 my %catags;
   303 my @arts;
   300 my @arts;
   304 my $cache_dom;
   301 my $cache_dom;
   305 
   302 
   306 sub main {
   303 sub main {
   307     s!/*$!/! for ($outdir, $dir);
   304   -d $cdir or help_and_bye;
   308     -d $cdir or help_and_bye;
   305   mkdir $outdir;
   309     mkdir $outdir;
   306   chdir $cdir or die "Couldn't access $cdir\n";
   310     chdir $cdir or die "Couldn't access $cdir\n";
   307   read_config $config_file;
   311     read_config $config_file;
   308   s!/*$!/! for ($outdir, $dir);
   312     my @navify_files = map {"$dir$_"} ($about, $home, $etc_files ? split(/[ \t]+/, $etc_files) : ());
   309   my @navify_files = map {"$dir$_"} ($about, $home, $etc_files ? split(/[ \t]+/, $etc_files) : ());
   313     -d $dir or die "$dir doesn't exist \n";
   310   -d $dir or die "$dir doesn't exist \n";
   314     find (\&wanted, $dir);
   311   find (\&wanted, $dir);
   315     for my $f (@files_to_move) {
   312   for my $f (@files_to_move) {
   316       my $dest = $f =~ s#$dir/?#$outdir#er;
   313     my $dest = $f =~ s#$dir/?#$outdir#er;
   317       if (grep(m#^$f$#, @navify_files)) {
   314     if (grep(m#^$f$#, @navify_files)) {
   318 	# YAY we have to sub navbar and copy whoop whoop
   315       # YAY we have to sub navbar and copy whoop whoop
   319 	open my $file_tc, '<', $f or die "file no open, grrr!";
   316       open my $file_tc, '<', $f or die "file no open, grrr!";
   320 	open my $file_out, '>', $dest;
   317       open my $file_out, '>', $dest;
   321 	local $/;
   318       local $/;
   322 	print $file_out ((<$file_tc>) =~ s/NAV/nav_up("$dest" =~ s%.+?\/%%r,1)/er);
   319       print $file_out ((<$file_tc>) =~ s/NAV/nav_up("$dest" =~ s%.+?\/%%r,1)/er);
   323 	map \&close, ($file_tc, $file_out);
   320       map \&close, ($file_tc, $file_out);
   324       } else {
   321     } else {
   325 	copy $f, $dest;
   322       copy $f, $dest;
   326       }
   323     }
   327     }
   324   }
   328     mkdir for @files_to_make;
   325   mkdir for @files_to_make;
   329     values %{conv_files()};
   326   values %{conv_files()};
   330     $cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file;
   327   $cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file;
   331     chdir $outdir;
   328   chdir $outdir;
   332     @arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps;
   329   @arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps;
   333     finalise_html();
   330   finalise_html();
   334     chdir '..';
   331   chdir '..';
   335     populate_cache();
   332   populate_cache();
   336 }
   333 }
   337 
   334 
   338 sub populate_cache {
   335 sub populate_cache {
   339   my $dom = XML::LibXML::Document->new('1.0', 'UTF-8');
   336   my $dom = XML::LibXML::Document->new('1.0', 'UTF-8');
   340   my $root = $dom->createElement('root');
   337   my $root = $dom->createElement('root');
   458 }
   455 }
   459 
   456 
   460 sub nav_up {
   457 sub nav_up {
   461   my ($path) = @_;
   458   my ($path) = @_;
   462   my @links = ($home, 'c/1.html', $about);
   459   my @links = ($home, 'c/1.html', $about);
   463   my $nav = shift ? $navbar : "<div class\"$nav_id\">$navbar</div>";
   460   my $nav = shift ? "<div class=\"$nav_id\">$navbar</div>" : $navbar;
   464   $nav =~ s#LINK#prel_path("/$path", "/". shift @links)#ger;
   461   $nav =~ s#LINK#prel_path("/$path", "/". shift @links)#ger;
   465 }
   462 }
   466 
   463 
   467 sub cssbs($s,$t,$p){
   464 sub cssbs($s,$t,$p){
   468   $s =~ s|CSS|prel_path("/$p","/$cat_css")|er
   465   $s =~ s|CSS|prel_path("/$p","/$cat_css")|er
   469     =~ s/TITLE|BTIT/$t/ger
   466     =~ s/TITLE|BTIT/$t/ger
   470     =~ s/NAV/nav_up($p)/er;
   467     =~ s/NAV/nav_up($p,1)/er;
   471 }
   468 }
   472 
   469 
   473 sub modify_art ($file, $dom) {
   470 sub modify_art ($file, $dom) {
   474   # I guess we can rely on navbar not changed, if it did then user
   471   # I guess we can rely on navbar not changed, if it did then user
   475   # would need to call with -d option
   472   # would need to call with -d option
   476   return $dom unless grep /^$file$/, @modified_files;
   473   return $dom unless grep /^$file$/, @modified_files;
   477   # We add css, navbar
   474   # We add css, navbar
   478   for ($dom->findnodes('/html/body')) {
   475   for ($dom->findnodes('/html/body')) {
   479     # my @cds = $_->childNodes;
   476     # my @cds = $_->childNodes;
   480     my $nav_el = $dom->createElement('div');
   477     my $nav_el = $dom->createElement('div');
   481     $nav_el->{id} = $nav_id;
   478     $nav_el->{class} = $nav_id;
   482     $nav_el->appendWellBalancedChunk(nav_up($file));
   479     $nav_el->appendWellBalancedChunk(nav_up($file));
   483     $_->insertBefore($nav_el, $_->childNodes->[0])
   480     $_->insertBefore($nav_el, $_->childNodes->[0])
   484   }
   481   }
   485   for ($dom->findnodes('/html/head')) {
   482   for ($dom->findnodes('/html/head')) {
   486     my $link = $dom->createElement('link');
   483     my $link = $dom->createElement('link');
   489     $_->appendChild($link);
   486     $_->appendChild($link);
   490   }
   487   }
   491   $dom;
   488   $dom;
   492 }
   489 }
   493 
   490 
       
   491 sub rss_st {
       
   492   $rss_entry
       
   493     =~ s/TITLE/$blog_title/er
       
   494     =~ s/BLINK/$domain/er
       
   495     =~ s/RDESC/$rdesc/er;
       
   496 }
       
   497 
   494 sub finalise_html {
   498 sub finalise_html {
   495   mkdir $no_name_dir;
   499   mkdir $no_name_dir;
   496   open my $fh, '>', "$no_name_dir/1.html";
   500   open my $fh, '>', "$no_name_dir/1.html";
   497   open my $mrss, '>', "$no_name_dir/rss.xml"; # The master rss file
   501   open my $mrss, '>', "$no_name_dir/rss.xml"; # The master rss file
   498   print $mrss $rss_entry;
   502   print $mrss rss_st();
   499   print $fh cssbs($h_intro . $h_c_intro, $blog_title, "a/c");
   503   print $fh cssbs($h_intro . $h_c_intro, $blog_title, "a/c");
   500   my (%c_files, %r_files);	# Cataogry files and rss
   504   my (%c_files, %r_files);	# Cataogry files and rss
   501   
   505   
   502   for my $cat (sort { $catags{$b} <=> $catags{$a} } keys(%catags)) {
   506   for my $cat (sort { $catags{$b} <=> $catags{$a} } keys(%catags)) {
   503     my $num = $catags{$cat};
   507     my $num = $catags{$cat};
   506       =~ s/CATAG/$cat/er
   510       =~ s/CATAG/$cat/er
   507       =~ s=LINK="$cat/1.html"=er;
   511       =~ s=LINK="$cat/1.html"=er;
   508     mkdir "$no_name_dir/$cat";
   512     mkdir "$no_name_dir/$cat";
   509     open $c_files{$cat}, '>', "$no_name_dir/$cat/1.html";
   513     open $c_files{$cat}, '>', "$no_name_dir/$cat/1.html";
   510     open $r_files{$cat}, '>', "$no_name_dir/$cat/rss.xml";
   514     open $r_files{$cat}, '>', "$no_name_dir/$cat/rss.xml";
   511     print {$r_files{$cat}} $rss_entry;
   515     print {$r_files{$cat}} rss_st();
   512     print {$c_files{$cat}}
   516     print {$c_files{$cat}}
   513       cssbs($h_intro, "$cat  <a id=\"rss\" href=\"rss.xml\">(rss)</span> ", "a/b/c");
   517       cssbs($h_intro, "$cat  <a id=\"rss\" href=\"rss.xml\">(rss)</span> ", "a/b/c");
   514   }
   518   }
   515   print $fh $h_chap;
   519   print $fh $h_chap;
   516   # :-)
   520   # :-)