#!/usr/bin/perl
package IkiWiki::Plugin::hlinclude;
use warnings;
use strict;
use IkiWiki 3.00;
use IkiWiki::Plugin::highlight;
use Encode;


sub import {
	hook(type => "getsetup", id => "hlinclude", call => \&getsetup);
#   hook(type => "checkconfig", id => "hlinclude", call => \&checkconfig);
	hook(type => "preprocess", id => "hlinclude", call => \&preprocess);
	hook(type => "sanitize", id => "hlinclude", call => \&sanitize);
}

sub getsetup () {
	return
		plugin => {
			safe => 1,
			rebuild => undef,
			section => "widget",
		},
}

#my $file;

sub preprocess(@) {
	my ($file)=$_[0] =~ /$config{wiki_file_regexp}/;
	
	my $data='<a href="'.$file.'">'.$file.'</a>:';
	

	return $data."[hlinclude: ".$file."]";
}

sub sanitize(@) {
	my %params=@_;
	my $file;
	while ( $params{content} =~ m/.*\[hlinclude:\ ([^\]]+)/g ) {
		my $path = bestlink($params{page},$1);
		my $src = srcfile($path, 1);
		my $ext = ($1 =~ m/([^.]+)$/)[0];
		$ext=IkiWiki::Plugin::highlight::ext2langfile($ext);
		my $f;
		open $f,"<". $src;
		my $code=join("",<$f>);
		close $src;
		my $data=Encode::decode_utf8(IkiWiki::Plugin::highlight::highlight($ext, $code));
#		$data="<ol>".$data."</ol>";
		$params{content} =~ s/\[hlinclude:\ [^\]]+\]/$data/;

}


	return $params{content};



}

1
