10 sub linkify ($$) { #{{{
14 $content =~ s{(\\?)$config{wiki_link_regexp}}{
15 $2 ? ( $1 ? "[[$2|$3]]" : htmllink($page, titlepage($3), 0, 0, pagetitle($2)))
16 : ( $1 ? "[[$3]]" : htmllink($page, titlepage($3)))
22 sub htmlize ($$) { #{{{
26 if (! $INC{"/usr/bin/markdown"}) {
28 $blosxom::version="is a proper perl module too much to ask?";
30 do "/usr/bin/markdown";
33 if ($type eq '.mdwn') {
34 $content=Markdown::Markdown($content);
37 error("htmlization of $type not supported");
40 if (exists $hooks{sanitize}) {
41 foreach my $id (keys %{$hooks{sanitize}}) {
42 $content=$hooks{sanitize}{$id}{call}->($content);
49 sub backlinks ($) { #{{{
53 foreach my $p (keys %links) {
54 next if bestlink($page, $p) eq $page;
55 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
56 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
58 # Trim common dir prefixes from both pages.
60 my $page_trimmed=$page;
62 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
64 $p_trimmed=~s/^\Q$dir\E// &&
65 $page_trimmed=~s/^\Q$dir\E//;
67 push @links, { url => $href, page => $p_trimmed };
71 return sort { $a->{page} cmp $b->{page} } @links;
74 sub parentlinks ($) { #{{{
81 foreach my $dir (reverse split("/", $page)) {
84 unshift @ret, { url => "$path$dir.html", page => $dir };
90 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
94 sub preprocess ($$) { #{{{
102 if (length $escape) {
103 return "[[$command $params]]";
105 elsif (exists $hooks{preprocess}{$command}) {
107 while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
110 return $hooks{preprocess}{$command}{call}->(page => $page, %params);
113 return "[[$command not processed]]";
117 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
121 sub add_depends ($$) { #{{{
125 if (! exists $depends{$page}) {
126 $depends{$page}=$globlist;
129 $depends{$page}=globlist_merge($depends{$page}, $globlist);
133 sub globlist_merge ($$) { #{{{
138 # Only add negated globs if they are not matched by the other globlist.
139 foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
140 (map { [ $b, $_ ] } split(" ", $a))) {
141 if ($i->[1]=~/^!(.*)/) {
142 if (! globlist_match($1, $i->[0])) {
154 sub genpage ($$$) { #{{{
159 my $title=pagetitle(basename($page));
161 my $template=HTML::Template->new(blind_cache => 1,
162 filename => "$config{templatedir}/page.tmpl");
164 if (length $config{cgiurl}) {
165 $template->param(editurl => cgiurl(do => "edit", page => $page));
166 $template->param(prefsurl => cgiurl(do => "prefs"));
168 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
172 if (length $config{historyurl}) {
173 my $u=$config{historyurl};
174 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
175 $template->param(historyurl => $u);
177 if ($config{discussion}) {
178 $template->param(discussionlink => htmllink($page, "Discussion", 1, 1));
180 $template->param(headercontent => $config{headercontent});
184 wikiname => $config{wikiname},
185 parentlinks => [parentlinks($page)],
187 backlinks => [backlinks($page)],
188 mtime => scalar(gmtime($mtime)),
189 styleurl => styleurl($page),
192 return $template->output;
195 sub check_overwrite ($$) { #{{{
196 # Important security check. Make sure to call this before saving
197 # any files to the source directory.
201 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
202 error("$dest already exists and was rendered from ".
203 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
205 ", before, so not rendering from $src");
212 return (stat($file))[9];
215 sub findlinks ($$) { #{{{
220 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
221 push @links, titlepage($2);
223 if ($config{discussion}) {
224 # Discussion links are a special case since they're not in the
225 # text of the page, but on its template.
226 return @links, "$page/discussion";
233 sub render ($) { #{{{
236 my $type=pagetype($file);
237 my $srcfile=srcfile($file);
238 if ($type ne 'unknown') {
239 my $content=readfile($srcfile);
240 my $page=pagename($file);
241 delete $depends{$page};
243 if (exists $hooks{filter}) {
244 foreach my $id (keys %{$hooks{filter}}) {
245 $content=$hooks{filter}{$id}{call}->(
252 $links{$page}=[findlinks($content, $page)];
254 $content=linkify($content, $page);
255 $content=preprocess($page, $content);
256 $content=htmlize($type, $content);
258 check_overwrite("$config{destdir}/".htmlpage($page), $page);
259 writefile(htmlpage($page), $config{destdir},
260 genpage($content, $page, mtime($srcfile)));
261 $oldpagemtime{$page}=time;
262 $renderedfiles{$page}=htmlpage($page);
265 my $content=readfile($srcfile, 1);
267 delete $depends{$file};
268 check_overwrite("$config{destdir}/$file", $file);
269 writefile($file, $config{destdir}, $content, 1);
270 $oldpagemtime{$file}=time;
271 $renderedfiles{$file}=$file;
279 my $dir=dirname($file);
280 while (rmdir($dir)) {
285 sub refresh () { #{{{
286 # find existing pages
289 eval q{use File::Find};
293 if (/$config{wiki_file_prune_regexp}/) {
294 $File::Find::prune=1;
296 elsif (! -d $_ && ! -l $_) {
297 my ($f)=/$config{wiki_file_regexp}/; # untaint
299 warn("skipping bad filename $_\n");
302 $f=~s/^\Q$config{srcdir}\E\/?//;
304 $exists{pagename($f)}=1;
312 if (/$config{wiki_file_prune_regexp}/) {
313 $File::Find::prune=1;
315 elsif (! -d $_ && ! -l $_) {
316 my ($f)=/$config{wiki_file_regexp}/; # untaint
318 warn("skipping bad filename $_\n");
321 # Don't add files that are in the
323 $f=~s/^\Q$config{underlaydir}\E\/?//;
324 if (! -e "$config{srcdir}/$f" &&
325 ! -l "$config{srcdir}/$f") {
327 $exists{pagename($f)}=1;
332 }, $config{underlaydir});
336 # check for added or removed pages
338 foreach my $file (@files) {
339 my $page=pagename($file);
340 if (! $oldpagemtime{$page}) {
341 debug("new page $page") unless exists $pagectime{$page};
344 $pagesources{$page}=$file;
345 $pagectime{$page}=mtime(srcfile($file))
346 unless exists $pagectime{$page};
350 foreach my $page (keys %oldpagemtime) {
351 if (! $exists{$page}) {
352 debug("removing old page $page");
353 push @del, $pagesources{$page};
354 prune($config{destdir}."/".$renderedfiles{$page});
355 delete $renderedfiles{$page};
356 $oldpagemtime{$page}=0;
357 delete $pagesources{$page};
361 # render any updated files
362 foreach my $file (@files) {
363 my $page=pagename($file);
365 if (! exists $oldpagemtime{$page} ||
366 mtime(srcfile($file)) > $oldpagemtime{$page}) {
367 debug("rendering changed file $file");
373 # if any files were added or removed, check to see if each page
374 # needs an update due to linking to them or inlining them.
375 # TODO: inefficient; pages may get rendered above and again here;
376 # problem is the bestlink may have changed and we won't know until
379 FILE: foreach my $file (@files) {
380 my $page=pagename($file);
381 foreach my $f (@add, @del) {
383 foreach my $link (@{$links{$page}}) {
384 if (bestlink($page, $link) eq $p) {
385 debug("rendering $file, which links to $p");
395 # Handle backlinks; if a page has added/removed links, update the
396 # pages it links to. Also handles rebuilding dependat pages.
397 # TODO: inefficient; pages may get rendered above and again here;
398 # problem is the backlinks could be wrong in the first pass render
400 if (%rendered || @del) {
401 foreach my $f (@files) {
403 if (exists $depends{$p}) {
404 foreach my $file (keys %rendered, @del) {
406 my $page=pagename($file);
407 if (globlist_match($page, $depends{$p})) {
408 debug("rendering $f, which depends on $page");
418 foreach my $file (keys %rendered, @del) {
419 my $page=pagename($file);
421 if (exists $links{$page}) {
422 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
424 (! exists $oldlinks{$page} ||
425 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
426 $linkchanged{$link}=1;
430 if (exists $oldlinks{$page}) {
431 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
433 (! exists $links{$page} ||
434 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
435 $linkchanged{$link}=1;
440 foreach my $link (keys %linkchanged) {
441 my $linkfile=$pagesources{$link};
442 if (defined $linkfile) {
443 debug("rendering $linkfile, to update its backlinks");
445 $rendered{$linkfile}=1;
450 if (@del && exists $hooks{delete}) {
451 foreach my $id (keys %{$hooks{delete}}) {
452 $hooks{delete}{$id}{call}->(@del);
455 if (%rendered && exists $hooks{change}) {
456 foreach my $id (keys %{$hooks{change}}) {
457 $hooks{change}{$id}{call}->(keys %rendered);