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