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