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