<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
#
# Include polygen output in a page
# 
# by Enrico Zini
package IkiWiki::Plugin::polygen;

use warnings;
use strict;
use IkiWiki 2.00;
use File::Find;

sub import { #{{{
	hook(type =&gt; "getsetup", id =&gt; "polygen", call =&gt; \&amp;getsetup);
	hook(type =&gt; "preprocess", id =&gt; "polygen", call =&gt; \&amp;preprocess);
} # }}}

sub getsetup () { #{{{
	return 
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; undef,
		},
} #}}}

sub preprocess (@) { #{{{
	my %params=@_;
	my $grammar = ($params{grammar} or 'polygen');
	my $symbol = ($params{symbol} or undef);

	# Sanitize parameters
	$grammar =~ IkiWiki::basename($grammar);
	$grammar =~ s/[^A-Za-z0-9]//g;
	$grammar =~ s/\.grm$//;
	$grammar .= '.grm';
	$symbol =~ s/[^A-Za-z0-9]//g if defined $symbol;
	$symbol = IkiWiki::possibly_foolish_untaint($symbol) if defined $symbol;

	my $grmfile = '/usr/share/polygen/ita/polygen.grm';
	if (! -d '/usr/share/polygen') {
		error gettext("polygen not installed");
	}
	find({wanted =&gt; sub {
			if (substr($File::Find::name, -length($grammar)) eq $grammar) {
				$grmfile = IkiWiki::possibly_foolish_untaint($File::Find::name);
			}
		},
		no_chdir =&gt; 1,
	}, '/usr/share/polygen');
	
	my $res;
	if (defined $symbol) {
		$res = `polygen -S $symbol $grmfile 2&gt;/dev/null`;
	}
	else {
		$res = `polygen $grmfile 2&gt;/dev/null`;
	}

	if ($?) {
		error gettext("command failed");
	}

	# Strip trailing spaces and newlines so that we flow well with the
	# markdown text
	$res =~ s/\s*$//;
	return $res;
} # }}}

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