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 $content=Encode::decode_utf8(Markdown::Markdown(Encode::encode_utf8($content)));
39 error("htmlization of $type not supported");
42 if (exists $hooks{sanitize}) {
43 foreach my $id (keys %{$hooks{sanitize}}) {
44 $content=$hooks{sanitize}{$id}{call}->($content);
51 sub backlinks ($) { #{{{
55 foreach my $p (keys %links) {
56 next if bestlink($page, $p) eq $page;
57 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
58 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
60 # Trim common dir prefixes from both pages.
62 my $page_trimmed=$page;
64 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
66 $p_trimmed=~s/^\Q$dir\E// &&
67 $page_trimmed=~s/^\Q$dir\E//;
69 push @links, { url => $href, page => $p_trimmed };
73 return sort { $a->{page} cmp $b->{page} } @links;
76 sub parentlinks ($) { #{{{
83 foreach my $dir (reverse split("/", $page)) {
86 unshift @ret, { url => "$path$dir.html", page => $dir };
92 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
96 sub preprocess ($$;$) { #{{{
99 my $onlystrip=shift || 0; # strip directives without processing
105 if (length $escape) {
106 return "[[$command $params]]";
111 elsif (exists $hooks{preprocess}{$command}) {
112 # Note: preserve order of params, some plugins may
113 # consider it significant.
115 while ($params =~ /(\w+)=\"?([^"]+)"?(\s+|$)/g) {
116 push @params, $1, $2;
118 return $hooks{preprocess}{$command}{call}->(@params, page => $page);
121 return "[[$command not processed]]";
125 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
129 sub add_depends ($$) { #{{{
133 if (! exists $depends{$page}) {
134 $depends{$page}=$globlist;
137 $depends{$page}=globlist_merge($depends{$page}, $globlist);
141 sub globlist_merge ($$) { #{{{
146 # Only add negated globs if they are not matched by the other globlist.
147 foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
148 (map { [ $b, $_ ] } split(" ", $a))) {
149 if ($i->[1]=~/^!(.*)/) {
150 if (! globlist_match($1, $i->[0])) {
162 sub genpage ($$$) { #{{{
167 my $title=pagetitle(basename($page));
169 my $template=HTML::Template->new(blind_cache => 1,
170 filename => "$config{templatedir}/page.tmpl");
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);
199 wikiname => $config{wikiname},
200 parentlinks => [parentlinks($page)],
202 backlinks => [backlinks($page)],
203 mtime => displaytime($mtime),
204 styleurl => styleurl($page),
207 if (exists $hooks{pagetemplate}) {
208 foreach my $id (keys %{$hooks{pagetemplate}}) {
209 $hooks{pagetemplate}{$id}{call}->($page, $template);
213 return $template->output;
216 sub check_overwrite ($$) { #{{{
217 # Important security check. Make sure to call this before saving
218 # any files to the source directory.
222 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
223 error("$dest already exists and was rendered from ".
224 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
226 ", before, so not rendering from $src");
230 sub displaytime ($) { #{{{
233 if ($config{timeformat} eq '%c' && ! exists $ENV{LC_CTIME}) {
234 return scalar(localtime($time)); # optimisation
238 return POSIX::strftime($config{timeformat}, localtime($time));
245 return (stat($file))[9];
248 sub findlinks ($$) { #{{{
253 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
254 push @links, titlepage($2);
256 if ($config{discussion}) {
257 # Discussion links are a special case since they're not in the
258 # text of the page, but on its template.
259 return @links, "$page/discussion";
266 sub render ($) { #{{{
269 my $type=pagetype($file);
270 my $srcfile=srcfile($file);
271 if ($type ne 'unknown') {
272 my $content=readfile($srcfile);
273 my $page=pagename($file);
274 delete $depends{$page};
276 if (exists $hooks{filter}) {
277 foreach my $id (keys %{$hooks{filter}}) {
278 $content=$hooks{filter}{$id}{call}->(
285 $links{$page}=[findlinks($page, $content)];
287 $content=linkify($page, $page, $content);
288 $content=preprocess($page, $content);
289 $content=htmlize($type, $content);
291 check_overwrite("$config{destdir}/".htmlpage($page), $page);
292 writefile(htmlpage($page), $config{destdir},
293 genpage($page, $content, mtime($srcfile)));
294 $oldpagemtime{$page}=time;
295 $renderedfiles{$page}=htmlpage($page);
298 my $content=readfile($srcfile, 1);
300 delete $depends{$file};
301 check_overwrite("$config{destdir}/$file", $file);
302 writefile($file, $config{destdir}, $content, 1);
303 $oldpagemtime{$file}=time;
304 $renderedfiles{$file}=$file;
312 my $dir=dirname($file);
313 while (rmdir($dir)) {
318 sub refresh () { #{{{
319 # find existing pages
322 eval q{use File::Find};
326 if (/$config{wiki_file_prune_regexp}/) {
327 $File::Find::prune=1;
329 elsif (! -d $_ && ! -l $_) {
330 my ($f)=/$config{wiki_file_regexp}/; # untaint
332 warn("skipping bad filename $_\n");
335 $f=~s/^\Q$config{srcdir}\E\/?//;
337 $exists{pagename($f)}=1;
345 if (/$config{wiki_file_prune_regexp}/) {
346 $File::Find::prune=1;
348 elsif (! -d $_ && ! -l $_) {
349 my ($f)=/$config{wiki_file_regexp}/; # untaint
351 warn("skipping bad filename $_\n");
354 # Don't add files that are in the
356 $f=~s/^\Q$config{underlaydir}\E\/?//;
357 if (! -e "$config{srcdir}/$f" &&
358 ! -l "$config{srcdir}/$f") {
360 $exists{pagename($f)}=1;
365 }, $config{underlaydir});
369 # check for added or removed pages
371 foreach my $file (@files) {
372 my $page=pagename($file);
373 if (! $oldpagemtime{$page}) {
374 debug("new page $page") unless exists $pagectime{$page};
377 $pagesources{$page}=$file;
378 if ($config{getctime} && -e "$config{srcdir}/$file") {
379 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
381 elsif (! exists $pagectime{$page}) {
382 $pagectime{$page}=mtime(srcfile($file));
387 foreach my $page (keys %oldpagemtime) {
388 if (! $exists{$page}) {
389 debug("removing old page $page");
390 push @del, $pagesources{$page};
391 prune($config{destdir}."/".$renderedfiles{$page});
392 delete $renderedfiles{$page};
393 $oldpagemtime{$page}=0;
394 delete $pagesources{$page};
398 # render any updated files
399 foreach my $file (@files) {
400 my $page=pagename($file);
402 if (! exists $oldpagemtime{$page} ||
403 mtime(srcfile($file)) > $oldpagemtime{$page}) {
404 debug("rendering changed file $file");
410 # if any files were added or removed, check to see if each page
411 # needs an update due to linking to them or inlining them.
412 # TODO: inefficient; pages may get rendered above and again here;
413 # problem is the bestlink may have changed and we won't know until
416 FILE: foreach my $file (@files) {
417 my $page=pagename($file);
418 foreach my $f (@add, @del) {
420 foreach my $link (@{$links{$page}}) {
421 if (bestlink($page, $link) eq $p) {
422 debug("rendering $file, which links to $p");
432 # Handle backlinks; if a page has added/removed links, update the
433 # pages it links to. Also handles rebuilding dependant pages.
434 # TODO: inefficient; pages may get rendered above and again here;
435 # problem is the backlinks could be wrong in the first pass render
437 if (%rendered || @del) {
438 foreach my $f (@files) {
440 if (exists $depends{$p}) {
441 foreach my $file (keys %rendered, @del) {
443 my $page=pagename($file);
444 if (globlist_match($page, $depends{$p})) {
445 debug("rendering $f, which depends on $page");
455 foreach my $file (keys %rendered, @del) {
456 my $page=pagename($file);
458 if (exists $links{$page}) {
459 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
461 (! exists $oldlinks{$page} ||
462 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
463 $linkchanged{$link}=1;
467 if (exists $oldlinks{$page}) {
468 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
470 (! exists $links{$page} ||
471 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
472 $linkchanged{$link}=1;
477 foreach my $link (keys %linkchanged) {
478 my $linkfile=$pagesources{$link};
479 if (defined $linkfile) {
480 debug("rendering $linkfile, to update its backlinks");
482 $rendered{$linkfile}=1;
487 if (@del && exists $hooks{delete}) {
488 foreach my $id (keys %{$hooks{delete}}) {
489 $hooks{delete}{$id}{call}->(@del);
492 if (%rendered && exists $hooks{change}) {
493 foreach my $id (keys %{$hooks{change}}) {
494 $hooks{change}{$id}{call}->(keys %rendered);