10 sub linkify ($$$) { #{{{
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 => $p_trimmed };
65 return sort { $a->{page} cmp $b->{page} } @links;
68 sub parentlinks ($) { #{{{
75 foreach my $dir (reverse split("/", $page)) {
78 unshift @ret, { url => "$path$dir.html", page => $dir };
84 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
88 sub preprocess ($$;$) { #{{{
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+|$)/g) {
108 push @params, $1, $2;
110 return $hooks{preprocess}{$command}{call}->(@params, page => $page);
113 return "[[$command not processed]]";
117 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
121 sub add_depends ($$) { #{{{
125 if (! exists $depends{$page}) {
126 $depends{$page}=$globlist;
129 $depends{$page}=globlist_merge($depends{$page}, $globlist);
133 sub globlist_merge ($$) { #{{{
138 # Only add negated globs if they are not matched by the other globlist.
139 foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
140 (map { [ $b, $_ ] } split(" ", $a))) {
141 if ($i->[1]=~/^!(.*)/) {
142 if (! globlist_match($1, $i->[0])) {
154 sub genpage ($$$) { #{{{
159 my $title=pagetitle(basename($page));
161 my $template=template("page.tmpl", blind_cache => 1);
164 if (length $config{cgiurl}) {
165 $template->param(editurl => cgiurl(do => "edit", page => $page));
166 $template->param(prefsurl => cgiurl(do => "prefs"));
168 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
173 if (length $config{historyurl}) {
174 my $u=$config{historyurl};
175 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
176 $template->param(historyurl => $u);
179 if ($config{discussion}) {
180 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
185 $template->param(have_actions => 1);
190 wikiname => $config{wikiname},
191 parentlinks => [parentlinks($page)],
193 backlinks => [backlinks($page)],
194 mtime => displaytime($mtime),
195 styleurl => styleurl($page),
198 if (exists $hooks{pagetemplate}) {
199 foreach my $id (keys %{$hooks{pagetemplate}}) {
200 $hooks{pagetemplate}{$id}{call}->($page, $template);
204 return $template->output;
207 sub check_overwrite ($$) { #{{{
208 # Important security check. Make sure to call this before saving
209 # any files to the source directory.
213 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
214 error("$dest already exists and was not rendered from $src before");
218 sub displaytime ($) { #{{{
222 # strftime doesn't know about encodings, so make sure
223 # its output is properly treated as utf8
224 return Encode::decode_utf8(POSIX::strftime(
225 $config{timeformat}, localtime($time)));
231 return (stat($file))[9];
234 sub findlinks ($$) { #{{{
239 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
240 push @links, titlepage($2);
242 if ($config{discussion}) {
243 # Discussion links are a special case since they're not in the
244 # text of the page, but on its template.
245 return @links, "$page/discussion";
252 sub render ($) { #{{{
255 my $type=pagetype($file);
256 my $srcfile=srcfile($file);
258 my $content=readfile($srcfile);
259 my $page=pagename($file);
260 delete $depends{$page};
262 if (exists $hooks{filter}) {
263 foreach my $id (keys %{$hooks{filter}}) {
264 $content=$hooks{filter}{$id}{call}->(
271 $links{$page}=[findlinks($page, $content)];
273 $content=linkify($page, $page, $content);
274 $content=preprocess($page, $content);
275 $content=htmlize($type, $content);
277 check_overwrite("$config{destdir}/".htmlpage($page), $page);
278 writefile(htmlpage($page), $config{destdir},
279 genpage($page, $content, mtime($srcfile)));
280 $oldpagemtime{$page}=time;
281 $renderedfiles{$page}=htmlpage($page);
284 my $content=readfile($srcfile, 1);
286 delete $depends{$file};
287 check_overwrite("$config{destdir}/$file", $file);
288 writefile($file, $config{destdir}, $content, 1);
289 $oldpagemtime{$file}=time;
290 $renderedfiles{$file}=$file;
298 my $dir=dirname($file);
299 while (rmdir($dir)) {
304 sub refresh () { #{{{
305 # find existing pages
308 eval q{use File::Find};
312 $_ = Encode::decode_utf8($_);
313 if (/$config{wiki_file_prune_regexp}/) {
314 $File::Find::prune=1;
316 elsif (! -d $_ && ! -l $_) {
317 my ($f)=/$config{wiki_file_regexp}/; # untaint
319 warn("skipping bad filename $_\n");
322 $f=~s/^\Q$config{srcdir}\E\/?//;
324 $exists{pagename($f)}=1;
332 $_ = Encode::decode_utf8($_);
333 if (/$config{wiki_file_prune_regexp}/) {
334 $File::Find::prune=1;
336 elsif (! -d $_ && ! -l $_) {
337 my ($f)=/$config{wiki_file_regexp}/; # untaint
339 warn("skipping bad filename $_\n");
342 # Don't add files that are in the
344 $f=~s/^\Q$config{underlaydir}\E\/?//;
345 if (! -e "$config{srcdir}/$f" &&
346 ! -l "$config{srcdir}/$f") {
348 $exists{pagename($f)}=1;
353 }, $config{underlaydir});
357 # check for added or removed pages
359 foreach my $file (@files) {
360 my $page=pagename($file);
361 if (! $oldpagemtime{$page}) {
362 debug("new page $page") unless exists $pagectime{$page};
365 $pagesources{$page}=$file;
366 if ($config{getctime} && -e "$config{srcdir}/$file") {
367 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
369 elsif (! exists $pagectime{$page}) {
370 $pagectime{$page}=mtime(srcfile($file));
375 foreach my $page (keys %oldpagemtime) {
376 if (! $exists{$page}) {
377 debug("removing old page $page");
378 push @del, $pagesources{$page};
379 prune($config{destdir}."/".$renderedfiles{$page});
380 delete $renderedfiles{$page};
381 $oldpagemtime{$page}=0;
382 delete $pagesources{$page};
386 # render any updated files
387 foreach my $file (@files) {
388 my $page=pagename($file);
390 if (! exists $oldpagemtime{$page} ||
391 mtime(srcfile($file)) > $oldpagemtime{$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 (globlist_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;
475 if (@del && exists $hooks{delete}) {
476 foreach my $id (keys %{$hooks{delete}}) {
477 $hooks{delete}{$id}{call}->(@del);
480 if (%rendered && exists $hooks{change}) {
481 foreach my $id (keys %{$hooks{change}}) {
482 $hooks{change}{$id}{call}->(keys %rendered);