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