summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 8ae0cbd4f64fe4f8b450114dc1cdb1ba0caad400 (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(postscan => sub {
  53. shift->(page => $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. if (exists $hooks{auth}) {
  80. $template->param(prefsurl => cgiurl(do => "prefs"));
  81. $actions++;
  82. }
  83. }
  84. if (defined $config{historyurl} && length $config{historyurl}) {
  85. my $u=$config{historyurl};
  86. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  87. $template->param(historyurl => $u);
  88. $actions++;
  89. }
  90. if ($config{discussion}) {
  91. if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
  92. (length $config{cgiurl} ||
  93. exists $links{$page."/".$config{discussionpage}})) {
  94. $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
  95. $actions++;
  96. }
  97. }
  98. if ($actions) {
  99. $template->param(have_actions => 1);
  100. }
  101. my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
  102. my ($backlinks, $more_backlinks);
  103. if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
  104. $backlinks=\@backlinks;
  105. $more_backlinks=[];
  106. }
  107. else {
  108. $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
  109. $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
  110. }
  111. $template->param(
  112. title => $page eq 'index'
  113. ? $config{wikiname}
  114. : pagetitle(basename($page)),
  115. wikiname => $config{wikiname},
  116. content => $content,
  117. backlinks => $backlinks,
  118. more_backlinks => $more_backlinks,
  119. mtime => displaytime($pagemtime{$page}),
  120. ctime => displaytime($pagectime{$page}),
  121. baseurl => baseurl($page),
  122. );
  123. run_hooks(pagetemplate => sub {
  124. shift->(page => $page, destpage => $page, template => $template);
  125. });
  126. $content=$template->output;
  127. run_hooks(format => sub {
  128. $content=shift->(
  129. page => $page,
  130. content => $content,
  131. );
  132. });
  133. return $content;
  134. }
  135. sub scan ($) {
  136. my $file=shift;
  137. debug(sprintf(gettext("scanning %s"), $file));
  138. my $type=pagetype($file);
  139. if (defined $type) {
  140. my $srcfile=srcfile($file);
  141. my $content=readfile($srcfile);
  142. my $page=pagename($file);
  143. will_render($page, htmlpage($page), 1);
  144. if ($config{discussion}) {
  145. # Discussion links are a special case since they're
  146. # not in the text of the page, but on its template.
  147. $links{$page}=[ $page."/".lc($config{discussionpage}) ];
  148. }
  149. else {
  150. $links{$page}=[];
  151. }
  152. delete $typedlinks{$page};
  153. run_hooks(scan => sub {
  154. shift->(
  155. page => $page,
  156. content => $content,
  157. );
  158. });
  159. # Preprocess in scan-only mode.
  160. preprocess($page, $page, $content, 1);
  161. }
  162. else {
  163. will_render($file, $file, 1);
  164. }
  165. }
  166. sub fast_file_copy (@) {
  167. my $srcfile=shift;
  168. my $destfile=shift;
  169. my $srcfd=shift;
  170. my $destfd=shift;
  171. my $cleanup=shift;
  172. my $blksize = 16384;
  173. my ($len, $buf, $written);
  174. while ($len = sysread $srcfd, $buf, $blksize) {
  175. if (! defined $len) {
  176. next if $! =~ /^Interrupted/;
  177. error("failed to read $srcfile: $!", $cleanup);
  178. }
  179. my $offset = 0;
  180. while ($len) {
  181. defined($written = syswrite $destfd, $buf, $len, $offset)
  182. or error("failed to write $destfile: $!", $cleanup);
  183. $len -= $written;
  184. $offset += $written;
  185. }
  186. }
  187. }
  188. sub render ($$) {
  189. my $file=shift;
  190. return if $rendered{$file};
  191. debug(shift);
  192. $rendered{$file}=1;
  193. my $type=pagetype($file);
  194. my $srcfile=srcfile($file);
  195. if (defined $type) {
  196. my $page=pagename($file);
  197. delete $depends{$page};
  198. delete $depends_simple{$page};
  199. will_render($page, htmlpage($page), 1);
  200. return if $type=~/^_/;
  201. my $content=htmlize($page, $page, $type,
  202. linkify($page, $page,
  203. preprocess($page, $page,
  204. filter($page, $page,
  205. readfile($srcfile)))));
  206. my $output=htmlpage($page);
  207. writefile($output, $config{destdir}, genpage($page, $content));
  208. }
  209. else {
  210. delete $depends{$file};
  211. delete $depends_simple{$file};
  212. will_render($file, $file, 1);
  213. if ($config{hardlink}) {
  214. # only hardlink if owned by same user
  215. my @stat=stat($srcfile);
  216. if ($stat[4] == $>) {
  217. prep_writefile($file, $config{destdir});
  218. unlink($config{destdir}."/".$file);
  219. if (link($srcfile, $config{destdir}."/".$file)) {
  220. return;
  221. }
  222. }
  223. # if hardlink fails, fall back to copying
  224. }
  225. my $srcfd=readfile($srcfile, 1, 1);
  226. writefile($file, $config{destdir}, undef, 1, sub {
  227. fast_file_copy($srcfile, $file, $srcfd, @_);
  228. });
  229. }
  230. }
  231. sub prune ($) {
  232. my $file=shift;
  233. unlink($file);
  234. my $dir=dirname($file);
  235. while (rmdir($dir)) {
  236. $dir=dirname($dir);
  237. }
  238. }
  239. sub srcdir_check () {
  240. # security check, avoid following symlinks in the srcdir path by default
  241. my $test=$config{srcdir};
  242. while (length $test) {
  243. if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
  244. error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
  245. }
  246. unless ($test=~s/\/+$//) {
  247. $test=dirname($test);
  248. }
  249. }
  250. }
  251. sub find_src_files () {
  252. my @files;
  253. my %pages;
  254. eval q{use File::Find};
  255. error($@) if $@;
  256. my ($page, $dir, $underlay);
  257. my $helper=sub {
  258. my $file=decode_utf8($_);
  259. return if -l $file || -d _;
  260. $file=~s/^\Q$dir\E\/?//;
  261. return if ! length $file;
  262. $page = pagename($file);
  263. if (! exists $pagesources{$page} &&
  264. file_pruned($file)) {
  265. $File::Find::prune=1;
  266. return;
  267. }
  268. my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
  269. if (! defined $f) {
  270. warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
  271. }
  272. if ($underlay) {
  273. # avoid underlaydir override attacks; see security.mdwn
  274. if (! -l "$config{srcdir}/$f" && ! -e _) {
  275. if (! $pages{$page}) {
  276. push @files, $f;
  277. $pages{$page}=1;
  278. }
  279. }
  280. }
  281. else {
  282. push @files, $f;
  283. if ($pages{$page}) {
  284. debug(sprintf(gettext("%s has multiple possible source pages"), $page));
  285. }
  286. $pages{$page}=1;
  287. }
  288. };
  289. find({
  290. no_chdir => 1,
  291. wanted => $helper,
  292. }, $dir=$config{srcdir});
  293. $underlay=1;
  294. foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
  295. find({
  296. no_chdir => 1,
  297. wanted => $helper,
  298. }, $dir=$_);
  299. };
  300. return \@files, \%pages;
  301. }
  302. sub find_new_files ($) {
  303. my $files=shift;
  304. my @new;
  305. my @internal_new;
  306. my $times_noted;
  307. foreach my $file (@$files) {
  308. my $page=pagename($file);
  309. if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
  310. # the page has changed its type
  311. $forcerebuild{$page}=1;
  312. }
  313. $pagesources{$page}=$file;
  314. if (! $pagemtime{$page}) {
  315. if (isinternal($page)) {
  316. push @internal_new, $file;
  317. }
  318. elsif ($config{rcs}) {
  319. push @new, $file;
  320. if ($config{gettime} && -e "$config{srcdir}/$file") {
  321. if (! $times_noted) {
  322. debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
  323. $times_noted=1;
  324. }
  325. eval {
  326. my $ctime=rcs_getctime("$config{srcdir}/$file");
  327. if ($ctime > 0) {
  328. $pagectime{$page}=$ctime;
  329. }
  330. };
  331. if ($@) {
  332. print STDERR $@;
  333. }
  334. my $mtime;
  335. eval {
  336. $mtime=rcs_getmtime("$config{srcdir}/$file");
  337. };
  338. if ($@) {
  339. print STDERR $@;
  340. }
  341. elsif ($mtime > 0) {
  342. utime($mtime, $mtime, "$config{srcdir}/$file");
  343. }
  344. }
  345. }
  346. $pagecase{lc $page}=$page;
  347. if (! exists $pagectime{$page}) {
  348. $pagectime{$page}=(srcfile_stat($file))[10];
  349. }
  350. }
  351. }
  352. return \@new, \@internal_new;
  353. }
  354. sub find_del_files ($) {
  355. my $pages=shift;
  356. my @del;
  357. my @internal_del;
  358. foreach my $page (keys %pagemtime) {
  359. if (! $pages->{$page}) {
  360. if (isinternal($page)) {
  361. push @internal_del, $pagesources{$page};
  362. }
  363. else {
  364. push @del, $pagesources{$page};
  365. }
  366. $links{$page}=[];
  367. delete $typedlinks{$page};
  368. $renderedfiles{$page}=[];
  369. $pagemtime{$page}=0;
  370. }
  371. }
  372. return \@del, \@internal_del;
  373. }
  374. sub remove_del (@) {
  375. foreach my $file (@_) {
  376. my $page=pagename($file);
  377. if (! isinternal($page)) {
  378. debug(sprintf(gettext("removing obsolete %s"), $page));
  379. }
  380. foreach my $old (@{$oldrenderedfiles{$page}}) {
  381. prune($config{destdir}."/".$old);
  382. }
  383. foreach my $source (keys %destsources) {
  384. if ($destsources{$source} eq $page) {
  385. delete $destsources{$source};
  386. }
  387. }
  388. delete $pagecase{lc $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 $f (@$files) {
  516. next if $rendered{$f};
  517. my $p=pagename($f);
  518. my $reason = undef;
  519. if (exists $depends_simple{$p}) {
  520. foreach my $d (keys %{$depends_simple{$p}}) {
  521. if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
  522. $lc_changed{$d})
  523. ||
  524. ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
  525. $lc_exists_changed{$d})
  526. ||
  527. ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
  528. $linkchangers->{$d})
  529. ) {
  530. $reason = $d;
  531. last;
  532. }
  533. }
  534. }
  535. if (exists $depends{$p} && ! defined $reason) {
  536. foreach my $dep (keys %{$depends{$p}}) {
  537. my $sub=pagespec_translate($dep);
  538. next unless defined $sub;
  539. # only consider internal files
  540. # if the page explicitly depends
  541. # on such files
  542. my $internal_dep=$dep =~ /internal\(/;
  543. my $in=sub {
  544. my $list=shift;
  545. my $type=shift;
  546. foreach my $file (@$list) {
  547. next if $file eq $f;
  548. my $page=pagename($file);
  549. if ($sub->($page, location => $p)) {
  550. if ($type == $IkiWiki::DEPEND_LINKS) {
  551. next unless $linkchangers->{lc($page)};
  552. }
  553. $reason=$page;
  554. return 1;
  555. }
  556. }
  557. return undef;
  558. };
  559. if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
  560. last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
  561. last if $internal_dep && (
  562. $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
  563. $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
  564. $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
  565. );
  566. }
  567. if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
  568. last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
  569. last if $internal_dep && (
  570. $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
  571. $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
  572. );
  573. }
  574. if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
  575. last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
  576. last if $internal_dep && (
  577. $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
  578. $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
  579. $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
  580. );
  581. }
  582. }
  583. }
  584. if (defined $reason) {
  585. render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
  586. return 1;
  587. }
  588. }
  589. return 0;
  590. }
  591. sub render_backlinks ($) {
  592. my $backlinkchanged=shift;
  593. foreach my $link (keys %$backlinkchanged) {
  594. my $linkfile=$pagesources{$link};
  595. if (defined $linkfile) {
  596. render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
  597. }
  598. }
  599. }
  600. sub gen_autofile ($$$) {
  601. my $autofile=shift;
  602. my $pages=shift;
  603. my $del=shift;
  604. if (file_pruned($autofile)) {
  605. return;
  606. }
  607. my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
  608. if (! defined $file) {
  609. return;
  610. }
  611. # Remember autofiles that were tried, and never try them again later.
  612. if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
  613. return;
  614. }
  615. $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
  616. if (srcfile($autofile, 1) || file_pruned($autofile)) {
  617. return;
  618. }
  619. if (-l $file || -d _ || -e _) {
  620. return;
  621. }
  622. my $page = pagename($file);
  623. if ($pages->{$page}) {
  624. return;
  625. }
  626. if (grep { $_ eq $autofile } @$del) {
  627. return;
  628. }
  629. $autofiles{$autofile}{generator}->();
  630. $pages->{$page}=1;
  631. return 1;
  632. }
  633. sub refresh () {
  634. srcdir_check();
  635. run_hooks(refresh => sub { shift->() });
  636. my ($files, $pages)=find_src_files();
  637. my ($new, $internal_new)=find_new_files($files);
  638. my ($del, $internal_del)=find_del_files($pages);
  639. my ($changed, $internal_changed)=find_changed($files);
  640. run_hooks(needsbuild => sub { shift->($changed) });
  641. my $oldlink_targets=calculate_old_links($changed, $del);
  642. foreach my $file (@$changed) {
  643. scan($file);
  644. }
  645. foreach my $autofile (keys %autofiles) {
  646. if (gen_autofile($autofile, $pages, $del)) {
  647. push @{$files}, $autofile;
  648. push @{$new}, $autofile if find_new_files([$autofile]);
  649. push @{$changed}, $autofile if find_changed([$autofile]);
  650. scan($autofile);
  651. }
  652. }
  653. calculate_links();
  654. remove_del(@$del, @$internal_del);
  655. foreach my $file (@$changed) {
  656. render($file, sprintf(gettext("building %s"), $file));
  657. }
  658. foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
  659. derender_internal($file);
  660. }
  661. my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
  662. $del, $oldlink_targets);
  663. foreach my $file (@$new, @$del) {
  664. render_linkers($file);
  665. }
  666. if ($rendered{"templates/page.tmpl"}) {
  667. foreach my $f (@$files) {
  668. next if $f eq "templates/page.tmpl";
  669. render($f, sprintf(gettext("building %s, which depends on %s"), $f, "templates/page.tmpl"));
  670. }
  671. }
  672. elsif (@$changed || @$internal_changed ||
  673. @$del || @$internal_del || @$internal_new) {
  674. 1 while render_dependent($files, $new, $internal_new,
  675. $del, $internal_del, $internal_changed,
  676. $linkchangers);
  677. }
  678. render_backlinks($backlinkchanged);
  679. remove_unrendered();
  680. if (@$del) {
  681. run_hooks(delete => sub { shift->(@$del) });
  682. }
  683. if (%rendered) {
  684. run_hooks(change => sub { shift->(keys %rendered) });
  685. }
  686. }
  687. sub clean_rendered {
  688. lockwiki();
  689. loadindex();
  690. remove_unrendered();
  691. foreach my $page (keys %oldrenderedfiles) {
  692. foreach my $file (@{$oldrenderedfiles{$page}}) {
  693. prune($config{destdir}."/".$file);
  694. }
  695. }
  696. }
  697. sub commandline_render () {
  698. lockwiki();
  699. loadindex();
  700. unlockwiki();
  701. my $srcfile=possibly_foolish_untaint($config{render});
  702. my $file=$srcfile;
  703. $file=~s/\Q$config{srcdir}\E\/?//;
  704. my $type=pagetype($file);
  705. die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
  706. my $content=readfile($srcfile);
  707. my $page=pagename($file);
  708. $pagesources{$page}=$file;
  709. $content=filter($page, $page, $content);
  710. $content=preprocess($page, $page, $content);
  711. $content=linkify($page, $page, $content);
  712. $content=htmlize($page, $page, $type, $content);
  713. $pagemtime{$page}=(stat($srcfile))[9];
  714. $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
  715. print genpage($page, $content);
  716. exit 0;
  717. }
  718. 1