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