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