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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#!/usr/bin/perl
# Ikiwiki getfield plugin.
# Substitute field values in the content of the page.
# See plugin/contrib/getfield for documentation.
package IkiWiki::Plugin::getfield;
use strict;
=head1 NAME
IkiWiki::Plugin::getfield - query the values of fields
=head1 VERSION
This describes version B<0.02> of IkiWiki::Plugin::getfield
=cut
our $VERSION = '0.02';
=head1 PREREQUISITES
IkiWiki
IkiWiki::Plugin::field
=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;
sub import {
hook(type => "getsetup", id => "getfield", call => \&getsetup);
hook(type => "filter", id => "getfield", call => \&do_filter, last=>1);
IkiWiki::loadplugin("field");
}
#---------------------------------------------------------------
# Hooks
# --------------------------------
sub getsetup () {
return
plugin => {
safe => 1,
rebuild => undef,
},
}
sub do_filter (@) {
my %params=@_;
my $page = $params{page};
my $destpage = ($params{destpage} ? $params{destpage} : $params{page});
my $page_file=$pagesources{$page};
my $page_type=pagetype($page_file);
if (defined $page_type)
{
while ($params{content} =~ /{{\$([-\w\/]+#)?[-\w]+}}/)
{
# substitute {{$var}} variables (source-page)
$params{content} =~ s/{{\$([-\w]+)}}/get_field_value($1,$page)/eg;
# substitute {{$page#var}} variables (source-page)
$params{content} =~ s/{{\$([-\w\/]+)#([-\w]+)}}/get_other_page_field_value($2,$page,$1)/eg;
}
}
$page_file=$pagesources{$destpage};
$page_type=pagetype($page_file);
if (defined $page_type)
{
while ($params{content} =~ /{{\+\$([-\w\/]+#)?[-\w]+\+}}/)
{
# substitute {{+$var+}} variables (dest-page)
$params{content} =~ s/{{\+\$([-\w]+)\+}}/get_field_value($1,$destpage)/eg;
# substitute {{+$page#var+}} variables (source-page)
$params{content} =~ s/{{\+\$([-\w\/]+)#([-\w]+)\+}}/get_other_page_field_value($2,$destpage,$1)/eg;
}
}
return $params{content};
} # do_filter
#---------------------------------------------------------------
# Private functions
# --------------------------------
sub get_other_page_field_value ($$$) {
my $field = shift;
my $page = shift;
my $other_page = shift;
my $use_page = bestlink($page, $other_page);
# add a dependency for the page from which we get the value
add_depends($page, $other_page);
my $val = get_field_value($field, $use_page);
if ($val eq $field)
{
return "${other_page}#$field";
}
return $val;
} # get_other_page_field_value
sub get_field_value ($$) {
my $field = shift;
my $page = shift;
my $value = IkiWiki::Plugin::field::field_get_value($field,$page);
return $value if defined $value;
# if there is no value, return the field name.
return $field;
} # get_field_value
1;
|