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