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