9 sub linkify ($$) { #{{{
13 $content =~ s{(\\?)$config{wiki_link_regexp}}{
14 $2 ? ( $1 ? "[[$2|$3]]" : htmllink($page, titlepage($3), 0, 0, pagetitle($2)))
15 : ( $1 ? "[[$3]]" : htmllink($page, titlepage($3)))
21 sub htmlize ($$) { #{{{
25 if (! $INC{"/usr/bin/markdown"}) {
27 $blosxom::version="is a proper perl module too much to ask?";
29 do "/usr/bin/markdown";
32 if ($type eq '.mdwn') {
33 return Markdown::Markdown($content);
36 error("htmlization of $type not supported");
40 sub backlinks ($) { #{{{
44 foreach my $p (keys %links) {
45 next if bestlink($page, $p) eq $page;
46 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
47 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
49 # Trim common dir prefixes from both pages.
51 my $page_trimmed=$page;
53 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
55 $p_trimmed=~s/^\Q$dir\E// &&
56 $page_trimmed=~s/^\Q$dir\E//;
58 push @links, { url => $href, page => $p_trimmed };
62 return sort { $a->{page} cmp $b->{page} } @links;
65 sub parentlinks ($) { #{{{
72 foreach my $dir (reverse split("/", $page)) {
75 unshift @ret, { url => "$path$dir.html", page => $dir };
81 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
85 sub rsspage ($) { #{{{
91 sub preprocess ($$) { #{{{
95 my %commands=(inline => \&preprocess_inline);
101 if (length $escape) {
102 "[[$command $params]]";
104 elsif (exists $commands{$command}) {
106 while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
109 $commands{$command}->($page, %params);
112 "[[bad directive $command]]";
116 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
120 sub blog_list ($$) { #{{{
125 foreach my $page (keys %pagesources) {
126 if (globlist_match($page, $globlist)) {
131 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
132 return @list if ! $maxitems || @list <= $maxitems;
133 return @list[0..$maxitems - 1];
136 sub get_inline_content ($$) { #{{{
137 my $parentpage=shift;
140 my $file=$pagesources{$page};
141 my $type=pagetype($file);
142 if ($type ne 'unknown') {
143 return htmlize($type, linkify(readfile(srcfile($file)), $parentpage));
150 sub preprocess_inline ($@) { #{{{
151 my $parentpage=shift;
154 if (! exists $params{pages}) {
157 if (! exists $params{archive}) {
158 $params{archive}="no";
160 if (! exists $params{show} && $params{archive} eq "no") {
163 $inlinepages{$parentpage}=$params{pages};
167 if (exists $params{rootpage}) {
168 my $formtemplate=HTML::Template->new(blind_cache => 1,
169 filename => "$config{templatedir}/blogpost.tmpl");
170 $formtemplate->param(cgiurl => $config{cgiurl});
171 $formtemplate->param(rootpage => $params{rootpage});
172 my $form=$formtemplate->output;
176 my $template=HTML::Template->new(blind_cache => 1,
177 filename => (($params{archive} eq "no")
178 ? "$config{templatedir}/inlinepage.tmpl"
179 : "$config{templatedir}/inlinepagetitle.tmpl"));
182 foreach my $page (blog_list($params{pages}, $params{show})) {
183 next if $page eq $parentpage;
185 $template->param(pagelink => htmllink($parentpage, $page));
186 $template->param(content => get_inline_content($parentpage, $page))
187 if $params{archive} eq "no";
188 $template->param(ctime => scalar(gmtime($pagectime{$page})));
189 $ret.=$template->output;
192 # TODO: should really add this to renderedfiles and call
193 # check_overwrite, but currently renderedfiles
194 # only supports listing one file per page.
196 writefile(rsspage($parentpage), $config{destdir},
197 genrss($parentpage, @pages));
203 sub genpage ($$$) { #{{{
208 my $title=pagetitle(basename($page));
210 my $template=HTML::Template->new(blind_cache => 1,
211 filename => "$config{templatedir}/page.tmpl");
213 if (length $config{cgiurl}) {
214 $template->param(editurl => cgiurl(do => "edit", page => $page));
215 $template->param(prefsurl => cgiurl(do => "prefs"));
217 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
221 if (length $config{historyurl}) {
222 my $u=$config{historyurl};
223 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
224 $template->param(historyurl => $u);
226 if ($config{hyperestraier}) {
227 $template->param(hyperestraierurl => cgiurl());
230 if ($config{rss} && $inlinepages{$page}) {
231 $template->param(rssurl => rsspage(basename($page)));
236 wikiname => $config{wikiname},
237 parentlinks => [parentlinks($page)],
239 backlinks => [backlinks($page)],
240 discussionlink => htmllink($page, "Discussion", 1, 1),
241 mtime => scalar(gmtime($mtime)),
242 styleurl => styleurl($page),
245 return $template->output;
248 sub date_822 ($) { #{{{
252 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
255 sub absolute_urls ($$) { #{{{
256 # sucky sub because rss sucks
262 $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
263 $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
267 sub genrss ($@) { #{{{
271 my $url="$config{url}/".htmlpage($page);
273 my $template=HTML::Template->new(blind_cache => 1,
274 filename => "$config{templatedir}/rsspage.tmpl");
277 foreach my $p (@pages) {
279 itemtitle => pagetitle(basename($p)),
280 itemurl => "$config{url}/$renderedfiles{$p}",
281 itempubdate => date_822($pagectime{$p}),
282 itemcontent => absolute_urls(get_inline_content($page, $p), $url),
283 } if exists $renderedfiles{$p};
287 title => $config{wikiname},
292 return $template->output;
295 sub check_overwrite ($$) { #{{{
296 # Important security check. Make sure to call this before saving
297 # any files to the source directory.
301 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
302 error("$dest already exists and was rendered from ".
303 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
305 ", before, so not rendering from $src");
312 return (stat($file))[9];
315 sub findlinks ($$) { #{{{
320 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
321 push @links, titlepage($2);
323 # Discussion links are a special case since they're not in the text
324 # of the page, but on its template.
325 return @links, "$page/discussion";
328 sub render ($) { #{{{
331 my $type=pagetype($file);
332 my $srcfile=srcfile($file);
333 if ($type ne 'unknown') {
334 my $content=readfile($srcfile);
335 my $page=pagename($file);
337 $links{$page}=[findlinks($content, $page)];
338 delete $inlinepages{$page};
340 $content=linkify($content, $page);
341 $content=preprocess($page, $content);
342 $content=htmlize($type, $content);
344 check_overwrite("$config{destdir}/".htmlpage($page), $page);
345 writefile(htmlpage($page), $config{destdir},
346 genpage($content, $page, mtime($srcfile)));
347 $oldpagemtime{$page}=time;
348 $renderedfiles{$page}=htmlpage($page);
351 my $content=readfile($srcfile, 1);
353 check_overwrite("$config{destdir}/$file", $file);
354 writefile($file, $config{destdir}, $content, 1);
355 $oldpagemtime{$file}=time;
356 $renderedfiles{$file}=$file;
364 my $dir=dirname($file);
365 while (rmdir($dir)) {
371 my $estdir="$config{wikistatedir}/hyperestraier";
372 my $cgi=basename($config{cgiurl});
374 open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
375 error("write $estdir/$cgi.tmpl: $!");
376 print TEMPLATE misctemplate("search",
377 "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
379 open(TEMPLATE, ">$estdir/$cgi.conf") ||
380 error("write $estdir/$cgi.conf: $!");
381 my $template=HTML::Template->new(
382 filename => "$config{templatedir}/estseek.conf"
384 eval q{use Cwd 'abs_path'};
387 tmplfile => "$estdir/$cgi.tmpl",
388 destdir => abs_path($config{destdir}),
391 print TEMPLATE $template->output;
393 $cgi="$estdir/".basename($config{cgiurl});
395 symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
396 error("symlink $cgi: $!");
399 sub estcmd ($;@) { #{{{
400 my @params=split(' ', shift);
401 push @params, "-cl", "$config{wikistatedir}/hyperestraier";
406 my $pid=open(CHILD, "|-");
412 close(CHILD) || error("estcmd @params exited nonzero: $?");
416 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
417 exec("estcmd", @params) || error("can't run estcmd");
421 sub refresh () { #{{{
422 # find existing pages
425 eval q{use File::Find};
429 if (/$config{wiki_file_prune_regexp}/) {
430 $File::Find::prune=1;
432 elsif (! -d $_ && ! -l $_) {
433 my ($f)=/$config{wiki_file_regexp}/; # untaint
435 warn("skipping bad filename $_\n");
438 $f=~s/^\Q$config{srcdir}\E\/?//;
440 $exists{pagename($f)}=1;
448 if (/$config{wiki_file_prune_regexp}/) {
449 $File::Find::prune=1;
451 elsif (! -d $_ && ! -l $_) {
452 my ($f)=/$config{wiki_file_regexp}/; # untaint
454 warn("skipping bad filename $_\n");
457 # Don't add files that are in the
459 $f=~s/^\Q$config{underlaydir}\E\/?//;
460 if (! -e "$config{srcdir}/$f" &&
461 ! -l "$config{srcdir}/$f") {
463 $exists{pagename($f)}=1;
468 }, $config{underlaydir});
472 # check for added or removed pages
474 foreach my $file (@files) {
475 my $page=pagename($file);
476 if (! $oldpagemtime{$page}) {
477 debug("new page $page") unless exists $pagectime{$page};
480 $pagesources{$page}=$file;
481 $pagectime{$page}=mtime(srcfile($file))
482 unless exists $pagectime{$page};
486 foreach my $page (keys %oldpagemtime) {
487 if (! $exists{$page}) {
488 debug("removing old page $page");
489 push @del, $pagesources{$page};
490 prune($config{destdir}."/".$renderedfiles{$page});
491 delete $renderedfiles{$page};
492 $oldpagemtime{$page}=0;
493 delete $pagesources{$page};
497 # render any updated files
498 foreach my $file (@files) {
499 my $page=pagename($file);
501 if (! exists $oldpagemtime{$page} ||
502 mtime(srcfile($file)) > $oldpagemtime{$page}) {
503 debug("rendering changed file $file");
509 # if any files were added or removed, check to see if each page
510 # needs an update due to linking to them or inlining them.
511 # TODO: inefficient; pages may get rendered above and again here;
512 # problem is the bestlink may have changed and we won't know until
515 FILE: foreach my $file (@files) {
516 my $page=pagename($file);
517 foreach my $f (@add, @del) {
519 foreach my $link (@{$links{$page}}) {
520 if (bestlink($page, $link) eq $p) {
521 debug("rendering $file, which links to $p");
531 # Handle backlinks; if a page has added/removed links, update the
532 # pages it links to. Also handle inlining here.
533 # TODO: inefficient; pages may get rendered above and again here;
534 # problem is the backlinks could be wrong in the first pass render
536 if (%rendered || @del) {
537 foreach my $f (@files) {
539 if (exists $inlinepages{$p}) {
540 foreach my $file (keys %rendered, @del) {
541 my $page=pagename($file);
542 if (globlist_match($page, $inlinepages{$p})) {
543 debug("rendering $f, which inlines $page");
553 foreach my $file (keys %rendered, @del) {
554 my $page=pagename($file);
556 if (exists $links{$page}) {
557 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
559 ! exists $oldlinks{$page} ||
560 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
561 $linkchanged{$link}=1;
565 if (exists $oldlinks{$page}) {
566 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
568 ! exists $links{$page} ||
569 ! grep { $_ eq $link } @{$links{$page}}) {
570 $linkchanged{$link}=1;
575 foreach my $link (keys %linkchanged) {
576 my $linkfile=$pagesources{$link};
577 if (defined $linkfile) {
578 debug("rendering $linkfile, to update its backlinks");
580 $rendered{$linkfile}=1;
585 if ($config{hyperestraier} && (%rendered || @del)) {
586 debug("updating hyperestraier search index");
588 estcmd("gather -cm -bc -cl -sd",
589 map { $config{destdir}."/".$renderedfiles{pagename($_)} }
596 debug("generating hyperestraier cgi config");