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