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