<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
# Page inlining and blogging.
package IkiWiki::Plugin::inline;

use warnings;
use strict;
use IkiWiki 1.00;
use URI;

sub import { #{{{
	hook(type =&gt; "getopt", id =&gt; "inline", call =&gt; \&amp;getopt);
	hook(type =&gt; "checkconfig", id =&gt; "inline", call =&gt; \&amp;checkconfig);
	hook(type =&gt; "preprocess", id =&gt; "inline", 
		call =&gt; \&amp;IkiWiki::preprocess_inline);
	hook(type =&gt; "pagetemplate", id =&gt; "inline",
		call =&gt; \&amp;IkiWiki::pagetemplate_inline);
	# Hook to change to do pinging since it's called late.
	# This ensures each page only pings once and prevents slow
	# pings interrupting page builds.
	hook(type =&gt; "change", id =&gt; "inline", 
		call =&gt; \&amp;IkiWiki::pingurl);
} # }}}

sub getopt () { #{{{
	eval q{use Getopt::Long};
	error($@) if $@;
	Getopt::Long::Configure('pass_through');
	GetOptions(
		"rss!" =&gt; \$config{rss},
		"atom!" =&gt; \$config{atom},
	);
}

sub checkconfig () { #{{{
	if (($config{rss} || $config{atom}) &amp;&amp; ! length $config{url}) {
		error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
	}
	if ($config{rss}) {
		push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
	}
	if ($config{atom}) {
		push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
	}
} #}}}

# Back to ikiwiki namespace for the rest, this code is very much
# internal to ikiwiki even though it's separated into a plugin.
package IkiWiki;

my %toping;
my %feedlinks;

sub yesno ($) { #{{{
	my $val=shift;
	return (defined $val &amp;&amp; lc($val) eq "yes");
} #}}}

sub preprocess_inline (@) { #{{{
	my %params=@_;
	
	if (! exists $params{pages}) {
		return "";
	}
	my $raw=yesno($params{raw});
	my $archive=yesno($params{archive});
	my $rss=($config{rss} &amp;&amp; exists $params{rss}) ? yesno($params{rss}) : $config{rss};
	my $atom=($config{atom} &amp;&amp; exists $params{atom}) ? yesno($params{atom}) : $config{atom};
	my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
	my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
	$feeds=0 if $params{preview};
	if (! exists $params{show} &amp;&amp; ! $archive) {
		$params{show}=10;
	}
	my $desc;
	if (exists $params{description}) {
		$desc = $params{description} 
	} else {
		$desc = $config{wikiname};
	}
	my $actions=yesno($params{actions});
	if (exists $params{template}) {
		$params{template}=~s/[^-_a-zA-Z0-9]+//g;
	}
	else {
		$params{template} = $archive ? "archivepage" : "inlinepage";
	}

	my @list;
	foreach my $page (keys %pagesources) {
		next if $page eq $params{page};
		if (pagespec_match($page, $params{pages}, $params{page})) {
			push @list, $page;
		}
	}

	if (exists $params{sort} &amp;&amp; $params{sort} eq 'title') {
		@list=sort @list;
	}
	elsif (! exists $params{sort} || $params{sort} eq 'age') {
		@list=sort { $pagectime{$b} &lt;=&gt; $pagectime{$a} } @list;
	}
	else {
		return sprintf(gettext("unknown sort type %s"), $params{sort});
	}

	if (yesno($params{reverse})) {
		@list=reverse(@list);
	}

	if (exists $params{skip}) {
		@list=@list[$params{skip} .. scalar @list - 1];
	}
	
	if ($params{show} &amp;&amp; @list &gt; $params{show}) {
		@list=@list[0..$params{show} - 1];
	}

	add_depends($params{page}, $params{pages});

	my $rssurl=rsspage(basename($params{page}));
	my $atomurl=atompage(basename($params{page}));
	my $ret="";

	if (exists $params{rootpage} &amp;&amp; $config{cgiurl}) {
		# Add a blog post form, with feed buttons.
		my $formtemplate=template("blogpost.tmpl", blind_cache =&gt; 1);
		$formtemplate-&gt;param(cgiurl =&gt; $config{cgiurl});
		$formtemplate-&gt;param(rootpage =&gt; $params{rootpage});
		$formtemplate-&gt;param(rssurl =&gt; $rssurl) if $feeds &amp;&amp; $rss;
		$formtemplate-&gt;param(atomurl =&gt; $atomurl) if $feeds &amp;&amp; $atom;
		$ret.=$formtemplate-&gt;output;
	}
	elsif ($feeds) {
		# Add feed buttons.
		my $linktemplate=template("feedlink.tmpl", blind_cache =&gt; 1);
		$linktemplate-&gt;param(rssurl =&gt; $rssurl) if $rss;
		$linktemplate-&gt;param(atomurl =&gt; $atomurl) if $atom;
		$ret.=$linktemplate-&gt;output;
	}
	
	my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache =&gt; 1);
	if (! @params) {
		return sprintf(gettext("nonexistant template %s"), $params{template});
	}
	my $template=HTML::Template-&gt;new(@params) unless $raw;
	
	foreach my $page (@list) {
		my $file = $pagesources{$page};
		my $type = pagetype($file);
		if (! $raw || ($raw &amp;&amp; ! defined $type)) {
			unless ($archive &amp;&amp; $quick) {
				# Get the content before populating the
				# template, since getting the content uses
				# the same template if inlines are nested.
				my $content=get_inline_content($page, $params{destpage});
				$template-&gt;param(content =&gt; $content);
			}
			# Don't use htmllink because this way the
			# title is separate and can be overridden by
			# other plugins.
			my $link=bestlink($params{page}, $page);
			$link=htmlpage($link) if defined $type;
			$link=abs2rel($link, dirname($params{destpage}));
			$template-&gt;param(pageurl =&gt; $link);
			$template-&gt;param(title =&gt; pagetitle(basename($page)));
			$template-&gt;param(ctime =&gt; displaytime($pagectime{$page}));

			if ($actions) {
				my $file = $pagesources{$page};
				my $type = pagetype($file);
				if ($config{discussion}) {
					my $discussionlink=gettext("discussion");
					if ($page !~ /.*\/\Q$discussionlink\E$/ &amp;&amp;
					    (length $config{cgiurl} ||
					     exists $links{$page."/".$discussionlink})) {
						$template-&gt;param(have_actions =&gt; 1);
						$template-&gt;param(discussionlink =&gt;
							htmllink($page,
								$params{page},
								gettext("Discussion"),
								noimageinline =&gt; 1,
								forcesubpage =&gt; 1));
					}
				}
				if (length $config{cgiurl} &amp;&amp; defined $type) {
					$template-&gt;param(have_actions =&gt; 1);
					$template-&gt;param(editurl =&gt; cgiurl(do =&gt; "edit", page =&gt; $page));
				}
			}

			run_hooks(pagetemplate =&gt; sub {
				shift-&gt;(page =&gt; $page, destpage =&gt; $params{page},
					template =&gt; $template,);
			});

			$ret.=$template-&gt;output;
			$template-&gt;clear_params;
		}
		else {
			if (defined $type) {
				$ret.="\n".
				      linkify($page, $params{page},
				      preprocess($page, $params{page},
				      filter($page,
				      readfile(srcfile($file)))));
			}
		}
	}
	
	if ($feeds) {
		if (exists $params{feedshow} &amp;&amp; @list &gt; $params{feedshow}) {
			@list=@list[0..$params{feedshow} - 1];
		}
	
		if ($rss) {
			will_render($params{page}, rsspage($params{page}));
			writefile(rsspage($params{page}), $config{destdir},
				genfeed("rss", $rssurl, $desc, $params{page}, @list));
			$toping{$params{page}}=1 unless $config{rebuild};
			$feedlinks{$params{destpage}}=qq{&lt;link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" /&gt;};
		}
		if ($atom) {
			will_render($params{page}, atompage($params{page}));
			writefile(atompage($params{page}), $config{destdir},
				genfeed("atom", $atomurl, $desc, $params{page}, @list));
			$toping{$params{page}}=1 unless $config{rebuild};
			$feedlinks{$params{destpage}}=qq{&lt;link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" /&gt;};
		}
	}
	
	return $ret;
} #}}}

sub pagetemplate_inline (@) { #{{{
	my %params=@_;
	my $page=$params{page};
	my $template=$params{template};

	$template-&gt;param(feedlinks =&gt; $feedlinks{$page})
		if exists $feedlinks{$page} &amp;&amp; $template-&gt;query(name =&gt; "feedlinks");
} #}}}

sub get_inline_content ($$) { #{{{
	my $page=shift;
	my $destpage=shift;
	
	my $file=$pagesources{$page};
	my $type=pagetype($file);
	if (defined $type) {
		return htmlize($page, $type,
		       linkify($page, $destpage,
		       preprocess($page, $destpage,
		       filter($page,
		       readfile(srcfile($file))))));
	}
	else {
		return "";
	}
} #}}}

sub date_822 ($) { #{{{
	my $time=shift;

	eval q{use POSIX};
	error($@) if $@;
	my $lc_time=POSIX::setlocale(&amp;POSIX::LC_TIME);
	POSIX::setlocale(&amp;POSIX::LC_TIME, "C");
	my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
	POSIX::setlocale(&amp;POSIX::LC_TIME, $lc_time);
	return $ret;
} #}}}

sub date_3339 ($) { #{{{
	my $time=shift;

	eval q{use POSIX};
	error($@) if $@;
	my $lc_time=POSIX::setlocale(&amp;POSIX::LC_TIME);
	POSIX::setlocale(&amp;POSIX::LC_TIME, "C");
	my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
	POSIX::setlocale(&amp;POSIX::LC_TIME, $lc_time);
	return $ret;
} #}}}

sub absolute_urls ($$) { #{{{
	# sucky sub because rss sucks
	my $content=shift;
	my $baseurl=shift;

	my $url=$baseurl;
	$url=~s/[^\/]+$//;
	
	$content=~s/(&lt;a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
	$content=~s/(&lt;a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
	$content=~s/(&lt;img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
	return $content;
} #}}}

sub rsspage ($) { #{{{
	my $page=shift;

	return $page.".rss";
} #}}}

sub atompage ($) { #{{{
	my $page=shift;

	return $page.".atom";
} #}}}

sub genfeed ($$$$@) { #{{{
	my $feedtype=shift;
	my $feedurl=shift;
	my $feeddesc=shift;
	my $page=shift;
	my @pages=@_;
	
	my $url=URI-&gt;new(encode_utf8($config{url}."/".htmlpage($page)));
	
	my $itemtemplate=template($feedtype."item.tmpl", blind_cache =&gt; 1);
	my $content="";
	my $lasttime = 0;
	foreach my $p (@pages) {
		my $u=URI-&gt;new(encode_utf8($config{url}."/".htmlpage($p)));
		
		my $pcontent = absolute_urls(get_inline_content($p, $page), $url);

		$itemtemplate-&gt;param(
			title =&gt; pagetitle(basename($p), 1),
			url =&gt; $u,
			permalink =&gt; $u,
			date_822 =&gt; date_822($pagectime{$p}),
			date_3339 =&gt; date_3339($pagectime{$p}),
		);

		if ($itemtemplate-&gt;query(name =&gt; "enclosure")) {
			my $file=$pagesources{$p};
			my $type=pagetype($file);
			if (defined $type) {
				$itemtemplate-&gt;param(content =&gt; $pcontent);
			}
			else {
				my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
				my $mime="unknown";
				eval q{use File::MimeInfo};
				if (! $@) {
					$mime = mimetype($file);
				}
				$itemtemplate-&gt;param(
					enclosure =&gt; $u,
					type =&gt; $mime,
					length =&gt; $size,
				);
			}
		}
		else {
			$itemtemplate-&gt;param(content =&gt; $pcontent);
		}

		run_hooks(pagetemplate =&gt; sub {
			shift-&gt;(page =&gt; $p, destpage =&gt; $page,
				template =&gt; $itemtemplate);
		});

		$content.=$itemtemplate-&gt;output;
		$itemtemplate-&gt;clear_params;

		$lasttime = $pagectime{$p} if $pagectime{$p} &gt; $lasttime;
	}

	my $template=template($feedtype."page.tmpl", blind_cache =&gt; 1);
	$template-&gt;param(
		title =&gt; $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
		wikiname =&gt; $config{wikiname},
		pageurl =&gt; $url,
		content =&gt; $content,
		feeddesc =&gt; $feeddesc,
		feeddate =&gt; date_3339($lasttime),
		feedurl =&gt; $feedurl,
		version =&gt; $IkiWiki::version,
	);
	run_hooks(pagetemplate =&gt; sub {
		shift-&gt;(page =&gt; $page, destpage =&gt; $page,
			template =&gt; $template);
	});
	
	return $template-&gt;output;
} #}}}

sub pingurl (@) { #{{{
	return unless @{$config{pingurl}} &amp;&amp; %toping;

	eval q{require RPC::XML::Client};
	if ($@) {
		debug(gettext("RPC::XML::Client not found, not pinging"));
		return;
	}

	# daemonize here so slow pings don't slow down wiki updates
	defined(my $pid = fork) or error("Can't fork: $!");
	return if $pid;
	chdir '/';
	eval q{use POSIX 'setsid'};
	setsid() or error("Can't start a new session: $!");
	open STDIN, '/dev/null';
	open STDOUT, '&gt;/dev/null';
	open STDERR, '&gt;&amp;STDOUT' or error("Can't dup stdout: $!");

	# Don't need to keep a lock on the wiki as a daemon.
	IkiWiki::unlockwiki();

	foreach my $page (keys %toping) {
		my $title=pagetitle(basename($page), 0);
		my $url="$config{url}/".htmlpage($page);
		foreach my $pingurl (@{$config{pingurl}}) {
			debug("Pinging $pingurl for $page");
			eval {
				my $client = RPC::XML::Client-&gt;new($pingurl);
				my $req = RPC::XML::request-&gt;new('weblogUpdates.ping',
					$title, $url);
				my $res = $client-&gt;send_request($req);
				if (! ref $res) {
					debug("Did not receive response to ping");
				}
				my $r=$res-&gt;value;
				if (! exists $r-&gt;{flerror} || $r-&gt;{flerror}) {
					debug("Ping rejected: ".(exists $r-&gt;{message} ? $r-&gt;{message} : "[unknown reason]"));
				}
			};
			if ($@) {
				debug "Ping failed: $@";
			}
		}
	}

	exit 0; # daemon done
} #}}}

1
</pre></body></html>