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";
34 if ($type eq '.mdwn') {
35 $content=Markdown::Markdown($content);
38 error("htmlization of $type not supported");
41 if (exists $hooks{sanitize}) {
42 foreach my $id (keys %{$hooks{sanitize}}) {
43 $content=$hooks{sanitize}{$id}{call}->($content);
50 sub backlinks ($) { #{{{
54 foreach my $p (keys %links) {
55 next if bestlink($page, $p) eq $page;
56 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
57 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
59 # Trim common dir prefixes from both pages.
61 my $page_trimmed=$page;
63 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
65 $p_trimmed=~s/^\Q$dir\E// &&
66 $page_trimmed=~s/^\Q$dir\E//;
68 push @links, { url => $href, page => $p_trimmed };
72 return sort { $a->{page} cmp $b->{page} } @links;
75 sub parentlinks ($) { #{{{
82 foreach my $dir (reverse split("/", $page)) {
85 unshift @ret, { url => "$path$dir.html", page => $dir };
91 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
95 sub preprocess ($$;$) { #{{{
98 my $onlystrip=shift || 0; # strip directives without processing
104 if (length $escape) {
105 return "[[$command $params]]";
110 elsif (exists $hooks{preprocess}{$command}) {
111 # Note: preserve order of params, some plugins may
112 # consider it significant.
114 while ($params =~ /(\w+)=\"?([^"]+)"?(\s+|$)/g) {
115 push @params, $1, $2;
117 return $hooks{preprocess}{$command}{call}->(@params, page => $page);
120 return "[[$command not processed]]";
124 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
128 sub add_depends ($$) { #{{{
132 if (! exists $depends{$page}) {
133 $depends{$page}=$globlist;
136 $depends{$page}=globlist_merge($depends{$page}, $globlist);
140 sub globlist_merge ($$) { #{{{
145 # Only add negated globs if they are not matched by the other globlist.
146 foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
147 (map { [ $b, $_ ] } split(" ", $a))) {
148 if ($i->[1]=~/^!(.*)/) {
149 if (! globlist_match($1, $i->[0])) {
161 sub genpage ($$$) { #{{{
166 my $title=pagetitle(basename($page));
168 my $template=HTML::Template->new(blind_cache => 1,
169 filename => "$config{templatedir}/page.tmpl");
172 if (length $config{cgiurl}) {
173 $template->param(editurl => cgiurl(do => "edit", page => $page));
174 $template->param(prefsurl => cgiurl(do => "prefs"));
176 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
181 if (length $config{historyurl}) {
182 my $u=$config{historyurl};
183 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
184 $template->param(historyurl => $u);
187 if ($config{discussion}) {
188 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
193 $template->param(have_actions => 1);
198 wikiname => $config{wikiname},
199 parentlinks => [parentlinks($page)],
201 backlinks => [backlinks($page)],
202 mtime => displaytime($mtime),
203 styleurl => styleurl($page),
206 if (exists $hooks{pagetemplate}) {
207 foreach my $id (keys %{$hooks{pagetemplate}}) {
208 $hooks{pagetemplate}{$id}{call}->($page, $template);
212 return $template->output;
215 sub check_overwrite ($$) { #{{{
216 # Important security check. Make sure to call this before saving
217 # any files to the source directory.
221 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
222 error("$dest already exists and was rendered from ".
223 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
225 ", before, so not rendering from $src");
229 sub displaytime ($) { #{{{
232 if ($config{timeformat} eq '%c') {
233 return scalar(localtime($time)); # optimisation
237 return POSIX::strftime($config{timeformat}, localtime($time));
244 return (stat($file))[9];
247 sub findlinks ($$) { #{{{
252 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
253 push @links, titlepage($2);
255 if ($config{discussion}) {
256 # Discussion links are a special case since they're not in the
257 # text of the page, but on its template.
258 return @links, "$page/discussion";
265 sub render ($) { #{{{
268 my $type=pagetype($file);
269 my $srcfile=srcfile($file);
270 if ($type ne 'unknown') {
271 my $content=readfile($srcfile);
272 my $page=pagename($file);
273 delete $depends{$page};
275 if (exists $hooks{filter}) {
276 foreach my $id (keys %{$hooks{filter}}) {
277 $content=$hooks{filter}{$id}{call}->(
284 $links{$page}=[findlinks($page, $content)];
286 $content=linkify($page, $page, $content);
287 $content=preprocess($page, $content);
288 $content=htmlize($type, $content);
290 check_overwrite("$config{destdir}/".htmlpage($page), $page);
291 writefile(htmlpage($page), $config{destdir},
292 genpage($page, $content, mtime($srcfile)));
293 $oldpagemtime{$page}=time;
294 $renderedfiles{$page}=htmlpage($page);
297 my $content=readfile($srcfile, 1);
299 delete $depends{$file};
300 check_overwrite("$config{destdir}/$file", $file);
301 writefile($file, $config{destdir}, $content, 1);
302 $oldpagemtime{$file}=time;
303 $renderedfiles{$file}=$file;
311 my $dir=dirname($file);
312 while (rmdir($dir)) {
317 sub refresh () { #{{{
318 # find existing pages
321 eval q{use File::Find};
325 if (/$config{wiki_file_prune_regexp}/) {
326 $File::Find::prune=1;
328 elsif (! -d $_ && ! -l $_) {
329 my ($f)=/$config{wiki_file_regexp}/; # untaint
331 warn("skipping bad filename $_\n");
334 $f=~s/^\Q$config{srcdir}\E\/?//;
336 $exists{pagename($f)}=1;
344 if (/$config{wiki_file_prune_regexp}/) {
345 $File::Find::prune=1;
347 elsif (! -d $_ && ! -l $_) {
348 my ($f)=/$config{wiki_file_regexp}/; # untaint
350 warn("skipping bad filename $_\n");
353 # Don't add files that are in the
355 $f=~s/^\Q$config{underlaydir}\E\/?//;
356 if (! -e "$config{srcdir}/$f" &&
357 ! -l "$config{srcdir}/$f") {
359 $exists{pagename($f)}=1;
364 }, $config{underlaydir});
368 # check for added or removed pages
370 foreach my $file (@files) {
371 my $page=pagename($file);
372 if (! $oldpagemtime{$page}) {
373 debug("new page $page") unless exists $pagectime{$page};
376 $pagesources{$page}=$file;
377 if ($config{getctime} && -e "$config{srcdir}/$file") {
378 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
380 elsif (! exists $pagectime{$page}) {
381 $pagectime{$page}=mtime(srcfile($file));
386 foreach my $page (keys %oldpagemtime) {
387 if (! $exists{$page}) {
388 debug("removing old page $page");
389 push @del, $pagesources{$page};
390 prune($config{destdir}."/".$renderedfiles{$page});
391 delete $renderedfiles{$page};
392 $oldpagemtime{$page}=0;
393 delete $pagesources{$page};
397 # render any updated files
398 foreach my $file (@files) {
399 my $page=pagename($file);
401 if (! exists $oldpagemtime{$page} ||
402 mtime(srcfile($file)) > $oldpagemtime{$page}) {
403 debug("rendering changed file $file");
409 # if any files were added or removed, check to see if each page
410 # needs an update due to linking to them or inlining them.
411 # TODO: inefficient; pages may get rendered above and again here;
412 # problem is the bestlink may have changed and we won't know until
415 FILE: foreach my $file (@files) {
416 my $page=pagename($file);
417 foreach my $f (@add, @del) {
419 foreach my $link (@{$links{$page}}) {
420 if (bestlink($page, $link) eq $p) {
421 debug("rendering $file, which links to $p");
431 # Handle backlinks; if a page has added/removed links, update the
432 # pages it links to. Also handles rebuilding dependant pages.
433 # TODO: inefficient; pages may get rendered above and again here;
434 # problem is the backlinks could be wrong in the first pass render
436 if (%rendered || @del) {
437 foreach my $f (@files) {
439 if (exists $depends{$p}) {
440 foreach my $file (keys %rendered, @del) {
442 my $page=pagename($file);
443 if (globlist_match($page, $depends{$p})) {
444 debug("rendering $f, which depends on $page");
454 foreach my $file (keys %rendered, @del) {
455 my $page=pagename($file);
457 if (exists $links{$page}) {
458 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
460 (! exists $oldlinks{$page} ||
461 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
462 $linkchanged{$link}=1;
466 if (exists $oldlinks{$page}) {
467 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
469 (! exists $links{$page} ||
470 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
471 $linkchanged{$link}=1;
476 foreach my $link (keys %linkchanged) {
477 my $linkfile=$pagesources{$link};
478 if (defined $linkfile) {
479 debug("rendering $linkfile, to update its backlinks");
481 $rendered{$linkfile}=1;
486 if (@del && exists $hooks{delete}) {
487 foreach my $id (keys %{$hooks{delete}}) {
488 $hooks{delete}{$id}{call}->(@del);
491 if (%rendered && exists $hooks{change}) {
492 foreach my $id (keys %{$hooks{change}}) {
493 $hooks{change}{$id}{call}->(keys %rendered);