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