summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: f33d6e66eb7de041d43734b0a4ec4ef3aa7b9b7c (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use File::Spec;
  6. use IkiWiki;
  7. sub linkify ($$) { #{{{
  8. my $page=shift;
  9. my $content=shift;
  10. $content =~ s{(\\?)$config{wiki_link_regexp}}{
  11. $2 ? ( $1 ? "[[$2|$3]]" : htmllink($page, titlepage($3), 0, 0, pagetitle($2)))
  12. : ( $1 ? "[[$3]]" : htmllink($page, titlepage($3)))
  13. }eg;
  14. return $content;
  15. } #}}}
  16. sub htmlize ($$) { #{{{
  17. my $type=shift;
  18. my $content=shift;
  19. if (! $INC{"/usr/bin/markdown"}) {
  20. no warnings 'once';
  21. $blosxom::version="is a proper perl module too much to ask?";
  22. use warnings 'all';
  23. do "/usr/bin/markdown";
  24. }
  25. if ($type eq '.mdwn') {
  26. $content=Markdown::Markdown($content);
  27. }
  28. else {
  29. error("htmlization of $type not supported");
  30. }
  31. if (exists $hooks{sanitize}) {
  32. foreach my $id (keys %{$hooks{sanitize}}) {
  33. $content=$hooks{sanitize}{$id}{call}->($content);
  34. }
  35. }
  36. return $content;
  37. } #}}}
  38. sub backlinks ($) { #{{{
  39. my $page=shift;
  40. my @links;
  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));
  45. # Trim common dir prefixes from both pages.
  46. my $p_trimmed=$p;
  47. my $page_trimmed=$page;
  48. my $dir;
  49. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  50. defined $dir &&
  51. $p_trimmed=~s/^\Q$dir\E// &&
  52. $page_trimmed=~s/^\Q$dir\E//;
  53. push @links, { url => $href, page => $p_trimmed };
  54. }
  55. }
  56. return sort { $a->{page} cmp $b->{page} } @links;
  57. } #}}}
  58. sub parentlinks ($) { #{{{
  59. my $page=shift;
  60. my @ret;
  61. my $pagelink="";
  62. my $path="";
  63. my $skip=1;
  64. foreach my $dir (reverse split("/", $page)) {
  65. if (! $skip) {
  66. $path.="../";
  67. unshift @ret, { url => "$path$dir.html", page => $dir };
  68. }
  69. else {
  70. $skip=0;
  71. }
  72. }
  73. unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
  74. return @ret;
  75. } #}}}
  76. sub preprocess ($$) { #{{{
  77. my $page=shift;
  78. my $content=shift;
  79. my $handle=sub {
  80. my $escape=shift;
  81. my $command=shift;
  82. my $params=shift;
  83. if (length $escape) {
  84. return "[[$command $params]]";
  85. }
  86. elsif (exists $hooks{preprocess}{$command}) {
  87. my %params;
  88. while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
  89. $params{$1}=$2;
  90. }
  91. return $hooks{preprocess}{$command}{call}->(page => $page, %params);
  92. }
  93. else {
  94. return "[[$command not processed]]";
  95. }
  96. };
  97. $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
  98. return $content;
  99. } #}}}
  100. sub add_depends ($$) { #{{{
  101. my $page=shift;
  102. my $globlist=shift;
  103. if (! exists $depends{$page}) {
  104. $depends{$page}=$globlist;
  105. }
  106. else {
  107. $depends{$page}=globlist_merge($depends{$page}, $globlist);
  108. }
  109. } # }}}
  110. sub globlist_merge ($$) { #{{{
  111. my $a=shift;
  112. my $b=shift;
  113. my $ret="";
  114. # Only add negated globs if they are not matched by the other globlist.
  115. foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
  116. (map { [ $b, $_ ] } split(" ", $a))) {
  117. if ($i->[1]=~/^!(.*)/) {
  118. if (! globlist_match($1, $i->[0])) {
  119. $ret.=" ".$i->[1];
  120. }
  121. }
  122. else {
  123. $ret.=" ".$i->[1];
  124. }
  125. }
  126. return $ret;
  127. } #}}}
  128. sub genpage ($$$) { #{{{
  129. my $page=shift;
  130. my $content=shift;
  131. my $mtime=shift;
  132. my $title=pagetitle(basename($page));
  133. my $template=HTML::Template->new(blind_cache => 1,
  134. filename => "$config{templatedir}/page.tmpl");
  135. my $actions=0;
  136. if (length $config{cgiurl}) {
  137. $template->param(editurl => cgiurl(do => "edit", page => $page));
  138. $template->param(prefsurl => cgiurl(do => "prefs"));
  139. if ($config{rcs}) {
  140. $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
  141. }
  142. $actions++;
  143. }
  144. if (length $config{historyurl}) {
  145. my $u=$config{historyurl};
  146. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  147. $template->param(historyurl => $u);
  148. $actions++;
  149. }
  150. if ($config{discussion}) {
  151. $template->param(discussionlink => htmllink($page, "Discussion", 1, 1));
  152. $actions++;
  153. }
  154. if ($actions) {
  155. $template->param(have_actions => 1);
  156. }
  157. if (exists $hooks{pagetemplate}) {
  158. foreach my $id (keys %{$hooks{pagetemplate}}) {
  159. $hooks{pagetemplate}{$id}{call}->($page, $template);
  160. }
  161. }
  162. $template->param(
  163. title => $title,
  164. wikiname => $config{wikiname},
  165. parentlinks => [parentlinks($page)],
  166. content => $content,
  167. backlinks => [backlinks($page)],
  168. mtime => scalar(gmtime($mtime)),
  169. styleurl => styleurl($page),
  170. );
  171. return $template->output;
  172. } #}}}
  173. sub check_overwrite ($$) { #{{{
  174. # Important security check. Make sure to call this before saving
  175. # any files to the source directory.
  176. my $dest=shift;
  177. my $src=shift;
  178. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  179. error("$dest already exists and was rendered from ".
  180. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  181. %renderedfiles)).
  182. ", before, so not rendering from $src");
  183. }
  184. } #}}}
  185. sub mtime ($) { #{{{
  186. my $file=shift;
  187. return (stat($file))[9];
  188. } #}}}
  189. sub findlinks ($$) { #{{{
  190. my $page=shift;
  191. my $content=shift;
  192. my @links;
  193. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  194. push @links, titlepage($2);
  195. }
  196. if ($config{discussion}) {
  197. # Discussion links are a special case since they're not in the
  198. # text of the page, but on its template.
  199. return @links, "$page/discussion";
  200. }
  201. else {
  202. return @links;
  203. }
  204. } #}}}
  205. sub render ($) { #{{{
  206. my $file=shift;
  207. my $type=pagetype($file);
  208. my $srcfile=srcfile($file);
  209. if ($type ne 'unknown') {
  210. my $content=readfile($srcfile);
  211. my $page=pagename($file);
  212. delete $depends{$page};
  213. if (exists $hooks{filter}) {
  214. foreach my $id (keys %{$hooks{filter}}) {
  215. $content=$hooks{filter}{$id}{call}->(
  216. page => $page,
  217. content => $content
  218. );
  219. }
  220. }
  221. $links{$page}=[findlinks($page, $content)];
  222. $content=linkify($page, $content);
  223. $content=preprocess($page, $content);
  224. $content=htmlize($type, $content);
  225. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  226. writefile(htmlpage($page), $config{destdir},
  227. genpage($page, $content, mtime($srcfile)));
  228. $oldpagemtime{$page}=time;
  229. $renderedfiles{$page}=htmlpage($page);
  230. }
  231. else {
  232. my $content=readfile($srcfile, 1);
  233. $links{$file}=[];
  234. delete $depends{$file};
  235. check_overwrite("$config{destdir}/$file", $file);
  236. writefile($file, $config{destdir}, $content, 1);
  237. $oldpagemtime{$file}=time;
  238. $renderedfiles{$file}=$file;
  239. }
  240. } #}}}
  241. sub prune ($) { #{{{
  242. my $file=shift;
  243. unlink($file);
  244. my $dir=dirname($file);
  245. while (rmdir($dir)) {
  246. $dir=dirname($dir);
  247. }
  248. } #}}}
  249. sub refresh () { #{{{
  250. # find existing pages
  251. my %exists;
  252. my @files;
  253. eval q{use File::Find};
  254. find({
  255. no_chdir => 1,
  256. wanted => sub {
  257. if (/$config{wiki_file_prune_regexp}/) {
  258. $File::Find::prune=1;
  259. }
  260. elsif (! -d $_ && ! -l $_) {
  261. my ($f)=/$config{wiki_file_regexp}/; # untaint
  262. if (! defined $f) {
  263. warn("skipping bad filename $_\n");
  264. }
  265. else {
  266. $f=~s/^\Q$config{srcdir}\E\/?//;
  267. push @files, $f;
  268. $exists{pagename($f)}=1;
  269. }
  270. }
  271. },
  272. }, $config{srcdir});
  273. find({
  274. no_chdir => 1,
  275. wanted => sub {
  276. if (/$config{wiki_file_prune_regexp}/) {
  277. $File::Find::prune=1;
  278. }
  279. elsif (! -d $_ && ! -l $_) {
  280. my ($f)=/$config{wiki_file_regexp}/; # untaint
  281. if (! defined $f) {
  282. warn("skipping bad filename $_\n");
  283. }
  284. else {
  285. # Don't add files that are in the
  286. # srcdir.
  287. $f=~s/^\Q$config{underlaydir}\E\/?//;
  288. if (! -e "$config{srcdir}/$f" &&
  289. ! -l "$config{srcdir}/$f") {
  290. push @files, $f;
  291. $exists{pagename($f)}=1;
  292. }
  293. }
  294. }
  295. },
  296. }, $config{underlaydir});
  297. my %rendered;
  298. # check for added or removed pages
  299. my @add;
  300. foreach my $file (@files) {
  301. my $page=pagename($file);
  302. if (! $oldpagemtime{$page}) {
  303. debug("new page $page") unless exists $pagectime{$page};
  304. push @add, $file;
  305. $links{$page}=[];
  306. $pagesources{$page}=$file;
  307. if ($config{getctime} && -e "$config{srcdir}/$file") {
  308. $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
  309. }
  310. elsif (! exists $pagectime{$page}) {
  311. $pagectime{$page}=mtime(srcfile($file));
  312. }
  313. }
  314. }
  315. my @del;
  316. foreach my $page (keys %oldpagemtime) {
  317. if (! $exists{$page}) {
  318. debug("removing old page $page");
  319. push @del, $pagesources{$page};
  320. prune($config{destdir}."/".$renderedfiles{$page});
  321. delete $renderedfiles{$page};
  322. $oldpagemtime{$page}=0;
  323. delete $pagesources{$page};
  324. }
  325. }
  326. # render any updated files
  327. foreach my $file (@files) {
  328. my $page=pagename($file);
  329. if (! exists $oldpagemtime{$page} ||
  330. mtime(srcfile($file)) > $oldpagemtime{$page}) {
  331. debug("rendering changed file $file");
  332. render($file);
  333. $rendered{$file}=1;
  334. }
  335. }
  336. # if any files were added or removed, check to see if each page
  337. # needs an update due to linking to them or inlining them.
  338. # TODO: inefficient; pages may get rendered above and again here;
  339. # problem is the bestlink may have changed and we won't know until
  340. # now
  341. if (@add || @del) {
  342. FILE: foreach my $file (@files) {
  343. my $page=pagename($file);
  344. foreach my $f (@add, @del) {
  345. my $p=pagename($f);
  346. foreach my $link (@{$links{$page}}) {
  347. if (bestlink($page, $link) eq $p) {
  348. debug("rendering $file, which links to $p");
  349. render($file);
  350. $rendered{$file}=1;
  351. next FILE;
  352. }
  353. }
  354. }
  355. }
  356. }
  357. # Handle backlinks; if a page has added/removed links, update the
  358. # pages it links to. Also handles rebuilding dependat pages.
  359. # TODO: inefficient; pages may get rendered above and again here;
  360. # problem is the backlinks could be wrong in the first pass render
  361. # above
  362. if (%rendered || @del) {
  363. foreach my $f (@files) {
  364. my $p=pagename($f);
  365. if (exists $depends{$p}) {
  366. foreach my $file (keys %rendered, @del) {
  367. next if $f eq $file;
  368. my $page=pagename($file);
  369. if (globlist_match($page, $depends{$p})) {
  370. debug("rendering $f, which depends on $page");
  371. render($f);
  372. $rendered{$f}=1;
  373. last;
  374. }
  375. }
  376. }
  377. }
  378. my %linkchanged;
  379. foreach my $file (keys %rendered, @del) {
  380. my $page=pagename($file);
  381. if (exists $links{$page}) {
  382. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  383. if (length $link &&
  384. (! exists $oldlinks{$page} ||
  385. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  386. $linkchanged{$link}=1;
  387. }
  388. }
  389. }
  390. if (exists $oldlinks{$page}) {
  391. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  392. if (length $link &&
  393. (! exists $links{$page} ||
  394. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  395. $linkchanged{$link}=1;
  396. }
  397. }
  398. }
  399. }
  400. foreach my $link (keys %linkchanged) {
  401. my $linkfile=$pagesources{$link};
  402. if (defined $linkfile) {
  403. debug("rendering $linkfile, to update its backlinks");
  404. render($linkfile);
  405. $rendered{$linkfile}=1;
  406. }
  407. }
  408. }
  409. if (@del && exists $hooks{delete}) {
  410. foreach my $id (keys %{$hooks{delete}}) {
  411. $hooks{delete}{$id}{call}->(@del);
  412. }
  413. }
  414. if (%rendered && exists $hooks{change}) {
  415. foreach my $id (keys %{$hooks{change}}) {
  416. $hooks{change}{$id}{call}->(keys %rendered);
  417. }
  418. }
  419. } #}}}
  420. 1