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+)=)?(?:"""\n?(.+)"""|"([^"]+)"|(\S+))(?:\s+|$)/sg) {
108 my $val=(defined $2 ? $2 : (defined $3 ? $3 : $4));
111 push @params, $1, $val;
114 push @params, $val, '';
117 return $hooks{preprocess}{$command}{call}->(
120 destpage => $destpage,
124 return "[[$command not processed]]";
128 $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".+"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}eg;
132 sub add_depends ($$) { #{{{
136 if (! exists $depends{$page}) {
137 $depends{$page}=$pagespec;
140 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
144 sub genpage ($$$) { #{{{
149 my $template=template("page.tmpl", blind_cache => 1);
152 if (length $config{cgiurl}) {
153 $template->param(editurl => cgiurl(do => "edit", page => $page));
154 $template->param(prefsurl => cgiurl(do => "prefs"));
156 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
161 if (length $config{historyurl}) {
162 my $u=$config{historyurl};
163 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
164 $template->param(historyurl => $u);
167 if ($config{discussion}) {
168 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
173 $template->param(have_actions => 1);
177 title => $page eq 'index'
179 : pagetitle(basename($page)),
180 wikiname => $config{wikiname},
181 parentlinks => [parentlinks($page)],
183 backlinks => [backlinks($page)],
184 mtime => displaytime($mtime),
185 baseurl => baseurl($page),
188 run_hooks(pagetemplate => sub {
189 shift->(page => $page, destpage => $page, template => $template);
192 $content=$template->output;
194 run_hooks(format => sub {
195 $content=shift->($content);
201 sub check_overwrite ($$) { #{{{
202 # Important security check. Make sure to call this before saving
203 # any files to the source directory.
207 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
208 error("$dest already exists and was not rendered from $src before");
212 sub displaytime ($) { #{{{
216 # strftime doesn't know about encodings, so make sure
217 # its output is properly treated as utf8
218 return decode_utf8(POSIX::strftime(
219 $config{timeformat}, localtime($time)));
225 return (stat($file))[9];
228 sub findlinks ($$) { #{{{
233 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
234 push @links, titlepage($2);
236 if ($config{discussion}) {
237 # Discussion links are a special case since they're not in the
238 # text of the page, but on its template.
239 return @links, "$page/discussion";
250 run_hooks(filter => sub {
251 $content=shift->(page => $page, content => $content);
257 sub render ($) { #{{{
260 my $type=pagetype($file);
261 my $srcfile=srcfile($file);
263 my $content=readfile($srcfile);
264 my $page=pagename($file);
265 delete $depends{$page};
267 $content=filter($page, $content);
269 $links{$page}=[findlinks($page, $content)];
271 $content=linkify($page, $page, $content);
272 $content=preprocess($page, $page, $content);
273 $content=htmlize($type, $content);
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);
282 my $content=readfile($srcfile, 1);
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;
296 my $dir=dirname($file);
297 while (rmdir($dir)) {
302 sub refresh () { #{{{
303 # find existing pages
306 eval q{use File::Find};
311 if (/$config{wiki_file_prune_regexp}/) {
312 $File::Find::prune=1;
314 elsif (! -d $_ && ! -l $_) {
315 my ($f)=/$config{wiki_file_regexp}/; # untaint
317 warn("skipping bad filename $_\n");
320 $f=~s/^\Q$config{srcdir}\E\/?//;
322 $exists{pagename($f)}=1;
331 if (/$config{wiki_file_prune_regexp}/) {
332 $File::Find::prune=1;
334 elsif (! -d $_ && ! -l $_) {
335 my ($f)=/$config{wiki_file_regexp}/; # untaint
337 warn("skipping bad filename $_\n");
340 # Don't add files that are in the
342 $f=~s/^\Q$config{underlaydir}\E\/?//;
343 if (! -e "$config{srcdir}/$f" &&
344 ! -l "$config{srcdir}/$f") {
346 $exists{pagename($f)}=1;
351 }, $config{underlaydir});
355 # check for added or removed pages
357 foreach my $file (@files) {
358 my $page=pagename($file);
359 if (! $oldpagemtime{$page}) {
360 debug("new page $page") unless exists $pagectime{$page};
363 $pagecase{lc $page}=$page;
364 $pagesources{$page}=$file;
365 if ($config{getctime} && -e "$config{srcdir}/$file") {
366 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
368 elsif (! exists $pagectime{$page}) {
369 $pagectime{$page}=mtime(srcfile($file));
374 foreach my $page (keys %oldpagemtime) {
375 if (! $exists{$page}) {
376 debug("removing old page $page");
377 push @del, $pagesources{$page};
378 prune($config{destdir}."/".$renderedfiles{$page});
379 delete $renderedfiles{$page};
380 $oldpagemtime{$page}=0;
381 delete $pagesources{$page};
385 # render any updated files
386 foreach my $file (@files) {
387 my $page=pagename($file);
389 if (! exists $oldpagemtime{$page} ||
390 mtime(srcfile($file)) > $oldpagemtime{$page} ||
391 $forcerebuild{$page}) {
392 debug("rendering $file");
398 # if any files were added or removed, check to see if each page
399 # needs an update due to linking to them or inlining them.
400 # TODO: inefficient; pages may get rendered above and again here;
401 # problem is the bestlink may have changed and we won't know until
404 FILE: foreach my $file (@files) {
405 my $page=pagename($file);
406 foreach my $f (@add, @del) {
408 foreach my $link (@{$links{$page}}) {
409 if (bestlink($page, $link) eq $p) {
410 debug("rendering $file, which links to $p");
420 # Handle backlinks; if a page has added/removed links, update the
421 # pages it links to. Also handles rebuilding dependant pages.
422 # TODO: inefficient; pages may get rendered above and again here;
423 # problem is the backlinks could be wrong in the first pass render
425 if (%rendered || @del) {
426 foreach my $f (@files) {
428 if (exists $depends{$p}) {
429 foreach my $file (keys %rendered, @del) {
431 my $page=pagename($file);
432 if (pagespec_match($page, $depends{$p})) {
433 debug("rendering $f, which depends on $page");
443 foreach my $file (keys %rendered, @del) {
444 my $page=pagename($file);
446 if (exists $links{$page}) {
447 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
449 (! exists $oldlinks{$page} ||
450 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
451 $linkchanged{$link}=1;
455 if (exists $oldlinks{$page}) {
456 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
458 (! exists $links{$page} ||
459 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
460 $linkchanged{$link}=1;
465 foreach my $link (keys %linkchanged) {
466 my $linkfile=$pagesources{$link};
467 if (defined $linkfile) {
468 debug("rendering $linkfile, to update its backlinks");
470 $rendered{$linkfile}=1;
476 run_hooks(delete => sub { shift->(@del) });
479 run_hooks(change => sub { shift->(keys %rendered) });