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 genpage ($$$) { #{{{
93 my $title=pagetitle(basename($page));
95 my $template=HTML::Template->new(blind_cache => 1,
96 filename => "$config{templatedir}/page.tmpl");
98 if (length $config{cgiurl}) {
99 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
100 $template->param(prefsurl => "$config{cgiurl}?do=prefs");
102 $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
106 if (length $config{historyurl}) {
107 my $u=$config{historyurl};
108 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
109 $template->param(historyurl => $u);
113 $template->param(rssurl => rsspage($page));
118 wikiname => $config{wikiname},
119 parentlinks => [parentlinks($page)],
121 backlinks => [backlinks($page)],
122 discussionlink => htmllink($page, "Discussion", 1, 1),
123 mtime => scalar(gmtime($mtime)),
126 return $template->output;
129 sub date_822 ($) { #{{{
133 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
136 sub absolute_urls ($$) { #{{{
142 $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
143 $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
147 sub genrss ($$$) { #{{{
152 my $url="$config{url}/".htmlpage($page);
154 my $template=HTML::Template->new(blind_cache => 1,
155 filename => "$config{templatedir}/rsspage.tmpl");
157 # Regular page gets a feed that is updated every time the
158 # page is changed, so the mtime is encoded in the guid.
161 itemtitle => pagetitle(basename($page)),
162 itemguid => "$url?mtime=$mtime",
164 itempubdate => date_822($mtime),
165 itemcontent => absolute_urls($content, $url), # rss sucks
170 title => $config{wikiname},
175 return $template->output;
178 sub check_overwrite ($$) { #{{{
179 # Important security check. Make sure to call this before saving
180 # any files to the source directory.
184 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
185 error("$dest already exists and was rendered from ".
186 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
188 ", before, so not rendering from $src");
195 return (stat($page))[9];
198 sub findlinks ($$) { #{{{
203 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
206 # Discussion links are a special case since they're not in the text
207 # of the page, but on its template.
208 return @links, "$page/discussion";
211 sub render ($) { #{{{
214 my $type=pagetype($file);
215 my $content=readfile("$config{srcdir}/$file");
216 if ($type ne 'unknown') {
217 my $page=pagename($file);
219 $links{$page}=[findlinks($content, $page)];
221 $content=linkify($content, $page);
222 $content=htmlize($type, $content);
224 check_overwrite("$config{destdir}/".htmlpage($page), $page);
225 writefile("$config{destdir}/".htmlpage($page),
226 genpage($content, $page, mtime("$config{srcdir}/$file")));
227 $oldpagemtime{$page}=time;
228 $renderedfiles{$page}=htmlpage($page);
230 # TODO: should really add this to renderedfiles and call
231 # check_overwrite, as above, but currently renderedfiles
232 # only supports listing one file per page.
234 writefile("$config{destdir}/".rsspage($page),
235 genrss($content, $page, mtime("$config{srcdir}/$file")));
240 check_overwrite("$config{destdir}/$file", $file);
241 writefile("$config{destdir}/$file", $content);
242 $oldpagemtime{$file}=time;
243 $renderedfiles{$file}=$file;
251 my $dir=dirname($file);
252 while (rmdir($dir)) {
257 sub refresh () { #{{{
258 # find existing pages
261 eval q{use File::Find};
265 if (/$config{wiki_file_prune_regexp}/) {
267 $File::Find::prune=1;
270 elsif (! -d $_ && ! -l $_) {
271 my ($f)=/$config{wiki_file_regexp}/; # untaint
273 warn("skipping bad filename $_\n");
276 $f=~s/^\Q$config{srcdir}\E\/?//;
278 $exists{pagename($f)}=1;
286 # check for added or removed pages
288 foreach my $file (@files) {
289 my $page=pagename($file);
290 if (! $oldpagemtime{$page}) {
291 debug("new page $page");
294 $pagesources{$page}=$file;
298 foreach my $page (keys %oldpagemtime) {
299 if (! $exists{$page}) {
300 debug("removing old page $page");
301 push @del, $pagesources{$page};
302 prune($config{destdir}."/".$renderedfiles{$page});
303 delete $renderedfiles{$page};
304 $oldpagemtime{$page}=0;
305 delete $pagesources{$page};
309 # render any updated files
310 foreach my $file (@files) {
311 my $page=pagename($file);
313 if (! exists $oldpagemtime{$page} ||
314 mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
315 debug("rendering changed file $file");
321 # if any files were added or removed, check to see if each page
322 # needs an update due to linking to them
323 # TODO: inefficient; pages may get rendered above and again here;
324 # problem is the bestlink may have changed and we won't know until
327 FILE: foreach my $file (@files) {
328 my $page=pagename($file);
329 foreach my $f (@add, @del) {
331 foreach my $link (@{$links{$page}}) {
332 if (bestlink($page, $link) eq $p) {
333 debug("rendering $file, which links to $p");
343 # handle backlinks; if a page has added/removed links, update the
345 # TODO: inefficient; pages may get rendered above and again here;
346 # problem is the backlinks could be wrong in the first pass render
350 foreach my $file (keys %rendered, @del) {
351 my $page=pagename($file);
352 if (exists $links{$page}) {
353 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
355 ! exists $oldlinks{$page} ||
356 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
357 $linkchanged{$link}=1;
361 if (exists $oldlinks{$page}) {
362 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
364 ! exists $links{$page} ||
365 ! grep { $_ eq $link } @{$links{$page}}) {
366 $linkchanged{$link}=1;
371 foreach my $link (keys %linkchanged) {
372 my $linkfile=$pagesources{$link};
373 if (defined $linkfile) {
374 debug("rendering $linkfile, to update its backlinks");