* Generalised preprocesser loop protection code.
[ikiwiki.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use Encode;
9
10 sub linkify ($$$) { #{{{
11         my $lpage=shift; # the page containing the links
12         my $page=shift; # the page the link will end up on (different for inline)
13         my $content=shift;
14
15         $content =~ s{(\\?)$config{wiki_link_regexp}}{
16                 $2 ? ( $1 ? "[[$2|$3]]" : htmllink($lpage, $page, titlepage($3), 0, 0, pagetitle($2)))
17                    : ( $1 ? "[[$3]]" :    htmllink($lpage, $page, titlepage($3)))
18         }eg;
19         
20         return $content;
21 } #}}}
22
23 sub htmlize ($$) { #{{{
24         my $type=shift;
25         my $content=shift;
26         
27         if (exists $hooks{htmlize}{$type}) {
28                 $content=$hooks{htmlize}{$type}{call}->($content);
29         }
30         else {
31                 error("htmlization of $type not supported");
32         }
33
34         run_hooks(sanitize => sub {
35                 $content=shift->($content);
36         });
37         
38         return $content;
39 } #}}}
40
41 sub backlinks ($) { #{{{
42         my $page=shift;
43
44         my @links;
45         foreach my $p (keys %links) {
46                 next if bestlink($page, $p) eq $page;
47                 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
48                         my $href=abs2rel(htmlpage($p), dirname($page));
49                         
50                         # Trim common dir prefixes from both pages.
51                         my $p_trimmed=$p;
52                         my $page_trimmed=$page;
53                         my $dir;
54                         1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
55                                 defined $dir &&
56                                 $p_trimmed=~s/^\Q$dir\E// &&
57                                 $page_trimmed=~s/^\Q$dir\E//;
58                                        
59                         push @links, { url => $href, page => pagetitle($p_trimmed) };
60                 }
61         }
62
63         return sort { $a->{page} cmp $b->{page} } @links;
64 } #}}}
65
66 sub parentlinks ($) { #{{{
67         my $page=shift;
68         
69         my @ret;
70         my $pagelink="";
71         my $path="";
72         my $skip=1;
73         return if $page eq 'index'; # toplevel
74         foreach my $dir (reverse split("/", $page)) {
75                 if (! $skip) {
76                         $path.="../";
77                         unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
78                 }
79                 else {
80                         $skip=0;
81                 }
82         }
83         unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
84         return @ret;
85 } #}}}
86
87 my @preprocessing;
88 sub preprocess ($$$) { #{{{
89         my $page=shift; # the page the data comes from
90         my $destpage=shift; # the page the data will appear in (different for inline)
91         my $content=shift;
92
93         my $handle=sub {
94                 my $escape=shift;
95                 my $command=shift;
96                 my $params=shift;
97                 if (length $escape) {
98                         return "[[$command $params]]";
99                 }
100                 elsif (grep { $_ eq $page } @preprocessing) {
101                         # Avoid loops of preprocessed pages preprocessing
102                         # other pages that preprocess them, etc.
103                         return "[[$command would cause preprocessing loop]]";
104                 }
105                 elsif (exists $hooks{preprocess}{$command}) {
106                         # Note: preserve order of params, some plugins may
107                         # consider it significant.
108                         my @params;
109                         while ($params =~ /(?:(\w+)=)?(?:"""(.*?)"""|"([^"]+)"|(\S+))(?:\s+|$)/sg) {
110                                 my $key=$1;
111                                 my $val;
112                                 if (defined $2) {
113                                         $val=$2;
114                                         $val=~s/\r\n/\n/mg;
115                                         $val=~s/^\n+//g;
116                                         $val=~s/\n+$//g;
117                                 }
118                                 elsif (defined $3) {
119                                         $val=$3;
120                                 }
121                                 elsif (defined $4) {
122                                         $val=$4;
123                                 }
124
125                                 if (defined $key) {
126                                         push @params, $key, $val;
127                                 }
128                                 else {
129                                         push @params, $val, '';
130                                 }
131                         }
132                         push @preprocessing, $page;
133                         my $ret=$hooks{preprocess}{$command}{call}->(
134                                 @params,
135                                 page => $page,
136                                 destpage => $destpage,
137                         );
138                         pop @preprocessing;
139                         return $ret;
140                 }
141                 else {
142                         return "[[$command not processed]]";
143                 }
144         };
145         
146         $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
147         return $content;
148 } #}}}
149
150 sub add_depends ($$) { #{{{
151         my $page=shift;
152         my $pagespec=shift;
153         
154         if (! exists $depends{$page}) {
155                 $depends{$page}=$pagespec;
156         }
157         else {
158                 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
159         }
160 } # }}}
161
162 sub genpage ($$$) { #{{{
163         my $page=shift;
164         my $content=shift;
165         my $mtime=shift;
166
167         my $template=template("page.tmpl", blind_cache => 1);
168         my $actions=0;
169
170         if (length $config{cgiurl}) {
171                 $template->param(editurl => cgiurl(do => "edit", page => $page));
172                 $template->param(prefsurl => cgiurl(do => "prefs"));
173                 if ($config{rcs}) {
174                         $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
175                 }
176                 $actions++;
177         }
178
179         if (length $config{historyurl}) {
180                 my $u=$config{historyurl};
181                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
182                 $template->param(historyurl => $u);
183                 $actions++;
184         }
185         if ($config{discussion}) {
186                 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
187                 $actions++;
188         }
189
190         if ($actions) {
191                 $template->param(have_actions => 1);
192         }
193
194         $template->param(
195                 title => $page eq 'index' 
196                         ? $config{wikiname} 
197                         : pagetitle(basename($page)),
198                 wikiname => $config{wikiname},
199                 parentlinks => [parentlinks($page)],
200                 content => $content,
201                 backlinks => [backlinks($page)],
202                 mtime => displaytime($mtime),
203                 baseurl => baseurl($page),
204         );
205
206         run_hooks(pagetemplate => sub {
207                 shift->(page => $page, destpage => $page, template => $template);
208         });
209         
210         $content=$template->output;
211
212         run_hooks(format => sub {
213                 $content=shift->($content);
214         });
215
216         return $content;
217 } #}}}
218
219 sub check_overwrite ($$) { #{{{
220         # Important security check. Make sure to call this before saving
221         # any files to the source directory.
222         my $dest=shift;
223         my $src=shift;
224         
225         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
226                 error("$dest already exists and was not rendered from $src before");
227         }
228 } #}}}
229
230 sub displaytime ($) { #{{{
231         my $time=shift;
232
233         eval q{use POSIX};
234         # strftime doesn't know about encodings, so make sure
235         # its output is properly treated as utf8
236         return decode_utf8(POSIX::strftime(
237                         $config{timeformat}, localtime($time)));
238 } #}}}
239
240 sub mtime ($) { #{{{
241         my $file=shift;
242         
243         return (stat($file))[9];
244 } #}}}
245
246 sub findlinks ($$) { #{{{
247         my $page=shift;
248         my $content=shift;
249
250         my @links;
251         while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
252                 push @links, titlepage($2);
253         }
254         if ($config{discussion}) {
255                 # Discussion links are a special case since they're not in the
256                 # text of the page, but on its template.
257                 return @links, "$page/discussion";
258         }
259         else {
260                 return @links;
261         }
262 } #}}}
263
264 sub filter ($$) {
265         my $page=shift;
266         my $content=shift;
267
268         run_hooks(filter => sub {
269                 $content=shift->(page => $page, content => $content);
270         });
271
272         return $content;
273 }
274
275 sub render ($) { #{{{
276         my $file=shift;
277         
278         my $type=pagetype($file);
279         my $srcfile=srcfile($file);
280         if (defined $type) {
281                 my $content=readfile($srcfile);
282                 my $page=pagename($file);
283                 delete $depends{$page};
284                 
285                 $content=filter($page, $content);
286                 
287                 $links{$page}=[findlinks($page, $content)];
288                 
289                 $content=preprocess($page, $page, $content);
290                 $content=linkify($page, $page, $content);
291                 $content=htmlize($type, $content);
292                 
293                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
294                 writefile(htmlpage($page), $config{destdir},
295                         genpage($page, $content, mtime($srcfile)));
296                 $oldpagemtime{$page}=time;
297                 $renderedfiles{$page}=htmlpage($page);
298         }
299         else {
300                 my $content=readfile($srcfile, 1);
301                 $links{$file}=[];
302                 delete $depends{$file};
303                 check_overwrite("$config{destdir}/$file", $file);
304                 writefile($file, $config{destdir}, $content, 1);
305                 $oldpagemtime{$file}=time;
306                 $renderedfiles{$file}=$file;
307         }
308 } #}}}
309
310 sub prune ($) { #{{{
311         my $file=shift;
312
313         unlink($file);
314         my $dir=dirname($file);
315         while (rmdir($dir)) {
316                 $dir=dirname($dir);
317         }
318 } #}}}
319
320 sub refresh () { #{{{
321         # find existing pages
322         my %exists;
323         my @files;
324         eval q{use File::Find};
325         find({
326                 no_chdir => 1,
327                 wanted => sub {
328                         $_=decode_utf8($_);
329                         if (/$config{wiki_file_prune_regexp}/) {
330                                 $File::Find::prune=1;
331                         }
332                         elsif (! -d $_ && ! -l $_) {
333                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
334                                 if (! defined $f) {
335                                         warn("skipping bad filename $_\n");
336                                 }
337                                 else {
338                                         $f=~s/^\Q$config{srcdir}\E\/?//;
339                                         push @files, $f;
340                                         $exists{pagename($f)}=1;
341                                 }
342                         }
343                 },
344         }, $config{srcdir});
345         find({
346                 no_chdir => 1,
347                 wanted => sub {
348                         $_=decode_utf8($_);
349                         if (/$config{wiki_file_prune_regexp}/) {
350                                 $File::Find::prune=1;
351                         }
352                         elsif (! -d $_ && ! -l $_) {
353                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
354                                 if (! defined $f) {
355                                         warn("skipping bad filename $_\n");
356                                 }
357                                 else {
358                                         # Don't add files that are in the
359                                         # srcdir.
360                                         $f=~s/^\Q$config{underlaydir}\E\/?//;
361                                         if (! -e "$config{srcdir}/$f" && 
362                                             ! -l "$config{srcdir}/$f") {
363                                                 push @files, $f;
364                                                 $exists{pagename($f)}=1;
365                                         }
366                                 }
367                         }
368                 },
369         }, $config{underlaydir});
370
371         my %rendered;
372
373         # check for added or removed pages
374         my @add;
375         foreach my $file (@files) {
376                 my $page=pagename($file);
377                 if (! $oldpagemtime{$page}) {
378                         debug("new page $page") unless exists $pagectime{$page};
379                         push @add, $file;
380                         $links{$page}=[];
381                         $pagecase{lc $page}=$page;
382                         $pagesources{$page}=$file;
383                         if ($config{getctime} && -e "$config{srcdir}/$file") {
384                                 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
385                         }
386                         elsif (! exists $pagectime{$page}) {
387                                 $pagectime{$page}=mtime(srcfile($file));
388                         }
389                 }
390         }
391         my @del;
392         foreach my $page (keys %oldpagemtime) {
393                 if (! $exists{$page}) {
394                         debug("removing old page $page");
395                         push @del, $pagesources{$page};
396                         prune($config{destdir}."/".$renderedfiles{$page});
397                         delete $renderedfiles{$page};
398                         $oldpagemtime{$page}=0;
399                         delete $pagesources{$page};
400                 }
401         }
402         
403         # render any updated files
404         foreach my $file (@files) {
405                 my $page=pagename($file);
406                 
407                 if (! exists $oldpagemtime{$page} ||
408                     mtime(srcfile($file)) > $oldpagemtime{$page} ||
409                     $forcerebuild{$page}) {
410                         debug("rendering $file");
411                         render($file);
412                         $rendered{$file}=1;
413                 }
414         }
415         
416         # if any files were added or removed, check to see if each page
417         # needs an update due to linking to them or inlining them.
418         # TODO: inefficient; pages may get rendered above and again here;
419         # problem is the bestlink may have changed and we won't know until
420         # now
421         if (@add || @del) {
422 FILE:           foreach my $file (@files) {
423                         my $page=pagename($file);
424                         foreach my $f (@add, @del) {
425                                 my $p=pagename($f);
426                                 foreach my $link (@{$links{$page}}) {
427                                         if (bestlink($page, $link) eq $p) {
428                                                 debug("rendering $file, which links to $p");
429                                                 render($file);
430                                                 $rendered{$file}=1;
431                                                 next FILE;
432                                         }
433                                 }
434                         }
435                 }
436         }
437
438         # Handle backlinks; if a page has added/removed links, update the
439         # pages it links to. Also handles rebuilding dependant pages.
440         # TODO: inefficient; pages may get rendered above and again here;
441         # problem is the backlinks could be wrong in the first pass render
442         # above
443         if (%rendered || @del) {
444                 foreach my $f (@files) {
445                         my $p=pagename($f);
446                         if (exists $depends{$p}) {
447                                 foreach my $file (keys %rendered, @del) {
448                                         next if $f eq $file;
449                                         my $page=pagename($file);
450                                         if (pagespec_match($page, $depends{$p})) {
451                                                 debug("rendering $f, which depends on $page");
452                                                 render($f);
453                                                 $rendered{$f}=1;
454                                                 last;
455                                         }
456                                 }
457                         }
458                 }
459                 
460                 my %linkchanged;
461                 foreach my $file (keys %rendered, @del) {
462                         my $page=pagename($file);
463                         
464                         if (exists $links{$page}) {
465                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
466                                         if (length $link &&
467                                             (! exists $oldlinks{$page} ||
468                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
469                                                 $linkchanged{$link}=1;
470                                         }
471                                 }
472                         }
473                         if (exists $oldlinks{$page}) {
474                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
475                                         if (length $link &&
476                                             (! exists $links{$page} || 
477                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
478                                                 $linkchanged{$link}=1;
479                                         }
480                                 }
481                         }
482                 }
483                 foreach my $link (keys %linkchanged) {
484                         my $linkfile=$pagesources{$link};
485                         if (defined $linkfile) {
486                                 debug("rendering $linkfile, to update its backlinks");
487                                 render($linkfile);
488                                 $rendered{$linkfile}=1;
489                         }
490                 }
491         }
492
493         if (@del) {
494                 run_hooks(delete => sub { shift->(@del) });
495         }
496         if (%rendered) {
497                 run_hooks(change => sub { shift->(keys %rendered) });
498         }
499 } #}}}
500
501 1