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 if (exists $hooks{sanitize}) {
35 foreach my $id (keys %{$hooks{sanitize}}) {
36 $content=$hooks{sanitize}{$id}{call}->($content);
43 sub backlinks ($) { #{{{
47 foreach my $p (keys %links) {
48 next if bestlink($page, $p) eq $page;
49 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
50 my $href=abs2rel(htmlpage($p), dirname($page));
52 # Trim common dir prefixes from both pages.
54 my $page_trimmed=$page;
56 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
58 $p_trimmed=~s/^\Q$dir\E// &&
59 $page_trimmed=~s/^\Q$dir\E//;
61 push @links, { url => $href, page => pagetitle($p_trimmed) };
65 return sort { $a->{page} cmp $b->{page} } @links;
68 sub parentlinks ($) { #{{{
75 return if $page eq 'index'; # toplevel
76 foreach my $dir (reverse split("/", $page)) {
79 unshift @ret, { url => "$path$dir.html", page => pagetitle($dir) };
85 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
89 sub preprocess ($$$;$) { #{{{
90 my $page=shift; # the page the data comes from
91 my $destpage=shift; # the page the data will appear in (different for inline)
93 my $onlystrip=shift || 0; # strip directives without processing
100 return "[[$command $params]]";
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+|$)/g) {
111 push @params, $1, (defined $2 ? $2 : $3);
114 push @params, (defined $2 ? $2 : $3), '';
117 return $hooks{preprocess}{$command}{call}->(
120 destpage => $destpage,
124 return "[[$command not processed]]";
128 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
132 sub add_depends ($$) { #{{{
136 if (! exists $depends{$page}) {
137 $depends{$page}=$globlist;
140 $depends{$page}=globlist_merge($depends{$page}, $globlist);
144 sub globlist_merge ($$) { #{{{
149 # Only add negated globs if they are not matched by the other globlist.
150 foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
151 (map { [ $b, $_ ] } split(" ", $a))) {
152 if ($i->[1]=~/^!(.*)/) {
153 if (! globlist_match($1, $i->[0])) {
165 sub genpage ($$$) { #{{{
170 my $template=template("page.tmpl", blind_cache => 1);
173 if (length $config{cgiurl}) {
174 $template->param(editurl => cgiurl(do => "edit", page => $page));
175 $template->param(prefsurl => cgiurl(do => "prefs"));
177 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
182 if (length $config{historyurl}) {
183 my $u=$config{historyurl};
184 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
185 $template->param(historyurl => $u);
188 if ($config{discussion}) {
189 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
194 $template->param(have_actions => 1);
198 title => $page eq 'index'
200 : pagetitle(basename($page)),
201 wikiname => $config{wikiname},
202 parentlinks => [parentlinks($page)],
204 backlinks => [backlinks($page)],
205 mtime => displaytime($mtime),
206 styleurl => styleurl($page),
209 if (exists $hooks{pagetemplate}) {
210 foreach my $id (keys %{$hooks{pagetemplate}}) {
211 $hooks{pagetemplate}{$id}{call}->(
214 template => $template,
219 return $template->output;
222 sub check_overwrite ($$) { #{{{
223 # Important security check. Make sure to call this before saving
224 # any files to the source directory.
228 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
229 error("$dest already exists and was not rendered from $src before");
233 sub displaytime ($) { #{{{
237 # strftime doesn't know about encodings, so make sure
238 # its output is properly treated as utf8
239 return decode_utf8(POSIX::strftime(
240 $config{timeformat}, localtime($time)));
246 return (stat($file))[9];
249 sub findlinks ($$) { #{{{
254 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
255 push @links, titlepage($2);
257 if ($config{discussion}) {
258 # Discussion links are a special case since they're not in the
259 # text of the page, but on its template.
260 return @links, "$page/discussion";
271 if (exists $hooks{filter}) {
272 foreach my $id (keys %{$hooks{filter}}) {
273 $content=$hooks{filter}{$id}{call}->(
283 sub render ($) { #{{{
286 my $type=pagetype($file);
287 my $srcfile=srcfile($file);
289 my $content=readfile($srcfile);
290 my $page=pagename($file);
291 delete $depends{$page};
293 $content=filter($page, $content);
295 $links{$page}=[findlinks($page, $content)];
297 $content=linkify($page, $page, $content);
298 $content=preprocess($page, $page, $content);
299 $content=htmlize($type, $content);
301 check_overwrite("$config{destdir}/".htmlpage($page), $page);
302 writefile(htmlpage($page), $config{destdir},
303 genpage($page, $content, mtime($srcfile)));
304 $oldpagemtime{$page}=time;
305 $renderedfiles{$page}=htmlpage($page);
308 my $content=readfile($srcfile, 1);
310 delete $depends{$file};
311 check_overwrite("$config{destdir}/$file", $file);
312 writefile($file, $config{destdir}, $content, 1);
313 $oldpagemtime{$file}=time;
314 $renderedfiles{$file}=$file;
322 my $dir=dirname($file);
323 while (rmdir($dir)) {
328 sub refresh () { #{{{
329 # find existing pages
332 eval q{use File::Find};
337 if (/$config{wiki_file_prune_regexp}/) {
338 $File::Find::prune=1;
340 elsif (! -d $_ && ! -l $_) {
341 my ($f)=/$config{wiki_file_regexp}/; # untaint
343 warn("skipping bad filename $_\n");
346 $f=~s/^\Q$config{srcdir}\E\/?//;
348 $exists{pagename($f)}=1;
357 if (/$config{wiki_file_prune_regexp}/) {
358 $File::Find::prune=1;
360 elsif (! -d $_ && ! -l $_) {
361 my ($f)=/$config{wiki_file_regexp}/; # untaint
363 warn("skipping bad filename $_\n");
366 # Don't add files that are in the
368 $f=~s/^\Q$config{underlaydir}\E\/?//;
369 if (! -e "$config{srcdir}/$f" &&
370 ! -l "$config{srcdir}/$f") {
372 $exists{pagename($f)}=1;
377 }, $config{underlaydir});
381 # check for added or removed pages
383 foreach my $file (@files) {
384 my $page=pagename($file);
385 if (! $oldpagemtime{$page}) {
386 debug("new page $page") unless exists $pagectime{$page};
389 $pagesources{$page}=$file;
390 if ($config{getctime} && -e "$config{srcdir}/$file") {
391 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
393 elsif (! exists $pagectime{$page}) {
394 $pagectime{$page}=mtime(srcfile($file));
399 foreach my $page (keys %oldpagemtime) {
400 if (! $exists{$page}) {
401 debug("removing old page $page");
402 push @del, $pagesources{$page};
403 prune($config{destdir}."/".$renderedfiles{$page});
404 delete $renderedfiles{$page};
405 $oldpagemtime{$page}=0;
406 delete $pagesources{$page};
410 # render any updated files
411 foreach my $file (@files) {
412 my $page=pagename($file);
414 if (! exists $oldpagemtime{$page} ||
415 mtime(srcfile($file)) > $oldpagemtime{$page}) {
416 debug("rendering $file");
422 # if any files were added or removed, check to see if each page
423 # needs an update due to linking to them or inlining them.
424 # TODO: inefficient; pages may get rendered above and again here;
425 # problem is the bestlink may have changed and we won't know until
428 FILE: foreach my $file (@files) {
429 my $page=pagename($file);
430 foreach my $f (@add, @del) {
432 foreach my $link (@{$links{$page}}) {
433 if (bestlink($page, $link) eq $p) {
434 debug("rendering $file, which links to $p");
444 # Handle backlinks; if a page has added/removed links, update the
445 # pages it links to. Also handles rebuilding dependant pages.
446 # TODO: inefficient; pages may get rendered above and again here;
447 # problem is the backlinks could be wrong in the first pass render
449 if (%rendered || @del) {
450 foreach my $f (@files) {
452 if (exists $depends{$p}) {
453 foreach my $file (keys %rendered, @del) {
455 my $page=pagename($file);
456 if (globlist_match($page, $depends{$p})) {
457 debug("rendering $f, which depends on $page");
467 foreach my $file (keys %rendered, @del) {
468 my $page=pagename($file);
470 if (exists $links{$page}) {
471 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
473 (! exists $oldlinks{$page} ||
474 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
475 $linkchanged{$link}=1;
479 if (exists $oldlinks{$page}) {
480 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
482 (! exists $links{$page} ||
483 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
484 $linkchanged{$link}=1;
489 foreach my $link (keys %linkchanged) {
490 my $linkfile=$pagesources{$link};
491 if (defined $linkfile) {
492 debug("rendering $linkfile, to update its backlinks");
494 $rendered{$linkfile}=1;
499 if (@del && exists $hooks{delete}) {
500 foreach my $id (keys %{$hooks{delete}}) {
501 $hooks{delete}{$id}{call}->(@del);
504 if (%rendered && exists $hooks{change}) {
505 foreach my $id (keys %{$hooks{change}}) {
506 $hooks{change}{$id}{call}->(keys %rendered);