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} };
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+|$)/sg) {
124 push @params, $key, $val;
127 push @params, $val, '';
130 return $hooks{preprocess}{$command}{call}->(
133 destpage => $destpage,
137 return "[[$command not processed]]";
141 $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
145 sub add_depends ($$) { #{{{
149 if (! exists $depends{$page}) {
150 $depends{$page}=$pagespec;
153 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
157 sub genpage ($$$) { #{{{
162 my $template=template("page.tmpl", blind_cache => 1);
165 if (length $config{cgiurl}) {
166 $template->param(editurl => cgiurl(do => "edit", page => $page));
167 $template->param(prefsurl => cgiurl(do => "prefs"));
169 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
174 if (length $config{historyurl}) {
175 my $u=$config{historyurl};
176 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
177 $template->param(historyurl => $u);
180 if ($config{discussion}) {
181 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
186 $template->param(have_actions => 1);
190 title => $page eq 'index'
192 : pagetitle(basename($page)),
193 wikiname => $config{wikiname},
194 parentlinks => [parentlinks($page)],
196 backlinks => [backlinks($page)],
197 mtime => displaytime($mtime),
198 baseurl => baseurl($page),
201 run_hooks(pagetemplate => sub {
202 shift->(page => $page, destpage => $page, template => $template);
205 $content=$template->output;
207 run_hooks(format => sub {
208 $content=shift->($content);
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=preprocess($page, $page, $content);
285 $content=linkify($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 $pagecase{lc $page}=$page;
377 $pagesources{$page}=$file;
378 if ($config{getctime} && -e "$config{srcdir}/$file") {
379 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
381 elsif (! exists $pagectime{$page}) {
382 $pagectime{$page}=mtime(srcfile($file));
387 foreach my $page (keys %oldpagemtime) {
388 if (! $exists{$page}) {
389 debug("removing old page $page");
390 push @del, $pagesources{$page};
391 prune($config{destdir}."/".$renderedfiles{$page});
392 delete $renderedfiles{$page};
393 $oldpagemtime{$page}=0;
394 delete $pagesources{$page};
398 # render any updated files
399 foreach my $file (@files) {
400 my $page=pagename($file);
402 if (! exists $oldpagemtime{$page} ||
403 mtime(srcfile($file)) > $oldpagemtime{$page} ||
404 $forcerebuild{$page}) {
405 debug("rendering $file");
411 # if any files were added or removed, check to see if each page
412 # needs an update due to linking to them or inlining them.
413 # TODO: inefficient; pages may get rendered above and again here;
414 # problem is the bestlink may have changed and we won't know until
417 FILE: foreach my $file (@files) {
418 my $page=pagename($file);
419 foreach my $f (@add, @del) {
421 foreach my $link (@{$links{$page}}) {
422 if (bestlink($page, $link) eq $p) {
423 debug("rendering $file, which links to $p");
433 # Handle backlinks; if a page has added/removed links, update the
434 # pages it links to. Also handles rebuilding dependant pages.
435 # TODO: inefficient; pages may get rendered above and again here;
436 # problem is the backlinks could be wrong in the first pass render
438 if (%rendered || @del) {
439 foreach my $f (@files) {
441 if (exists $depends{$p}) {
442 foreach my $file (keys %rendered, @del) {
444 my $page=pagename($file);
445 if (pagespec_match($page, $depends{$p})) {
446 debug("rendering $f, which depends on $page");
456 foreach my $file (keys %rendered, @del) {
457 my $page=pagename($file);
459 if (exists $links{$page}) {
460 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
462 (! exists $oldlinks{$page} ||
463 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
464 $linkchanged{$link}=1;
468 if (exists $oldlinks{$page}) {
469 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
471 (! exists $links{$page} ||
472 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
473 $linkchanged{$link}=1;
478 foreach my $link (keys %linkchanged) {
479 my $linkfile=$pagesources{$link};
480 if (defined $linkfile) {
481 debug("rendering $linkfile, to update its backlinks");
483 $rendered{$linkfile}=1;
489 run_hooks(delete => sub { shift->(@del) });
492 run_hooks(change => sub { shift->(keys %rendered) });