646e254a59cba73e84db87b291d53546e7631741
[ikiwiki.git] / IkiWiki / Render.pm
1 package IkiWiki;
2
3 use warnings;
4 use strict;
5 use File::Spec;
6
7 sub linkify ($$) { #{{{
8         my $content=shift;
9         my $page=shift;
10
11         $content =~ s{(\\?)$config{wiki_link_regexp}}{
12                 $1 ? "[[$2]]" : htmllink($page, $2)
13         }eg;
14         
15         return $content;
16 } #}}}
17
18 sub htmlize ($$) { #{{{
19         my $type=shift;
20         my $content=shift;
21         
22         if (! $INC{"/usr/bin/markdown"}) {
23                 no warnings 'once';
24                 $blosxom::version="is a proper perl module too much to ask?";
25                 use warnings 'all';
26                 do "/usr/bin/markdown";
27         }
28         
29         if ($type eq '.mdwn') {
30                 return Markdown::Markdown($content);
31         }
32         else {
33                 error("htmlization of $type not supported");
34         }
35 } #}}}
36
37 sub backlinks ($) { #{{{
38         my $page=shift;
39
40         my @links;
41         foreach my $p (keys %links) {
42                 next if bestlink($page, $p) eq $page;
43                 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
44                         my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
45                         
46                         # Trim common dir prefixes from both pages.
47                         my $p_trimmed=$p;
48                         my $page_trimmed=$page;
49                         my $dir;
50                         1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
51                                 defined $dir &&
52                                 $p_trimmed=~s/^\Q$dir\E// &&
53                                 $page_trimmed=~s/^\Q$dir\E//;
54                                        
55                         push @links, { url => $href, page => $p_trimmed };
56                 }
57         }
58
59         return sort { $a->{page} cmp $b->{page} } @links;
60 } #}}}
61
62 sub parentlinks ($) { #{{{
63         my $page=shift;
64         
65         my @ret;
66         my $pagelink="";
67         my $path="";
68         my $skip=1;
69         foreach my $dir (reverse split("/", $page)) {
70                 if (! $skip) {
71                         $path.="../";
72                         unshift @ret, { url => "$path$dir.html", page => $dir };
73                 }
74                 else {
75                         $skip=0;
76                 }
77         }
78         unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
79         return @ret;
80 } #}}}
81
82 sub rsspage ($) { #{{{
83         my $page=shift;
84
85         return $page.".rss";
86 } #}}}
87
88 sub genpage ($$$) { #{{{
89         my $content=shift;
90         my $page=shift;
91         my $mtime=shift;
92
93         my $title=pagetitle(basename($page));
94         
95         my $template=HTML::Template->new(blind_cache => 1,
96                 filename => "$config{templatedir}/page.tmpl");
97         
98         if (length $config{cgiurl}) {
99                 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
100                 $template->param(prefsurl => "$config{cgiurl}?do=prefs");
101                 if ($config{rcs}) {
102                         $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
103                 }
104         }
105
106         if (length $config{historyurl}) {
107                 my $u=$config{historyurl};
108                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
109                 $template->param(historyurl => $u);
110         }
111
112         if ($config{rss}) {
113                 $template->param(rssurl => rsspage($page));
114         }
115         
116         $template->param(
117                 title => $title,
118                 wikiname => $config{wikiname},
119                 parentlinks => [parentlinks($page)],
120                 content => $content,
121                 backlinks => [backlinks($page)],
122                 discussionlink => htmllink($page, "Discussion", 1, 1),
123                 mtime => scalar(gmtime($mtime)),
124         );
125         
126         return $template->output;
127 } #}}}
128
129 sub date_822 ($) { #{{{
130         my $time=shift;
131
132         eval q{use POSIX};
133         return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
134 } #}}}
135
136 sub absolute_urls ($$) { #{{{
137         my $content=shift;
138         my $url=shift;
139
140         $url=~s/[^\/]+$//;
141         
142         $content=~s{<a\s+href="([^"]+)"}{
143                 "<a href=\"$url$1\""
144         }ieg;
145         $content=~s{<img\s+src="([^"]+)"}{
146                 "<img src=\"$url$1\""
147         }ieg;
148         return $content;
149 } #}}}
150
151 sub genrss ($$$) { #{{{
152         my $content=shift;
153         my $page=shift;
154         my $mtime=shift;
155
156         my $url="$config{url}/".htmlpage($page);
157         
158         my $template=HTML::Template->new(blind_cache => 1,
159                 filename => "$config{templatedir}/rsspage.tmpl");
160         
161         # Regular page gets a feed that is updated every time the
162         # page is changed, so the mtime is encoded in the guid.
163         my @items=(
164                 {
165                         itemtitle => pagetitle(basename($page)),
166                         itemguid => "$url?mtime=$mtime",
167                         itemurl => $url,
168                         itempubdate => date_822($mtime),
169                         itemcontent => absolute_urls($content, $url), # rss sucks
170                 },
171         );
172         
173         $template->param(
174                 title => pagetitle(basename($page)),
175                 pageurl => $url,
176                 items => \@items,
177         );
178         
179         return $template->output;
180 } #}}}
181
182 sub check_overwrite ($$) { #{{{
183         # Important security check. Make sure to call this before saving
184         # any files to the source directory.
185         my $dest=shift;
186         my $src=shift;
187         
188         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
189                 error("$dest already exists and was rendered from ".
190                         join(" ",(grep { $renderedfiles{$_} eq $dest } keys
191                                 %renderedfiles)).
192                         ", before, so not rendering from $src");
193         }
194 } #}}}
195
196 sub mtime ($) { #{{{
197         my $page=shift;
198         
199         return (stat($page))[9];
200 } #}}}
201
202 sub findlinks ($$) { #{{{
203         my $content=shift;
204         my $page=shift;
205
206         my @links;
207         while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
208                 push @links, lc($1);
209         }
210         # Discussion links are a special case since they're not in the text
211         # of the page, but on its template.
212         return @links, "$page/discussion";
213 } #}}}
214
215 sub render ($) { #{{{
216         my $file=shift;
217         
218         my $type=pagetype($file);
219         my $content=readfile("$config{srcdir}/$file");
220         if ($type ne 'unknown') {
221                 my $page=pagename($file);
222                 
223                 $links{$page}=[findlinks($content, $page)];
224                 
225                 $content=linkify($content, $page);
226                 $content=htmlize($type, $content);
227                 
228                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
229                 writefile("$config{destdir}/".htmlpage($page),
230                         genpage($content, $page, mtime("$config{srcdir}/$file")));              
231                 $oldpagemtime{$page}=time;
232                 $renderedfiles{$page}=htmlpage($page);
233
234                 # TODO: should really add this to renderedfiles and call
235                 # check_overwrite, as above, but currently renderedfiles
236                 # only supports listing one file per page.
237                 if ($config{rss}) {
238                         writefile("$config{destdir}/".rsspage($page),
239                                 genrss($content, $page, mtime("$config{srcdir}/$file")));
240                 }
241         }
242         else {
243                 $links{$file}=[];
244                 check_overwrite("$config{destdir}/$file", $file);
245                 writefile("$config{destdir}/$file", $content);
246                 $oldpagemtime{$file}=time;
247                 $renderedfiles{$file}=$file;
248         }
249 } #}}}
250
251 sub prune ($) { #{{{
252         my $file=shift;
253
254         unlink($file);
255         my $dir=dirname($file);
256         while (rmdir($dir)) {
257                 $dir=dirname($dir);
258         }
259 } #}}}
260
261 sub refresh () { #{{{
262         # find existing pages
263         my %exists;
264         my @files;
265         eval q{use File::Find};
266         find({
267                 no_chdir => 1,
268                 wanted => sub {
269                         if (/$config{wiki_file_prune_regexp}/) {
270                                 no warnings 'once';
271                                 $File::Find::prune=1;
272                                 use warnings "all";
273                         }
274                         elsif (! -d $_ && ! -l $_) {
275                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
276                                 if (! defined $f) {
277                                         warn("skipping bad filename $_\n");
278                                 }
279                                 else {
280                                         $f=~s/^\Q$config{srcdir}\E\/?//;
281                                         push @files, $f;
282                                         $exists{pagename($f)}=1;
283                                 }
284                         }
285                 },
286         }, $config{srcdir});
287
288         my %rendered;
289
290         # check for added or removed pages
291         my @add;
292         foreach my $file (@files) {
293                 my $page=pagename($file);
294                 if (! $oldpagemtime{$page}) {
295                         debug("new page $page");
296                         push @add, $file;
297                         $links{$page}=[];
298                         $pagesources{$page}=$file;
299                 }
300         }
301         my @del;
302         foreach my $page (keys %oldpagemtime) {
303                 if (! $exists{$page}) {
304                         debug("removing old page $page");
305                         push @del, $pagesources{$page};
306                         prune($config{destdir}."/".$renderedfiles{$page});
307                         delete $renderedfiles{$page};
308                         $oldpagemtime{$page}=0;
309                         delete $pagesources{$page};
310                 }
311         }
312         
313         # render any updated files
314         foreach my $file (@files) {
315                 my $page=pagename($file);
316                 
317                 if (! exists $oldpagemtime{$page} ||
318                     mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
319                         debug("rendering changed file $file");
320                         render($file);
321                         $rendered{$file}=1;
322                 }
323         }
324         
325         # if any files were added or removed, check to see if each page
326         # needs an update due to linking to them
327         # TODO: inefficient; pages may get rendered above and again here;
328         # problem is the bestlink may have changed and we won't know until
329         # now
330         if (@add || @del) {
331 FILE:           foreach my $file (@files) {
332                         my $page=pagename($file);
333                         foreach my $f (@add, @del) {
334                                 my $p=pagename($f);
335                                 foreach my $link (@{$links{$page}}) {
336                                         if (bestlink($page, $link) eq $p) {
337                                                 debug("rendering $file, which links to $p");
338                                                 render($file);
339                                                 $rendered{$file}=1;
340                                                 next FILE;
341                                         }
342                                 }
343                         }
344                 }
345         }
346
347         # handle backlinks; if a page has added/removed links, update the
348         # pages it links to
349         # TODO: inefficient; pages may get rendered above and again here;
350         # problem is the backlinks could be wrong in the first pass render
351         # above
352         if (%rendered) {
353                 my %linkchanged;
354                 foreach my $file (keys %rendered, @del) {
355                         my $page=pagename($file);
356                         if (exists $links{$page}) {
357                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
358                                         if (length $link &&
359                                             ! exists $oldlinks{$page} ||
360                                             ! grep { $_ eq $link } @{$oldlinks{$page}}) {
361                                                 $linkchanged{$link}=1;
362                                         }
363                                 }
364                         }
365                         if (exists $oldlinks{$page}) {
366                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
367                                         if (length $link &&
368                                             ! exists $links{$page} ||
369                                             ! grep { $_ eq $link } @{$links{$page}}) {
370                                                 $linkchanged{$link}=1;
371                                         }
372                                 }
373                         }
374                 }
375                 foreach my $link (keys %linkchanged) {
376                         my $linkfile=$pagesources{$link};
377                         if (defined $linkfile) {
378                                 debug("rendering $linkfile, to update its backlinks");
379                                 render($linkfile);
380                         }
381                 }
382         }
383 } #}}}
384
385 1