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