NAME
WE_Content::Tools - tools for content objects
SYNOPSIS
use WE_Content::Tools;
$content_object->find(sub { ... });
DESCRIPTION
METHODS
- get_structure_diffs($template)
-
Return a list of differences against a template object. Only language data is compared. See Algorithm::Diff for the output format.
- upgrade($template)
-
Upgrade the content file to the current $template.
- find($callback)
-
Traverses the content object and calls
$callback
for each node in the content tree. The following arguments will be supplied to the callback:- $object
-
$object
is aa reference to the current object. A change to this reference will also manipulate the original object. - -parents => [$parent1, $parent2, ...]
-
A list of parent objects. The root object is not in the list. Descendants are appended to the right, that is, too find the parent use
[-1]
as index, the grandfather is[-2]
and Adam is[0]
. - -path => $pathstring
-
The
$pathstring
can be evaluated to access the node. Example:->{'data'}->[0]->{'type'}
- -dotted => $dotstring
-
Same as
-path
, but use a dot notation. Example:data.0.type
- -key => $key
-
Only for hash items:
$keys
is the current key. The value is in$object
.
TODO:
implement prune suggest to add something similar to Data::Walker
EXAMPLES
This example script will set the title element of the "en" language tree to the first text (usually the headline):
use WE_Content::Base;
use WE_Content::Tools;
use strict;
use File::Basename;
my $indir = shift or die;
my $outdir = "/tmp/we_data_converted";
mkdir $outdir;
for my $f (glob("$indir/content/*.bin")) {
warn "$f...\n";
my $content_object = WE_Content::Base->new(-file => $f) or die;
my $first_text;
my $title;
$content_object->find(sub {
my($o, %args) = @_;
#if ($args{-dotted} eq "data.en.ct.0.text") {
if ($args{-path} eq "->{'data'}->{'en'}->{'ct'}->[0]->{'text'}") {
$first_text = $o;
} elsif ($args{-path} eq "->{'data'}->{'en'}->{'title'}") {
$title = $o;
}
});
#if (defined $title) {
# warn "Skipping, title is already set to $title.\n";
#} els
if (defined $first_text) {
#$content_object->set_by_dotted('data.en.title', $first_text);
$content_object->{Object}{'data'}->{'en'}->{'title'} = $first_text;
} else {
warn "No first text found...\n";
}
open(OUT, ">$outdir/" . basename($f)) or die $!;
print OUT $content_object->serialize;
close OUT;
}
AUTHOR
Slaven Rezic - slaven@rezic.de