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