my $path = File::Spec->catdir('menus', $domain);
opendir my $dir, $path or die "can't open $path: $!";
- my @files = sort grep -f "$path/$_", readdir $dir;
+ my @files = sort grep -f "$path/$_", grep /\.yaml$/, readdir $dir;
close $dir;
my $nodes = [];
my $nodes_by_id = {};
for my $file (@files) {
my $data;
- if ($yaml_xs) {
- $data = YAML::XS::LoadFile(File::Spec->catfile($path, $file));
- } else {
- $data = YAML::LoadFile(File::Spec->catfile($path, $file));
- }
+ eval {
+ if ($yaml_xs) {
+ $data = YAML::XS::LoadFile(File::Spec->catfile($path, $file));
+ } else {
+ $data = YAML::LoadFile(File::Spec->catfile($path, $file));
+ }
+ 1;
+ } or do {
+ die "Error while parsing $file: $@";
+ };
+
+ # check if this file is internally consistent.
+ die 'not an array ref' unless $data && 'ARRAY' eq ref $data; # TODO get better diag to user
+
+ # in particular duplicate ids tend to come up as a user error when editing the menu files
+ #my %uniq_ids;
+ #$uniq_ids{$_->{id}}++ && die "Error in $file: duplicate id $_->{id}" for @$data;
+
_merge($nodes, $nodes_by_id, $data);
}
sub _merge {
my ($nodes, $by_id, $data) = @_;
- die 'not an array ref' unless $data && 'ARRAY' eq ref $data; # TODO check this sooner, to get better diag to user
-
for my $node (@$data) {
my $id = $node->{id};
my %by_parent;
# order them by parent
for my $node ($self->nodes) {
- push @{ $by_parent{ $node->{parent} } //= [] }, $node;
+ push @{ $by_parent{ $node->{parent} // '' } //= [] }, $node;
}
my $tree = { };
return undef if !$node->{href} && !$node->{module} && !$node->{params};
- my $href = $node->{href} || $node->{module} || 'controller.pl';
- my @tokens;
+ return $node->{href_for_node} ||= do {
+ my $href = $node->{href} || $node->{module} || 'controller.pl';
+ my @tokens;
- while (my ($key, $value) = each %{ $node->{params} }) {
- push @tokens, uri_encode($key, 1) . "=" . uri_encode($value, 1);
- }
+ while (my ($key, $value) = each %{ $node->{params} }) {
+ push @tokens, uri_encode($key, 1) . "=" . uri_encode($value, 1);
+ }
- return join '?', $href, grep $_, join '&', @tokens;
+ join '?', $href, grep $_, join '&', @tokens;
+ }
}
sub name_for_node {