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 => scalar(gmtime($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");
229 return (stat($file))[9];
232 sub findlinks ($$) { #{{{
237 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
238 push @links, titlepage($2);
240 if ($config{discussion}) {
241 # Discussion links are a special case since they're not in the
242 # text of the page, but on its template.
243 return @links, "$page/discussion";
250 sub render ($) { #{{{
253 my $type=pagetype($file);
254 my $srcfile=srcfile($file);
255 if ($type ne 'unknown') {
256 my $content=readfile($srcfile);
257 my $page=pagename($file);
258 delete $depends{$page};
260 if (exists $hooks{filter}) {
261 foreach my $id (keys %{$hooks{filter}}) {
262 $content=$hooks{filter}{$id}{call}->(
269 $links{$page}=[findlinks($page, $content)];
271 $content=linkify($page, $page, $content);
272 $content=preprocess($page, $content);
273 $content=htmlize($type, $content);
275 check_overwrite("$config{destdir}/".htmlpage($page), $page);
276 writefile(htmlpage($page), $config{destdir},
277 genpage($page, $content, mtime($srcfile)));
278 $oldpagemtime{$page}=time;
279 $renderedfiles{$page}=htmlpage($page);
282 my $content=readfile($srcfile, 1);
284 delete $depends{$file};
285 check_overwrite("$config{destdir}/$file", $file);
286 writefile($file, $config{destdir}, $content, 1);
287 $oldpagemtime{$file}=time;
288 $renderedfiles{$file}=$file;
296 my $dir=dirname($file);
297 while (rmdir($dir)) {
302 sub refresh () { #{{{
303 # find existing pages
306 eval q{use File::Find};
310 if (/$config{wiki_file_prune_regexp}/) {
311 $File::Find::prune=1;
313 elsif (! -d $_ && ! -l $_) {
314 my ($f)=/$config{wiki_file_regexp}/; # untaint
316 warn("skipping bad filename $_\n");
319 $f=~s/^\Q$config{srcdir}\E\/?//;
321 $exists{pagename($f)}=1;
329 if (/$config{wiki_file_prune_regexp}/) {
330 $File::Find::prune=1;
332 elsif (! -d $_ && ! -l $_) {
333 my ($f)=/$config{wiki_file_regexp}/; # untaint
335 warn("skipping bad filename $_\n");
338 # Don't add files that are in the
340 $f=~s/^\Q$config{underlaydir}\E\/?//;
341 if (! -e "$config{srcdir}/$f" &&
342 ! -l "$config{srcdir}/$f") {
344 $exists{pagename($f)}=1;
349 }, $config{underlaydir});
353 # check for added or removed pages
355 foreach my $file (@files) {
356 my $page=pagename($file);
357 if (! $oldpagemtime{$page}) {
358 debug("new page $page") unless exists $pagectime{$page};
361 $pagesources{$page}=$file;
362 if ($config{getctime} && -e "$config{srcdir}/$file") {
363 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
365 elsif (! exists $pagectime{$page}) {
366 $pagectime{$page}=mtime(srcfile($file));
371 foreach my $page (keys %oldpagemtime) {
372 if (! $exists{$page}) {
373 debug("removing old page $page");
374 push @del, $pagesources{$page};
375 prune($config{destdir}."/".$renderedfiles{$page});
376 delete $renderedfiles{$page};
377 $oldpagemtime{$page}=0;
378 delete $pagesources{$page};
382 # render any updated files
383 foreach my $file (@files) {
384 my $page=pagename($file);
386 if (! exists $oldpagemtime{$page} ||
387 mtime(srcfile($file)) > $oldpagemtime{$page}) {
388 debug("rendering changed file $file");
394 # if any files were added or removed, check to see if each page
395 # needs an update due to linking to them or inlining them.
396 # TODO: inefficient; pages may get rendered above and again here;
397 # problem is the bestlink may have changed and we won't know until
400 FILE: foreach my $file (@files) {
401 my $page=pagename($file);
402 foreach my $f (@add, @del) {
404 foreach my $link (@{$links{$page}}) {
405 if (bestlink($page, $link) eq $p) {
406 debug("rendering $file, which links to $p");
416 # Handle backlinks; if a page has added/removed links, update the
417 # pages it links to. Also handles rebuilding dependat pages.
418 # TODO: inefficient; pages may get rendered above and again here;
419 # problem is the backlinks could be wrong in the first pass render
421 if (%rendered || @del) {
422 foreach my $f (@files) {
424 if (exists $depends{$p}) {
425 foreach my $file (keys %rendered, @del) {
427 my $page=pagename($file);
428 if (globlist_match($page, $depends{$p})) {
429 debug("rendering $f, which depends on $page");
439 foreach my $file (keys %rendered, @del) {
440 my $page=pagename($file);
442 if (exists $links{$page}) {
443 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
445 (! exists $oldlinks{$page} ||
446 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
447 $linkchanged{$link}=1;
451 if (exists $oldlinks{$page}) {
452 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
454 (! exists $links{$page} ||
455 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
456 $linkchanged{$link}=1;
461 foreach my $link (keys %linkchanged) {
462 my $linkfile=$pagesources{$link};
463 if (defined $linkfile) {
464 debug("rendering $linkfile, to update its backlinks");
466 $rendered{$linkfile}=1;
471 if (@del && exists $hooks{delete}) {
472 foreach my $id (keys %{$hooks{delete}}) {
473 $hooks{delete}{$id}{call}->(@del);
476 if (%rendered && exists $hooks{change}) {
477 foreach my $id (keys %{$hooks{change}}) {
478 $hooks{change}{$id}{call}->(keys %rendered);