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$dir.html", page => pagetitle($dir) };
83 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
87 sub preprocess ($$$;$) { #{{{
88 my $page=shift; # the page the data comes from
89 my $destpage=shift; # the page the data will appear in (different for inline)
91 my $onlystrip=shift || 0; # strip directives without processing
98 return "[[$command $params]]";
103 elsif (exists $hooks{preprocess}{$command}) {
104 # Note: preserve order of params, some plugins may
105 # consider it significant.
107 while ($params =~ /(?:(\w+)=)?(?:"([^"]+)"|(\S+))(?:\s+|$)/g) {
109 push @params, $1, (defined $2 ? $2 : $3);
112 push @params, (defined $2 ? $2 : $3), '';
115 return $hooks{preprocess}{$command}{call}->(
118 destpage => $destpage,
122 return "[[$command not processed]]";
126 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
130 sub add_depends ($$) { #{{{
134 if (! exists $depends{$page}) {
135 $depends{$page}=$globlist;
138 $depends{$page}=globlist_merge($depends{$page}, $globlist);
142 sub globlist_merge ($$) { #{{{
147 # Only add negated globs if they are not matched by the other globlist.
148 foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
149 (map { [ $b, $_ ] } split(" ", $a))) {
150 if ($i->[1]=~/^!(.*)/) {
151 if (! globlist_match($1, $i->[0])) {
163 sub genpage ($$$) { #{{{
168 my $template=template("page.tmpl", blind_cache => 1);
171 if (length $config{cgiurl}) {
172 $template->param(editurl => cgiurl(do => "edit", page => $page));
173 $template->param(prefsurl => cgiurl(do => "prefs"));
175 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
180 if (length $config{historyurl}) {
181 my $u=$config{historyurl};
182 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
183 $template->param(historyurl => $u);
186 if ($config{discussion}) {
187 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
192 $template->param(have_actions => 1);
196 title => $page eq 'index'
198 : pagetitle(basename($page)),
199 wikiname => $config{wikiname},
200 parentlinks => [parentlinks($page)],
202 backlinks => [backlinks($page)],
203 mtime => displaytime($mtime),
204 styleurl => styleurl($page),
207 run_hooks(pagetemplate => sub {
208 shift->(page => $page, destpage => $page, template => $template);
211 return $template->output;
214 sub check_overwrite ($$) { #{{{
215 # Important security check. Make sure to call this before saving
216 # any files to the source directory.
220 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
221 error("$dest already exists and was not rendered from $src before");
225 sub displaytime ($) { #{{{
229 # strftime doesn't know about encodings, so make sure
230 # its output is properly treated as utf8
231 return decode_utf8(POSIX::strftime(
232 $config{timeformat}, localtime($time)));
238 return (stat($file))[9];
241 sub findlinks ($$) { #{{{
246 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
247 push @links, titlepage($2);
249 if ($config{discussion}) {
250 # Discussion links are a special case since they're not in the
251 # text of the page, but on its template.
252 return @links, "$page/discussion";
263 run_hooks(filter => sub {
264 $content=shift->(page => $page, content => $content);
270 sub render ($) { #{{{
273 my $type=pagetype($file);
274 my $srcfile=srcfile($file);
276 my $content=readfile($srcfile);
277 my $page=pagename($file);
278 delete $depends{$page};
280 $content=filter($page, $content);
282 $links{$page}=[findlinks($page, $content)];
284 $content=linkify($page, $page, $content);
285 $content=preprocess($page, $page, $content);
286 $content=htmlize($type, $content);
288 check_overwrite("$config{destdir}/".htmlpage($page), $page);
289 writefile(htmlpage($page), $config{destdir},
290 genpage($page, $content, mtime($srcfile)));
291 $oldpagemtime{$page}=time;
292 $renderedfiles{$page}=htmlpage($page);
295 my $content=readfile($srcfile, 1);
297 delete $depends{$file};
298 check_overwrite("$config{destdir}/$file", $file);
299 writefile($file, $config{destdir}, $content, 1);
300 $oldpagemtime{$file}=time;
301 $renderedfiles{$file}=$file;
309 my $dir=dirname($file);
310 while (rmdir($dir)) {
315 sub refresh () { #{{{
316 # find existing pages
319 eval q{use File::Find};
324 if (/$config{wiki_file_prune_regexp}/) {
325 $File::Find::prune=1;
327 elsif (! -d $_ && ! -l $_) {
328 my ($f)=/$config{wiki_file_regexp}/; # untaint
330 warn("skipping bad filename $_\n");
333 $f=~s/^\Q$config{srcdir}\E\/?//;
335 $exists{pagename($f)}=1;
344 if (/$config{wiki_file_prune_regexp}/) {
345 $File::Find::prune=1;
347 elsif (! -d $_ && ! -l $_) {
348 my ($f)=/$config{wiki_file_regexp}/; # untaint
350 warn("skipping bad filename $_\n");
353 # Don't add files that are in the
355 $f=~s/^\Q$config{underlaydir}\E\/?//;
356 if (! -e "$config{srcdir}/$f" &&
357 ! -l "$config{srcdir}/$f") {
359 $exists{pagename($f)}=1;
364 }, $config{underlaydir});
368 # check for added or removed pages
370 foreach my $file (@files) {
371 my $page=pagename($file);
372 if (! $oldpagemtime{$page}) {
373 debug("new page $page") unless exists $pagectime{$page};
376 $pagesources{$page}=$file;
377 if ($config{getctime} && -e "$config{srcdir}/$file") {
378 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
380 elsif (! exists $pagectime{$page}) {
381 $pagectime{$page}=mtime(srcfile($file));
386 foreach my $page (keys %oldpagemtime) {
387 if (! $exists{$page}) {
388 debug("removing old page $page");
389 push @del, $pagesources{$page};
390 prune($config{destdir}."/".$renderedfiles{$page});
391 delete $renderedfiles{$page};
392 $oldpagemtime{$page}=0;
393 delete $pagesources{$page};
397 # render any updated files
398 foreach my $file (@files) {
399 my $page=pagename($file);
401 if (! exists $oldpagemtime{$page} ||
402 mtime(srcfile($file)) > $oldpagemtime{$page} ||
403 $forcerebuild{$page}) {
404 debug("rendering $file");
410 # if any files were added or removed, check to see if each page
411 # needs an update due to linking to them or inlining them.
412 # TODO: inefficient; pages may get rendered above and again here;
413 # problem is the bestlink may have changed and we won't know until
416 FILE: foreach my $file (@files) {
417 my $page=pagename($file);
418 foreach my $f (@add, @del) {
420 foreach my $link (@{$links{$page}}) {
421 if (bestlink($page, $link) eq $p) {
422 debug("rendering $file, which links to $p");
432 # Handle backlinks; if a page has added/removed links, update the
433 # pages it links to. Also handles rebuilding dependant pages.
434 # TODO: inefficient; pages may get rendered above and again here;
435 # problem is the backlinks could be wrong in the first pass render
437 if (%rendered || @del) {
438 foreach my $f (@files) {
440 if (exists $depends{$p}) {
441 foreach my $file (keys %rendered, @del) {
443 my $page=pagename($file);
444 if (globlist_match($page, $depends{$p})) {
445 debug("rendering $f, which depends on $page");
455 foreach my $file (keys %rendered, @del) {
456 my $page=pagename($file);
458 if (exists $links{$page}) {
459 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
461 (! exists $oldlinks{$page} ||
462 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
463 $linkchanged{$link}=1;
467 if (exists $oldlinks{$page}) {
468 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
470 (! exists $links{$page} ||
471 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
472 $linkchanged{$link}=1;
477 foreach my $link (keys %linkchanged) {
478 my $linkfile=$pagesources{$link};
479 if (defined $linkfile) {
480 debug("rendering $linkfile, to update its backlinks");
482 $rendered{$linkfile}=1;
488 run_hooks(delete => sub { shift->(@del) });
491 run_hooks(change => sub { shift->(keys %rendered) });