Added a comment
[ikiwiki.git] / doc / forum / Calendar:_listing_multiple_entries_per_day / comment_3_d23f0cedd0b9e937eaf200eef55ac457._comment
1 [[!comment format=mdwn
2  username="https://www.google.com/accounts/o8/id?id=AItOawnXybLxkPMYpP3yw4b_I6IdC3cKTD-xEdU"
3  nickname="Matt"
4  subject="comment 3"
5  date="2011-11-30T20:42:55Z"
6  content="""
7 I got to grip with things to make a patch to do this. :-)
8
9 Here it is in case of use to anyone.
10
11 <pre>
12 From f0554c5b61e1915086d5cf071f095ff233c2590d Mon Sep 17 00:00:00 2001
13 From: Matt Ford <matt@dancingfrog.co.uk>
14 Date: Wed, 30 Nov 2011 19:40:10 +0000
15 Subject: [PATCH] Patch to allow daily archival generation and link to them in
16  the calendar
17
18 ---
19  IkiWiki/Plugin/calendar.pm   |   17 ++++++++++++++++-
20  ikiwiki-calendar.in          |   34 +++++++++++++++++++++++++++++++---
21  templates/calendarday.tmpl   |    5 +++++
22  templates/calendarmonth.tmpl |    2 +-
23  templates/calendaryear.tmpl  |    2 +-
24  5 files changed, 54 insertions(+), 6 deletions(-)
25  create mode 100644 templates/calendarday.tmpl
26
27 diff --git a/IkiWiki/Plugin/calendar.pm b/IkiWiki/Plugin/calendar.pm
28 index c7d2b7c..9999c03 100644
29 --- a/IkiWiki/Plugin/calendar.pm
30 +++ b/IkiWiki/Plugin/calendar.pm
31 @@ -47,6 +47,13 @@ sub getsetup () {
32                         safe => 1,
33                         rebuild => 1,
34                 },
35 +               archiveday => {
36 +                       type => \"boolean\",
37 +                       example => 1,
38 +                       description => \"enable archiving on a daily basis (otherwise monthly)\",
39 +                       safe => 1,
40 +                       rebuild => 1,
41 +               },
42                 archive_pagespec => {
43                         type => \"pagespec\",
44                         example => \"page(posts/*) and !*/Discussion\",
45 @@ -222,11 +229,19 @@ EOF
46                                 $tag='month-calendar-day-link';
47                         }
48                         $calendar.=qq{\t\t<td class=\"$tag $downame{$wday}\">};
49 -                       $calendar.=htmllink($params{page}, $params{destpage}, 
50 +                       if (exists $pagesources{\"$archivebase/$params{year}/$params{month}/\".sprintf(\"%02d\",$day)}) {
51 +                               $calendar.=htmllink($params{page}, $params{destpage}, 
52 +                                               \"$archivebase/$params{year}/$params{month}/\".sprintf(\"%02d\",$day),
53 +                                               noimageinline => 1,
54 +                                               linktext => \"$day\",
55 +                                               title => \"$key\");
56 +                       }else{
57 +                               $calendar.=htmllink($params{page}, $params{destpage}, 
58                                 $linkcache{$key},
59                                 noimageinline => 1,
60                                 linktext => $day,
61                                 title => pagetitle(IkiWiki::basename($linkcache{$key})));
62 +                       }
63                         $calendar.=qq{</td>\n};
64                 }
65                 else {
66 diff --git a/ikiwiki-calendar.in b/ikiwiki-calendar.in
67 index 037ef7d..af22bc5 100755
68 --- a/ikiwiki-calendar.in
69 +++ b/ikiwiki-calendar.in
70 @@ -30,21 +30,44 @@ IkiWiki::checkconfig();
71  my $archivebase = 'archives';
72  $archivebase = $config{archivebase} if defined $config{archivebase};
73  
74 +my $archiveday = 0;
75 +$archiveday = $config{archiveday} if defined $config{archiveday};
76 +
77  if (! defined $pagespec) {
78         $pagespec=$config{archive_pagespec} || \"*\";
79  }
80  
81 -sub writearchive ($$;$) {
82 +sub is_leap_year {
83 +       my $year=shift;
84 +       return ($year % 4 == 0 && (($year % 100 != 0) || $year % 400 == 0));
85 +}
86 +
87 +sub month_days {
88 +       my $month=shift;
89 +        my $year=shift;
90 +       my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$month-1];
91 +       if ($month == 2 && is_leap_year($year)) {
92 +               $days_in_month++;
93 +       }
94 +       return $days_in_month;
95 +}
96 +
97 +sub writearchive ($$;$;$) {
98         my $template=template(shift);
99         my $year=shift;
100         my $month=shift;
101 +        my $day=shift;
102  
103 -       my $page=defined $month ? \"$year/$month\" : $year;
104 +       my $page;
105 +        if (defined $year)  {$page = \"$year\"};
106 +        if (defined $month) {$page = \"$year/$month\"};
107 +        if (defined $day)   {$page = \"$year/$month/$day\"};
108  
109         my $pagefile=newpagefile(\"$archivebase/$page\", $config{default_pageext});
110         $template->param(pagespec => $pagespec);
111         $template->param(year => $year);
112         $template->param(month => $month) if defined $month;
113 +       $template->param(day => $day) if defined $day;
114  
115         if ($force || ! -e \"$config{srcdir}/$pagefile\") {
116                 writefile($pagefile, $config{srcdir}, $template->output);
117 @@ -54,8 +77,13 @@ sub writearchive ($$;$) {
118  
119  foreach my $y ($startyear..$endyear) {
120         writearchive(\"calendaryear.tmpl\", $y);
121 -       foreach my $m (qw{01 02 03 04 05 06 07 08 09 10 11 12}) {
122 +       foreach my $m (map {sprintf(\"%02d\",$_)} (1..12)) {
123                 writearchive(\"calendarmonth.tmpl\", $y, $m);
124 +               if ($archiveday ) {
125 +                       foreach my $d (map {sprintf(\"%02d\",$_)} (1..month_days($m,$y))) {
126 +                               writearchive(\"calendarday.tmpl\", $y, $m, $d);
127 +                       }
128 +               }
129         }
130  }
131  
132 diff --git a/templates/calendarday.tmpl b/templates/calendarday.tmpl
133 new file mode 100644
134 index 0000000..ac963b9
135 --- /dev/null
136 +++ b/templates/calendarday.tmpl
137 @@ -0,0 +1,5 @@
138 +[[!sidebar content=\"\"\"
139 +[[!calendar type=month month=<TMPL_VAR MONTH> year=<TMPL_VAR YEAR> pages=\"<TMPL_VAR PAGESPEC>\"]]
140 +\"\"\"]]
141 +
142 +[[!inline pages=\"creation_day(<TMPL_VAR DAY>) and creation_month(<TMPL_VAR MONTH>) and creation_year(<TMPL_VAR YEAR>) and <TMPL_VAR PAGESPEC>\" archive=\"yes\" show=0 feeds=no reverse=yes]]
143 diff --git a/templates/calendarmonth.tmpl b/templates/calendarmonth.tmpl
144 index 23cd954..c998c16 100644
145 --- a/templates/calendarmonth.tmpl
146 +++ b/templates/calendarmonth.tmpl
147 @@ -2,4 +2,4 @@
148  [[!calendar type=month month=<TMPL_VAR MONTH> year=<TMPL_VAR YEAR> pages=\"<TMPL_VAR PAGESPEC>\"]]
149  \"\"\"]]
150  
151 -[[!inline pages=\"creation_month(<TMPL_VAR MONTH>) and creation_year(<TMPL_VAR YEAR>) and <TMPL_VAR PAGESPEC>\" show=0 feeds=no reverse=yes]]
152 +[[!inline pages=\"creation_month(<TMPL_VAR MONTH>) and creation_year(<TMPL_VAR YEAR>) and <TMPL_VAR PAGESPEC>\" archive=\"yes\" show=0 feeds=no reverse=yes]]
153 diff --git a/templates/calendaryear.tmpl b/templates/calendaryear.tmpl
154 index 714bd6d..b6e33c5 100644
155 --- a/templates/calendaryear.tmpl
156 +++ b/templates/calendaryear.tmpl
157 @@ -1 +1 @@
158 -[[!calendar type=year year=<TMPL_VAR YEAR> pages=\"<TMPL_VAR PAGESPEC>\"]]
159 +[[!calendar type=year year=<TMPL_VAR YEAR> pages=\"<TMPL_VAR PAGESPEC>\" archive=\"yes\"]]
160 -- 
161 1.7.7.3
162
163 :
164
165 </pre>
166 """]]