summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: c5922c933a7516b9a06476ce68855e9289664c97 (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 => displaytime($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 displaytime ($) { #{{{
  190. my $time=shift;
  191. if ($config{timeformat} eq '%c') {
  192. return scalar(localtime($time)); # optimisation
  193. }
  194. else {
  195. eval q{use POSIX};
  196. return POSIX::strftime($config{timeformat}, localtime($time));
  197. }
  198. } #}}}
  199. sub mtime ($) { #{{{
  200. my $file=shift;
  201. return (stat($file))[9];
  202. } #}}}
  203. sub findlinks ($$) { #{{{
  204. my $page=shift;
  205. my $content=shift;
  206. my @links;
  207. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  208. push @links, titlepage($2);
  209. }
  210. if ($config{discussion}) {
  211. # Discussion links are a special case since they're not in the
  212. # text of the page, but on its template.
  213. return @links, "$page/discussion";
  214. }
  215. else {
  216. return @links;
  217. }
  218. } #}}}
  219. sub render ($) { #{{{
  220. my $file=shift;
  221. my $type=pagetype($file);
  222. my $srcfile=srcfile($file);
  223. if ($type ne 'unknown') {
  224. my $content=readfile($srcfile);
  225. my $page=pagename($file);
  226. delete $depends{$page};
  227. if (exists $hooks{filter}) {
  228. foreach my $id (keys %{$hooks{filter}}) {
  229. $content=$hooks{filter}{$id}{call}->(
  230. page => $page,
  231. content => $content
  232. );
  233. }
  234. }
  235. $links{$page}=[findlinks($page, $content)];
  236. $content=linkify($page, $page, $content);
  237. $content=preprocess($page, $content);
  238. $content=htmlize($type, $content);
  239. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  240. writefile(htmlpage($page), $config{destdir},
  241. genpage($page, $content, mtime($srcfile)));
  242. $oldpagemtime{$page}=time;
  243. $renderedfiles{$page}=htmlpage($page);
  244. }
  245. else {
  246. my $content=readfile($srcfile, 1);
  247. $links{$file}=[];
  248. delete $depends{$file};
  249. check_overwrite("$config{destdir}/$file", $file);
  250. writefile($file, $config{destdir}, $content, 1);
  251. $oldpagemtime{$file}=time;
  252. $renderedfiles{$file}=$file;
  253. }
  254. } #}}}
  255. sub prune ($) { #{{{
  256. my $file=shift;
  257. unlink($file);
  258. my $dir=dirname($file);
  259. while (rmdir($dir)) {
  260. $dir=dirname($dir);
  261. }
  262. } #}}}
  263. sub refresh () { #{{{
  264. # find existing pages
  265. my %exists;
  266. my @files;
  267. eval q{use File::Find};
  268. find({
  269. no_chdir => 1,
  270. wanted => sub {
  271. if (/$config{wiki_file_prune_regexp}/) {
  272. $File::Find::prune=1;
  273. }
  274. elsif (! -d $_ && ! -l $_) {
  275. my ($f)=/$config{wiki_file_regexp}/; # untaint
  276. if (! defined $f) {
  277. warn("skipping bad filename $_\n");
  278. }
  279. else {
  280. $f=~s/^\Q$config{srcdir}\E\/?//;
  281. push @files, $f;
  282. $exists{pagename($f)}=1;
  283. }
  284. }
  285. },
  286. }, $config{srcdir});
  287. find({
  288. no_chdir => 1,
  289. wanted => sub {
  290. if (/$config{wiki_file_prune_regexp}/) {
  291. $File::Find::prune=1;
  292. }
  293. elsif (! -d $_ && ! -l $_) {
  294. my ($f)=/$config{wiki_file_regexp}/; # untaint
  295. if (! defined $f) {
  296. warn("skipping bad filename $_\n");
  297. }
  298. else {
  299. # Don't add files that are in the
  300. # srcdir.
  301. $f=~s/^\Q$config{underlaydir}\E\/?//;
  302. if (! -e "$config{srcdir}/$f" &&
  303. ! -l "$config{srcdir}/$f") {
  304. push @files, $f;
  305. $exists{pagename($f)}=1;
  306. }
  307. }
  308. }
  309. },
  310. }, $config{underlaydir});
  311. my %rendered;
  312. # check for added or removed pages
  313. my @add;
  314. foreach my $file (@files) {
  315. my $page=pagename($file);
  316. if (! $oldpagemtime{$page}) {
  317. debug("new page $page") unless exists $pagectime{$page};
  318. push @add, $file;
  319. $links{$page}=[];
  320. $pagesources{$page}=$file;
  321. if ($config{getctime} && -e "$config{srcdir}/$file") {
  322. $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
  323. }
  324. elsif (! exists $pagectime{$page}) {
  325. $pagectime{$page}=mtime(srcfile($file));
  326. }
  327. }
  328. }
  329. my @del;
  330. foreach my $page (keys %oldpagemtime) {
  331. if (! $exists{$page}) {
  332. debug("removing old page $page");
  333. push @del, $pagesources{$page};
  334. prune($config{destdir}."/".$renderedfiles{$page});
  335. delete $renderedfiles{$page};
  336. $oldpagemtime{$page}=0;
  337. delete $pagesources{$page};
  338. }
  339. }
  340. # render any updated files
  341. foreach my $file (@files) {
  342. my $page=pagename($file);
  343. if (! exists $oldpagemtime{$page} ||
  344. mtime(srcfile($file)) > $oldpagemtime{$page}) {
  345. debug("rendering changed file $file");
  346. render($file);
  347. $rendered{$file}=1;
  348. }
  349. }
  350. # if any files were added or removed, check to see if each page
  351. # needs an update due to linking to them or inlining them.
  352. # TODO: inefficient; pages may get rendered above and again here;
  353. # problem is the bestlink may have changed and we won't know until
  354. # now
  355. if (@add || @del) {
  356. FILE: foreach my $file (@files) {
  357. my $page=pagename($file);
  358. foreach my $f (@add, @del) {
  359. my $p=pagename($f);
  360. foreach my $link (@{$links{$page}}) {
  361. if (bestlink($page, $link) eq $p) {
  362. debug("rendering $file, which links to $p");
  363. render($file);
  364. $rendered{$file}=1;
  365. next FILE;
  366. }
  367. }
  368. }
  369. }
  370. }
  371. # Handle backlinks; if a page has added/removed links, update the
  372. # pages it links to. Also handles rebuilding dependat pages.
  373. # TODO: inefficient; pages may get rendered above and again here;
  374. # problem is the backlinks could be wrong in the first pass render
  375. # above
  376. if (%rendered || @del) {
  377. foreach my $f (@files) {
  378. my $p=pagename($f);
  379. if (exists $depends{$p}) {
  380. foreach my $file (keys %rendered, @del) {
  381. next if $f eq $file;
  382. my $page=pagename($file);
  383. if (globlist_match($page, $depends{$p})) {
  384. debug("rendering $f, which depends on $page");
  385. render($f);
  386. $rendered{$f}=1;
  387. last;
  388. }
  389. }
  390. }
  391. }
  392. my %linkchanged;
  393. foreach my $file (keys %rendered, @del) {
  394. my $page=pagename($file);
  395. if (exists $links{$page}) {
  396. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  397. if (length $link &&
  398. (! exists $oldlinks{$page} ||
  399. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  400. $linkchanged{$link}=1;
  401. }
  402. }
  403. }
  404. if (exists $oldlinks{$page}) {
  405. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  406. if (length $link &&
  407. (! exists $links{$page} ||
  408. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  409. $linkchanged{$link}=1;
  410. }
  411. }
  412. }
  413. }
  414. foreach my $link (keys %linkchanged) {
  415. my $linkfile=$pagesources{$link};
  416. if (defined $linkfile) {
  417. debug("rendering $linkfile, to update its backlinks");
  418. render($linkfile);
  419. $rendered{$linkfile}=1;
  420. }
  421. }
  422. }
  423. if (@del && exists $hooks{delete}) {
  424. foreach my $id (keys %{$hooks{delete}}) {
  425. $hooks{delete}{$id}{call}->(@del);
  426. }
  427. }
  428. if (%rendered && exists $hooks{change}) {
  429. foreach my $id (keys %{$hooks{change}}) {
  430. $hooks{change}{$id}{call}->(keys %rendered);
  431. }
  432. }
  433. } #}}}
  434. 1