summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 690945c49f89f91668763ebf5dfc32d5810b2a9a (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. return if $page eq 'index'; # toplevel
  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+))(?:\s+|$)/g) {
  91. if (defined $1) {
  92. push @params, $1, (defined $2 ? $2 : $3);
  93. }
  94. else {
  95. push @params, (defined $2 ? $2 : $3), '';
  96. }
  97. }
  98. return $hooks{preprocess}{$command}{call}->(@params, page => $page);
  99. }
  100. else {
  101. return "[[$command not processed]]";
  102. }
  103. };
  104. $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
  105. return $content;
  106. } #}}}
  107. sub add_depends ($$) { #{{{
  108. my $page=shift;
  109. my $globlist=shift;
  110. if (! exists $depends{$page}) {
  111. $depends{$page}=$globlist;
  112. }
  113. else {
  114. $depends{$page}=globlist_merge($depends{$page}, $globlist);
  115. }
  116. } # }}}
  117. sub globlist_merge ($$) { #{{{
  118. my $a=shift;
  119. my $b=shift;
  120. my $ret="";
  121. # Only add negated globs if they are not matched by the other globlist.
  122. foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
  123. (map { [ $b, $_ ] } split(" ", $a))) {
  124. if ($i->[1]=~/^!(.*)/) {
  125. if (! globlist_match($1, $i->[0])) {
  126. $ret.=" ".$i->[1];
  127. }
  128. }
  129. else {
  130. $ret.=" ".$i->[1];
  131. }
  132. }
  133. return $ret;
  134. } #}}}
  135. sub genpage ($$$) { #{{{
  136. my $page=shift;
  137. my $content=shift;
  138. my $mtime=shift;
  139. my $template=template("page.tmpl", blind_cache => 1);
  140. my $actions=0;
  141. if (length $config{cgiurl}) {
  142. $template->param(editurl => cgiurl(do => "edit", page => $page));
  143. $template->param(prefsurl => cgiurl(do => "prefs"));
  144. if ($config{rcs}) {
  145. $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
  146. }
  147. $actions++;
  148. }
  149. if (length $config{historyurl}) {
  150. my $u=$config{historyurl};
  151. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  152. $template->param(historyurl => $u);
  153. $actions++;
  154. }
  155. if ($config{discussion}) {
  156. $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
  157. $actions++;
  158. }
  159. if ($actions) {
  160. $template->param(have_actions => 1);
  161. }
  162. $template->param(
  163. title => $page eq 'index'
  164. ? $config{wikiname}
  165. : pagetitle(basename($page)),
  166. wikiname => $config{wikiname},
  167. parentlinks => [parentlinks($page)],
  168. content => $content,
  169. backlinks => [backlinks($page)],
  170. mtime => displaytime($mtime),
  171. styleurl => styleurl($page),
  172. );
  173. if (exists $hooks{pagetemplate}) {
  174. foreach my $id (keys %{$hooks{pagetemplate}}) {
  175. $hooks{pagetemplate}{$id}{call}->($page, $template);
  176. }
  177. }
  178. return $template->output;
  179. } #}}}
  180. sub check_overwrite ($$) { #{{{
  181. # Important security check. Make sure to call this before saving
  182. # any files to the source directory.
  183. my $dest=shift;
  184. my $src=shift;
  185. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  186. error("$dest already exists and was not rendered from $src before");
  187. }
  188. } #}}}
  189. sub displaytime ($) { #{{{
  190. my $time=shift;
  191. eval q{use POSIX};
  192. # strftime doesn't know about encodings, so make sure
  193. # its output is properly treated as utf8
  194. return decode_utf8(POSIX::strftime(
  195. $config{timeformat}, localtime($time)));
  196. } #}}}
  197. sub mtime ($) { #{{{
  198. my $file=shift;
  199. return (stat($file))[9];
  200. } #}}}
  201. sub findlinks ($$) { #{{{
  202. my $page=shift;
  203. my $content=shift;
  204. my @links;
  205. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  206. push @links, titlepage($2);
  207. }
  208. if ($config{discussion}) {
  209. # Discussion links are a special case since they're not in the
  210. # text of the page, but on its template.
  211. return @links, "$page/discussion";
  212. }
  213. else {
  214. return @links;
  215. }
  216. } #}}}
  217. sub filter ($$) {
  218. my $page=shift;
  219. my $content=shift;
  220. if (exists $hooks{filter}) {
  221. foreach my $id (keys %{$hooks{filter}}) {
  222. $content=$hooks{filter}{$id}{call}->(
  223. page => $page,
  224. content => $content
  225. );
  226. }
  227. }
  228. return $content;
  229. }
  230. sub render ($) { #{{{
  231. my $file=shift;
  232. my $type=pagetype($file);
  233. my $srcfile=srcfile($file);
  234. if (defined $type) {
  235. my $content=readfile($srcfile);
  236. my $page=pagename($file);
  237. delete $depends{$page};
  238. $content=filter($page, $content);
  239. $links{$page}=[findlinks($page, $content)];
  240. $content=linkify($page, $page, $content);
  241. $content=preprocess($page, $content);
  242. $content=htmlize($type, $content);
  243. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  244. writefile(htmlpage($page), $config{destdir},
  245. genpage($page, $content, mtime($srcfile)));
  246. $oldpagemtime{$page}=time;
  247. $renderedfiles{$page}=htmlpage($page);
  248. }
  249. else {
  250. my $content=readfile($srcfile, 1);
  251. $links{$file}=[];
  252. delete $depends{$file};
  253. check_overwrite("$config{destdir}/$file", $file);
  254. writefile($file, $config{destdir}, $content, 1);
  255. $oldpagemtime{$file}=time;
  256. $renderedfiles{$file}=$file;
  257. }
  258. } #}}}
  259. sub prune ($) { #{{{
  260. my $file=shift;
  261. unlink($file);
  262. my $dir=dirname($file);
  263. while (rmdir($dir)) {
  264. $dir=dirname($dir);
  265. }
  266. } #}}}
  267. sub refresh () { #{{{
  268. # find existing pages
  269. my %exists;
  270. my @files;
  271. eval q{use File::Find};
  272. find({
  273. no_chdir => 1,
  274. wanted => sub {
  275. $_=decode_utf8($_);
  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. $f=~s/^\Q$config{srcdir}\E\/?//;
  286. push @files, $f;
  287. $exists{pagename($f)}=1;
  288. }
  289. }
  290. },
  291. }, $config{srcdir});
  292. find({
  293. no_chdir => 1,
  294. wanted => sub {
  295. $_=decode_utf8($_);
  296. if (/$config{wiki_file_prune_regexp}/) {
  297. $File::Find::prune=1;
  298. }
  299. elsif (! -d $_ && ! -l $_) {
  300. my ($f)=/$config{wiki_file_regexp}/; # untaint
  301. if (! defined $f) {
  302. warn("skipping bad filename $_\n");
  303. }
  304. else {
  305. # Don't add files that are in the
  306. # srcdir.
  307. $f=~s/^\Q$config{underlaydir}\E\/?//;
  308. if (! -e "$config{srcdir}/$f" &&
  309. ! -l "$config{srcdir}/$f") {
  310. push @files, $f;
  311. $exists{pagename($f)}=1;
  312. }
  313. }
  314. }
  315. },
  316. }, $config{underlaydir});
  317. my %rendered;
  318. # check for added or removed pages
  319. my @add;
  320. foreach my $file (@files) {
  321. my $page=pagename($file);
  322. if (! $oldpagemtime{$page}) {
  323. debug("new page $page") unless exists $pagectime{$page};
  324. push @add, $file;
  325. $links{$page}=[];
  326. $pagesources{$page}=$file;
  327. if ($config{getctime} && -e "$config{srcdir}/$file") {
  328. $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
  329. }
  330. elsif (! exists $pagectime{$page}) {
  331. $pagectime{$page}=mtime(srcfile($file));
  332. }
  333. }
  334. }
  335. my @del;
  336. foreach my $page (keys %oldpagemtime) {
  337. if (! $exists{$page}) {
  338. debug("removing old page $page");
  339. push @del, $pagesources{$page};
  340. prune($config{destdir}."/".$renderedfiles{$page});
  341. delete $renderedfiles{$page};
  342. $oldpagemtime{$page}=0;
  343. delete $pagesources{$page};
  344. }
  345. }
  346. # render any updated files
  347. foreach my $file (@files) {
  348. my $page=pagename($file);
  349. if (! exists $oldpagemtime{$page} ||
  350. mtime(srcfile($file)) > $oldpagemtime{$page}) {
  351. debug("rendering $file");
  352. render($file);
  353. $rendered{$file}=1;
  354. }
  355. }
  356. # if any files were added or removed, check to see if each page
  357. # needs an update due to linking to them or inlining them.
  358. # TODO: inefficient; pages may get rendered above and again here;
  359. # problem is the bestlink may have changed and we won't know until
  360. # now
  361. if (@add || @del) {
  362. FILE: foreach my $file (@files) {
  363. my $page=pagename($file);
  364. foreach my $f (@add, @del) {
  365. my $p=pagename($f);
  366. foreach my $link (@{$links{$page}}) {
  367. if (bestlink($page, $link) eq $p) {
  368. debug("rendering $file, which links to $p");
  369. render($file);
  370. $rendered{$file}=1;
  371. next FILE;
  372. }
  373. }
  374. }
  375. }
  376. }
  377. # Handle backlinks; if a page has added/removed links, update the
  378. # pages it links to. Also handles rebuilding dependant pages.
  379. # TODO: inefficient; pages may get rendered above and again here;
  380. # problem is the backlinks could be wrong in the first pass render
  381. # above
  382. if (%rendered || @del) {
  383. foreach my $f (@files) {
  384. my $p=pagename($f);
  385. if (exists $depends{$p}) {
  386. foreach my $file (keys %rendered, @del) {
  387. next if $f eq $file;
  388. my $page=pagename($file);
  389. if (globlist_match($page, $depends{$p})) {
  390. debug("rendering $f, which depends on $page");
  391. render($f);
  392. $rendered{$f}=1;
  393. last;
  394. }
  395. }
  396. }
  397. }
  398. my %linkchanged;
  399. foreach my $file (keys %rendered, @del) {
  400. my $page=pagename($file);
  401. if (exists $links{$page}) {
  402. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  403. if (length $link &&
  404. (! exists $oldlinks{$page} ||
  405. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  406. $linkchanged{$link}=1;
  407. }
  408. }
  409. }
  410. if (exists $oldlinks{$page}) {
  411. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  412. if (length $link &&
  413. (! exists $links{$page} ||
  414. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  415. $linkchanged{$link}=1;
  416. }
  417. }
  418. }
  419. }
  420. foreach my $link (keys %linkchanged) {
  421. my $linkfile=$pagesources{$link};
  422. if (defined $linkfile) {
  423. debug("rendering $linkfile, to update its backlinks");
  424. render($linkfile);
  425. $rendered{$linkfile}=1;
  426. }
  427. }
  428. }
  429. if (@del && exists $hooks{delete}) {
  430. foreach my $id (keys %{$hooks{delete}}) {
  431. $hooks{delete}{$id}{call}->(@del);
  432. }
  433. }
  434. if (%rendered && exists $hooks{change}) {
  435. foreach my $id (keys %{$hooks{change}}) {
  436. $hooks{change}{$id}{call}->(keys %rendered);
  437. }
  438. }
  439. } #}}}
  440. 1