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}++ > 10) {
101 # Avoid loops of preprocessed pages preprocessing
102 # other pages that preprocess them, etc.
103 return "[[$command preprocessing loop detected on $page at depth $preprocessing{$page}]]";
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 my $ret=$hooks{preprocess}{$command}{call}->(
135 destpage => $destpage,
137 $preprocessing{$page}--;
141 return "[[$command $params]]";
145 $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
149 sub add_depends ($$) { #{{{
153 if (! exists $depends{$page}) {
154 $depends{$page}=$pagespec;
157 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
161 sub genpage ($$$) { #{{{
166 my $template=template("page.tmpl", blind_cache => 1);
169 if (length $config{cgiurl}) {
170 $template->param(editurl => cgiurl(do => "edit", page => $page));
171 $template->param(prefsurl => cgiurl(do => "prefs"));
173 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
178 if (length $config{historyurl}) {
179 my $u=$config{historyurl};
180 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
181 $template->param(historyurl => $u);
184 if ($config{discussion}) {
185 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
190 $template->param(have_actions => 1);
194 title => $page eq 'index'
196 : pagetitle(basename($page)),
197 wikiname => $config{wikiname},
198 parentlinks => [parentlinks($page)],
200 backlinks => [backlinks($page)],
201 mtime => displaytime($mtime),
202 baseurl => baseurl($page),
205 run_hooks(pagetemplate => sub {
206 shift->(page => $page, destpage => $page, template => $template);
209 $content=$template->output;
211 run_hooks(format => sub {
212 $content=shift->($content);
218 sub check_overwrite ($$) { #{{{
219 # Important security check. Make sure to call this before saving
220 # any files to the source directory.
224 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
225 error("$dest already exists and was not rendered from $src before");
229 sub displaytime ($) { #{{{
233 # strftime doesn't know about encodings, so make sure
234 # its output is properly treated as utf8
235 return decode_utf8(POSIX::strftime(
236 $config{timeformat}, localtime($time)));
242 return (stat($file))[9];
245 sub findlinks ($$) { #{{{
250 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
251 push @links, titlepage($2);
253 if ($config{discussion}) {
254 # Discussion links are a special case since they're not in the
255 # text of the page, but on its template.
256 return @links, "$page/discussion";
267 run_hooks(filter => sub {
268 $content=shift->(page => $page, content => $content);
274 sub render ($) { #{{{
277 my $type=pagetype($file);
278 my $srcfile=srcfile($file);
280 my $content=readfile($srcfile);
281 my $page=pagename($file);
282 delete $depends{$page};
284 $content=filter($page, $content);
286 $links{$page}=[findlinks($page, $content)];
288 $content=preprocess($page, $page, $content);
289 $content=linkify($page, $page, $content);
290 $content=htmlize($type, $content);
292 check_overwrite("$config{destdir}/".htmlpage($page), $page);
293 writefile(htmlpage($page), $config{destdir},
294 genpage($page, $content, mtime($srcfile)));
295 $oldpagemtime{$page}=time;
296 $renderedfiles{$page}=htmlpage($page);
299 my $content=readfile($srcfile, 1);
301 delete $depends{$file};
302 check_overwrite("$config{destdir}/$file", $file);
303 writefile($file, $config{destdir}, $content, 1);
304 $oldpagemtime{$file}=time;
305 $renderedfiles{$file}=$file;
313 my $dir=dirname($file);
314 while (rmdir($dir)) {
319 sub refresh () { #{{{
320 # find existing pages
323 eval q{use File::Find};
328 if (/$config{wiki_file_prune_regexp}/) {
329 $File::Find::prune=1;
331 elsif (! -d $_ && ! -l $_) {
332 my ($f)=/$config{wiki_file_regexp}/; # untaint
334 warn("skipping bad filename $_\n");
337 $f=~s/^\Q$config{srcdir}\E\/?//;
339 $exists{pagename($f)}=1;
348 if (/$config{wiki_file_prune_regexp}/) {
349 $File::Find::prune=1;
351 elsif (! -d $_ && ! -l $_) {
352 my ($f)=/$config{wiki_file_regexp}/; # untaint
354 warn("skipping bad filename $_\n");
357 # Don't add files that are in the
359 $f=~s/^\Q$config{underlaydir}\E\/?//;
360 if (! -e "$config{srcdir}/$f" &&
361 ! -l "$config{srcdir}/$f") {
363 $exists{pagename($f)}=1;
368 }, $config{underlaydir});
372 # check for added or removed pages
374 foreach my $file (@files) {
375 my $page=pagename($file);
376 if (! $oldpagemtime{$page}) {
377 debug("new page $page") unless exists $pagectime{$page};
380 $pagecase{lc $page}=$page;
381 $pagesources{$page}=$file;
382 if ($config{getctime} && -e "$config{srcdir}/$file") {
383 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
385 elsif (! exists $pagectime{$page}) {
386 $pagectime{$page}=mtime(srcfile($file));
391 foreach my $page (keys %oldpagemtime) {
392 if (! $exists{$page}) {
393 debug("removing old page $page");
394 push @del, $pagesources{$page};
395 prune($config{destdir}."/".$renderedfiles{$page});
396 delete $renderedfiles{$page};
397 $oldpagemtime{$page}=0;
398 delete $pagesources{$page};
402 # render any updated files
403 foreach my $file (@files) {
404 my $page=pagename($file);
406 if (! exists $oldpagemtime{$page} ||
407 mtime(srcfile($file)) > $oldpagemtime{$page} ||
408 $forcerebuild{$page}) {
409 debug("rendering $file");
415 # if any files were added or removed, check to see if each page
416 # needs an update due to linking to them or inlining them.
417 # TODO: inefficient; pages may get rendered above and again here;
418 # problem is the bestlink may have changed and we won't know until
421 FILE: foreach my $file (@files) {
422 my $page=pagename($file);
423 foreach my $f (@add, @del) {
425 foreach my $link (@{$links{$page}}) {
426 if (bestlink($page, $link) eq $p) {
427 debug("rendering $file, which links to $p");
437 # Handle backlinks; if a page has added/removed links, update the
438 # pages it links to. Also handles rebuilding dependant pages.
439 # TODO: inefficient; pages may get rendered above and again here;
440 # problem is the backlinks could be wrong in the first pass render
442 if (%rendered || @del) {
443 foreach my $f (@files) {
445 if (exists $depends{$p}) {
446 foreach my $file (keys %rendered, @del) {
448 my $page=pagename($file);
449 if (pagespec_match($page, $depends{$p})) {
450 debug("rendering $f, which depends on $page");
460 foreach my $file (keys %rendered, @del) {
461 my $page=pagename($file);
463 if (exists $links{$page}) {
464 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
466 (! exists $oldlinks{$page} ||
467 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
468 $linkchanged{$link}=1;
472 if (exists $oldlinks{$page}) {
473 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
475 (! exists $links{$page} ||
476 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
477 $linkchanged{$link}=1;
482 foreach my $link (keys %linkchanged) {
483 my $linkfile=$pagesources{$link};
484 if (defined $linkfile) {
485 debug("rendering $linkfile, to update its backlinks");
487 $rendered{$linkfile}=1;
493 run_hooks(delete => sub { shift->(@del) });
496 run_hooks(change => sub { shift->(keys %rendered) });