summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 54e409040ad119b8e6adf9bba850dbf21a255090 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use File::Spec;
  6. use IkiWiki;
  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 (! $INC{"/usr/bin/markdown"}) {
  21. no warnings 'once';
  22. $blosxom::version="is a proper perl module too much to ask?";
  23. use warnings 'all';
  24. do "/usr/bin/markdown";
  25. use Encode;
  26. }
  27. if ($type eq '.mdwn') {
  28. # Workaround for perl bug (#376329)
  29. $content=Encode::encode_utf8($content);
  30. $content=Encode::encode_utf8($content);
  31. $content=Markdown::Markdown($content);
  32. $content=Encode::decode_utf8($content);
  33. $content=Encode::decode_utf8($content);
  34. }
  35. else {
  36. error("htmlization of $type not supported");
  37. }
  38. if (exists $hooks{sanitize}) {
  39. foreach my $id (keys %{$hooks{sanitize}}) {
  40. $content=$hooks{sanitize}{$id}{call}->($content);
  41. }
  42. }
  43. return $content;
  44. } #}}}
  45. sub backlinks ($) { #{{{
  46. my $page=shift;
  47. my @links;
  48. foreach my $p (keys %links) {
  49. next if bestlink($page, $p) eq $page;
  50. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  51. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  52. # Trim common dir prefixes from both pages.
  53. my $p_trimmed=$p;
  54. my $page_trimmed=$page;
  55. my $dir;
  56. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  57. defined $dir &&
  58. $p_trimmed=~s/^\Q$dir\E// &&
  59. $page_trimmed=~s/^\Q$dir\E//;
  60. push @links, { url => $href, page => $p_trimmed };
  61. }
  62. }
  63. return sort { $a->{page} cmp $b->{page} } @links;
  64. } #}}}
  65. sub parentlinks ($) { #{{{
  66. my $page=shift;
  67. my @ret;
  68. my $pagelink="";
  69. my $path="";
  70. my $skip=1;
  71. foreach my $dir (reverse split("/", $page)) {
  72. if (! $skip) {
  73. $path.="../";
  74. unshift @ret, { url => "$path$dir.html", page => $dir };
  75. }
  76. else {
  77. $skip=0;
  78. }
  79. }
  80. unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
  81. return @ret;
  82. } #}}}
  83. sub preprocess ($$;$) { #{{{
  84. my $page=shift;
  85. my $content=shift;
  86. my $onlystrip=shift || 0; # strip directives without processing
  87. my $handle=sub {
  88. my $escape=shift;
  89. my $command=shift;
  90. my $params=shift;
  91. if (length $escape) {
  92. return "[[$command $params]]";
  93. }
  94. elsif ($onlystrip) {
  95. return "";
  96. }
  97. elsif (exists $hooks{preprocess}{$command}) {
  98. # Note: preserve order of params, some plugins may
  99. # consider it significant.
  100. my @params;
  101. while ($params =~ /(\w+)=\"?([^"]+)"?(\s+|$)/g) {
  102. push @params, $1, $2;
  103. }
  104. return $hooks{preprocess}{$command}{call}->(@params, page => $page);
  105. }
  106. else {
  107. return "[[$command not processed]]";
  108. }
  109. };
  110. $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
  111. return $content;
  112. } #}}}
  113. sub add_depends ($$) { #{{{
  114. my $page=shift;
  115. my $globlist=shift;
  116. if (! exists $depends{$page}) {
  117. $depends{$page}=$globlist;
  118. }
  119. else {
  120. $depends{$page}=globlist_merge($depends{$page}, $globlist);
  121. }
  122. } # }}}
  123. sub globlist_merge ($$) { #{{{
  124. my $a=shift;
  125. my $b=shift;
  126. my $ret="";
  127. # Only add negated globs if they are not matched by the other globlist.
  128. foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
  129. (map { [ $b, $_ ] } split(" ", $a))) {
  130. if ($i->[1]=~/^!(.*)/) {
  131. if (! globlist_match($1, $i->[0])) {
  132. $ret.=" ".$i->[1];
  133. }
  134. }
  135. else {
  136. $ret.=" ".$i->[1];
  137. }
  138. }
  139. return $ret;
  140. } #}}}
  141. sub genpage ($$$) { #{{{
  142. my $page=shift;
  143. my $content=shift;
  144. my $mtime=shift;
  145. my $title=pagetitle(basename($page));
  146. my $template=HTML::Template->new(blind_cache => 1,
  147. filename => "$config{templatedir}/page.tmpl");
  148. my $actions=0;
  149. if (length $config{cgiurl}) {
  150. $template->param(editurl => cgiurl(do => "edit", page => $page));
  151. $template->param(prefsurl => cgiurl(do => "prefs"));
  152. if ($config{rcs}) {
  153. $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
  154. }
  155. $actions++;
  156. }
  157. if (length $config{historyurl}) {
  158. my $u=$config{historyurl};
  159. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  160. $template->param(historyurl => $u);
  161. $actions++;
  162. }
  163. if ($config{discussion}) {
  164. $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
  165. $actions++;
  166. }
  167. if ($actions) {
  168. $template->param(have_actions => 1);
  169. }
  170. $template->param(
  171. title => $title,
  172. wikiname => $config{wikiname},
  173. parentlinks => [parentlinks($page)],
  174. content => $content,
  175. backlinks => [backlinks($page)],
  176. mtime => displaytime($mtime),
  177. styleurl => styleurl($page),
  178. );
  179. if (exists $hooks{pagetemplate}) {
  180. foreach my $id (keys %{$hooks{pagetemplate}}) {
  181. $hooks{pagetemplate}{$id}{call}->($page, $template);
  182. }
  183. }
  184. return $template->output;
  185. } #}}}
  186. sub check_overwrite ($$) { #{{{
  187. # Important security check. Make sure to call this before saving
  188. # any files to the source directory.
  189. my $dest=shift;
  190. my $src=shift;
  191. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  192. error("$dest already exists and was rendered from ".
  193. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  194. %renderedfiles)).
  195. ", before, so not rendering from $src");
  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 Encode::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 render ($) { #{{{
  227. my $file=shift;
  228. my $type=pagetype($file);
  229. my $srcfile=srcfile($file);
  230. if ($type ne 'unknown') {
  231. my $content=readfile($srcfile);
  232. my $page=pagename($file);
  233. delete $depends{$page};
  234. if (exists $hooks{filter}) {
  235. foreach my $id (keys %{$hooks{filter}}) {
  236. $content=$hooks{filter}{$id}{call}->(
  237. page => $page,
  238. content => $content
  239. );
  240. }
  241. }
  242. $links{$page}=[findlinks($page, $content)];
  243. $content=linkify($page, $page, $content);
  244. $content=preprocess($page, $content);
  245. $content=htmlize($type, $content);
  246. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  247. writefile(htmlpage($page), $config{destdir},
  248. genpage($page, $content, mtime($srcfile)));
  249. $oldpagemtime{$page}=time;
  250. $renderedfiles{$page}=htmlpage($page);
  251. }
  252. else {
  253. my $content=readfile($srcfile, 1);
  254. $links{$file}=[];
  255. delete $depends{$file};
  256. check_overwrite("$config{destdir}/$file", $file);
  257. writefile($file, $config{destdir}, $content, 1);
  258. $oldpagemtime{$file}=time;
  259. $renderedfiles{$file}=$file;
  260. }
  261. } #}}}
  262. sub prune ($) { #{{{
  263. my $file=shift;
  264. unlink($file);
  265. my $dir=dirname($file);
  266. while (rmdir($dir)) {
  267. $dir=dirname($dir);
  268. }
  269. } #}}}
  270. sub refresh () { #{{{
  271. # find existing pages
  272. my %exists;
  273. my @files;
  274. eval q{use File::Find};
  275. find({
  276. no_chdir => 1,
  277. wanted => sub {
  278. if (/$config{wiki_file_prune_regexp}/) {
  279. $File::Find::prune=1;
  280. }
  281. elsif (! -d $_ && ! -l $_) {
  282. my ($f)=/$config{wiki_file_regexp}/; # untaint
  283. if (! defined $f) {
  284. warn("skipping bad filename $_\n");
  285. }
  286. else {
  287. $f=~s/^\Q$config{srcdir}\E\/?//;
  288. push @files, $f;
  289. $exists{pagename($f)}=1;
  290. }
  291. }
  292. },
  293. }, $config{srcdir});
  294. find({
  295. no_chdir => 1,
  296. wanted => sub {
  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 changed file $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