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 ($$) { #{{{
106 if (length $escape) {
107 return "[[$command $params]]";
109 elsif (exists $hooks{preprocess}{$command}) {
111 while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
114 return $hooks{preprocess}{$command}{call}->(page => $page, %params);
117 return "[[$command not processed]]";
121 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
125 sub add_depends ($$) { #{{{
129 if (! exists $depends{$page}) {
130 $depends{$page}=$globlist;
133 $depends{$page}=globlist_merge($depends{$page}, $globlist);
137 sub globlist_merge ($$) { #{{{
142 # Only add negated globs if they are not matched by the other globlist.
143 foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
144 (map { [ $b, $_ ] } split(" ", $a))) {
145 if ($i->[1]=~/^!(.*)/) {
146 if (! globlist_match($1, $i->[0])) {
158 sub genpage ($$$) { #{{{
163 my $title=pagetitle(basename($page));
165 my $template=HTML::Template->new(blind_cache => 1,
166 filename => "$config{templatedir}/page.tmpl");
169 if (length $config{cgiurl}) {
170 $template->param(editurl => cgiurl(do => "edit", page => $page));
171 $template->param(prefsurl => cgiurl(do => "prefs"));
173 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
178 if (length $config{historyurl}) {
179 my $u=$config{historyurl};
180 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
181 $template->param(historyurl => $u);
184 if ($config{discussion}) {
185 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
190 $template->param(have_actions => 1);
193 if (exists $hooks{pagetemplate}) {
194 foreach my $id (keys %{$hooks{pagetemplate}}) {
195 $hooks{pagetemplate}{$id}{call}->($page, $template);
201 wikiname => $config{wikiname},
202 parentlinks => [parentlinks($page)],
204 backlinks => [backlinks($page)],
205 mtime => displaytime($mtime),
206 styleurl => styleurl($page),
209 return $template->output;
212 sub check_overwrite ($$) { #{{{
213 # Important security check. Make sure to call this before saving
214 # any files to the source directory.
218 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
219 error("$dest already exists and was rendered from ".
220 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
222 ", before, so not rendering from $src");
226 sub displaytime ($) { #{{{
229 if ($config{timeformat} eq '%c') {
230 return scalar(localtime($time)); # optimisation
234 return POSIX::strftime($config{timeformat}, localtime($time));
241 return (stat($file))[9];
244 sub findlinks ($$) { #{{{
249 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
250 push @links, titlepage($2);
252 if ($config{discussion}) {
253 # Discussion links are a special case since they're not in the
254 # text of the page, but on its template.
255 return @links, "$page/discussion";
262 sub render ($) { #{{{
265 my $type=pagetype($file);
266 my $srcfile=srcfile($file);
267 if ($type ne 'unknown') {
268 my $content=readfile($srcfile);
269 my $page=pagename($file);
270 delete $depends{$page};
272 if (exists $hooks{filter}) {
273 foreach my $id (keys %{$hooks{filter}}) {
274 $content=$hooks{filter}{$id}{call}->(
281 $links{$page}=[findlinks($page, $content)];
283 $content=linkify($page, $page, $content);
284 $content=preprocess($page, $content);
285 $content=htmlize($type, $content);
287 check_overwrite("$config{destdir}/".htmlpage($page), $page);
288 writefile(htmlpage($page), $config{destdir},
289 genpage($page, $content, mtime($srcfile)));
290 $oldpagemtime{$page}=time;
291 $renderedfiles{$page}=htmlpage($page);
294 my $content=readfile($srcfile, 1);
296 delete $depends{$file};
297 check_overwrite("$config{destdir}/$file", $file);
298 writefile($file, $config{destdir}, $content, 1);
299 $oldpagemtime{$file}=time;
300 $renderedfiles{$file}=$file;
308 my $dir=dirname($file);
309 while (rmdir($dir)) {
314 sub refresh () { #{{{
315 # find existing pages
318 eval q{use File::Find};
322 if (/$config{wiki_file_prune_regexp}/) {
323 $File::Find::prune=1;
325 elsif (! -d $_ && ! -l $_) {
326 my ($f)=/$config{wiki_file_regexp}/; # untaint
328 warn("skipping bad filename $_\n");
331 $f=~s/^\Q$config{srcdir}\E\/?//;
333 $exists{pagename($f)}=1;
341 if (/$config{wiki_file_prune_regexp}/) {
342 $File::Find::prune=1;
344 elsif (! -d $_ && ! -l $_) {
345 my ($f)=/$config{wiki_file_regexp}/; # untaint
347 warn("skipping bad filename $_\n");
350 # Don't add files that are in the
352 $f=~s/^\Q$config{underlaydir}\E\/?//;
353 if (! -e "$config{srcdir}/$f" &&
354 ! -l "$config{srcdir}/$f") {
356 $exists{pagename($f)}=1;
361 }, $config{underlaydir});
365 # check for added or removed pages
367 foreach my $file (@files) {
368 my $page=pagename($file);
369 if (! $oldpagemtime{$page}) {
370 debug("new page $page") unless exists $pagectime{$page};
373 $pagesources{$page}=$file;
374 if ($config{getctime} && -e "$config{srcdir}/$file") {
375 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
377 elsif (! exists $pagectime{$page}) {
378 $pagectime{$page}=mtime(srcfile($file));
383 foreach my $page (keys %oldpagemtime) {
384 if (! $exists{$page}) {
385 debug("removing old page $page");
386 push @del, $pagesources{$page};
387 prune($config{destdir}."/".$renderedfiles{$page});
388 delete $renderedfiles{$page};
389 $oldpagemtime{$page}=0;
390 delete $pagesources{$page};
394 # render any updated files
395 foreach my $file (@files) {
396 my $page=pagename($file);
398 if (! exists $oldpagemtime{$page} ||
399 mtime(srcfile($file)) > $oldpagemtime{$page}) {
400 debug("rendering changed file $file");
406 # if any files were added or removed, check to see if each page
407 # needs an update due to linking to them or inlining them.
408 # TODO: inefficient; pages may get rendered above and again here;
409 # problem is the bestlink may have changed and we won't know until
412 FILE: foreach my $file (@files) {
413 my $page=pagename($file);
414 foreach my $f (@add, @del) {
416 foreach my $link (@{$links{$page}}) {
417 if (bestlink($page, $link) eq $p) {
418 debug("rendering $file, which links to $p");
428 # Handle backlinks; if a page has added/removed links, update the
429 # pages it links to. Also handles rebuilding dependat pages.
430 # TODO: inefficient; pages may get rendered above and again here;
431 # problem is the backlinks could be wrong in the first pass render
433 if (%rendered || @del) {
434 foreach my $f (@files) {
436 if (exists $depends{$p}) {
437 foreach my $file (keys %rendered, @del) {
439 my $page=pagename($file);
440 if (globlist_match($page, $depends{$p})) {
441 debug("rendering $f, which depends on $page");
451 foreach my $file (keys %rendered, @del) {
452 my $page=pagename($file);
454 if (exists $links{$page}) {
455 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
457 (! exists $oldlinks{$page} ||
458 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
459 $linkchanged{$link}=1;
463 if (exists $oldlinks{$page}) {
464 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
466 (! exists $links{$page} ||
467 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
468 $linkchanged{$link}=1;
473 foreach my $link (keys %linkchanged) {
474 my $linkfile=$pagesources{$link};
475 if (defined $linkfile) {
476 debug("rendering $linkfile, to update its backlinks");
478 $rendered{$linkfile}=1;
483 if (@del && exists $hooks{delete}) {
484 foreach my $id (keys %{$hooks{delete}}) {
485 $hooks{delete}{$id}{call}->(@del);
488 if (%rendered && exists $hooks{change}) {
489 foreach my $id (keys %{$hooks{change}}) {
490 $hooks{change}{$id}{call}->(keys %rendered);