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