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+|$)/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}=$pagespec;
138 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
142 sub genpage ($$$) { #{{{
147 my $template=template("page.tmpl", blind_cache => 1);
150 if (length $config{cgiurl}) {
151 $template->param(editurl => cgiurl(do => "edit", page => $page));
152 $template->param(prefsurl => cgiurl(do => "prefs"));
154 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
159 if (length $config{historyurl}) {
160 my $u=$config{historyurl};
161 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
162 $template->param(historyurl => $u);
165 if ($config{discussion}) {
166 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
171 $template->param(have_actions => 1);
175 title => $page eq 'index'
177 : pagetitle(basename($page)),
178 wikiname => $config{wikiname},
179 parentlinks => [parentlinks($page)],
181 backlinks => [backlinks($page)],
182 mtime => displaytime($mtime),
183 styleurl => styleurl($page),
186 run_hooks(pagetemplate => sub {
187 shift->(page => $page, destpage => $page, template => $template);
190 $content=$template->output;
192 run_hooks(format => sub {
193 $content=shift->($content);
199 sub check_overwrite ($$) { #{{{
200 # Important security check. Make sure to call this before saving
201 # any files to the source directory.
205 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
206 error("$dest already exists and was not rendered from $src before");
210 sub displaytime ($) { #{{{
214 # strftime doesn't know about encodings, so make sure
215 # its output is properly treated as utf8
216 return decode_utf8(POSIX::strftime(
217 $config{timeformat}, localtime($time)));
223 return (stat($file))[9];
226 sub findlinks ($$) { #{{{
231 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
232 push @links, titlepage($2);
234 if ($config{discussion}) {
235 # Discussion links are a special case since they're not in the
236 # text of the page, but on its template.
237 return @links, "$page/discussion";
248 run_hooks(filter => sub {
249 $content=shift->(page => $page, content => $content);
255 sub render ($) { #{{{
258 my $type=pagetype($file);
259 my $srcfile=srcfile($file);
261 my $content=readfile($srcfile);
262 my $page=pagename($file);
263 delete $depends{$page};
265 $content=filter($page, $content);
267 $links{$page}=[findlinks($page, $content)];
269 $content=linkify($page, $page, $content);
270 $content=preprocess($page, $page, $content);
271 $content=htmlize($type, $content);
273 check_overwrite("$config{destdir}/".htmlpage($page), $page);
274 writefile(htmlpage($page), $config{destdir},
275 genpage($page, $content, mtime($srcfile)));
276 $oldpagemtime{$page}=time;
277 $renderedfiles{$page}=htmlpage($page);
280 my $content=readfile($srcfile, 1);
282 delete $depends{$file};
283 check_overwrite("$config{destdir}/$file", $file);
284 writefile($file, $config{destdir}, $content, 1);
285 $oldpagemtime{$file}=time;
286 $renderedfiles{$file}=$file;
294 my $dir=dirname($file);
295 while (rmdir($dir)) {
300 sub refresh () { #{{{
301 # find existing pages
304 eval q{use File::Find};
309 if (/$config{wiki_file_prune_regexp}/) {
310 $File::Find::prune=1;
312 elsif (! -d $_ && ! -l $_) {
313 my ($f)=/$config{wiki_file_regexp}/; # untaint
315 warn("skipping bad filename $_\n");
318 $f=~s/^\Q$config{srcdir}\E\/?//;
320 $exists{pagename($f)}=1;
329 if (/$config{wiki_file_prune_regexp}/) {
330 $File::Find::prune=1;
332 elsif (! -d $_ && ! -l $_) {
333 my ($f)=/$config{wiki_file_regexp}/; # untaint
335 warn("skipping bad filename $_\n");
338 # Don't add files that are in the
340 $f=~s/^\Q$config{underlaydir}\E\/?//;
341 if (! -e "$config{srcdir}/$f" &&
342 ! -l "$config{srcdir}/$f") {
344 $exists{pagename($f)}=1;
349 }, $config{underlaydir});
353 # check for added or removed pages
355 foreach my $file (@files) {
356 my $page=pagename($file);
357 if (! $oldpagemtime{$page}) {
358 debug("new page $page") unless exists $pagectime{$page};
361 $pagesources{$page}=$file;
362 if ($config{getctime} && -e "$config{srcdir}/$file") {
363 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
365 elsif (! exists $pagectime{$page}) {
366 $pagectime{$page}=mtime(srcfile($file));
371 foreach my $page (keys %oldpagemtime) {
372 if (! $exists{$page}) {
373 debug("removing old page $page");
374 push @del, $pagesources{$page};
375 prune($config{destdir}."/".$renderedfiles{$page});
376 delete $renderedfiles{$page};
377 $oldpagemtime{$page}=0;
378 delete $pagesources{$page};
382 # render any updated files
383 foreach my $file (@files) {
384 my $page=pagename($file);
386 if (! exists $oldpagemtime{$page} ||
387 mtime(srcfile($file)) > $oldpagemtime{$page} ||
388 $forcerebuild{$page}) {
389 debug("rendering $file");
395 # if any files were added or removed, check to see if each page
396 # needs an update due to linking to them or inlining them.
397 # TODO: inefficient; pages may get rendered above and again here;
398 # problem is the bestlink may have changed and we won't know until
401 FILE: foreach my $file (@files) {
402 my $page=pagename($file);
403 foreach my $f (@add, @del) {
405 foreach my $link (@{$links{$page}}) {
406 if (bestlink($page, $link) eq $p) {
407 debug("rendering $file, which links to $p");
417 # Handle backlinks; if a page has added/removed links, update the
418 # pages it links to. Also handles rebuilding dependant pages.
419 # TODO: inefficient; pages may get rendered above and again here;
420 # problem is the backlinks could be wrong in the first pass render
422 if (%rendered || @del) {
423 foreach my $f (@files) {
425 if (exists $depends{$p}) {
426 foreach my $file (keys %rendered, @del) {
428 my $page=pagename($file);
429 if (pagespec_match($page, $depends{$p})) {
430 debug("rendering $f, which depends on $page");
440 foreach my $file (keys %rendered, @del) {
441 my $page=pagename($file);
443 if (exists $links{$page}) {
444 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
446 (! exists $oldlinks{$page} ||
447 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
448 $linkchanged{$link}=1;
452 if (exists $oldlinks{$page}) {
453 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
455 (! exists $links{$page} ||
456 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
457 $linkchanged{$link}=1;
462 foreach my $link (keys %linkchanged) {
463 my $linkfile=$pagesources{$link};
464 if (defined $linkfile) {
465 debug("rendering $linkfile, to update its backlinks");
467 $rendered{$linkfile}=1;
473 run_hooks(delete => sub { shift->(@del) });
476 run_hooks(change => sub { shift->(keys %rendered) });