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