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