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