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