summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 854d5105ebecc793e97d2c3f3a32c8ad3b3e0131 (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 $content=shift;
  9. my $page=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 $content=shift;
  130. my $page=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. if (length $config{cgiurl}) {
  136. $template->param(editurl => cgiurl(do => "edit", page => $page));
  137. $template->param(prefsurl => cgiurl(do => "prefs"));
  138. if ($config{rcs}) {
  139. $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
  140. }
  141. }
  142. if (length $config{historyurl}) {
  143. my $u=$config{historyurl};
  144. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  145. $template->param(historyurl => $u);
  146. }
  147. if ($config{discussion}) {
  148. $template->param(discussionlink => htmllink($page, "Discussion", 1, 1));
  149. }
  150. $template->param(headercontent => $config{headercontent});
  151. $template->param(
  152. title => $title,
  153. wikiname => $config{wikiname},
  154. parentlinks => [parentlinks($page)],
  155. content => $content,
  156. backlinks => [backlinks($page)],
  157. mtime => scalar(gmtime($mtime)),
  158. styleurl => styleurl($page),
  159. );
  160. return $template->output;
  161. } #}}}
  162. sub check_overwrite ($$) { #{{{
  163. # Important security check. Make sure to call this before saving
  164. # any files to the source directory.
  165. my $dest=shift;
  166. my $src=shift;
  167. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  168. error("$dest already exists and was rendered from ".
  169. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  170. %renderedfiles)).
  171. ", before, so not rendering from $src");
  172. }
  173. } #}}}
  174. sub mtime ($) { #{{{
  175. my $file=shift;
  176. return (stat($file))[9];
  177. } #}}}
  178. sub findlinks ($$) { #{{{
  179. my $content=shift;
  180. my $page=shift;
  181. my @links;
  182. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  183. push @links, titlepage($2);
  184. }
  185. if ($config{discussion}) {
  186. # Discussion links are a special case since they're not in the
  187. # text of the page, but on its template.
  188. return @links, "$page/discussion";
  189. }
  190. else {
  191. return @links;
  192. }
  193. } #}}}
  194. sub render ($) { #{{{
  195. my $file=shift;
  196. my $type=pagetype($file);
  197. my $srcfile=srcfile($file);
  198. if ($type ne 'unknown') {
  199. my $content=readfile($srcfile);
  200. my $page=pagename($file);
  201. delete $depends{$page};
  202. if (exists $hooks{filter}) {
  203. foreach my $id (keys %{$hooks{filter}}) {
  204. $content=$hooks{filter}{$id}{call}->(
  205. page => $page,
  206. content => $content
  207. );
  208. }
  209. }
  210. $links{$page}=[findlinks($content, $page)];
  211. $content=linkify($content, $page);
  212. $content=preprocess($page, $content);
  213. $content=htmlize($type, $content);
  214. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  215. writefile(htmlpage($page), $config{destdir},
  216. genpage($content, $page, mtime($srcfile)));
  217. $oldpagemtime{$page}=time;
  218. $renderedfiles{$page}=htmlpage($page);
  219. }
  220. else {
  221. my $content=readfile($srcfile, 1);
  222. $links{$file}=[];
  223. delete $depends{$file};
  224. check_overwrite("$config{destdir}/$file", $file);
  225. writefile($file, $config{destdir}, $content, 1);
  226. $oldpagemtime{$file}=time;
  227. $renderedfiles{$file}=$file;
  228. }
  229. } #}}}
  230. sub prune ($) { #{{{
  231. my $file=shift;
  232. unlink($file);
  233. my $dir=dirname($file);
  234. while (rmdir($dir)) {
  235. $dir=dirname($dir);
  236. }
  237. } #}}}
  238. sub refresh () { #{{{
  239. # find existing pages
  240. my %exists;
  241. my @files;
  242. eval q{use File::Find};
  243. find({
  244. no_chdir => 1,
  245. wanted => sub {
  246. if (/$config{wiki_file_prune_regexp}/) {
  247. $File::Find::prune=1;
  248. }
  249. elsif (! -d $_ && ! -l $_) {
  250. my ($f)=/$config{wiki_file_regexp}/; # untaint
  251. if (! defined $f) {
  252. warn("skipping bad filename $_\n");
  253. }
  254. else {
  255. $f=~s/^\Q$config{srcdir}\E\/?//;
  256. push @files, $f;
  257. $exists{pagename($f)}=1;
  258. }
  259. }
  260. },
  261. }, $config{srcdir});
  262. find({
  263. no_chdir => 1,
  264. wanted => sub {
  265. if (/$config{wiki_file_prune_regexp}/) {
  266. $File::Find::prune=1;
  267. }
  268. elsif (! -d $_ && ! -l $_) {
  269. my ($f)=/$config{wiki_file_regexp}/; # untaint
  270. if (! defined $f) {
  271. warn("skipping bad filename $_\n");
  272. }
  273. else {
  274. # Don't add files that are in the
  275. # srcdir.
  276. $f=~s/^\Q$config{underlaydir}\E\/?//;
  277. if (! -e "$config{srcdir}/$f" &&
  278. ! -l "$config{srcdir}/$f") {
  279. push @files, $f;
  280. $exists{pagename($f)}=1;
  281. }
  282. }
  283. }
  284. },
  285. }, $config{underlaydir});
  286. my %rendered;
  287. # check for added or removed pages
  288. my @add;
  289. foreach my $file (@files) {
  290. my $page=pagename($file);
  291. if (! $oldpagemtime{$page}) {
  292. debug("new page $page") unless exists $pagectime{$page};
  293. push @add, $file;
  294. $links{$page}=[];
  295. $pagesources{$page}=$file;
  296. $pagectime{$page}=mtime(srcfile($file))
  297. unless exists $pagectime{$page};
  298. }
  299. }
  300. my @del;
  301. foreach my $page (keys %oldpagemtime) {
  302. if (! $exists{$page}) {
  303. debug("removing old page $page");
  304. push @del, $pagesources{$page};
  305. prune($config{destdir}."/".$renderedfiles{$page});
  306. delete $renderedfiles{$page};
  307. $oldpagemtime{$page}=0;
  308. delete $pagesources{$page};
  309. }
  310. }
  311. # render any updated files
  312. foreach my $file (@files) {
  313. my $page=pagename($file);
  314. if (! exists $oldpagemtime{$page} ||
  315. mtime(srcfile($file)) > $oldpagemtime{$page}) {
  316. debug("rendering changed file $file");
  317. render($file);
  318. $rendered{$file}=1;
  319. }
  320. }
  321. # if any files were added or removed, check to see if each page
  322. # needs an update due to linking to them or inlining 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
  325. # now
  326. if (@add || @del) {
  327. FILE: foreach my $file (@files) {
  328. my $page=pagename($file);
  329. foreach my $f (@add, @del) {
  330. my $p=pagename($f);
  331. foreach my $link (@{$links{$page}}) {
  332. if (bestlink($page, $link) eq $p) {
  333. debug("rendering $file, which links to $p");
  334. render($file);
  335. $rendered{$file}=1;
  336. next FILE;
  337. }
  338. }
  339. }
  340. }
  341. }
  342. # Handle backlinks; if a page has added/removed links, update the
  343. # pages it links to. Also handles rebuilding dependat pages.
  344. # TODO: inefficient; pages may get rendered above and again here;
  345. # problem is the backlinks could be wrong in the first pass render
  346. # above
  347. if (%rendered || @del) {
  348. foreach my $f (@files) {
  349. my $p=pagename($f);
  350. if (exists $depends{$p}) {
  351. foreach my $file (keys %rendered, @del) {
  352. next if $f eq $file;
  353. my $page=pagename($file);
  354. if (globlist_match($page, $depends{$p})) {
  355. debug("rendering $f, which depends on $page");
  356. render($f);
  357. $rendered{$f}=1;
  358. last;
  359. }
  360. }
  361. }
  362. }
  363. my %linkchanged;
  364. foreach my $file (keys %rendered, @del) {
  365. my $page=pagename($file);
  366. if (exists $links{$page}) {
  367. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  368. if (length $link &&
  369. (! exists $oldlinks{$page} ||
  370. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  371. $linkchanged{$link}=1;
  372. }
  373. }
  374. }
  375. if (exists $oldlinks{$page}) {
  376. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  377. if (length $link &&
  378. (! exists $links{$page} ||
  379. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  380. $linkchanged{$link}=1;
  381. }
  382. }
  383. }
  384. }
  385. foreach my $link (keys %linkchanged) {
  386. my $linkfile=$pagesources{$link};
  387. if (defined $linkfile) {
  388. debug("rendering $linkfile, to update its backlinks");
  389. render($linkfile);
  390. $rendered{$linkfile}=1;
  391. }
  392. }
  393. }
  394. if (@del && exists $hooks{delete}) {
  395. foreach my $id (keys %{$hooks{delete}}) {
  396. $hooks{delete}{$id}{call}->(@del);
  397. }
  398. }
  399. if (%rendered && exists $hooks{change}) {
  400. foreach my $id (keys %{$hooks{change}}) {
  401. $hooks{change}{$id}{call}->(keys %rendered);
  402. }
  403. }
  404. } #}}}
  405. 1