]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Render.pm
* Patch from Faidon to use svn --limit when possible for recentchanges,
[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 linkify ($$$) { #{{{
11         my $lpage=shift;
12         my $page=shift;
13         my $content=shift;
14
15         $content =~ s{(\\?)$config{wiki_link_regexp}}{
16                 $2 ? ( $1 ? "[[$2|$3]]" : htmllink($lpage, $page, titlepage($3), 0, 0, pagetitle($2)))
17                    : ( $1 ? "[[$3]]" :    htmllink($lpage, $page, titlepage($3)))
18         }eg;
19         
20         return $content;
21 } #}}}
22
23 sub htmlize ($$) { #{{{
24         my $type=shift;
25         my $content=shift;
26         
27         if (exists $hooks{htmlize}{$type}) {
28                 $content=$hooks{htmlize}{$type}{call}->($content);
29         }
30         else {
31                 error("htmlization of $type not supported");
32         }
33
34         if (exists $hooks{sanitize}) {
35                 foreach my $id (keys %{$hooks{sanitize}}) {
36                         $content=$hooks{sanitize}{$id}{call}->($content);
37                 }
38         }
39         
40         return $content;
41 } #}}}
42
43 sub backlinks ($) { #{{{
44         my $page=shift;
45
46         my @links;
47         foreach my $p (keys %links) {
48                 next if bestlink($page, $p) eq $page;
49                 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
50                         my $href=abs2rel(htmlpage($p), dirname($page));
51                         
52                         # Trim common dir prefixes from both pages.
53                         my $p_trimmed=$p;
54                         my $page_trimmed=$page;
55                         my $dir;
56                         1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
57                                 defined $dir &&
58                                 $p_trimmed=~s/^\Q$dir\E// &&
59                                 $page_trimmed=~s/^\Q$dir\E//;
60                                        
61                         push @links, { url => $href, page => $p_trimmed };
62                 }
63         }
64
65         return sort { $a->{page} cmp $b->{page} } @links;
66 } #}}}
67
68 sub parentlinks ($) { #{{{
69         my $page=shift;
70         
71         my @ret;
72         my $pagelink="";
73         my $path="";
74         my $skip=1;
75         return if $page eq 'index'; # toplevel
76         foreach my $dir (reverse split("/", $page)) {
77                 if (! $skip) {
78                         $path.="../";
79                         unshift @ret, { url => "$path$dir.html", page => $dir };
80                 }
81                 else {
82                         $skip=0;
83                 }
84         }
85         unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
86         return @ret;
87 } #}}}
88
89 sub preprocess ($$;$) { #{{{
90         my $page=shift;
91         my $content=shift;
92         my $onlystrip=shift || 0; # strip directives without processing
93
94         my $handle=sub {
95                 my $escape=shift;
96                 my $command=shift;
97                 my $params=shift;
98                 if (length $escape) {
99                         return "[[$command $params]]";
100                 }
101                 elsif ($onlystrip) {
102                         return "";
103                 }
104                 elsif (exists $hooks{preprocess}{$command}) {
105                         # Note: preserve order of params, some plugins may
106                         # consider it significant.
107                         my @params;
108                         while ($params =~ /(\w+)=\"?([^"]+)"?(\s+|$)/g) {
109                                 push @params, $1, $2;
110                         }
111                         return $hooks{preprocess}{$command}{call}->(@params, page => $page);
112                 }
113                 else {
114                         return "[[$command not processed]]";
115                 }
116         };
117         
118         $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
119         return $content;
120 } #}}}
121
122 sub add_depends ($$) { #{{{
123         my $page=shift;
124         my $globlist=shift;
125         
126         if (! exists $depends{$page}) {
127                 $depends{$page}=$globlist;
128         }
129         else {
130                 $depends{$page}=globlist_merge($depends{$page}, $globlist);
131         }
132 } # }}}
133
134 sub globlist_merge ($$) { #{{{
135         my $a=shift;
136         my $b=shift;
137
138         my $ret="";
139         # Only add negated globs if they are not matched by the other globlist.
140         foreach my $i ((map { [ $a, $_ ] } split(" ", $b)), 
141                        (map { [ $b, $_ ] } split(" ", $a))) {
142                 if ($i->[1]=~/^!(.*)/) {
143                         if (! globlist_match($1, $i->[0])) {
144                                 $ret.=" ".$i->[1];
145                         }
146                 }
147                 else {
148                         $ret.=" ".$i->[1];
149                 }
150         }
151         
152         return $ret;
153 } #}}}
154
155 sub genpage ($$$) { #{{{
156         my $page=shift;
157         my $content=shift;
158         my $mtime=shift;
159
160         my $template=template("page.tmpl", blind_cache => 1);
161         my $actions=0;
162
163         if (length $config{cgiurl}) {
164                 $template->param(editurl => cgiurl(do => "edit", page => $page));
165                 $template->param(prefsurl => cgiurl(do => "prefs"));
166                 if ($config{rcs}) {
167                         $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
168                 }
169                 $actions++;
170         }
171
172         if (length $config{historyurl}) {
173                 my $u=$config{historyurl};
174                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
175                 $template->param(historyurl => $u);
176                 $actions++;
177         }
178         if ($config{discussion}) {
179                 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
180                 $actions++;
181         }
182
183         if ($actions) {
184                 $template->param(have_actions => 1);
185         }
186
187         $template->param(
188                 title => $page eq 'index' 
189                         ? $config{wikiname} 
190                         : pagetitle(basename($page)),
191                 wikiname => $config{wikiname},
192                 parentlinks => [parentlinks($page)],
193                 content => $content,
194                 backlinks => [backlinks($page)],
195                 mtime => displaytime($mtime),
196                 styleurl => styleurl($page),
197         );
198
199         if (exists $hooks{pagetemplate}) {
200                 foreach my $id (keys %{$hooks{pagetemplate}}) {
201                         $hooks{pagetemplate}{$id}{call}->($page, $template);
202                 }
203         }
204         
205         return $template->output;
206 } #}}}
207
208 sub check_overwrite ($$) { #{{{
209         # Important security check. Make sure to call this before saving
210         # any files to the source directory.
211         my $dest=shift;
212         my $src=shift;
213         
214         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
215                 error("$dest already exists and was not rendered from $src before");
216         }
217 } #}}}
218
219 sub displaytime ($) { #{{{
220         my $time=shift;
221
222         eval q{use POSIX};
223         # strftime doesn't know about encodings, so make sure
224         # its output is properly treated as utf8
225         return Encode::decode_utf8(POSIX::strftime(
226                         $config{timeformat}, localtime($time)));
227 } #}}}
228
229 sub mtime ($) { #{{{
230         my $file=shift;
231         
232         return (stat($file))[9];
233 } #}}}
234
235 sub findlinks ($$) { #{{{
236         my $page=shift;
237         my $content=shift;
238
239         my @links;
240         while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
241                 push @links, titlepage($2);
242         }
243         if ($config{discussion}) {
244                 # Discussion links are a special case since they're not in the
245                 # text of the page, but on its template.
246                 return @links, "$page/discussion";
247         }
248         else {
249                 return @links;
250         }
251 } #}}}
252
253 sub render ($) { #{{{
254         my $file=shift;
255         
256         my $type=pagetype($file);
257         my $srcfile=srcfile($file);
258         if (defined $type) {
259                 my $content=readfile($srcfile);
260                 my $page=pagename($file);
261                 delete $depends{$page};
262                 
263                 if (exists $hooks{filter}) {
264                         foreach my $id (keys %{$hooks{filter}}) {
265                                 $content=$hooks{filter}{$id}{call}->(
266                                         page => $page,
267                                         content => $content
268                                 );
269                         }
270                 }
271                 
272                 $links{$page}=[findlinks($page, $content)];
273                 
274                 $content=linkify($page, $page, $content);
275                 $content=preprocess($page, $content);
276                 $content=htmlize($type, $content);
277                 
278                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
279                 writefile(htmlpage($page), $config{destdir},
280                         genpage($page, $content, mtime($srcfile)));
281                 $oldpagemtime{$page}=time;
282                 $renderedfiles{$page}=htmlpage($page);
283         }
284         else {
285                 my $content=readfile($srcfile, 1);
286                 $links{$file}=[];
287                 delete $depends{$file};
288                 check_overwrite("$config{destdir}/$file", $file);
289                 writefile($file, $config{destdir}, $content, 1);
290                 $oldpagemtime{$file}=time;
291                 $renderedfiles{$file}=$file;
292         }
293 } #}}}
294
295 sub prune ($) { #{{{
296         my $file=shift;
297
298         unlink($file);
299         my $dir=dirname($file);
300         while (rmdir($dir)) {
301                 $dir=dirname($dir);
302         }
303 } #}}}
304
305 sub refresh () { #{{{
306         # find existing pages
307         my %exists;
308         my @files;
309         eval q{use File::Find};
310         find({
311                 no_chdir => 1,
312                 wanted => sub {
313                         $_ = Encode::decode_utf8($_);
314                         if (/$config{wiki_file_prune_regexp}/) {
315                                 $File::Find::prune=1;
316                         }
317                         elsif (! -d $_ && ! -l $_) {
318                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
319                                 if (! defined $f) {
320                                         warn("skipping bad filename $_\n");
321                                 }
322                                 else {
323                                         $f=~s/^\Q$config{srcdir}\E\/?//;
324                                         push @files, $f;
325                                         $exists{pagename($f)}=1;
326                                 }
327                         }
328                 },
329         }, $config{srcdir});
330         find({
331                 no_chdir => 1,
332                 wanted => sub {
333                         $_ = Encode::decode_utf8($_);
334                         if (/$config{wiki_file_prune_regexp}/) {
335                                 $File::Find::prune=1;
336                         }
337                         elsif (! -d $_ && ! -l $_) {
338                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
339                                 if (! defined $f) {
340                                         warn("skipping bad filename $_\n");
341                                 }
342                                 else {
343                                         # Don't add files that are in the
344                                         # srcdir.
345                                         $f=~s/^\Q$config{underlaydir}\E\/?//;
346                                         if (! -e "$config{srcdir}/$f" && 
347                                             ! -l "$config{srcdir}/$f") {
348                                                 push @files, $f;
349                                                 $exists{pagename($f)}=1;
350                                         }
351                                 }
352                         }
353                 },
354         }, $config{underlaydir});
355
356         my %rendered;
357
358         # check for added or removed pages
359         my @add;
360         foreach my $file (@files) {
361                 my $page=pagename($file);
362                 if (! $oldpagemtime{$page}) {
363                         debug("new page $page") unless exists $pagectime{$page};
364                         push @add, $file;
365                         $links{$page}=[];
366                         $pagesources{$page}=$file;
367                         if ($config{getctime} && -e "$config{srcdir}/$file") {
368                                 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
369                         }
370                         elsif (! exists $pagectime{$page}) {
371                                 $pagectime{$page}=mtime(srcfile($file));
372                         }
373                 }
374         }
375         my @del;
376         foreach my $page (keys %oldpagemtime) {
377                 if (! $exists{$page}) {
378                         debug("removing old page $page");
379                         push @del, $pagesources{$page};
380                         prune($config{destdir}."/".$renderedfiles{$page});
381                         delete $renderedfiles{$page};
382                         $oldpagemtime{$page}=0;
383                         delete $pagesources{$page};
384                 }
385         }
386         
387         # render any updated files
388         foreach my $file (@files) {
389                 my $page=pagename($file);
390                 
391                 if (! exists $oldpagemtime{$page} ||
392                     mtime(srcfile($file)) > $oldpagemtime{$page}) {
393                         debug("rendering $file");
394                         render($file);
395                         $rendered{$file}=1;
396                 }
397         }
398         
399         # if any files were added or removed, check to see if each page
400         # needs an update due to linking to them or inlining them.
401         # TODO: inefficient; pages may get rendered above and again here;
402         # problem is the bestlink may have changed and we won't know until
403         # now
404         if (@add || @del) {
405 FILE:           foreach my $file (@files) {
406                         my $page=pagename($file);
407                         foreach my $f (@add, @del) {
408                                 my $p=pagename($f);
409                                 foreach my $link (@{$links{$page}}) {
410                                         if (bestlink($page, $link) eq $p) {
411                                                 debug("rendering $file, which links to $p");
412                                                 render($file);
413                                                 $rendered{$file}=1;
414                                                 next FILE;
415                                         }
416                                 }
417                         }
418                 }
419         }
420
421         # Handle backlinks; if a page has added/removed links, update the
422         # pages it links to. Also handles rebuilding dependant pages.
423         # TODO: inefficient; pages may get rendered above and again here;
424         # problem is the backlinks could be wrong in the first pass render
425         # above
426         if (%rendered || @del) {
427                 foreach my $f (@files) {
428                         my $p=pagename($f);
429                         if (exists $depends{$p}) {
430                                 foreach my $file (keys %rendered, @del) {
431                                         next if $f eq $file;
432                                         my $page=pagename($file);
433                                         if (globlist_match($page, $depends{$p})) {
434                                                 debug("rendering $f, which depends on $page");
435                                                 render($f);
436                                                 $rendered{$f}=1;
437                                                 last;
438                                         }
439                                 }
440                         }
441                 }
442                 
443                 my %linkchanged;
444                 foreach my $file (keys %rendered, @del) {
445                         my $page=pagename($file);
446                         
447                         if (exists $links{$page}) {
448                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
449                                         if (length $link &&
450                                             (! exists $oldlinks{$page} ||
451                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
452                                                 $linkchanged{$link}=1;
453                                         }
454                                 }
455                         }
456                         if (exists $oldlinks{$page}) {
457                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
458                                         if (length $link &&
459                                             (! exists $links{$page} || 
460                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
461                                                 $linkchanged{$link}=1;
462                                         }
463                                 }
464                         }
465                 }
466                 foreach my $link (keys %linkchanged) {
467                         my $linkfile=$pagesources{$link};
468                         if (defined $linkfile) {
469                                 debug("rendering $linkfile, to update its backlinks");
470                                 render($linkfile);
471                                 $rendered{$linkfile}=1;
472                         }
473                 }
474         }
475
476         if (@del && exists $hooks{delete}) {
477                 foreach my $id (keys %{$hooks{delete}}) {
478                         $hooks{delete}{$id}{call}->(@del);
479                 }
480         }
481         if (%rendered && exists $hooks{change}) {
482                 foreach my $id (keys %{$hooks{change}}) {
483                         $hooks{change}{$id}{call}->(keys %rendered);
484                 }
485         }
486 } #}}}
487
488 1