]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
My fix to support encoded underscores in page titles broke links to pages
[git.ikiwiki.info.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
3 package IkiWiki;
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use Encode;
10 my %backlinks;
11 my $backlinks_calculated=0;
13 sub calculate_backlinks () { #{{{
14         return if $backlinks_calculated;
15         %backlinks=();
16         foreach my $page (keys %links) {
17                 foreach my $link (@{$links{$page}}) {
18                         my $bestlink=bestlink($page, $link);
19                         if (length $bestlink && $bestlink ne $page) {
20                                 $backlinks{$bestlink}{$page}=1;
21                         }
22                 }
23         }
24         $backlinks_calculated=1;
25 } #}}}
27 sub backlinks ($) { #{{{
28         my $page=shift;
30         calculate_backlinks();
32         my @links;
33         return unless $backlinks{$page};
34         foreach my $p (keys %{$backlinks{$page}}) {
35                 my $href=abs2rel(htmlpage($p), dirname($page));
36                         
37                 # Trim common dir prefixes from both pages.
38                 my $p_trimmed=$p;
39                 my $page_trimmed=$page;
40                 my $dir;
41                 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
42                         defined $dir &&
43                         $p_trimmed=~s/^\Q$dir\E// &&
44                         $page_trimmed=~s/^\Q$dir\E//;
45                                
46                 push @links, { url => $href, page => pagetitle($p_trimmed) };
47         }
49         return sort { $a->{page} cmp $b->{page} } @links;
50 } #}}}
52 sub parentlinks ($) { #{{{
53         my $page=shift;
54         
55         my @ret;
56         my $pagelink="";
57         my $path="";
58         my $skip=1;
59         return if $page eq 'index'; # toplevel
60         foreach my $dir (reverse split("/", $page)) {
61                 if (! $skip) {
62                         $path.="../";
63                         unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
64                 }
65                 else {
66                         $skip=0;
67                 }
68         }
69         unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
70         return @ret;
71 } #}}}
73 sub genpage ($$$) { #{{{
74         my $page=shift;
75         my $content=shift;
76         my $mtime=shift;
78         my $template=template("page.tmpl", blind_cache => 1);
79         my $actions=0;
81         if (length $config{cgiurl}) {
82                 $template->param(editurl => cgiurl(do => "edit", page => $page));
83                 $template->param(prefsurl => cgiurl(do => "prefs"));
84                 if ($config{rcs}) {
85                         $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
86                 }
87                 $actions++;
88         }
90         if (length $config{historyurl}) {
91                 my $u=$config{historyurl};
92                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
93                 $template->param(historyurl => $u);
94                 $actions++;
95         }
96         if ($config{discussion}) {
97                 my $discussionlink=gettext("discussion");
98                 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
99                    (length $config{cgiurl} ||
100                     exists $links{$page."/".$discussionlink})) {
101                         $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
102                         $actions++;
103                 }
104         }
106         if ($actions) {
107                 $template->param(have_actions => 1);
108         }
110         $template->param(
111                 title => $page eq 'index' 
112                         ? $config{wikiname} 
113                         : pagetitle(basename($page)),
114                 wikiname => $config{wikiname},
115                 parentlinks => [parentlinks($page)],
116                 content => $content,
117                 backlinks => [backlinks($page)],
118                 mtime => displaytime($mtime),
119                 baseurl => baseurl($page),
120         );
122         run_hooks(pagetemplate => sub {
123                 shift->(page => $page, destpage => $page, template => $template);
124         });
125         
126         $content=$template->output;
128         run_hooks(format => sub {
129                 $content=shift->(
130                         page => $page,
131                         content => $content,
132                 );
133         });
135         return $content;
136 } #}}}
138 sub mtime ($) { #{{{
139         my $file=shift;
140         
141         return (stat($file))[9];
142 } #}}}
144 sub scan ($) { #{{{
145         my $file=shift;
147         my $type=pagetype($file);
148         if (defined $type) {
149                 my $srcfile=srcfile($file);
150                 my $content=readfile($srcfile);
151                 my $page=pagename($file);
152                 will_render($page, htmlpage($page), 1);
154                 # Always needs to be done, since filters might add links
155                 # to the content.
156                 $content=filter($page, $content);
158                 my @links;
159                 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
160                         push @links, linkpage($2);
161                 }
162                 if ($config{discussion}) {
163                         # Discussion links are a special case since they're
164                         # not in the text of the page, but on its template.
165                         push @links, $page."/".gettext("discussion");
166                 }
167                 $links{$page}=\@links;
168                 
169                 # Preprocess in scan-only mode.
170                 preprocess($page, $page, $content, 1);
171         }
172         else {
173                 will_render($file, $file, 1);
174         }
175 } #}}}
177 sub render ($) { #{{{
178         my $file=shift;
179         
180         my $type=pagetype($file);
181         my $srcfile=srcfile($file);
182         if (defined $type) {
183                 my $content=readfile($srcfile);
184                 my $page=pagename($file);
185                 delete $depends{$page};
186                 will_render($page, htmlpage($page), 1);
187                 
188                 $content=filter($page, $content);
189                 $content=preprocess($page, $page, $content);
190                 $content=linkify($page, $page, $content);
191                 $content=htmlize($page, $type, $content);
192                 
193                 writefile(htmlpage($page), $config{destdir},
194                         genpage($page, $content, mtime($srcfile)));
195                 $oldpagemtime{$page}=time;
196         }
197         else {
198                 my $srcfd=readfile($srcfile, 1, 1);
199                 delete $depends{$file};
200                 will_render($file, $file, 1);
201                 writefile($file, $config{destdir}, undef, 1, sub {
202                         my $destfd=shift;
203                         my $cleanup=shift;
205                         my $blksize = 16384;
206                         my ($len, $buf, $written);
207                         while ($len = sysread $srcfd, $buf, $blksize) {
208                                 if (! defined $len) {
209                                         next if $! =~ /^Interrupted/;
210                                         error("failed to read $srcfile: $!", $cleanup);
211                                 }
212                                 my $offset = 0;
213                                 while ($len) {
214                                         defined($written = syswrite $destfd, $buf, $len, $offset)
215                                                 or error("failed to write $file: $!", $cleanup);
216                                         $len -= $written;
217                                         $offset += $written;
218                                 }
219                         }
220                 });
221                 $oldpagemtime{$file}=time;
222         }
223 } #}}}
225 sub prune ($) { #{{{
226         my $file=shift;
228         unlink($file);
229         my $dir=dirname($file);
230         while (rmdir($dir)) {
231                 $dir=dirname($dir);
232         }
233 } #}}}
235 sub refresh () { #{{{
236         # find existing pages
237         my %exists;
238         my @files;
239         eval q{use File::Find};
240         error($@) if $@;
241         find({
242                 no_chdir => 1,
243                 wanted => sub {
244                         $_=decode_utf8($_);
245                         if (file_pruned($_, $config{srcdir})) {
246                                 $File::Find::prune=1;
247                         }
248                         elsif (! -d $_ && ! -l $_) {
249                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
250                                 if (! defined $f) {
251                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
252                                 }
253                                 else {
254                                         $f=~s/^\Q$config{srcdir}\E\/?//;
255                                         push @files, $f;
256                                         $exists{pagename($f)}=1;
257                                 }
258                         }
259                 },
260         }, $config{srcdir});
261         find({
262                 no_chdir => 1,
263                 wanted => sub {
264                         $_=decode_utf8($_);
265                         if (file_pruned($_, $config{underlaydir})) {
266                                 $File::Find::prune=1;
267                         }
268                         elsif (! -d $_ && ! -l $_) {
269                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
270                                 if (! defined $f) {
271                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
272                                 }
273                                 else {
274                                         # Don't add pages that are in the
275                                         # srcdir.
276                                         $f=~s/^\Q$config{underlaydir}\E\/?//;
277                                         if (! -e "$config{srcdir}/$f" && 
278                                             ! -l "$config{srcdir}/$f") {
279                                                 my $page=pagename($f);
280                                                 if (! $exists{$page}) {
281                                                         push @files, $f;
282                                                         $exists{$page}=1;
283                                                 }
284                                         }
285                                 }
286                         }
287                 },
288         }, $config{underlaydir});
290         my %rendered;
292         # check for added or removed pages
293         my @add;
294         foreach my $file (@files) {
295                 my $page=pagename($file);
296                 $pagesources{$page}=$file;
297                 if (! $oldpagemtime{$page}) {
298                         push @add, $file;
299                         $pagecase{lc $page}=$page;
300                         if ($config{getctime} && -e "$config{srcdir}/$file") {
301                                 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
302                         }
303                         elsif (! exists $pagectime{$page}) {
304                                 $pagectime{$page}=mtime(srcfile($file));
305                         }
306                 }
307         }
308         my @del;
309         foreach my $page (keys %oldpagemtime) {
310                 if (! $exists{$page}) {
311                         debug(sprintf(gettext("removing old page %s"), $page));
312                         push @del, $pagesources{$page};
313                         $links{$page}=[];
314                         $renderedfiles{$page}=[];
315                         $oldpagemtime{$page}=0;
316                         prune($config{destdir}."/".$_)
317                                 foreach @{$oldrenderedfiles{$page}};
318                         delete $pagesources{$page};
319                 }
320         }
322         # scan changed and new files
323         my @changed;
324         foreach my $file (@files) {
325                 my $page=pagename($file);
326                 
327                 if (! exists $oldpagemtime{$page} ||
328                     mtime(srcfile($file)) > $oldpagemtime{$page} ||
329                     $forcerebuild{$page}) {
330                         debug(sprintf(gettext("scanning %s"), $file));
331                         push @changed, $file;
332                         scan($file);
333                 }
334         }
335         calculate_backlinks();
337         # render changed and new pages
338         foreach my $file (@changed) {
339                 debug(sprintf(gettext("rendering %s"), $file));
340                 render($file);
341                 $rendered{$file}=1;
342         }
343         
344         # rebuild pages that link to added or removed pages
345         if (@add || @del) {
346                 foreach my $f (@add, @del) {
347                         my $p=pagename($f);
348                         foreach my $page (keys %{$backlinks{$p}}) {
349                                 my $file=$pagesources{$page};
350                                 next if $rendered{$file};
351                                 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
352                                 render($file);
353                                 $rendered{$file}=1;
354                         }
355                 }
356         }
358         if (%rendered || @del) {
359                 # rebuild dependant pages
360                 foreach my $f (@files) {
361                         next if $rendered{$f};
362                         my $p=pagename($f);
363                         if (exists $depends{$p}) {
364                                 foreach my $file (keys %rendered, @del) {
365                                         next if $f eq $file;
366                                         my $page=pagename($file);
367                                         if (pagespec_match($page, $depends{$p}, $p)) {
368                                                 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
369                                                 render($f);
370                                                 $rendered{$f}=1;
371                                                 last;
372                                         }
373                                 }
374                         }
375                 }
376                 
377                 # handle backlinks; if a page has added/removed links,
378                 # update the pages it links to
379                 my %linkchanged;
380                 foreach my $file (keys %rendered, @del) {
381                         my $page=pagename($file);
382                         
383                         if (exists $links{$page}) {
384                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
385                                         if (length $link &&
386                                             (! exists $oldlinks{$page} ||
387                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
388                                                 $linkchanged{$link}=1;
389                                         }
390                                 }
391                         }
392                         if (exists $oldlinks{$page}) {
393                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
394                                         if (length $link &&
395                                             (! exists $links{$page} || 
396                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
397                                                 $linkchanged{$link}=1;
398                                         }
399                                 }
400                         }
401                 }
402                 foreach my $link (keys %linkchanged) {
403                         my $linkfile=$pagesources{$link};
404                         if (defined $linkfile) {
405                                 next if $rendered{$linkfile};
406                                 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
407                                 render($linkfile);
408                                 $rendered{$linkfile}=1;
409                         }
410                 }
411         }
413         # remove no longer rendered files
414         foreach my $src (keys %rendered) {
415                 my $page=pagename($src);
416                 foreach my $file (@{$oldrenderedfiles{$page}}) {
417                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
418                                 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
419                                 prune($config{destdir}."/".$file);
420                         }
421                 }
422         }
424         if (@del) {
425                 run_hooks(delete => sub { shift->(@del) });
426         }
427         if (%rendered) {
428                 run_hooks(change => sub { shift->(keys %rendered) });
429         }
430 } #}}}
432 sub commandline_render () { #{{{
433         loadplugins();
434         checkconfig();
435         lockwiki();
436         loadindex();
437         unlockwiki();
439         my $srcfile=possibly_foolish_untaint($config{render});
440         my $file=$srcfile;
441         $file=~s/\Q$config{srcdir}\E\/?//;
443         my $type=pagetype($file);
444         die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
445         my $content=readfile($srcfile);
446         my $page=pagename($file);
447         $pagesources{$page}=$file;
448         $content=filter($page, $content);
449         $content=preprocess($page, $page, $content);
450         $content=linkify($page, $page, $content);
451         $content=htmlize($page, $type, $content);
453         print genpage($page, $content, mtime($srcfile));
454         exit 0;
455 } #}}}