genorg.pl
author Pranshu Sharma <pranshu@bauherren.ovh>
Sun, 15 Dec 2024 20:38:17 +1000
changeset 4 c98c7c32ab46
parent 3 bf4ae6f1dbbf
child 5 24fc5a406138
permissions -rw-r--r--
Cache output, found the best soltuion Instead of repilicating html tree to be reparsed, keep all the parsed info in xml, and for links cache them as well. Forgot to do more commits
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
     1
#!/usr/bin/perl
1
0b7be2e78d3b Added more documentation
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 0
diff changeset
     2
# Author: Pranshu Sharma <pranshu@bauherren.ovh>
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
     3
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
     4
use v5.36.0;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
     5
use strict;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
     6
use warnings;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
     7
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
     8
use XML::LibXML;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
     9
use File::Find;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    10
use File::Copy qw(copy move);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    11
use Cwd;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    12
use experimental qw(declared_refs);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    13
use Data::Dumper;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    14
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    15
# Fix up CLI interface
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    16
# Add option ro set cache to 0 through command line
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    17
# Print required elisp code it is in $req_config
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    18
# Add config option for $max_rss and $max_cat
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    19
# Sort cataogirs by entires
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    20
# Variable for map_css
4
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
    21
# Remove the desc
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    22
# Domain thing as well
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
    23
# Load custom filpe for htmlize and all that
4
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
    24
# 2 articles 1 day better sorting
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    25
my $cache = 1;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    26
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    27
my $config_file = "genorg-conf";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    28
my $dir = "blog";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    29
my $outdir = "out/";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    30
my $emacs = "emacs";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    31
my $css = "style.css";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    32
my $map_css ="other.css";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    33
my $no_name_dir = "c";
4
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
    34
my $cache_file = "genorg-cache/cache.xml";
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    35
my $domain = "p.bauherren.ovh";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    36
my $max_rss = 30;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    37
# The first page has one less article.  Think of it as a feature.
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
    38
my $max_cat = 10;
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    39
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    40
my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid ($<);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    41
my $template = <<"END";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    42
#+title: Test document
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    43
#+subtitle: This is the subtitle
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    44
#+author: $username
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    45
#+keywords: tag thing | related
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    46
#+options: html-link-use-abs-url:nil html-postamble:auto
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    47
#+options: html-preamble:t html-scripts:nil html-style:t
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    48
#+options: html5-fancy:nil tex:t
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    49
#+options: tex:mathjax
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    50
#+html_doctype: html5
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    51
#+html_container: div
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    52
#+html_content_class: content
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    53
#+html_link_home:
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    54
#+html_link_up:
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    55
#+html_mathjax:
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    56
#+html_equation_reference_format: \eqref{%s}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    57
#+html_head:
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    58
#+html_head_extra:
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    59
#+infojs_opt:
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    60
#+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)
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    61
#+latex_header:
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    62
END
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    63
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    64
my $req_config = <<"END";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    65
(with-eval-after-load 'org
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    66
    (org-link-set-parameters "genorg"
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    67
			 :follow nil
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    68
			 :export
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    69
			 #'(lambda (link desc _ _)
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    70
			     (format "<genorg desc=\"%s\" link=\"%s\"/>" desc link))
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    71
			 :store nil))
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    72
END
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    73
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    74
my $elisp_code = <<"END" =~ s/\n\s+//gr;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    75
(let ((ls '(LIST)))
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    76
  (require 'org)
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    77
  (mapc
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    78
   (lambda (b)
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    79
     (with-current-buffer b
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    80
       (when (and (not (string-match "^ " (buffer-name b)))
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    81
		  (eq major-mode 'org-mode))
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    82
	 (org-export-to-file 'html (pop ls)))))
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    83
   (buffer-list))
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    84
  (kill-emacs))
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    85
END
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    86
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    87
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    88
my ($h_intro, $h_c_intro, $h_cat_li, $h_chap, $h_en_cat, $h_en, $h_end)
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    89
  = split /SPLIT/ ,<<"END" =~ s/\n\s+//gr;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    90
<!DOCTYPE html>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    91
<html lang="en">
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    92
  <head>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    93
    <meta charset="UTF-8">
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    94
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    95
    <title>TITLE</title>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    96
    <link rel="stylesheet" href="style.css">
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    97
  </head>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    98
SPLIT
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
    99
  <body>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   100
    <h1> Pranshu's Blog </h1>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   101
    <ul id="cataogries">
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   102
SPLIT 
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   103
      <li> <a href="LINK">  CATAG (NUMBER)</a>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   104
SPLIT
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   105
    </ul>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   106
    <div>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   107
      <h2> All articles </h2> <hr>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   108
SPLIT
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   109
   <a href="LINK">
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   110
     <strong> TITLE </strong> <span>DATE</span>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   111
     <p>DESCREPTION</p>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   112
     <i>CATAG</i>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   113
     <hr>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   114
  </a>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   115
SPLIT
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   116
   <a href="LINK">
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   117
     <strong> TITLE </strong> <span>DATE</span>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   118
     <p>DESCREPTION</p>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   119
     <hr>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   120
   </a>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   121
SPLIT
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   122
    </div>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   123
  </body>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   124
</html>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   125
END
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   126
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   127
my $help = <<"END";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   128
genorg [option] [dir|file]
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   129
Option can be:
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   130
 -t FILE : Make a template org file at FILE
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   131
 -h      : Help
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   132
 -c FILE : sepcify a new config file instead of genorg-conf
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   133
 -r      : unconditionally remake org files
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   134
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   135
If no option is set, accept a directory to generate.
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   136
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   137
$config_file follow the syntax of:
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   138
VAR VALUE
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   139
where VAR can be:
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   140
 emacs-path : The value should be path of emacs. 'emacs' by default.
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   141
 css        : The path of the css file to use.
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   142
 in_dir     : The directory that contains the files.
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   143
 out_dir    : the output directory.
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   144
 no_name_dir: The directory for generate files.  This means in_dir/no_name_dir
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   145
              cannot exist.
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   146
END
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   147
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   148
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   149
my $rss_entry = <<"END" =~ s/\n\s+//gr;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   150
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   151
<rss version=\"2.0\">
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   152
 <channel>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   153
  <title>W3Schools Home Page</title>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   154
  <link>https://www.w3schools.com</link>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   155
  <description>Free web building tutorials</description>"
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   156
END
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   157
  
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   158
my %config_vars =
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   159
  (
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   160
   'emacs-path' => \$emacs,
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   161
   css => \$css,
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   162
   indir => \$dir,
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   163
   outdir => \$outdir,
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   164
   no_name_dir => \$no_name_dir
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   165
  );
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   166
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   167
sub read_config ($path) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   168
  open my $fh, '<', $path or die "Couldn't open config file\n";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   169
  while (<$fh>) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   170
    next if /^\s+$/;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   171
    chomp;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   172
    my ($directive, $rest) = split /\s+/, $_, 2;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   173
    if (exists($config_vars{$directive})) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   174
      ${$config_vars{$directive}} = $rest;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   175
    } else {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   176
      print "Unknown variable $directive\n";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   177
    }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   178
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   179
  close $fh;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   180
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   181
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   182
sub help_and_bye {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   183
  print $help;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   184
  exit;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   185
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   186
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   187
sub template ($file) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   188
  $file // die "-t needs an argument.\n";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   189
  $file =~ s/\.org$//;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   190
  open my $fh, '>', $file . ".org" or die $file . " couldn't be opened\n";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   191
  print $fh $template;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   192
  close $fh;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   193
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   194
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   195
# Reletive file from the perspect of file1, to file2
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   196
sub prel_path ($from, $to) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   197
  return '.' if $from eq $to;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   198
  my \(@f1, @f2) = map [m{/[^/]+}g], ($from, $to);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   199
  # return substr($f1[$#f1], 1) if $from =~ $to;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   200
  while (@f1 && @f2 && $f1[0] =~ $f2[0]) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   201
    shift @f1;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   202
    shift @f2;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   203
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   204
  my $p = @f1-1;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   205
  local $" = "";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   206
  "../" x ($p > 0 ? $p : 0) . substr("@f2", 1);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   207
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   208
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   209
my @org_exps;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   210
my @files_to_make;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   211
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   212
sub wanted {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   213
  my $n_path = $outdir . prel_path ($dir,  "$File::Find::name/");
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   214
  # We need to mirror the direcotry structure
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   215
  if (-d) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   216
    push @files_to_make, $n_path || die "Couldn't make directory $n_path\n"
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   217
      unless -d $n_path;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   218
    return;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   219
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   220
  if (/\.org$/) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   221
    push @org_exps, substr $n_path, length($outdir);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   222
  } else {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   223
    copy $_, $n_path;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   224
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   225
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   226
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   227
my @modified_files;
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   228
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   229
# Export @org_exps
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   230
sub conv_files {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   231
  my %changed_files;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   232
  # Remove files that are already converted
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   233
  for my $rel_file (@org_exps) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   234
    # Emacs need the full file path
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   235
    my ($file, $to) = map { getcwd . "/$_" . $rel_file} ($dir, $outdir);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   236
    s/org$/html/ for ($to, $rel_file);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   237
    unless ($cache && -f $to && (stat($to))[9] > (stat($file))[9]) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   238
      $changed_files{$file} = $to;
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   239
      push @modified_files, $rel_file;
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   240
    }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   241
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   242
  %changed_files // exit;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   243
  $elisp_code =~ s/LIST/join '', map "\"$_\" ", values %changed_files /e;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   244
  open my $fh, '-|', ($emacs, '--batch', '~/.emacs.d/init.el', keys %changed_files, "--eval" , "$elisp_code");
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   245
  close $fh;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   246
  \%changed_files
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   247
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   248
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   249
my $cdir;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   250
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   251
$ARGV[0] // die "No arguments provided\n";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   252
while ($_ = shift @ARGV) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   253
  /^(-h|--?help)$/ && help_and_bye;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   254
  /^-t$/ && do { template shift @ARGV ; exit };
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   255
  /^-c$/ && do { $config_file = shift @ARGV; next };
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   256
  /^-d$/ && do { $cache = 0; next };
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   257
  $cdir = $_;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   258
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   259
$cdir || die "directory not provided\n";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   260
main();
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   261
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   262
my %catags;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   263
my @arts;
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   264
my $cache_dom;
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   265
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   266
sub main {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   267
  -d $cdir or help_and_bye;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   268
  mkdir $outdir;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   269
  chdir $cdir or die "Couldn't access $cdir\n";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   270
  read_config $config_file;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   271
  -d $dir or die "$dir doesn't exist \n";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   272
  find (\&wanted, $dir);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   273
  mkdir for @files_to_make;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   274
  values %{conv_files()};
4
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   275
  my $cache_dom = XML::LibXML->load_xml(location => $cache_file) if $cache && -f $cache_file;
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   276
  chdir $outdir;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   277
  @arts = sort { $b->{date} <=> $a->{date}} map { html_fixup($_) } @org_exps;
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   278
  chdir '..';
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   279
  populate_cache();
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   280
  # finalise_html();
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   281
}
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   282
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   283
sub populate_cache {
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   284
  my $dom = XML::LibXML::Document->new('1.0', 'UTF-8');
4
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   285
  my $root = $dom->createElement('root');
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   286
  my %c_els;
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   287
  for my $art (@arts) {
4
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   288
    # We need the: related, file, date
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   289
    my ($art_el, $date, $title, $ca, $related) =
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   290
      map {$dom->createElement($_)} ("art", "date", "title", "catag", "related");
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   291
    $art_el->{path} = $art->{file};
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   292
    $ca->appendText($art->{catag});
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   293
    $title->appendText($art->{title});
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   294
    $date->appendText($art->{date}->text_easy);
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   295
    my $rel_str;
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   296
    for my $thing ($art->{related}) {
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   297
      for (@{$thing}) {
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   298
	my ($catag, $artt) = @{$_};
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   299
	my $rel_str .= "\"$catag\"" . ($artt && "->\"$artt\"");
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   300
	$related->appendText("\"$catag\"" . ($artt && "->\"$artt\" "));
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   301
      }
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   302
    }
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   303
    $art_el->appendChild($_) for ($date, $ca, $related);
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   304
    $root->appendChild($art_el);
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   305
  }
4
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   306
  $root->appendChild($_) for values %c_els;
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   307
  $dom->setDocumentElement($root);
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   308
  open my $fh, '>' ,$cache_file;
4
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   309
  print $fh ($dom->toString(1) =~ s/(.+)/$1\n<!-- Nicley formated :() -->/r);
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   310
  close $fh;
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   311
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   312
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   313
sub entry {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   314
  my ($data, $catagory) = @_;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   315
  my $str = defined $catagory
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   316
    ? ($h_en_cat =~ s/CATAG/$data->{catag}/er) : $h_en;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   317
  my $prel_path = "/$no_name_dir/" . (defined $catagory ? "c" : "c/b");
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   318
  $str =~ s/DATE/$data->{date}->fmt/er
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   319
    =~ s/TITLE/$data->{title}/er
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   320
    =~ s/DESCREPTION/$data->{desc}/er
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   321
    =~ s|LINK|prel_path($prel_path, '/'. $data->{file})|er
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   322
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   323
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   324
sub rss_en ($data) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   325
  my $thing = <<"END" =~ s/\n\s+//gr;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   326
  <item>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   327
    <title>TITLE</title>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   328
    <link>LINK</link>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   329
    <description>DESC</description>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   330
  </item>
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   331
END
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   332
  $thing =~ s|LINK|"$domain/" . $data->{file}|er
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   333
    =~ s/TITLE/$data->{title}/er
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   334
    =~ s/DESC/$data->{desc}/er;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   335
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   336
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   337
sub min ($x, $y) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   338
  ($x, $y)[$x > $y]
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   339
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   340
2
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   341
# @_ has to be in format of [STRING, LINK]
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   342
sub list2paginaiton {
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   343
  my $page = shift;
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   344
  '<ul>' . join ("", map {
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   345
    my ($str, $file) = @{$_};
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   346
    $str = "---$str" if $file && ($file == $page);
2
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   347
    defined $file ?
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   348
      "<li><a href=\"$file.html\">$str</a>" : $str
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   349
    } ((["←", $page - 1]) x!! ($page - 1),
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   350
       @_,
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   351
       (["→", $page + 1]) x ($page != $_[$#_]->[1]))) . '</ul>'
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   352
     }
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   353
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   354
sub move_on_if_neccasary ($fh, $art_num, $total_articles, $path) {
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   355
  return $fh if $art_num == 0 || $art_num % $max_cat;
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   356
  my $cpage = $art_num / $max_cat;
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   357
  print $fh panigation($cpage, int 0.6 +  $total_articles / $max_cat, $path);
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   358
  open $fh, '>', $path . '/' . ($cpage + 1).'.html';
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   359
  $fh;
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   360
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   361
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   362
sub panigation ($page, $no_pages, $dir) {
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   363
  say ($no_pages - 4);
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   364
  if ($no_pages < 9) {
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   365
    list2paginaiton ($page, (map [$_, $_], (1..$no_pages)))
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   366
  } elsif (4 < $page <= ($no_pages - 4)) {
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   367
    say "HERE";
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   368
    list2paginaiton
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   369
      ($page,
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   370
       [1, 1],
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   371
       ["<li>..."],
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   372
       (map [$_, $_], (($page-2)..($page+2))),
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   373
       ["<li>..."],
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   374
       [($no_pages) x 2])  
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   375
    } elsif ($page < 6) {
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   376
      list2paginaiton $page,
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   377
	((map [$_, $_], (1..5)),
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   378
	 ["<li>..."],
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   379
	 [($no_pages) x 2])
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   380
      } else {
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   381
	list2paginaiton $page,
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   382
	  ([1,1],
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   383
	   ["<li>..."],
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   384
	   (map [$_, $_], (($no_pages - 5)..$no_pages)))
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   385
	}
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   386
}
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   387
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   388
sub finalise_html {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   389
  mkdir $no_name_dir;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   390
  open my $fh, '>', "$no_name_dir/1.html";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   391
  open my $mrss, '>', "$no_name_dir/rss.xml"; # The master rss file
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   392
  print $mrss $rss_entry;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   393
  print $fh $h_intro . $h_c_intro;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   394
  my (%c_files, %r_files);	# Cataogry files and rss
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   395
  while (my ($cat, $num) = each %catags) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   396
    # ($h_intro, $h_c_intro, $h_cat_li, $h_chap, $h_en_cat, $h_en, $h_end)
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   397
    print $fh $h_cat_li =~ s/NUMBER/$num/re
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   398
      =~ s/CATAG/$cat/er
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   399
      =~ s=LINK="$cat/1.html"=er;
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   400
    mkdir "$no_name_dir/$cat";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   401
    open $c_files{$cat}, '>', "$no_name_dir/$cat/1.html";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   402
    open $r_files{$cat}, '>', "$no_name_dir/$cat/rss.xml";
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   403
    print {$r_files{$cat}} $rss_entry;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   404
    print {$c_files{$cat}} $h_intro;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   405
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   406
  print $fh $h_chap;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   407
  # :-)
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   408
  my $total_articles = () =  map {(1)x$_} values %catags;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   409
  # Now we iterate through the articles and add their index
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   410
  my ($n_fh, $n_mrss, %n_rss) = (0,0);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   411
  my %n_cat;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   412
  for my $art (@arts) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   413
    my ($catag, $title) = @{$art}{qw(catag title)};
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   414
    my $rentry = rss_en $art;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   415
    if (($n_rss{$catag} // 0) < $max_rss) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   416
      print {$r_files{$catag}} $rentry;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   417
      ++$n_rss{$catag};
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   418
    }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   419
    if ($n_mrss < $max_rss) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   420
      print $mrss $rentry;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   421
      ++$n_mrss
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   422
    }
2
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   423
    # ($fh, $art_num, $total_articles, $path)
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   424
    $fh = move_on_if_neccasary $fh, $n_fh, $total_articles, $no_name_dir;
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   425
    $n_fh++;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   426
    print $fh entry $art, 1;
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   427
    my $ncat = \$n_cat{$catag};
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   428
    ++$$ncat;
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   429
    my $cfh = \$c_files{$catag};
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   430
    $$cfh = move_on_if_neccasary $$cfh, $$ncat, $catags{$catag}, "$no_name_dir/$catag";
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   431
    print {$$cfh} entry $art;
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   432
    my $a_file = $art->{file};
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   433
    # say $a_file;
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   434
    if (grep /^$a_file$/, @modified_files) {
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   435
      open my $h_file, '>', $a_file;
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   436
      # print $h_file $art->{dom}->toString;
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   437
      close $h_file;
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   438
    }
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   439
  }
2
c4ca65113229 Pagination major progress
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 1
diff changeset
   440
  print $fh panigation (($n_fh / $max_cat) x 2, $no_name_dir) if $max_cat < $total_articles;
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   441
  print $fh $h_end;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   442
  close $fh;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   443
  for (values %c_files) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   444
    print $_ $h_end;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   445
    close;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   446
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   447
  print $mrss '</channel> </rss>';
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   448
  for (values %r_files) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   449
    print $_ '</channel> </rss>';;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   450
    close;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   451
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   452
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   453
4
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   454
# I don't want to prepopulate, as it is not known which ones are
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   455
# usless and outdated, and it would be a waste of cycles parsing
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   456
# something to know it is not needed
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   457
sub getcache {
4
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   458
  $cache_dom || return 0;
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   459
  return 0;
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   460
}
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   461
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   462
sub html_fixup ($filename) {
4
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   463
   # for (getcache($filename)) {
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   464
    # $_ && return $_;
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   465
  # }
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   466
  unless (grep /^$filename$/, @modified_files) {
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   467
    for (getcache($filename)) {
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   468
      return $_ if $_
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   469
    }
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   470
  }
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   471
  my $dom =
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   472
    XML::LibXML->load_html(location  => $filename, recover   => 1);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   473
  my %data = ('file', $filename);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   474
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   475
  # We don't need style or table of contents heading (if exists)
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   476
  for my $xpath ('/html/head/style', '/html/body/div/div[@id="table-of-contents"]/h2') {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   477
    $_->parentNode->removeChild($_) for $dom->findnodes($xpath) 
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   478
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   479
  
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   480
  # Info we need
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   481
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   482
  # Date
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   483
  for ($dom->findnodes('/html/body/div[@id="postamble"]/p[@class="date"]')) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   484
    my $text = $_->to_literal;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   485
    if ($text =~ /^Date: (\d+)-(\d+)-(\d+)/) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   486
      my $date = PDate->new($1, $2, $3);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   487
      $data{date} = $date;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   488
      my $p = $_->parentNode;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   489
      $p->parentNode->removeChild($p);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   490
    }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   491
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   492
  # Title\
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   493
  for ($dom->findnodes('/html/head/title')) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   494
    $data{title}  = $_->to_literal;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   495
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   496
  # keywords/catogry
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   497
  for my $node ($dom->findnodes('/html/head/meta')) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   498
    (my $cont = $node->getAttribute('content')) || next;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   499
    for ($node->getAttribute('name')) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   500
      if (/description/) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   501
	$data{desc} = $cont;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   502
      } elsif (/keywords/) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   503
	my ($key, $rest) = parse_keywords($cont);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   504
	@data{qw[catag title]} = @{$key};
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   505
	$catags{$data{catag}}++;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   506
	$rest =~ s/\s+\|\s+//;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   507
	while ($rest) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   508
	  (my $t,$rest) = parse_keywords($rest);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   509
	  push @{$data{related}}, $t;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   510
	}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   511
      } else {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   512
	next;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   513
      }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   514
      $node->parentNode->removeChild($node);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   515
    }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   516
  }
3
bf4ae6f1dbbf Half shit cache fixup
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 2
diff changeset
   517
  $data{dom} = $dom;
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   518
  \%data;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   519
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   520
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   521
sub parse_keywords ($str) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   522
  if ($str =~ s/\s*(?:"(.+?)"|(\w+))\s*->(?:\s*(?:"(.+?)"|(\w+)))//) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   523
    ([$1 // $2, $3 // $4], $str);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   524
  } else {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   525
    die "Keywords not arranged properly\n"
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   526
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   527
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   528
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   529
# I wonder if sean combs has made a similar class
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   530
{
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   531
  package PDate;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   532
  
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   533
  sub new {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   534
    my $class = shift;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   535
    my $self = { year => 0 + shift,
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   536
		 month => 0 + shift,
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   537
		 day => 0 + shift,
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   538
	       };
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   539
    bless $self, $class;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   540
    return $self;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   541
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   542
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   543
  # $d1 is greater than $d2
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   544
  sub cmp  {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   545
    my ($d1, $d2) = @_;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   546
    for ($d1->{year} <=> $d2->{year},
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   547
	 $d1->{month} <=> $d2->{month},
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   548
	 $d1->{day} <=> $d2->{day}) {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   549
      return $_ unless $_ == 0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   550
    }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   551
    0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   552
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   553
  use overload '<=>' => \&cmp;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   554
 
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   555
  sub fmt {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   556
    my $self = shift;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   557
    my @months =
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   558
      qw(January Febuary March April May June July August September November October December);
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   559
    my $n = $self->{day};
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   560
    if ($n == 1) { $n = '1st' }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   561
    elsif (($n - 2) % 10 == 0) { $n = "${n}nd" }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   562
    elsif (($n - 3) % 10 == 0) { $n = "${n}rd" }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   563
    else { $n = "${n}st" }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   564
    $months[$self->{month} - 1] . " $n, " . $self->{year}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   565
  }
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   566
    
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   567
  sub short_fmt {
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   568
    my $self = shift;
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   569
    join "-", ($self->{year}, $self->{month}, $self->{day});
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   570
  }
4
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   571
  sub text_easy {
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   572
    my $self = shift;
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   573
    join "-", ($self->{year}, $self->{month}, $self->{day});
c98c7c32ab46 Cache output, found the best soltuion
Pranshu Sharma <pranshu@bauherren.ovh>
parents: 3
diff changeset
   574
  }
0
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   575
}
1f2f88ea2e78 inital commit
Pranshu Sharma <pranshu@bauherren.ovh>
parents:
diff changeset
   576