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