<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;

sub import { #{{{
	IkiWiki::hook(type =&gt; "preprocess", id =&gt; "inline", 
		call =&gt; \&amp;IkiWiki::preprocess_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.
	IkiWiki::hook(type =&gt; "change", id =&gt; "inline", 
		call =&gt; \&amp;IkiWiki::pingurl);
} # }}}

# 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 $processing_inline=0;

sub preprocess_inline (@) { #{{{
	my %params=@_;

	if (! exists $params{pages}) {
		return "";
	}
	if (! exists $params{archive}) {
		$params{archive}="no";
	}
	if (! exists $params{show} &amp;&amp; $params{archive} eq "no") {
		$params{show}=10;
	}

	# Avoid nested inlines, to avoid loops etc.
	if ($processing_inline) {
		return "";
	}
	$processing_inline=1;

	my @list;
	foreach my $page (keys %pagesources) {
		next if $page eq $params{page};
		if (globlist_match($page, $params{pages})) {
			push @list, $page;
		}
	}
	@list=sort { $pagectime{$b} &lt;=&gt; $pagectime{$a} } @list;
	if ($params{show} &amp;&amp; @list &gt; $params{show}) {
		@list=@list[0..$params{show} - 1];
	}

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

	my $ret="";
	
	if (exists $params{rootpage} &amp;&amp; $config{cgiurl}) {
		# Add a blog post form, with a rss link button.
		my $formtemplate=template("blogpost.tmpl", blind_cache =&gt; 1);
		$formtemplate-&gt;param(cgiurl =&gt; $config{cgiurl});
		$formtemplate-&gt;param(rootpage =&gt; $params{rootpage});
		if ($config{rss}) {
			$formtemplate-&gt;param(rssurl =&gt; rsspage(basename($params{page})));
		}
		$ret.=$formtemplate-&gt;output;
	}
	elsif ($config{rss}) {
		# Add a rss link button.
		my $linktemplate=template("rsslink.tmpl", blind_cache =&gt; 1);
		$linktemplate-&gt;param(rssurl =&gt; rsspage(basename($params{page})));
		$ret.=$linktemplate-&gt;output;
	}
	
	my $template=template(
		(($params{archive} eq "no")
			? "inlinepage.tmpl"
			: "inlinepagetitle.tmpl"),
		blind_cache =&gt; 1,
	);
	
	foreach my $page (@list) {
		# Don't use htmllink because this way the title is separate
		# and can be overridden by other plugins.
		my $link=htmlpage(bestlink($params{page}, $page));
		$link=abs2rel($link, dirname($params{page}));
		$template-&gt;param(pageurl =&gt; $link);
		$template-&gt;param(title =&gt; pagetitle(basename($page)));
		$template-&gt;param(content =&gt; get_inline_content($page, $params{page}))
			if $params{archive} eq "no";
		$template-&gt;param(ctime =&gt; displaytime($pagectime{$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;
	}
	
	# TODO: should really add this to renderedfiles and call
	# check_overwrite, but currently renderedfiles
	# only supports listing one file per page.
	if ($config{rss}) {
		writefile(rsspage($params{page}), $config{destdir},
			genrss($params{page}, @list));
		$toping{$params{page}}=1 unless $config{rebuild};
	}
	
	$processing_inline=0;

	return $ret;
} #}}}

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

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

	eval q{use POSIX};
	return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
} #}}}

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

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

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

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

sub genrss ($@) { #{{{
	my $page=shift;
	my @pages=@_;
	
	my $url="$config{url}/".htmlpage($page);
	
	my $itemtemplate=template("rssitem.tmpl", blind_cache =&gt; 1, 
		die_on_bad_params =&gt; 0);
	my $content="";
	foreach my $p (@pages) {
		next unless exists $renderedfiles{$p};

		$itemtemplate-&gt;param(
			title =&gt; pagetitle(basename($p)),
			url =&gt; "$config{url}/$renderedfiles{$p}",
			pubdate =&gt; date_822($pagectime{$p}),
			content =&gt; absolute_urls(get_inline_content($p, $page), $url),
		);
		run_hooks(pagetemplate =&gt; sub {
			shift-&gt;(page =&gt; $p, destpage =&gt; $page,
				template =&gt; $itemtemplate);
		});
		$content.=$itemtemplate-&gt;output;
		$itemtemplate-&gt;clear_params;
	}

	my $template=template("rsspage.tmpl", blind_cache =&gt; 1);
	$template-&gt;param(
		title =&gt; $config{wikiname},
		wikiname =&gt; $config{wikiname},
		pageurl =&gt; $url,
		content =&gt; $content,
	);
	
	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("RPC::XML::Client not found, not pinging");
		return;
	}

	foreach my $page (keys %toping) {
		my $title=pagetitle(basename($page));
		my $url="$config{url}/".htmlpage($page);
		foreach my $pingurl (@{$config{pingurl}}) {
			my $client = RPC::XML::Client-&gt;new($pingurl);
			my $req = RPC::XML::request-&gt;new('weblogUpdates.ping',
				$title, $url);
			debug("Pinging $pingurl for $page");
			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: ".$r-&gt;{message});
			}
		}
	}
} #}}}

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