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 ($$$) { #{{{
28 if (exists $hooks{htmlize}{$type}) {
29 $content=$hooks{htmlize}{$type}{call}->(
35 error("htmlization of $type not supported");
38 run_hooks(sanitize => sub {
48 sub backlinks ($) { #{{{
52 foreach my $p (keys %links) {
53 next if bestlink($page, $p) eq $page;
54 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
55 my $href=abs2rel(htmlpage($p), dirname($page));
57 # Trim common dir prefixes from both pages.
59 my $page_trimmed=$page;
61 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
63 $p_trimmed=~s/^\Q$dir\E// &&
64 $page_trimmed=~s/^\Q$dir\E//;
66 push @links, { url => $href, page => pagetitle($p_trimmed) };
70 return sort { $a->{page} cmp $b->{page} } @links;
73 sub parentlinks ($) { #{{{
80 return if $page eq 'index'; # toplevel
81 foreach my $dir (reverse split("/", $page)) {
84 unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
90 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
95 sub preprocess ($$$) { #{{{
96 my $page=shift; # the page the data comes from
97 my $destpage=shift; # the page the data will appear in (different for inline)
104 if (length $escape) {
105 return "[[$command $params]]";
107 elsif (exists $hooks{preprocess}{$command}) {
108 # Note: preserve order of params, some plugins may
109 # consider it significant.
111 while ($params =~ /(?:(\w+)=)?(?:"""(.*?)"""|"([^"]+)"|(\S+))(?:\s+|$)/sg) {
128 push @params, $key, $val;
131 push @params, $val, '';
134 if ($preprocessing{$page}++ > 3) {
135 # Avoid loops of preprocessed pages preprocessing
136 # other pages that preprocess them, etc.
137 return "[[$command preprocessing loop detected on $page at depth $preprocessing{$page}]]";
139 my $ret=$hooks{preprocess}{$command}{call}->(
142 destpage => $destpage,
144 $preprocessing{$page}--;
148 return "[[$command $params]]";
152 $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
156 sub add_depends ($$) { #{{{
160 if (! exists $depends{$page}) {
161 $depends{$page}=$pagespec;
164 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
168 sub genpage ($$$) { #{{{
173 my $template=template("page.tmpl", blind_cache => 1);
176 if (length $config{cgiurl}) {
177 $template->param(editurl => cgiurl(do => "edit", page => $page));
178 $template->param(prefsurl => cgiurl(do => "prefs"));
180 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
185 if (length $config{historyurl}) {
186 my $u=$config{historyurl};
187 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
188 $template->param(historyurl => $u);
191 if ($config{discussion}) {
192 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
197 $template->param(have_actions => 1);
201 title => $page eq 'index'
203 : pagetitle(basename($page)),
204 wikiname => $config{wikiname},
205 parentlinks => [parentlinks($page)],
207 backlinks => [backlinks($page)],
208 mtime => displaytime($mtime),
209 baseurl => baseurl($page),
212 run_hooks(pagetemplate => sub {
213 shift->(page => $page, destpage => $page, template => $template);
216 $content=$template->output;
218 run_hooks(format => sub {
228 sub check_overwrite ($$) { #{{{
229 # Important security check. Make sure to call this before saving
230 # any files to the source directory.
234 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
235 error("$dest already exists and was not rendered from $src before");
239 sub displaytime ($) { #{{{
243 # strftime doesn't know about encodings, so make sure
244 # its output is properly treated as utf8
245 return decode_utf8(POSIX::strftime(
246 $config{timeformat}, localtime($time)));
252 return (stat($file))[9];
255 sub findlinks ($$) { #{{{
260 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
261 push @links, titlepage($2);
263 if ($config{discussion}) {
264 # Discussion links are a special case since they're not in the
265 # text of the page, but on its template.
266 return @links, "$page/discussion";
277 run_hooks(filter => sub {
278 $content=shift->(page => $page, content => $content);
284 sub render ($) { #{{{
287 my $type=pagetype($file);
288 my $srcfile=srcfile($file);
290 my $content=readfile($srcfile);
291 my $page=pagename($file);
292 delete $depends{$page};
294 $content=filter($page, $content);
296 $links{$page}=[findlinks($page, $content)];
298 $content=preprocess($page, $page, $content);
299 $content=linkify($page, $page, $content);
300 $content=htmlize($page, $type, $content);
302 check_overwrite("$config{destdir}/".htmlpage($page), $page);
303 writefile(htmlpage($page), $config{destdir},
304 genpage($page, $content, mtime($srcfile)));
305 $oldpagemtime{$page}=time;
306 $renderedfiles{$page}=htmlpage($page);
309 my $content=readfile($srcfile, 1);
311 delete $depends{$file};
312 check_overwrite("$config{destdir}/$file", $file);
313 writefile($file, $config{destdir}, $content, 1);
314 $oldpagemtime{$file}=time;
315 $renderedfiles{$file}=$file;
323 my $dir=dirname($file);
324 while (rmdir($dir)) {
329 sub refresh () { #{{{
330 # find existing pages
333 eval q{use File::Find};
338 if (/$config{wiki_file_prune_regexp}/) {
339 $File::Find::prune=1;
341 elsif (! -d $_ && ! -l $_) {
342 my ($f)=/$config{wiki_file_regexp}/; # untaint
344 warn("skipping bad filename $_\n");
347 $f=~s/^\Q$config{srcdir}\E\/?//;
349 $exists{pagename($f)}=1;
358 if (/$config{wiki_file_prune_regexp}/) {
359 $File::Find::prune=1;
361 elsif (! -d $_ && ! -l $_) {
362 my ($f)=/$config{wiki_file_regexp}/; # untaint
364 warn("skipping bad filename $_\n");
367 # Don't add files that are in the
369 $f=~s/^\Q$config{underlaydir}\E\/?//;
370 if (! -e "$config{srcdir}/$f" &&
371 ! -l "$config{srcdir}/$f") {
373 $exists{pagename($f)}=1;
378 }, $config{underlaydir});
382 # check for added or removed pages
384 foreach my $file (@files) {
385 my $page=pagename($file);
386 if (! $oldpagemtime{$page}) {
387 debug("new page $page") unless exists $pagectime{$page};
390 $pagecase{lc $page}=$page;
391 $pagesources{$page}=$file;
392 if ($config{getctime} && -e "$config{srcdir}/$file") {
393 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
395 elsif (! exists $pagectime{$page}) {
396 $pagectime{$page}=mtime(srcfile($file));
401 foreach my $page (keys %oldpagemtime) {
402 if (! $exists{$page}) {
403 debug("removing old page $page");
404 push @del, $pagesources{$page};
405 prune($config{destdir}."/".$renderedfiles{$page});
406 delete $renderedfiles{$page};
407 $oldpagemtime{$page}=0;
408 delete $pagesources{$page};
412 # render any updated files
413 foreach my $file (@files) {
414 my $page=pagename($file);
416 if (! exists $oldpagemtime{$page} ||
417 mtime(srcfile($file)) > $oldpagemtime{$page} ||
418 $forcerebuild{$page}) {
419 debug("rendering $file");
425 # if any files were added or removed, check to see if each page
426 # needs an update due to linking to them or inlining them.
427 # TODO: inefficient; pages may get rendered above and again here;
428 # problem is the bestlink may have changed and we won't know until
431 FILE: foreach my $file (@files) {
432 my $page=pagename($file);
433 foreach my $f (@add, @del) {
435 foreach my $link (@{$links{$page}}) {
436 if (bestlink($page, $link) eq $p) {
437 debug("rendering $file, which links to $p");
447 # Handle backlinks; if a page has added/removed links, update the
448 # pages it links to. Also handles rebuilding dependant pages.
449 # TODO: inefficient; pages may get rendered above and again here;
450 # problem is the backlinks could be wrong in the first pass render
452 if (%rendered || @del) {
453 foreach my $f (@files) {
455 if (exists $depends{$p}) {
456 foreach my $file (keys %rendered, @del) {
458 my $page=pagename($file);
459 if (pagespec_match($page, $depends{$p})) {
460 debug("rendering $f, which depends on $page");
470 foreach my $file (keys %rendered, @del) {
471 my $page=pagename($file);
473 if (exists $links{$page}) {
474 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
476 (! exists $oldlinks{$page} ||
477 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
478 $linkchanged{$link}=1;
482 if (exists $oldlinks{$page}) {
483 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
485 (! exists $links{$page} ||
486 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
487 $linkchanged{$link}=1;
492 foreach my $link (keys %linkchanged) {
493 my $linkfile=$pagesources{$link};
494 if (defined $linkfile) {
495 debug("rendering $linkfile, to update its backlinks");
497 $rendered{$linkfile}=1;
503 run_hooks(delete => sub { shift->(@del) });
506 run_hooks(change => sub { shift->(keys %rendered) });