-
Notifications
You must be signed in to change notification settings - Fork 5
/
xslt.pm
100 lines (79 loc) · 2.17 KB
/
xslt.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/perl
# Ikiwiki xslt plugin.
# See plugins/contrib/xslt and ikiwiki/directive/xslt for documentation.
package IkiWiki::Plugin::xslt;
use warnings;
use strict;
=head1 NAME
IkiWiki::Plugin::xslt - ikiwiki directive to process an XML file with XSLT
=head1 VERSION
This describes version B<1.20100519> of IkiWiki::Plugin::xslt
=cut
our $VERSION = '1.20100519';
=head1 PREREQUISITES
IkiWiki
XML::LibXML
XML::LibXSLT
=head1 AUTHOR
Kathryn Andersen (RUBYKAT)
http://github.com/rubykat
=head1 COPYRIGHT
Copyright (c) 2009 Kathryn Andersen
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
use IkiWiki 3.00;
use XML::LibXSLT;
use XML::LibXML;
my $XSLT_parser;
my $XSLT_xslt;
sub import {
hook(type => "getsetup", id => "xslt", call => \&getsetup);
hook(type => "preprocess", id => "xslt", call => \&preprocess);
$XSLT_parser = XML::LibXML->new();
$XSLT_xslt = XML::LibXSLT->new();
}
sub getsetup () {
return
plugin => {
safe => 1,
rebuild => undef,
},
}
sub preprocess (@) {
my %params=@_;
# check the files exist
my %near = ();
foreach my $param (qw{stylesheet file}) {
if (! exists $params{$param})
{
error sprintf(gettext('%s parameter is required'), $param);
}
if ($param eq 'stylesheet' and $params{$param} !~ /.xsl$/)
{
error sprintf(gettext('%s must have .xsl extension'), $param);
}
if ($param eq 'file' and $params{$param} !~ /.xml$/)
{
error sprintf(gettext('%s must have .xml extension'), $param);
}
$near{$param} = bestlink($params{page}, $params{$param});
if (! $near{$param})
{
error sprintf(gettext('cannot find bestlink for "%s"'),
$params{$param});
}
if (! exists $pagesources{$near{$param}})
{
error sprintf(gettext('cannot find file "%s"'), $near{$param});
}
add_depends($params{page}, $near{$param});
}
my $source = $XSLT_parser->parse_file(srcfile($near{file}));
my $style_doc = $XSLT_parser->parse_file(srcfile($near{stylesheet}));
my $stylesheet = $XSLT_xslt->parse_stylesheet($style_doc);
my $results = $stylesheet->transform($source);
return $stylesheet->output_string($results);
}
1;
__END__