use htmlpage a couple of places instead of hardcoding the extension
[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 sub preprocess ($$$;$) { #{{{
88         my $page=shift; # the page the data comes from
89         my $destpage=shift; # the page the data will appear in (different for inline)
90         my $content=shift;
91         my $onlystrip=shift || 0; # strip directives without processing
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 ($onlystrip) {
101                         return "";
102                 }
103                 elsif (exists $hooks{preprocess}{$command}) {
104                         # Note: preserve order of params, some plugins may
105                         # consider it significant.
106                         my @params;
107                         while ($params =~ /(?:(\w+)=)?(?:"([^"]+)"|(\S+))(?:\s+|$)/g) {
108                                 if (defined $1) {
109                                         push @params, $1, (defined $2 ? $2 : $3);
110                                 }
111                                 else {
112                                         push @params, (defined $2 ? $2 : $3), '';
113                                 }
114                         }
115                         return $hooks{preprocess}{$command}{call}->(
116                                 @params,
117                                 page => $page,
118                                 destpage => $destpage,
119                         );
120                 }
121                 else {
122                         return "[[$command not processed]]";
123                 }
124         };
125         
126         $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
127         return $content;
128 } #}}}
129
130 sub add_depends ($$) { #{{{
131         my $page=shift;
132         my $pagespec=shift;
133         
134         if (! exists $depends{$page}) {
135                 $depends{$page}=$pagespec;
136         }
137         else {
138                 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
139         }
140 } # }}}
141
142 sub genpage ($$$) { #{{{
143         my $page=shift;
144         my $content=shift;
145         my $mtime=shift;
146
147         my $template=template("page.tmpl", blind_cache => 1);
148         my $actions=0;
149
150         if (length $config{cgiurl}) {
151                 $template->param(editurl => cgiurl(do => "edit", page => $page));
152                 $template->param(prefsurl => cgiurl(do => "prefs"));
153                 if ($config{rcs}) {
154                         $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
155                 }
156                 $actions++;
157         }
158
159         if (length $config{historyurl}) {
160                 my $u=$config{historyurl};
161                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
162                 $template->param(historyurl => $u);
163                 $actions++;
164         }
165         if ($config{discussion}) {
166                 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
167                 $actions++;
168         }
169
170         if ($actions) {
171                 $template->param(have_actions => 1);
172         }
173
174         $template->param(
175                 title => $page eq 'index' 
176                         ? $config{wikiname} 
177                         : pagetitle(basename($page)),
178                 wikiname => $config{wikiname},
179                 parentlinks => [parentlinks($page)],
180                 content => $content,
181                 backlinks => [backlinks($page)],
182                 mtime => displaytime($mtime),
183                 styleurl => styleurl($page),
184         );
185
186         run_hooks(pagetemplate => sub {
187                 shift->(page => $page, destpage => $page, template => $template);
188         });
189         
190         return $template->output;
191 } #}}}
192
193 sub check_overwrite ($$) { #{{{
194         # Important security check. Make sure to call this before saving
195         # any files to the source directory.
196         my $dest=shift;
197         my $src=shift;
198         
199         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
200                 error("$dest already exists and was not rendered from $src before");
201         }
202 } #}}}
203
204 sub displaytime ($) { #{{{
205         my $time=shift;
206
207         eval q{use POSIX};
208         # strftime doesn't know about encodings, so make sure
209         # its output is properly treated as utf8
210         return decode_utf8(POSIX::strftime(
211                         $config{timeformat}, localtime($time)));
212 } #}}}
213
214 sub mtime ($) { #{{{
215         my $file=shift;
216         
217         return (stat($file))[9];
218 } #}}}
219
220 sub findlinks ($$) { #{{{
221         my $page=shift;
222         my $content=shift;
223
224         my @links;
225         while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
226                 push @links, titlepage($2);
227         }
228         if ($config{discussion}) {
229                 # Discussion links are a special case since they're not in the
230                 # text of the page, but on its template.
231                 return @links, "$page/discussion";
232         }
233         else {
234                 return @links;
235         }
236 } #}}}
237
238 sub filter ($$) {
239         my $page=shift;
240         my $content=shift;
241
242         run_hooks(filter => sub {
243                 $content=shift->(page => $page, content => $content);
244         });
245
246         return $content;
247 }
248
249 sub render ($) { #{{{
250         my $file=shift;
251         
252         my $type=pagetype($file);
253         my $srcfile=srcfile($file);
254         if (defined $type) {
255                 my $content=readfile($srcfile);
256                 my $page=pagename($file);
257                 delete $depends{$page};
258                 
259                 $content=filter($page, $content);
260                 
261                 $links{$page}=[findlinks($page, $content)];
262                 
263                 $content=linkify($page, $page, $content);
264                 $content=preprocess($page, $page, $content);
265                 $content=htmlize($type, $content);
266                 
267                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
268                 writefile(htmlpage($page), $config{destdir},
269                         genpage($page, $content, mtime($srcfile)));
270                 $oldpagemtime{$page}=time;
271                 $renderedfiles{$page}=htmlpage($page);
272         }
273         else {
274                 my $content=readfile($srcfile, 1);
275                 $links{$file}=[];
276                 delete $depends{$file};
277                 check_overwrite("$config{destdir}/$file", $file);
278                 writefile($file, $config{destdir}, $content, 1);
279                 $oldpagemtime{$file}=time;
280                 $renderedfiles{$file}=$file;
281         }
282 } #}}}
283
284 sub prune ($) { #{{{
285         my $file=shift;
286
287         unlink($file);
288         my $dir=dirname($file);
289         while (rmdir($dir)) {
290                 $dir=dirname($dir);
291         }
292 } #}}}
293
294 sub refresh () { #{{{
295         # find existing pages
296         my %exists;
297         my @files;
298         eval q{use File::Find};
299         find({
300                 no_chdir => 1,
301                 wanted => sub {
302                         $_=decode_utf8($_);
303                         if (/$config{wiki_file_prune_regexp}/) {
304                                 $File::Find::prune=1;
305                         }
306                         elsif (! -d $_ && ! -l $_) {
307                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
308                                 if (! defined $f) {
309                                         warn("skipping bad filename $_\n");
310                                 }
311                                 else {
312                                         $f=~s/^\Q$config{srcdir}\E\/?//;
313                                         push @files, $f;
314                                         $exists{pagename($f)}=1;
315                                 }
316                         }
317                 },
318         }, $config{srcdir});
319         find({
320                 no_chdir => 1,
321                 wanted => sub {
322                         $_=decode_utf8($_);
323                         if (/$config{wiki_file_prune_regexp}/) {
324                                 $File::Find::prune=1;
325                         }
326                         elsif (! -d $_ && ! -l $_) {
327                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
328                                 if (! defined $f) {
329                                         warn("skipping bad filename $_\n");
330                                 }
331                                 else {
332                                         # Don't add files that are in the
333                                         # srcdir.
334                                         $f=~s/^\Q$config{underlaydir}\E\/?//;
335                                         if (! -e "$config{srcdir}/$f" && 
336                                             ! -l "$config{srcdir}/$f") {
337                                                 push @files, $f;
338                                                 $exists{pagename($f)}=1;
339                                         }
340                                 }
341                         }
342                 },
343         }, $config{underlaydir});
344
345         my %rendered;
346
347         # check for added or removed pages
348         my @add;
349         foreach my $file (@files) {
350                 my $page=pagename($file);
351                 if (! $oldpagemtime{$page}) {
352                         debug("new page $page") unless exists $pagectime{$page};
353                         push @add, $file;
354                         $links{$page}=[];
355                         $pagesources{$page}=$file;
356                         if ($config{getctime} && -e "$config{srcdir}/$file") {
357                                 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
358                         }
359                         elsif (! exists $pagectime{$page}) {
360                                 $pagectime{$page}=mtime(srcfile($file));
361                         }
362                 }
363         }
364         my @del;
365         foreach my $page (keys %oldpagemtime) {
366                 if (! $exists{$page}) {
367                         debug("removing old page $page");
368                         push @del, $pagesources{$page};
369                         prune($config{destdir}."/".$renderedfiles{$page});
370                         delete $renderedfiles{$page};
371                         $oldpagemtime{$page}=0;
372                         delete $pagesources{$page};
373                 }
374         }
375         
376         # render any updated files
377         foreach my $file (@files) {
378                 my $page=pagename($file);
379                 
380                 if (! exists $oldpagemtime{$page} ||
381                     mtime(srcfile($file)) > $oldpagemtime{$page} ||
382                     $forcerebuild{$page}) {
383                         debug("rendering $file");
384                         render($file);
385                         $rendered{$file}=1;
386                 }
387         }
388         
389         # if any files were added or removed, check to see if each page
390         # needs an update due to linking to them or inlining them.
391         # TODO: inefficient; pages may get rendered above and again here;
392         # problem is the bestlink may have changed and we won't know until
393         # now
394         if (@add || @del) {
395 FILE:           foreach my $file (@files) {
396                         my $page=pagename($file);
397                         foreach my $f (@add, @del) {
398                                 my $p=pagename($f);
399                                 foreach my $link (@{$links{$page}}) {
400                                         if (bestlink($page, $link) eq $p) {
401                                                 debug("rendering $file, which links to $p");
402                                                 render($file);
403                                                 $rendered{$file}=1;
404                                                 next FILE;
405                                         }
406                                 }
407                         }
408                 }
409         }
410
411         # Handle backlinks; if a page has added/removed links, update the
412         # pages it links to. Also handles rebuilding dependant pages.
413         # TODO: inefficient; pages may get rendered above and again here;
414         # problem is the backlinks could be wrong in the first pass render
415         # above
416         if (%rendered || @del) {
417                 foreach my $f (@files) {
418                         my $p=pagename($f);
419                         if (exists $depends{$p}) {
420                                 foreach my $file (keys %rendered, @del) {
421                                         next if $f eq $file;
422                                         my $page=pagename($file);
423                                         if (pagespec_match($page, $depends{$p})) {
424                                                 debug("rendering $f, which depends on $page");
425                                                 render($f);
426                                                 $rendered{$f}=1;
427                                                 last;
428                                         }
429                                 }
430                         }
431                 }
432                 
433                 my %linkchanged;
434                 foreach my $file (keys %rendered, @del) {
435                         my $page=pagename($file);
436                         
437                         if (exists $links{$page}) {
438                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
439                                         if (length $link &&
440                                             (! exists $oldlinks{$page} ||
441                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
442                                                 $linkchanged{$link}=1;
443                                         }
444                                 }
445                         }
446                         if (exists $oldlinks{$page}) {
447                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
448                                         if (length $link &&
449                                             (! exists $links{$page} || 
450                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
451                                                 $linkchanged{$link}=1;
452                                         }
453                                 }
454                         }
455                 }
456                 foreach my $link (keys %linkchanged) {
457                         my $linkfile=$pagesources{$link};
458                         if (defined $linkfile) {
459                                 debug("rendering $linkfile, to update its backlinks");
460                                 render($linkfile);
461                                 $rendered{$linkfile}=1;
462                         }
463                 }
464         }
465
466         if (@del) {
467                 run_hooks(delete => sub { shift->(@del) });
468         }
469         if (%rendered) {
470                 run_hooks(change => sub { shift->(keys %rendered) });
471         }
472 } #}}}
473
474 1