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