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 finalize ($$$) { #{{{
87 my $title=pagetitle(basename($page));
89 my $template=HTML::Template->new(blind_cache => 1,
90 filename => "$config{templatedir}/page.tmpl");
92 if (length $config{cgiurl}) {
93 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
94 $template->param(prefsurl => "$config{cgiurl}?do=prefs");
96 $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
100 if (length $config{historyurl}) {
101 my $u=$config{historyurl};
102 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
103 $template->param(historyurl => $u);
108 wikiname => $config{wikiname},
109 parentlinks => [parentlinks($page)],
111 backlinks => [backlinks($page)],
112 discussionlink => htmllink($page, "Discussion", 1, 1),
113 mtime => scalar(gmtime($mtime)),
116 return $template->output;
119 sub check_overwrite ($$) { #{{{
120 # Important security check. Make sure to call this before saving
121 # any files to the source directory.
125 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
126 error("$dest already exists and was rendered from ".
127 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
129 ", before, so not rendering from $src");
136 return (stat($page))[9];
139 sub findlinks ($$) { #{{{
144 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
147 # Discussion links are a special case since they're not in the text
148 # of the page, but on its template.
149 return @links, "$page/discussion";
152 sub render ($) { #{{{
155 my $type=pagetype($file);
156 my $content=readfile("$config{srcdir}/$file");
157 if ($type ne 'unknown') {
158 my $page=pagename($file);
160 $links{$page}=[findlinks($content, $page)];
162 $content=linkify($content, $page);
163 $content=htmlize($type, $content);
164 $content=finalize($content, $page,
165 mtime("$config{srcdir}/$file"));
167 check_overwrite("$config{destdir}/".htmlpage($page), $page);
168 writefile("$config{destdir}/".htmlpage($page), $content);
169 $oldpagemtime{$page}=time;
170 $renderedfiles{$page}=htmlpage($page);
174 check_overwrite("$config{destdir}/$file", $file);
175 writefile("$config{destdir}/$file", $content);
176 $oldpagemtime{$file}=time;
177 $renderedfiles{$file}=$file;
185 my $dir=dirname($file);
186 while (rmdir($dir)) {
191 sub refresh () { #{{{
192 # find existing pages
195 eval q{use File::Find};
199 if (/$config{wiki_file_prune_regexp}/) {
201 $File::Find::prune=1;
204 elsif (! -d $_ && ! -l $_) {
205 my ($f)=/$config{wiki_file_regexp}/; # untaint
207 warn("skipping bad filename $_\n");
210 $f=~s/^\Q$config{srcdir}\E\/?//;
212 $exists{pagename($f)}=1;
220 # check for added or removed pages
222 foreach my $file (@files) {
223 my $page=pagename($file);
224 if (! $oldpagemtime{$page}) {
225 debug("new page $page");
228 $pagesources{$page}=$file;
232 foreach my $page (keys %oldpagemtime) {
233 if (! $exists{$page}) {
234 debug("removing old page $page");
235 push @del, $pagesources{$page};
236 prune($config{destdir}."/".$renderedfiles{$page});
237 delete $renderedfiles{$page};
238 $oldpagemtime{$page}=0;
239 delete $pagesources{$page};
243 # render any updated files
244 foreach my $file (@files) {
245 my $page=pagename($file);
247 if (! exists $oldpagemtime{$page} ||
248 mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
249 debug("rendering changed file $file");
255 # if any files were added or removed, check to see if each page
256 # needs an update due to linking to them
257 # TODO: inefficient; pages may get rendered above and again here;
258 # problem is the bestlink may have changed and we won't know until
261 FILE: foreach my $file (@files) {
262 my $page=pagename($file);
263 foreach my $f (@add, @del) {
265 foreach my $link (@{$links{$page}}) {
266 if (bestlink($page, $link) eq $p) {
267 debug("rendering $file, which links to $p");
277 # handle backlinks; if a page has added/removed links, update the
279 # TODO: inefficient; pages may get rendered above and again here;
280 # problem is the backlinks could be wrong in the first pass render
284 foreach my $file (keys %rendered, @del) {
285 my $page=pagename($file);
286 if (exists $links{$page}) {
287 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
289 ! exists $oldlinks{$page} ||
290 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
291 $linkchanged{$link}=1;
295 if (exists $oldlinks{$page}) {
296 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
298 ! exists $links{$page} ||
299 ! grep { $_ eq $link } @{$links{$page}}) {
300 $linkchanged{$link}=1;
305 foreach my $link (keys %linkchanged) {
306 my $linkfile=$pagesources{$link};
307 if (defined $linkfile) {
308 debug("rendering $linkfile, to update its backlinks");