summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: f9fbc801f750419c76ca301dbcaac1ec62d32aa6 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use Encode;
  7. my (%backlinks, %rendered);
  8. our %brokenlinks;
  9. my $links_calculated=0;
  10. sub calculate_links () {
  11. return if $links_calculated;
  12. %backlinks=%brokenlinks=();
  13. foreach my $page (keys %links) {
  14. foreach my $link (@{$links{$page}}) {
  15. my $bestlink=bestlink($page, $link);
  16. if (length $bestlink) {
  17. $backlinks{$bestlink}{$page}=1
  18. if $bestlink ne $page;
  19. }
  20. else {
  21. push @{$brokenlinks{$link}}, $page;
  22. }
  23. }
  24. }
  25. $links_calculated=1;
  26. }
  27. sub backlink_pages ($) {
  28. my $page=shift;
  29. calculate_links();
  30. return keys %{$backlinks{$page}};
  31. }
  32. sub backlinks ($) {
  33. my $page=shift;
  34. my @links;
  35. foreach my $p (backlink_pages($page)) {
  36. my $href=urlto($p, $page);
  37. # Trim common dir prefixes from both pages.
  38. my $p_trimmed=$p;
  39. my $page_trimmed=$page;
  40. my $dir;
  41. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  42. defined $dir &&
  43. $p_trimmed=~s/^\Q$dir\E// &&
  44. $page_trimmed=~s/^\Q$dir\E//;
  45. push @links, { url => $href, page => pagetitle($p_trimmed) };
  46. }
  47. return @links;
  48. }
  49. sub genpage ($$) {
  50. my $page=shift;
  51. my $content=shift;
  52. run_hooks(indexhtml => sub {
  53. shift->(page => $page, destpage => $page, content => $content);
  54. });
  55. my $templatefile;
  56. run_hooks(templatefile => sub {
  57. return if defined $templatefile;
  58. my $file=shift->(page => $page);
  59. if (defined $file && defined template_file($file)) {
  60. $templatefile=$file;
  61. }
  62. });
  63. my $template;
  64. if (defined $templatefile) {
  65. $template=template_depends($templatefile, $page,
  66. blind_cache => 1);
  67. }
  68. else {
  69. # no explicit depends as special case
  70. $template=template('page.tmpl',
  71. blind_cache => 1);
  72. }
  73. my $actions=0;
  74. if (length $config{cgiurl}) {
  75. if (IkiWiki->can("cgi_editpage")) {
  76. $template->param(editurl => cgiurl(do => "edit", page => $page));
  77. $actions++;
  78. }
  79. }
  80. if (defined $config{historyurl} && length $config{historyurl}) {
  81. my $u=$config{historyurl};
  82. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  83. $template->param(historyurl => $u);
  84. $actions++;
  85. }
  86. if ($config{discussion}) {
  87. if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
  88. (length $config{cgiurl} ||
  89. exists $links{$page."/".$config{discussionpage}})) {
  90. $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
  91. $actions++;
  92. }
  93. }
  94. if ($actions) {
  95. $template->param(have_actions => 1);
  96. }
  97. templateactions($template, $page);
  98. my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
  99. my ($backlinks, $more_backlinks);
  100. if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
  101. $backlinks=\@backlinks;
  102. $more_backlinks=[];
  103. }
  104. else {
  105. $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
  106. $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
  107. }
  108. $template->param(
  109. title => $page eq 'index'
  110. ? $config{wikiname}
  111. : pagetitle(basename($page)),
  112. wikiname => $config{wikiname},
  113. content => $content,
  114. backlinks => $backlinks,
  115. more_backlinks => $more_backlinks,
  116. mtime => displaytime($pagemtime{$page}),
  117. ctime => displaytime($pagectime{$page}, undef, 1),
  118. baseurl => baseurl($page),
  119. html5 => $config{html5},
  120. );
  121. run_hooks(pagetemplate => sub {
  122. shift->(page => $page, destpage => $page, template => $template);
  123. });
  124. $content=$template->output;
  125. run_hooks(format => sub {
  126. $content=shift->(
  127. page => $page,
  128. content => $content,
  129. );
  130. });
  131. return $content;
  132. }
  133. sub scan ($) {
  134. my $file=shift;
  135. debug(sprintf(gettext("scanning %s"), $file));
  136. my $type=pagetype($file);
  137. if (defined $type) {
  138. my $srcfile=srcfile($file);
  139. my $content=readfile($srcfile);
  140. my $page=pagename($file);
  141. will_render($page, htmlpage($page), 1);
  142. if ($config{discussion}) {
  143. # Discussion links are a special case since they're
  144. # not in the text of the page, but on its template.
  145. $links{$page}=[ $page."/".lc($config{discussionpage}) ];
  146. }
  147. else {
  148. $links{$page}=[];
  149. }
  150. delete $typedlinks{$page};
  151. run_hooks(scan => sub {
  152. shift->(
  153. page => $page,
  154. content => $content,
  155. );
  156. });
  157. # Preprocess in scan-only mode.
  158. preprocess($page, $page, $content, 1);
  159. }
  160. else {
  161. will_render($file, $file, 1);
  162. }
  163. }
  164. sub fast_file_copy (@) {
  165. my $srcfile=shift;
  166. my $destfile=shift;
  167. my $srcfd=shift;
  168. my $destfd=shift;
  169. my $cleanup=shift;
  170. my $blksize = 16384;
  171. my ($len, $buf, $written);
  172. while ($len = sysread $srcfd, $buf, $blksize) {
  173. if (! defined $len) {
  174. next if $! =~ /^Interrupted/;
  175. error("failed to read $srcfile: $!", $cleanup);
  176. }
  177. my $offset = 0;
  178. while ($len) {
  179. defined($written = syswrite $destfd, $buf, $len, $offset)
  180. or error("failed to write $destfile: $!", $cleanup);
  181. $len -= $written;
  182. $offset += $written;
  183. }
  184. }
  185. }
  186. sub render ($$) {
  187. my $file=shift;
  188. return if $rendered{$file};
  189. debug(shift);
  190. $rendered{$file}=1;
  191. my $type=pagetype($file);
  192. my $srcfile=srcfile($file);
  193. if (defined $type) {
  194. my $page=pagename($file);
  195. delete $depends{$page};
  196. delete $depends_simple{$page};
  197. will_render($page, htmlpage($page), 1);
  198. return if $type=~/^_/;
  199. my $content=htmlize($page, $page, $type,
  200. linkify($page, $page,
  201. preprocess($page, $page,
  202. filter($page, $page,
  203. readfile($srcfile)))));
  204. my $output=htmlpage($page);
  205. writefile($output, $config{destdir}, genpage($page, $content));
  206. }
  207. else {
  208. delete $depends{$file};
  209. delete $depends_simple{$file};
  210. will_render($file, $file, 1);
  211. if ($config{hardlink}) {
  212. # only hardlink if owned by same user
  213. my @stat=stat($srcfile);
  214. if ($stat[4] == $>) {
  215. prep_writefile($file, $config{destdir});
  216. unlink($config{destdir}."/".$file);
  217. if (link($srcfile, $config{destdir}."/".$file)) {
  218. return;
  219. }
  220. }
  221. # if hardlink fails, fall back to copying
  222. }
  223. my $srcfd=readfile($srcfile, 1, 1);
  224. writefile($file, $config{destdir}, undef, 1, sub {
  225. fast_file_copy($srcfile, $file, $srcfd, @_);
  226. });
  227. }
  228. }
  229. sub prune ($) {
  230. my $file=shift;
  231. unlink($file);
  232. my $dir=dirname($file);
  233. while (rmdir($dir)) {
  234. $dir=dirname($dir);
  235. }
  236. }
  237. sub srcdir_check () {
  238. # security check, avoid following symlinks in the srcdir path by default
  239. my $test=$config{srcdir};
  240. while (length $test) {
  241. if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
  242. error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
  243. }
  244. unless ($test=~s/\/+$//) {
  245. $test=dirname($test);
  246. }
  247. }
  248. }
  249. sub find_src_files () {
  250. my @files;
  251. my %pages;
  252. eval q{use File::Find};
  253. error($@) if $@;
  254. my ($page, $dir, $underlay);
  255. my $helper=sub {
  256. my $file=decode_utf8($_);
  257. return if -l $file || -d _;
  258. $file=~s/^\Q$dir\E\/?//;
  259. return if ! length $file;
  260. $page = pagename($file);
  261. if (! exists $pagesources{$page} &&
  262. file_pruned($file)) {
  263. $File::Find::prune=1;
  264. return;
  265. }
  266. my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
  267. if (! defined $f) {
  268. warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
  269. return;
  270. }
  271. if ($underlay) {
  272. # avoid underlaydir override attacks; see security.mdwn
  273. if (! -l "$config{srcdir}/$f" && ! -e _) {
  274. if (! $pages{$page}) {
  275. push @files, $f;
  276. $pages{$page}=1;
  277. }
  278. }
  279. }
  280. else {
  281. push @files, $f;
  282. if ($pages{$page}) {
  283. debug(sprintf(gettext("%s has multiple possible source pages"), $page));
  284. }
  285. $pages{$page}=1;
  286. }
  287. };
  288. find({
  289. no_chdir => 1,
  290. wanted => $helper,
  291. }, $dir=$config{srcdir});
  292. $underlay=1;
  293. foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
  294. find({
  295. no_chdir => 1,
  296. wanted => $helper,
  297. }, $dir=$_);
  298. };
  299. return \@files, \%pages;
  300. }
  301. sub find_new_files ($) {
  302. my $files=shift;
  303. my @new;
  304. my @internal_new;
  305. my $times_noted;
  306. foreach my $file (@$files) {
  307. my $page=pagename($file);
  308. if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
  309. # the page has changed its type
  310. $forcerebuild{$page}=1;
  311. }
  312. $pagesources{$page}=$file;
  313. if (! $pagemtime{$page}) {
  314. if (isinternal($page)) {
  315. push @internal_new, $file;
  316. }
  317. elsif ($config{rcs}) {
  318. push @new, $file;
  319. if ($config{gettime} && -e "$config{srcdir}/$file") {
  320. if (! $times_noted) {
  321. debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
  322. $times_noted=1;
  323. }
  324. eval {
  325. my $ctime=rcs_getctime("$config{srcdir}/$file");
  326. if ($ctime > 0) {
  327. $pagectime{$page}=$ctime;
  328. }
  329. };
  330. if ($@) {
  331. print STDERR $@;
  332. }
  333. my $mtime;
  334. eval {
  335. $mtime=rcs_getmtime("$config{srcdir}/$file");
  336. };
  337. if ($@) {
  338. print STDERR $@;
  339. }
  340. elsif ($mtime > 0) {
  341. utime($mtime, $mtime, "$config{srcdir}/$file");
  342. }
  343. }
  344. }
  345. $pagecase{lc $page}=$page;
  346. if (! exists $pagectime{$page}) {
  347. $pagectime{$page}=(srcfile_stat($file))[10];
  348. }
  349. }
  350. }
  351. return \@new, \@internal_new;
  352. }
  353. sub find_del_files ($) {
  354. my $pages=shift;
  355. my @del;
  356. my @internal_del;
  357. foreach my $page (keys %pagesources) {
  358. if (! $pages->{$page}) {
  359. if (isinternal($page)) {
  360. push @internal_del, $pagesources{$page};
  361. }
  362. else {
  363. push @del, $pagesources{$page};
  364. }
  365. $links{$page}=[];
  366. delete $typedlinks{$page};
  367. $renderedfiles{$page}=[];
  368. $pagemtime{$page}=0;
  369. }
  370. }
  371. return \@del, \@internal_del;
  372. }
  373. sub remove_del (@) {
  374. foreach my $file (@_) {
  375. my $page=pagename($file);
  376. if (! isinternal($page)) {
  377. debug(sprintf(gettext("removing obsolete %s"), $page));
  378. }
  379. foreach my $old (@{$oldrenderedfiles{$page}}) {
  380. prune($config{destdir}."/".$old);
  381. }
  382. foreach my $source (keys %destsources) {
  383. if ($destsources{$source} eq $page) {
  384. delete $destsources{$source};
  385. }
  386. }
  387. delete $pagecase{lc $page};
  388. $delpagesources{$page}=$pagesources{$page};
  389. delete $pagesources{$page};
  390. }
  391. }
  392. sub find_changed ($) {
  393. my $files=shift;
  394. my @changed;
  395. my @internal_changed;
  396. foreach my $file (@$files) {
  397. my $page=pagename($file);
  398. my ($srcfile, @stat)=srcfile_stat($file);
  399. if (! exists $pagemtime{$page} ||
  400. $stat[9] > $pagemtime{$page} ||
  401. $forcerebuild{$page}) {
  402. $pagemtime{$page}=$stat[9];
  403. if (isinternal($page)) {
  404. # Preprocess internal page in scan-only mode.
  405. preprocess($page, $page, readfile($srcfile), 1);
  406. push @internal_changed, $file;
  407. }
  408. else {
  409. push @changed, $file;
  410. }
  411. }
  412. }
  413. return \@changed, \@internal_changed;
  414. }
  415. sub calculate_old_links ($$) {
  416. my ($changed, $del)=@_;
  417. my %oldlink_targets;
  418. foreach my $file (@$changed, @$del) {
  419. my $page=pagename($file);
  420. if (exists $oldlinks{$page}) {
  421. foreach my $l (@{$oldlinks{$page}}) {
  422. $oldlink_targets{$page}{$l}=bestlink($page, $l);
  423. }
  424. }
  425. }
  426. return \%oldlink_targets;
  427. }
  428. sub derender_internal ($) {
  429. my $file=shift;
  430. my $page=pagename($file);
  431. delete $depends{$page};
  432. delete $depends_simple{$page};
  433. foreach my $old (@{$renderedfiles{$page}}) {
  434. delete $destsources{$old};
  435. }
  436. $renderedfiles{$page}=[];
  437. }
  438. sub render_linkers ($) {
  439. my $f=shift;
  440. my $p=pagename($f);
  441. foreach my $page (keys %{$backlinks{$p}}) {
  442. my $file=$pagesources{$page};
  443. render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
  444. }
  445. }
  446. sub remove_unrendered () {
  447. foreach my $src (keys %rendered) {
  448. my $page=pagename($src);
  449. foreach my $file (@{$oldrenderedfiles{$page}}) {
  450. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  451. debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
  452. prune($config{destdir}."/".$file);
  453. }
  454. }
  455. }
  456. }
  457. sub link_types_changed ($$) {
  458. # each is of the form { type => { link => 1 } }
  459. my $new = shift;
  460. my $old = shift;
  461. return 0 if !defined $new && !defined $old;
  462. return 1 if (!defined $new && %$old) || (!defined $old && %$new);
  463. while (my ($type, $links) = each %$new) {
  464. foreach my $link (keys %$links) {
  465. return 1 unless exists $old->{$type}{$link};
  466. }
  467. }
  468. while (my ($type, $links) = each %$old) {
  469. foreach my $link (keys %$links) {
  470. return 1 unless exists $new->{$type}{$link};
  471. }
  472. }
  473. return 0;
  474. }
  475. sub calculate_changed_links ($$$) {
  476. my ($changed, $del, $oldlink_targets)=@_;
  477. my (%backlinkchanged, %linkchangers);
  478. foreach my $file (@$changed, @$del) {
  479. my $page=pagename($file);
  480. if (exists $links{$page}) {
  481. foreach my $l (@{$links{$page}}) {
  482. my $target=bestlink($page, $l);
  483. if (! exists $oldlink_targets->{$page}{$l} ||
  484. $target ne $oldlink_targets->{$page}{$l}) {
  485. $backlinkchanged{$target}=1;
  486. $linkchangers{lc($page)}=1;
  487. }
  488. delete $oldlink_targets->{$page}{$l};
  489. }
  490. }
  491. if (exists $oldlink_targets->{$page} &&
  492. %{$oldlink_targets->{$page}}) {
  493. foreach my $target (values %{$oldlink_targets->{$page}}) {
  494. $backlinkchanged{$target}=1;
  495. }
  496. $linkchangers{lc($page)}=1;
  497. }
  498. # we currently assume that changing the type of a link doesn't
  499. # change backlinks
  500. if (!exists $linkchangers{lc($page)}) {
  501. if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
  502. $linkchangers{lc($page)}=1;
  503. }
  504. }
  505. }
  506. return \%backlinkchanged, \%linkchangers;
  507. }
  508. sub render_dependent ($$$$$$$) {
  509. my ($files, $new, $internal_new, $del, $internal_del,
  510. $internal_changed, $linkchangers)=@_;
  511. my @changed=(keys %rendered, @$del);
  512. my @exists_changed=(@$new, @$del);
  513. my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
  514. my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
  515. foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
  516. if ($rendered{$p} || grep { $_ eq $p } @$del) {
  517. foreach my $f (@$files) {
  518. next if $rendered{$f};
  519. render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
  520. }
  521. return 0;
  522. }
  523. }
  524. foreach my $f (@$files) {
  525. next if $rendered{$f};
  526. my $p=pagename($f);
  527. my $reason = undef;
  528. if (exists $depends_simple{$p} && ! defined $reason) {
  529. foreach my $d (keys %{$depends_simple{$p}}) {
  530. if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
  531. $lc_changed{$d})
  532. ||
  533. ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
  534. $lc_exists_changed{$d})
  535. ||
  536. ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
  537. $linkchangers->{$d})
  538. ) {
  539. $reason = $d;
  540. last;
  541. }
  542. }
  543. }
  544. if (exists $depends{$p} && ! defined $reason) {
  545. foreach my $dep (keys %{$depends{$p}}) {
  546. my $sub=pagespec_translate($dep);
  547. next unless defined $sub;
  548. # only consider internal files
  549. # if the page explicitly depends
  550. # on such files
  551. my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
  552. my $in=sub {
  553. my $list=shift;
  554. my $type=shift;
  555. foreach my $file (@$list) {
  556. next if $file eq $f;
  557. my $page=pagename($file);
  558. if ($sub->($page, location => $p)) {
  559. if ($type == $IkiWiki::DEPEND_LINKS) {
  560. next unless $linkchangers->{lc($page)};
  561. }
  562. $reason=$page;
  563. return 1;
  564. }
  565. }
  566. return undef;
  567. };
  568. if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
  569. last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
  570. last if $internal_dep && (
  571. $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
  572. $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
  573. $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
  574. );
  575. }
  576. if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
  577. last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
  578. last if $internal_dep && (
  579. $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
  580. $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
  581. );
  582. }
  583. if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
  584. last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
  585. last if $internal_dep && (
  586. $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
  587. $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
  588. $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
  589. );
  590. }
  591. }
  592. }
  593. if (defined $reason) {
  594. render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
  595. return 1;
  596. }
  597. }
  598. return 0;
  599. }
  600. sub render_backlinks ($) {
  601. my $backlinkchanged=shift;
  602. foreach my $link (keys %$backlinkchanged) {
  603. my $linkfile=$pagesources{$link};
  604. if (defined $linkfile) {
  605. render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
  606. }
  607. }
  608. }
  609. sub gen_autofile ($$$) {
  610. my $autofile=shift;
  611. my $pages=shift;
  612. my $del=shift;
  613. if (file_pruned($autofile)) {
  614. return;
  615. }
  616. my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
  617. if (! defined $file) {
  618. return;
  619. }
  620. # Remember autofiles that were tried, and never try them again later.
  621. if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
  622. return;
  623. }
  624. $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
  625. if (srcfile($autofile, 1) || file_pruned($autofile)) {
  626. return;
  627. }
  628. if (-l $file || -d _ || -e _) {
  629. return;
  630. }
  631. my $page = pagename($file);
  632. if ($pages->{$page}) {
  633. return;
  634. }
  635. if (grep { $_ eq $autofile } @$del) {
  636. return;
  637. }
  638. $autofiles{$autofile}{generator}->();
  639. $pages->{$page}=1;
  640. return 1;
  641. }
  642. sub refresh () {
  643. srcdir_check();
  644. run_hooks(refresh => sub { shift->() });
  645. my ($files, $pages)=find_src_files();
  646. my ($new, $internal_new)=find_new_files($files);
  647. my ($del, $internal_del)=find_del_files($pages);
  648. my ($changed, $internal_changed)=find_changed($files);
  649. run_hooks(needsbuild => sub { shift->($changed) });
  650. my $oldlink_targets=calculate_old_links($changed, $del);
  651. foreach my $file (@$changed) {
  652. scan($file);
  653. }
  654. foreach my $autofile (keys %autofiles) {
  655. if (gen_autofile($autofile, $pages, $del)) {
  656. push @{$files}, $autofile;
  657. push @{$new}, $autofile if find_new_files([$autofile]);
  658. push @{$changed}, $autofile if find_changed([$autofile]);
  659. scan($autofile);
  660. }
  661. }
  662. calculate_links();
  663. remove_del(@$del, @$internal_del);
  664. foreach my $file (@$changed) {
  665. render($file, sprintf(gettext("building %s"), $file));
  666. }
  667. foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
  668. derender_internal($file);
  669. }
  670. my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
  671. $del, $oldlink_targets);
  672. foreach my $file (@$new, @$del) {
  673. render_linkers($file);
  674. }
  675. if (@$changed || @$internal_changed ||
  676. @$del || @$internal_del || @$internal_new) {
  677. 1 while render_dependent($files, $new, $internal_new,
  678. $del, $internal_del, $internal_changed,
  679. $linkchangers);
  680. }
  681. render_backlinks($backlinkchanged);
  682. remove_unrendered();
  683. if (@$del || @$internal_del) {
  684. run_hooks(delete => sub { shift->(@$del, @$internal_del) });
  685. }
  686. if (%rendered) {
  687. run_hooks(change => sub { shift->(keys %rendered) });
  688. }
  689. }
  690. sub clean_rendered {
  691. lockwiki();
  692. loadindex();
  693. remove_unrendered();
  694. foreach my $page (keys %oldrenderedfiles) {
  695. foreach my $file (@{$oldrenderedfiles{$page}}) {
  696. prune($config{destdir}."/".$file);
  697. }
  698. }
  699. }
  700. sub commandline_render () {
  701. lockwiki();
  702. loadindex();
  703. unlockwiki();
  704. my $srcfile=possibly_foolish_untaint($config{render});
  705. my $file=$srcfile;
  706. $file=~s/\Q$config{srcdir}\E\/?//;
  707. my $type=pagetype($file);
  708. die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
  709. my $content=readfile($srcfile);
  710. my $page=pagename($file);
  711. $pagesources{$page}=$file;
  712. $content=filter($page, $page, $content);
  713. $content=preprocess($page, $page, $content);
  714. $content=linkify($page, $page, $content);
  715. $content=htmlize($page, $page, $type, $content);
  716. $pagemtime{$page}=(stat($srcfile))[9];
  717. $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
  718. print genpage($page, $content);
  719. exit 0;
  720. }
  721. 1