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