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