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