10 sub linkify ($$$) { #{{{
11 my $lpage=shift; # the page containing the links
12 my $page=shift; # the page the link will end up on (different for inline)
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)))
23 sub htmlize ($$) { #{{{
27 if (exists $hooks{htmlize}{$type}) {
28 $content=$hooks{htmlize}{$type}{call}->($content);
31 error("htmlization of $type not supported");
34 run_hooks(sanitize => sub {
35 $content=shift->($content);
41 sub backlinks ($) { #{{{
45 foreach my $p (keys %links) {
46 next if bestlink($page, $p) eq $page;
47 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
48 my $href=abs2rel(htmlpage($p), dirname($page));
50 # Trim common dir prefixes from both pages.
52 my $page_trimmed=$page;
54 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
56 $p_trimmed=~s/^\Q$dir\E// &&
57 $page_trimmed=~s/^\Q$dir\E//;
59 push @links, { url => $href, page => pagetitle($p_trimmed) };
63 return sort { $a->{page} cmp $b->{page} } @links;
66 sub parentlinks ($) { #{{{
73 return if $page eq 'index'; # toplevel
74 foreach my $dir (reverse split("/", $page)) {
77 unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
83 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
88 sub preprocess ($$$) { #{{{
89 my $page=shift; # the page the data comes from
90 my $destpage=shift; # the page the data will appear in (different for inline)
98 return "[[$command $params]]";
100 elsif ($preprocessing{$page}) {
101 # Avoid loops of preprocessed pages preprocessing
102 # other pages that preprocess them, etc.
103 return "[[$command would cause preprocessing loop]]";
105 elsif (exists $hooks{preprocess}{$command}) {
106 # Note: preserve order of params, some plugins may
107 # consider it significant.
109 while ($params =~ /(?:(\w+)=)?(?:"""(.*?)"""|"([^"]+)"|(\S+))(?:\s+|$)/sg) {
126 push @params, $key, $val;
129 push @params, $val, '';
132 $preprocessing{$page}=1;
133 my $ret=$hooks{preprocess}{$command}{call}->(
136 destpage => $destpage,
138 delete $preprocessing{$page};
142 return "[[$command not processed]]";
146 $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
150 sub add_depends ($$) { #{{{
154 if (! exists $depends{$page}) {
155 $depends{$page}=$pagespec;
158 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
162 sub genpage ($$$) { #{{{
167 my $template=template("page.tmpl", blind_cache => 1);
170 if (length $config{cgiurl}) {
171 $template->param(editurl => cgiurl(do => "edit", page => $page));
172 $template->param(prefsurl => cgiurl(do => "prefs"));
174 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
179 if (length $config{historyurl}) {
180 my $u=$config{historyurl};
181 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
182 $template->param(historyurl => $u);
185 if ($config{discussion}) {
186 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
191 $template->param(have_actions => 1);
195 title => $page eq 'index'
197 : pagetitle(basename($page)),
198 wikiname => $config{wikiname},
199 parentlinks => [parentlinks($page)],
201 backlinks => [backlinks($page)],
202 mtime => displaytime($mtime),
203 baseurl => baseurl($page),
206 run_hooks(pagetemplate => sub {
207 shift->(page => $page, destpage => $page, template => $template);
210 $content=$template->output;
212 run_hooks(format => sub {
213 $content=shift->($content);
219 sub check_overwrite ($$) { #{{{
220 # Important security check. Make sure to call this before saving
221 # any files to the source directory.
225 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
226 error("$dest already exists and was not rendered from $src before");
230 sub displaytime ($) { #{{{
234 # strftime doesn't know about encodings, so make sure
235 # its output is properly treated as utf8
236 return decode_utf8(POSIX::strftime(
237 $config{timeformat}, localtime($time)));
243 return (stat($file))[9];
246 sub findlinks ($$) { #{{{
251 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
252 push @links, titlepage($2);
254 if ($config{discussion}) {
255 # Discussion links are a special case since they're not in the
256 # text of the page, but on its template.
257 return @links, "$page/discussion";
268 run_hooks(filter => sub {
269 $content=shift->(page => $page, content => $content);
275 sub render ($) { #{{{
278 my $type=pagetype($file);
279 my $srcfile=srcfile($file);
281 my $content=readfile($srcfile);
282 my $page=pagename($file);
283 delete $depends{$page};
285 $content=filter($page, $content);
287 $links{$page}=[findlinks($page, $content)];
289 $content=preprocess($page, $page, $content);
290 $content=linkify($page, $page, $content);
291 $content=htmlize($type, $content);
293 check_overwrite("$config{destdir}/".htmlpage($page), $page);
294 writefile(htmlpage($page), $config{destdir},
295 genpage($page, $content, mtime($srcfile)));
296 $oldpagemtime{$page}=time;
297 $renderedfiles{$page}=htmlpage($page);
300 my $content=readfile($srcfile, 1);
302 delete $depends{$file};
303 check_overwrite("$config{destdir}/$file", $file);
304 writefile($file, $config{destdir}, $content, 1);
305 $oldpagemtime{$file}=time;
306 $renderedfiles{$file}=$file;
314 my $dir=dirname($file);
315 while (rmdir($dir)) {
320 sub refresh () { #{{{
321 # find existing pages
324 eval q{use File::Find};
329 if (/$config{wiki_file_prune_regexp}/) {
330 $File::Find::prune=1;
332 elsif (! -d $_ && ! -l $_) {
333 my ($f)=/$config{wiki_file_regexp}/; # untaint
335 warn("skipping bad filename $_\n");
338 $f=~s/^\Q$config{srcdir}\E\/?//;
340 $exists{pagename($f)}=1;
349 if (/$config{wiki_file_prune_regexp}/) {
350 $File::Find::prune=1;
352 elsif (! -d $_ && ! -l $_) {
353 my ($f)=/$config{wiki_file_regexp}/; # untaint
355 warn("skipping bad filename $_\n");
358 # Don't add files that are in the
360 $f=~s/^\Q$config{underlaydir}\E\/?//;
361 if (! -e "$config{srcdir}/$f" &&
362 ! -l "$config{srcdir}/$f") {
364 $exists{pagename($f)}=1;
369 }, $config{underlaydir});
373 # check for added or removed pages
375 foreach my $file (@files) {
376 my $page=pagename($file);
377 if (! $oldpagemtime{$page}) {
378 debug("new page $page") unless exists $pagectime{$page};
381 $pagecase{lc $page}=$page;
382 $pagesources{$page}=$file;
383 if ($config{getctime} && -e "$config{srcdir}/$file") {
384 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
386 elsif (! exists $pagectime{$page}) {
387 $pagectime{$page}=mtime(srcfile($file));
392 foreach my $page (keys %oldpagemtime) {
393 if (! $exists{$page}) {
394 debug("removing old page $page");
395 push @del, $pagesources{$page};
396 prune($config{destdir}."/".$renderedfiles{$page});
397 delete $renderedfiles{$page};
398 $oldpagemtime{$page}=0;
399 delete $pagesources{$page};
403 # render any updated files
404 foreach my $file (@files) {
405 my $page=pagename($file);
407 if (! exists $oldpagemtime{$page} ||
408 mtime(srcfile($file)) > $oldpagemtime{$page} ||
409 $forcerebuild{$page}) {
410 debug("rendering $file");
416 # if any files were added or removed, check to see if each page
417 # needs an update due to linking to them or inlining them.
418 # TODO: inefficient; pages may get rendered above and again here;
419 # problem is the bestlink may have changed and we won't know until
422 FILE: foreach my $file (@files) {
423 my $page=pagename($file);
424 foreach my $f (@add, @del) {
426 foreach my $link (@{$links{$page}}) {
427 if (bestlink($page, $link) eq $p) {
428 debug("rendering $file, which links to $p");
438 # Handle backlinks; if a page has added/removed links, update the
439 # pages it links to. Also handles rebuilding dependant pages.
440 # TODO: inefficient; pages may get rendered above and again here;
441 # problem is the backlinks could be wrong in the first pass render
443 if (%rendered || @del) {
444 foreach my $f (@files) {
446 if (exists $depends{$p}) {
447 foreach my $file (keys %rendered, @del) {
449 my $page=pagename($file);
450 if (pagespec_match($page, $depends{$p})) {
451 debug("rendering $f, which depends on $page");
461 foreach my $file (keys %rendered, @del) {
462 my $page=pagename($file);
464 if (exists $links{$page}) {
465 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
467 (! exists $oldlinks{$page} ||
468 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
469 $linkchanged{$link}=1;
473 if (exists $oldlinks{$page}) {
474 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
476 (! exists $links{$page} ||
477 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
478 $linkchanged{$link}=1;
483 foreach my $link (keys %linkchanged) {
484 my $linkfile=$pagesources{$link};
485 if (defined $linkfile) {
486 debug("rendering $linkfile, to update its backlinks");
488 $rendered{$linkfile}=1;
494 run_hooks(delete => sub { shift->(@del) });
497 run_hooks(change => sub { shift->(keys %rendered) });