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