-
Notifications
You must be signed in to change notification settings - Fork 5
/
ymlfront.pm
414 lines (370 loc) · 10 KB
/
ymlfront.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/perl
package IkiWiki::Plugin::ymlfront;
use warnings;
use strict;
use Encode qw{encode};
=head1 NAME
IkiWiki::Plugin::ymlfront - add YAML-format data to a page
=head1 VERSION
This describes version B<1.20120105> of IkiWiki::Plugin::ymlfront
=cut
our $VERSION = '1.20120105';
=head1 DESCRIPTION
This allows field-data to be defined in YAML format on a page.
This is a back-end for the "field" plugin.
See doc/plugins/contrib/ymlfront and ikiwiki/directive/ymlfront for docs.
=head1 PREREQUISITES
IkiWiki
IkiWiki::Plugin::field
YAML::Any
=head1 AUTHOR
Kathryn Andersen (RUBYKAT)
http://github.com/rubykat
=head1 COPYRIGHT
Copyright (c) 2009-2011 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 => "ymlfront", call => \&getsetup);
hook(type => "checkconfig", id => "ymlfront", call => \&checkconfig);
hook(type => "needsbuild", id => "ymlfront", call => \&needsbuild);
hook(type => "filter", id => "ymlfront", call => \&filter, first=>1);
hook(type => "preprocess", id => "ymlfront", call => \&preprocess, scan=>1);
hook(type => "scan", id => "ymlfront", call => \&scan);
hook(type => "checkcontent", id => "ymlfront", call => \&checkcontent);
IkiWiki::loadplugin('field');
IkiWiki::Plugin::field::field_register(id=>'ymlfront',
get_value=>\&yml_get_value,
first=>1);
}
# ------------------------------------------------------------
# Hooks
# --------------------------------
sub getsetup () {
return
plugin => {
safe => 1,
rebuild => 1,
},
ymlfront_delim => {
type => "array",
example => "ymlfront_delim => [qw(--YAML-START-- --YAML-END--)]",
description => "delimiters of YAML data",
safe => 0,
rebuild => undef,
},
ymlfront_set_content => {
type => "boolean",
example => "ymlfront_set_content => 1",
description => "allow ymlfront to define the content of the page inside the YAML",
safe => 0,
rebuild => undef,
},
}
sub checkconfig () {
eval {require YAML::Any; import YAML::Any;};
eval {require YAML; import YAML;} if $@;
if ($@)
{
return error ("ymlfront: failed to use YAML::Any or YAML");
}
$YAML::UseBlock = 1;
$YAML::Syck::ImplicitUnicode = 1;
if (!defined $config{ymlfront_delim})
{
$config{ymlfront_delim} = [qw(--- ---)];
}
if (!defined $config{ymlfront_set_content})
{
$config{ymlfront_set_content} = 0;
}
} # checkconfig
# Without this, pages get refreshed with one-refresh-old values of their
# metadata, found in %pagestate. Or perhaps that only happens for a certain
# order of scanning pages - but that's not defined or predictable. Also
# without this, a deleted metadata field never disappears until a rebuild.
sub needsbuild() {
my $chgd = shift;
my $deld = shift;
for my $fns ( $chgd, $deld ) {
for my $fn ( @$fns ) {
my $type = pagetype($fn);
next unless defined $type;
my $page = pagename($fn);
undef %{$pagestate{$page}{ymlfront}};
}
}
return;
}
sub scan (@) {
my %params=@_;
my $page = $params{page};
my $page_file=$pagesources{$page} || return;
my $page_type=pagetype($page_file);
if (!defined $page_type)
{
return;
}
my $extracted_yml = extract_yml(%params);
if (defined $extracted_yml
and defined $extracted_yml->{yml})
{
my $parsed_yml = parse_yml(%params, data=>$extracted_yml->{yml});
if (defined $parsed_yml)
{
# save the data to pagestate
foreach my $fn (keys %{$parsed_yml})
{
my $fval = $parsed_yml->{$fn};
$pagestate{$page}{ymlfront}{$fn} = $fval;
}
# update meta values
foreach my $key (qw{title description author})
{
if (exists $pagestate{$page}{ymlfront}{$key}
and $pagestate{$page}{ymlfront}{$key})
{
my $val = (ref $pagestate{$page}{ymlfront}{$key}
? join(' ', @{$pagestate{$page}{ymlfront}{$key}})
: $pagestate{$page}{ymlfront}{$key});
if ($key eq 'title' and exists $pagestate{$page}{ymlfront}{titlesort})
{
IkiWiki::Plugin::meta::preprocess(
$key=>$val,
sortas=>$pagestate{$page}{ymlfront}{titlesort},
page=>$page);
}
elsif ($key eq 'author' and exists $pagestate{$page}{ymlfront}{authorsort})
{
IkiWiki::Plugin::meta::preprocess(
$key=>$val,
sortas=>$pagestate{$page}{ymlfront}{authorsort},
page=>$page);
}
else
{
IkiWiki::Plugin::meta::preprocess(
$key=>$val,
page=>$page);
}
}
}
} # defined parsed_yml
}
} # scan
# use this for data in a [[!ymlfront ...]] directive
sub preprocess (@) {
my %params=@_;
my $page = $params{page};
if (! exists $params{data}
or ! defined $params{data}
or !$params{data})
{
error gettext("missing data parameter")
}
# All the work of this is done in scan mode;
# when in preprocessing mode, just return an empty string.
my $scan=! defined wantarray;
if (!$scan)
{
return '';
}
my $parsed_yml = parse_yml(%params);
if (defined $parsed_yml)
{
# save the data to pagestate
foreach my $fn (keys %{$parsed_yml})
{
my $fval = $parsed_yml->{$fn};
$pagestate{$page}{ymlfront}{$fn} = $fval;
}
# update meta hash
if (exists $pagestate{$page}{ymlfront}{title}
and $pagestate{$page}{ymlfront}{title})
{
$pagestate{$page}{meta}{title} = $pagestate{$page}{ymlfront}{title};
}
if (exists $pagestate{$page}{ymlfront}{description}
and $pagestate{$page}{ymlfront}{description})
{
$pagestate{$page}{meta}{description} = $pagestate{$page}{ymlfront}{description};
}
if (exists $pagestate{$page}{ymlfront}{author}
and $pagestate{$page}{ymlfront}{author})
{
$pagestate{$page}{meta}{author} = $pagestate{$page}{ymlfront}{author};
}
}
else
{
error gettext("ymlfront: data not legal YAML")
}
return '';
} # preprocess
sub filter (@) {
my %params=@_;
my $page = $params{page};
my $page_file=$pagesources{$page} || return $params{content};
my $page_type=pagetype($page_file);
if (!defined $page_type)
{
return $params{content};
}
my $extracted_yml = extract_yml(%params);
if (defined $extracted_yml
and defined $extracted_yml->{yml}
and defined $extracted_yml->{content})
{
$params{content} = $extracted_yml->{content};
# check for a content value
if ($config{ymlfront_set_content}
and exists $pagestate{$page}{ymlfront}{content}
and defined $pagestate{$page}{ymlfront}{content}
and $pagestate{$page}{ymlfront}{content})
{
$params{content} .= $pagestate{$page}{ymlfront}{content};
}
}
return $params{content};
} # filter
# check the correctness of the YAML code before saving a page
sub checkcontent {
my %params=@_;
my $page = $params{page};
my $page_file=$pagesources{$page};
if ($page_file)
{
my $page_type=pagetype($page_file);
if (!defined $page_type)
{
return undef;
}
}
my $extracted_yml = extract_yml(%params);
if (defined $extracted_yml
and !defined $extracted_yml->{yml})
{
debug("ymlfront: Save of $page failed: $@");
return gettext("YAML data incorrect: $@");
}
return undef;
} # checkcontent
# ------------------------------------------------------------
# Field functions
# --------------------------------
sub yml_get_value ($$) {
my $field_name = shift;
my $page = shift;
my $value = undef;
$field_name =~ tr/A-Z/a-z/;
if (exists $pagestate{$page}{ymlfront}{$field_name})
{
$value = $pagestate{$page}{ymlfront}{$field_name};
}
return $value;
} # yml_get_value
# ------------------------------------------------------------
# Helper functions
# --------------------------------
# extract the YAML data from the given content
# Expects page, content
# Returns { yml=>$yml_str, content=>$content } or undef
# if undef is returned, there is no YAML
# but if $yml_str is undef then there was YAML but it was not legal
sub extract_yml {
my %params=@_;
my $page = $params{page};
my $content = $params{content};
my $page_file=$pagesources{$page};
if ($page_file)
{
my $page_type=pagetype($page_file);
if (!defined $page_type)
{
return undef;
}
}
my $start_of_content = '';
my $yml_str = '';
my $rest_of_content = '';
if ($content)
{
my $ystart = $config{ymlfront_delim}[0];
my $yend = $config{ymlfront_delim}[1];
if ($ystart eq '---' and $yend eq '---')
{
if ($content =~ /^---[\n\r](.*?[\n\r])---[\n\r](.*)$/s)
{
$yml_str = $1;
$rest_of_content = $2;
}
}
elsif ($content =~ /^(.*?)${ystart}[\n\r](.*?[\n\r])${yend}([\n\r].*)$/s)
{
$yml_str = $2;
$rest_of_content = $1 . $3;
}
}
if ($yml_str) # possible YAML
{
# if {{$page}} is there, do an immediate substitution
$yml_str =~ s/\{\{\$page\}\}/$page/sg;
my $ydata;
eval {$ydata = Load(encode('UTF-8', $yml_str));};
if ($@)
{
debug("ymlfront: Load of $page data failed: $@");
return { yml=>undef, content=>$content };
}
if (!$ydata)
{
debug("ymlfront: no legal YAML for $page");
return { yml=>undef, content=>$content };
}
return { yml=>$yml_str,
content=>$start_of_content . $rest_of_content};
}
return undef;
} # extract_yml
# parse the YAML data from the given string
# Expects page, data
# Returns \%yml_data or undef
sub parse_yml {
my %params=@_;
my $page = $params{page};
my $yml_str = $params{data};
if ($yml_str)
{
# if {{$page}} is there, do an immediate substitution
$yml_str =~ s/\{\{\$page\}\}/$page/sg;
my $ydata;
eval {$ydata = Load(encode('UTF-8', $yml_str));};
if ($@)
{
debug("ymlfront parse: Load of $page data failed: $@");
return undef;
}
if (!$ydata)
{
debug("ymlfront parse: no legal YAML for $page");
return undef;
}
if ($ydata)
{
my %lc_data = ();
# make lower-cased versions of the data
foreach my $fn (keys %{$ydata})
{
my $fval = $ydata->{$fn};
my $lc_fn = $fn;
$lc_fn =~ tr/A-Z/a-z/;
$lc_data{$lc_fn} = $fval;
}
return \%lc_data;
}
}
return undef;
} # parse_yml
1;