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