7 sub linkify ($$) { #{{{
11 $content =~ s{(\\?)$config{wiki_link_regexp}}{
12 $1 ? "[[$2]]" : htmllink($page, $2)
18 sub htmlize ($$) { #{{{
22 if (! $INC{"/usr/bin/markdown"}) {
24 $blosxom::version="is a proper perl module too much to ask?";
26 do "/usr/bin/markdown";
29 if ($type eq '.mdwn') {
30 return Markdown::Markdown($content);
33 error("htmlization of $type not supported");
37 sub backlinks ($) { #{{{
41 foreach my $p (keys %links) {
42 next if bestlink($page, $p) eq $page;
43 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
44 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
46 # Trim common dir prefixes from both pages.
48 my $page_trimmed=$page;
50 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
52 $p_trimmed=~s/^\Q$dir\E// &&
53 $page_trimmed=~s/^\Q$dir\E//;
55 push @links, { url => $href, page => $p_trimmed };
59 return sort { $a->{page} cmp $b->{page} } @links;
62 sub parentlinks ($) { #{{{
69 foreach my $dir (reverse split("/", $page)) {
72 unshift @ret, { url => "$path$dir.html", page => $dir };
78 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
82 sub rsspage ($) { #{{{
88 sub postprocess { #{{{
89 # Takes content to postprocess followed by a list of postprocessor
90 # commands and subroutine references to run for the commands.
100 "[[$command $params]]";
102 elsif (exists $commands{$command}) {
104 while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
107 $commands{$command}->($page, %params);
110 "[[bad directive $command]]";
114 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
118 sub blog_list ($$) { #{{{
123 foreach my $page (keys %pagesources) {
124 if (globlist_match($page, $globlist)) {
129 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
130 return @list if @list <= $maxitems;
131 return @list[0..$maxitems - 1];
134 sub get_inline_content ($$) { #{{{
135 my $parentpage=shift;
138 my $file=$pagesources{$page};
139 my $type=pagetype($file);
140 if ($type ne 'unknown') {
141 return htmlize($type, linkify(readfile("$config{srcdir}/$file"), $parentpage));
148 sub postprocess_html_inline { #{{{
149 my $parentpage=shift;
152 if (! exists $params{show}) {
155 if (! exists $params{pages}) {
158 $inlinepages{$parentpage}=$params{pages};
160 my $template=HTML::Template->new(blind_cache => 1,
161 filename => "$config{templatedir}/inlinepage.tmpl");
164 foreach my $page (blog_list($params{pages}, $params{show})) {
165 $template->param(pagelink => htmllink($parentpage, $page));
166 $template->param(content => get_inline_content($parentpage, $page));
167 $template->param(ctime => scalar(gmtime($pagectime{$page})));
168 $ret.=$template->output;
174 sub genpage ($$$) { #{{{
179 $content = postprocess($page, $content, inline => \&postprocess_html_inline);
181 my $title=pagetitle(basename($page));
183 my $template=HTML::Template->new(blind_cache => 1,
184 filename => "$config{templatedir}/page.tmpl");
186 if (length $config{cgiurl}) {
187 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
188 $template->param(prefsurl => "$config{cgiurl}?do=prefs");
190 $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
194 if (length $config{historyurl}) {
195 my $u=$config{historyurl};
196 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
197 $template->param(historyurl => $u);
201 $template->param(rssurl => rsspage($page));
206 wikiname => $config{wikiname},
207 parentlinks => [parentlinks($page)],
209 backlinks => [backlinks($page)],
210 discussionlink => htmllink($page, "Discussion", 1, 1),
211 mtime => scalar(gmtime($mtime)),
214 return $template->output;
217 sub date_822 ($) { #{{{
221 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
224 sub absolute_urls ($$) { #{{{
225 # sucky sub because rss sucks
231 $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
232 $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
236 sub genrss ($$$) { #{{{
241 my $url="$config{url}/".htmlpage($page);
243 my $template=HTML::Template->new(blind_cache => 1,
244 filename => "$config{templatedir}/rsspage.tmpl");
249 my $parentpage=shift;
252 if (! exists $params{show}) {
255 if (! exists $params{pages}) {
258 $inlinepages{$parentpage}=$params{pages};
261 foreach my $page (blog_list($params{pages}, $params{show})) {
263 itemtitle => pagetitle(basename($page)),
264 itemurl => "$config{url}/$renderedfiles{$page}",
265 itempubdate => date_822($pagectime{$page}),
266 itemcontent => absolute_urls(get_inline_content($parentpage, $page), $url),
267 } if exists $renderedfiles{$page};
273 $content = postprocess($page, $content, inline => $gen_blog);
275 # Regular page gets a feed that is updated every time the
276 # page is changed, so the mtime is encoded in the guid.
278 itemtitle => pagetitle(basename($page)),
279 itemguid => "$url?mtime=$mtime",
281 itempubdate => date_822($mtime),
282 itemcontent => absolute_urls($content, $url),
286 title => $config{wikiname},
291 return $template->output;
294 sub check_overwrite ($$) { #{{{
295 # Important security check. Make sure to call this before saving
296 # any files to the source directory.
300 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
301 error("$dest already exists and was rendered from ".
302 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
304 ", before, so not rendering from $src");
311 return (stat($page))[9];
314 sub findlinks ($$) { #{{{
319 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
322 # Discussion links are a special case since they're not in the text
323 # of the page, but on its template.
324 return @links, "$page/discussion";
327 sub render ($) { #{{{
330 my $type=pagetype($file);
331 my $content=readfile("$config{srcdir}/$file");
332 if ($type ne 'unknown') {
333 my $page=pagename($file);
335 $links{$page}=[findlinks($content, $page)];
337 $content=linkify($content, $page);
338 $content=htmlize($type, $content);
340 check_overwrite("$config{destdir}/".htmlpage($page), $page);
341 writefile("$config{destdir}/".htmlpage($page),
342 genpage($content, $page, mtime("$config{srcdir}/$file")));
343 $oldpagemtime{$page}=time;
344 $renderedfiles{$page}=htmlpage($page);
346 # TODO: should really add this to renderedfiles and call
347 # check_overwrite, as above, but currently renderedfiles
348 # only supports listing one file per page.
350 writefile("$config{destdir}/".rsspage($page),
351 genrss($content, $page, mtime("$config{srcdir}/$file")));
356 check_overwrite("$config{destdir}/$file", $file);
357 writefile("$config{destdir}/$file", $content);
358 $oldpagemtime{$file}=time;
359 $renderedfiles{$file}=$file;
367 my $dir=dirname($file);
368 while (rmdir($dir)) {
373 sub refresh () { #{{{
374 # find existing pages
377 eval q{use File::Find};
381 if (/$config{wiki_file_prune_regexp}/) {
383 $File::Find::prune=1;
386 elsif (! -d $_ && ! -l $_) {
387 my ($f)=/$config{wiki_file_regexp}/; # untaint
389 warn("skipping bad filename $_\n");
392 $f=~s/^\Q$config{srcdir}\E\/?//;
394 $exists{pagename($f)}=1;
402 # check for added or removed pages
404 foreach my $file (@files) {
405 my $page=pagename($file);
406 if (! $oldpagemtime{$page}) {
407 debug("new page $page") unless exists $pagectime{$page};
410 $pagesources{$page}=$file;
411 $pagectime{$page}=time unless exists $pagectime{$page};
415 foreach my $page (keys %oldpagemtime) {
416 if (! $exists{$page}) {
417 debug("removing old page $page");
418 push @del, $pagesources{$page};
419 prune($config{destdir}."/".$renderedfiles{$page});
420 delete $renderedfiles{$page};
421 $oldpagemtime{$page}=0;
422 delete $pagesources{$page};
426 # render any updated files
427 foreach my $file (@files) {
428 my $page=pagename($file);
430 if (! exists $oldpagemtime{$page} ||
431 mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
432 debug("rendering changed file $file");
438 # if any files were added or removed, check to see if each page
439 # needs an update due to linking to them or inlining them.
440 # TODO: inefficient; pages may get rendered above and again here;
441 # problem is the bestlink may have changed and we won't know until
444 FILE: foreach my $file (@files) {
445 my $page=pagename($file);
446 foreach my $f (@add, @del) {
448 foreach my $link (@{$links{$page}}) {
449 if (bestlink($page, $link) eq $p) {
450 debug("rendering $file, which links to $p");
456 if (exists $inlinepages{$page} &&
457 globlist_match($p, $inlinepages{$page})) {
458 debug("rendering $file, which inlines $p");
466 # handle backlinks; if a page has added/removed links, update the
468 # TODO: inefficient; pages may get rendered above and again here;
469 # problem is the backlinks could be wrong in the first pass render
473 foreach my $file (keys %rendered, @del) {
474 my $page=pagename($file);
475 if (exists $links{$page}) {
476 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
478 ! exists $oldlinks{$page} ||
479 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
480 $linkchanged{$link}=1;
484 if (exists $oldlinks{$page}) {
485 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
487 ! exists $links{$page} ||
488 ! grep { $_ eq $link } @{$links{$page}}) {
489 $linkchanged{$link}=1;
494 foreach my $link (keys %linkchanged) {
495 my $linkfile=$pagesources{$link};
496 if (defined $linkfile) {
497 debug("rendering $linkfile, to update its backlinks");