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 (! $INC{"/usr/bin/markdown"}) {
29 $blosxom::version="is a proper perl module too much to ask?";
31 do "/usr/bin/markdown";
35 if ($type eq '.mdwn') {
36 # Markdown does character based stuff that does not work
37 # well with utf-8 strings.
38 $content=Encode::decode_utf8(Markdown::Markdown(Encode::encode_utf8($content)));
41 error("htmlization of $type not supported");
44 if (exists $hooks{sanitize}) {
45 foreach my $id (keys %{$hooks{sanitize}}) {
46 $content=$hooks{sanitize}{$id}{call}->($content);
53 sub backlinks ($) { #{{{
57 foreach my $p (keys %links) {
58 next if bestlink($page, $p) eq $page;
59 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
60 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
62 # Trim common dir prefixes from both pages.
64 my $page_trimmed=$page;
66 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
68 $p_trimmed=~s/^\Q$dir\E// &&
69 $page_trimmed=~s/^\Q$dir\E//;
71 push @links, { url => $href, page => $p_trimmed };
75 return sort { $a->{page} cmp $b->{page} } @links;
78 sub parentlinks ($) { #{{{
85 foreach my $dir (reverse split("/", $page)) {
88 unshift @ret, { url => "$path$dir.html", page => $dir };
94 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
98 sub preprocess ($$;$) { #{{{
101 my $onlystrip=shift || 0; # strip directives without processing
107 if (length $escape) {
108 return "[[$command $params]]";
113 elsif (exists $hooks{preprocess}{$command}) {
114 # Note: preserve order of params, some plugins may
115 # consider it significant.
117 while ($params =~ /(\w+)=\"?([^"]+)"?(\s+|$)/g) {
118 push @params, $1, $2;
120 return $hooks{preprocess}{$command}{call}->(@params, page => $page);
123 return "[[$command not processed]]";
127 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
131 sub add_depends ($$) { #{{{
135 if (! exists $depends{$page}) {
136 $depends{$page}=$globlist;
139 $depends{$page}=globlist_merge($depends{$page}, $globlist);
143 sub globlist_merge ($$) { #{{{
148 # Only add negated globs if they are not matched by the other globlist.
149 foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
150 (map { [ $b, $_ ] } split(" ", $a))) {
151 if ($i->[1]=~/^!(.*)/) {
152 if (! globlist_match($1, $i->[0])) {
164 sub genpage ($$$) { #{{{
169 my $title=pagetitle(basename($page));
171 my $template=HTML::Template->new(blind_cache => 1,
172 filename => "$config{templatedir}/page.tmpl");
175 if (length $config{cgiurl}) {
176 $template->param(editurl => cgiurl(do => "edit", page => $page));
177 $template->param(prefsurl => cgiurl(do => "prefs"));
179 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
184 if (length $config{historyurl}) {
185 my $u=$config{historyurl};
186 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
187 $template->param(historyurl => $u);
190 if ($config{discussion}) {
191 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
196 $template->param(have_actions => 1);
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}->($page, $template);
215 return $template->output;
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 rendered from ".
226 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
228 ", before, so not rendering from $src");
232 sub displaytime ($) { #{{{
235 if ($config{timeformat} eq '%c') {
236 return scalar(localtime($time)); # optimisation
240 return POSIX::strftime($config{timeformat}, localtime($time));
247 return (stat($file))[9];
250 sub findlinks ($$) { #{{{
255 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
256 push @links, titlepage($2);
258 if ($config{discussion}) {
259 # Discussion links are a special case since they're not in the
260 # text of the page, but on its template.
261 return @links, "$page/discussion";
268 sub render ($) { #{{{
271 my $type=pagetype($file);
272 my $srcfile=srcfile($file);
273 if ($type ne 'unknown') {
274 my $content=readfile($srcfile);
275 my $page=pagename($file);
276 delete $depends{$page};
278 if (exists $hooks{filter}) {
279 foreach my $id (keys %{$hooks{filter}}) {
280 $content=$hooks{filter}{$id}{call}->(
287 $links{$page}=[findlinks($page, $content)];
289 $content=linkify($page, $page, $content);
290 $content=preprocess($page, $content);
291 $content=htmlize($type, $content);
293 check_overwrite("$config{destdir}/".htmlpage($page), $page);
294 writefile(htmlpage($page), $config{destdir},
295 genpage($page, $content, mtime($srcfile)));
296 $oldpagemtime{$page}=time;
297 $renderedfiles{$page}=htmlpage($page);
300 my $content=readfile($srcfile, 1);
302 delete $depends{$file};
303 check_overwrite("$config{destdir}/$file", $file);
304 writefile($file, $config{destdir}, $content, 1);
305 $oldpagemtime{$file}=time;
306 $renderedfiles{$file}=$file;
314 my $dir=dirname($file);
315 while (rmdir($dir)) {
320 sub refresh () { #{{{
321 # find existing pages
324 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;
347 if (/$config{wiki_file_prune_regexp}/) {
348 $File::Find::prune=1;
350 elsif (! -d $_ && ! -l $_) {
351 my ($f)=/$config{wiki_file_regexp}/; # untaint
353 warn("skipping bad filename $_\n");
356 # Don't add files that are in the
358 $f=~s/^\Q$config{underlaydir}\E\/?//;
359 if (! -e "$config{srcdir}/$f" &&
360 ! -l "$config{srcdir}/$f") {
362 $exists{pagename($f)}=1;
367 }, $config{underlaydir});
371 # check for added or removed pages
373 foreach my $file (@files) {
374 my $page=pagename($file);
375 if (! $oldpagemtime{$page}) {
376 debug("new page $page") unless exists $pagectime{$page};
379 $pagesources{$page}=$file;
380 if ($config{getctime} && -e "$config{srcdir}/$file") {
381 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
383 elsif (! exists $pagectime{$page}) {
384 $pagectime{$page}=mtime(srcfile($file));
389 foreach my $page (keys %oldpagemtime) {
390 if (! $exists{$page}) {
391 debug("removing old page $page");
392 push @del, $pagesources{$page};
393 prune($config{destdir}."/".$renderedfiles{$page});
394 delete $renderedfiles{$page};
395 $oldpagemtime{$page}=0;
396 delete $pagesources{$page};
400 # render any updated files
401 foreach my $file (@files) {
402 my $page=pagename($file);
404 if (! exists $oldpagemtime{$page} ||
405 mtime(srcfile($file)) > $oldpagemtime{$page}) {
406 debug("rendering changed file $file");
412 # if any files were added or removed, check to see if each page
413 # needs an update due to linking to them or inlining them.
414 # TODO: inefficient; pages may get rendered above and again here;
415 # problem is the bestlink may have changed and we won't know until
418 FILE: foreach my $file (@files) {
419 my $page=pagename($file);
420 foreach my $f (@add, @del) {
422 foreach my $link (@{$links{$page}}) {
423 if (bestlink($page, $link) eq $p) {
424 debug("rendering $file, which links to $p");
434 # Handle backlinks; if a page has added/removed links, update the
435 # pages it links to. Also handles rebuilding dependant pages.
436 # TODO: inefficient; pages may get rendered above and again here;
437 # problem is the backlinks could be wrong in the first pass render
439 if (%rendered || @del) {
440 foreach my $f (@files) {
442 if (exists $depends{$p}) {
443 foreach my $file (keys %rendered, @del) {
445 my $page=pagename($file);
446 if (globlist_match($page, $depends{$p})) {
447 debug("rendering $f, which depends on $page");
457 foreach my $file (keys %rendered, @del) {
458 my $page=pagename($file);
460 if (exists $links{$page}) {
461 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
463 (! exists $oldlinks{$page} ||
464 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
465 $linkchanged{$link}=1;
469 if (exists $oldlinks{$page}) {
470 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
472 (! exists $links{$page} ||
473 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
474 $linkchanged{$link}=1;
479 foreach my $link (keys %linkchanged) {
480 my $linkfile=$pagesources{$link};
481 if (defined $linkfile) {
482 debug("rendering $linkfile, to update its backlinks");
484 $rendered{$linkfile}=1;
489 if (@del && exists $hooks{delete}) {
490 foreach my $id (keys %{$hooks{delete}}) {
491 $hooks{delete}{$id}{call}->(@del);
494 if (%rendered && exists $hooks{change}) {
495 foreach my $id (keys %{$hooks{change}}) {
496 $hooks{change}{$id}{call}->(keys %rendered);