| 1 | package Term::Fabulous::Layout; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | use Object::Pad 0.825; | ||
| 11 | |||
| 12 | class Term::Fabulous::Layout :strict(params) { | ||
| 13 | use Feature::Compat::Try; | ||
| 14 | use Text::KDL::XS qw(parse_kdl); | ||
| 15 | use Term::Fabulous::Check qw(describe); | ||
| 16 | |||
| 17 | my $MODULE_NAME = qr/\A[A-Za-z_]\w*(?:::\w+)*\z/a; | ||
| 18 | my $ALIAS = qr/\A[A-Z]\w*\z/a; | ||
| 19 | my $LAYOUT_ROLE = 'Term::Fabulous::Role::CanParseLayout'; | ||
| 20 | |||
| 21 | field $raw :reader; | ||
| 22 | field $root_widget :reader; | ||
| 23 | field $required_modules :reader = {}; | ||
| 24 | field $_root_node; | ||
| 25 | |||
| 26 | ADJUST :params ( :$string = undef, :$file = undef, :$allowed_namespaces = undef ) { | ||
| 27 | die "Term::Fabulous::Layout: provide either 'string' or 'file'" | ||
| 28 | if !defined $string && !defined $file; | ||
| 29 | die "Term::Fabulous::Layout: provide 'string' or 'file', not both" if defined $string && defined $file; | ||
| 30 | my $allowed = _checked_namespaces($allowed_namespaces); | ||
| 31 | |||
| 32 | my $source = $string // _open_file($file); | ||
| 33 | try { | ||
| 34 | $raw = parse_kdl($source); | ||
| 35 | } | ||
| 36 | catch ($error) { | ||
| 37 | die "Term::Fabulous::Layout: failed to parse KDL: $error"; | ||
| 38 | } | ||
| 39 | |||
| 40 | $required_modules = _use_instructions( $raw, $allowed ); | ||
| 41 | _load_widget_class( $_, $required_modules->{$_} ) foreach sort keys %$required_modules; | ||
| 42 | $_root_node = _root_node( $raw, $required_modules ); | ||
| 43 | } | ||
| 44 | |||
| 45 | # Text::KDL::XS reads a filehandle as UTF-8 bytes, a string as characters | ||
| 46 | sub _open_file ($path) { | ||
| 47 | open my $handle, '<:raw', $path or die "Term::Fabulous::Layout: cannot open '$path': $!"; | ||
| 48 | return $handle; | ||
| 49 | } | ||
| 50 | |||
| 51 | # The namespaces a layout may load modules from, or undef for any. | ||
| 52 | sub _checked_namespaces ($namespaces) { | ||
| 53 | return undef unless defined $namespaces; | ||
| 54 | die "Term::Fabulous::Layout: allowed_namespaces must be an array reference of package names, got " . describe($namespaces) | ||
| 55 | unless ref $namespaces eq 'ARRAY'; | ||
| 56 | die "Term::Fabulous::Layout: allowed_namespaces needs at least one package name; leave it out to allow any module" unless @$namespaces; | ||
| 57 | foreach my $namespace (@$namespaces) { | ||
| 58 | die "Term::Fabulous::Layout: allowed_namespaces holds an invalid package name " . describe($namespace) | ||
| 59 | unless defined $namespace && !ref $namespace && $namespace =~ $MODULE_NAME; | ||
| 60 | } | ||
| 61 | return [@$namespaces]; | ||
| 62 | } | ||
| 63 | |||
| 64 | # Maps every alias declared by a top-level 'use' node to its module. | ||
| 65 | # Every module is checked against the allowed namespaces before any is | ||
| 66 | # loaded, because loading runs its code. | ||
| 67 | sub _use_instructions ( $document, $allowed ) { | ||
| 68 | my %module_by_alias; | ||
| 69 | foreach my $node ( $document->nodes->@* ) { | ||
| 70 | next unless $node->name eq 'use'; | ||
| 71 | my ( $alias, $module ) = _parse_use($node); | ||
| 72 | _check_allowed( $module, $allowed ); | ||
| 73 | die "Term::Fabulous::Layout: widget alias '$alias' is declared twice" if exists $module_by_alias{$alias}; | ||
| 74 | $module_by_alias{$alias} = $module; | ||
| 75 | } | ||
| 76 | return \%module_by_alias; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _parse_use ($node) { | ||
| 80 | my @args = map { $_->is_string ? $_->value : undef } $node->args->@*; | ||
| 81 | my $well_formed = !$node->props->@* && !$node->children->@* && !grep { !defined } @args; | ||
| 82 | |||
| 83 | if ( $well_formed && @args == 1 ) { | ||
| 84 | my ($module) = @args; | ||
| 85 | _check_module_name($module); | ||
| 86 | die "Term::Fabulous::Layout: 'use $module' without 'as' needs a module name starting with an uppercase letter, because the name is also the widget node name" | ||
| 87 | unless $module =~ /\A[A-Z]/; | ||
| 88 | return ( $module, $module ); | ||
| 89 | } | ||
| 90 | if ( $well_formed && @args == 3 && $args[1] eq 'as' ) { | ||
| 91 | my ( $module, undef, $alias ) = @args; | ||
| 92 | _check_module_name($module); | ||
| 93 | die "Term::Fabulous::Layout: invalid widget alias '$alias'; it must start with an uppercase letter and contain only letters, digits and '_'" | ||
| 94 | unless $alias =~ $ALIAS; | ||
| 95 | return ( $alias, $module ); | ||
| 96 | } | ||
| 97 | die "Term::Fabulous::Layout: invalid 'use' instruction; expected 'use Module::Name' or 'use Module::Name as Alias'"; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _check_module_name ($module) { | ||
| 101 | die "Term::Fabulous::Layout: invalid module name '$module' in 'use'" unless $module =~ $MODULE_NAME; | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | # A module is in a namespace when it is the namespace or below it. | ||
| 106 | sub _check_allowed ( $module, $allowed ) { | ||
| 107 | return unless defined $allowed; | ||
| 108 | return if grep { $module eq $_ || index( $module, "${_}::" ) == 0 } @$allowed; | ||
| 109 | die "Term::Fabulous::Layout: 'use $module' is not allowed; allowed_namespaces permits only modules in " . join( ', ', @$allowed ); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub _load_widget_class ( $alias, $module ) { | ||
| 113 | ( my $path = "$module.pm" ) =~ s{::}{/}g; | ||
| 114 | try { | ||
| 115 | require $path; | ||
| 116 | } | ||
| 117 | catch ($error) { | ||
| 118 | die "Term::Fabulous::Layout: cannot load '$module' for widget alias '$alias': $error"; | ||
| 119 | } | ||
| 120 | die "Term::Fabulous::Layout: '$module' (widget alias '$alias') does not compose $LAYOUT_ROLE, so it cannot be built from a layout" | ||
| 121 | unless $module->DOES($LAYOUT_ROLE); | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _root_node ( $document, $module_by_alias ) { | ||
| 126 | my @widget_nodes; | ||
| 127 | foreach my $node ( $document->nodes->@* ) { | ||
| 128 | my $name = $node->name; | ||
| 129 | next if $name eq 'use'; | ||
| 130 | die "Term::Fabulous::Layout: unexpected top-level node '$name'; only 'use' instructions and one root widget are allowed" | ||
| 131 | unless exists $module_by_alias->{$name}; | ||
| 132 | push @widget_nodes, $node; | ||
| 133 | } | ||
| 134 | die "Term::Fabulous::Layout: no root widget found in the layout" unless @widget_nodes; | ||
| 135 | die "Term::Fabulous::Layout: multiple root widgets found in the layout (" . join( ', ', map { $_->name } @widget_nodes ) . ")" | ||
| 136 | if @widget_nodes > 1; | ||
| 137 | return $widget_nodes[0]; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _widget_id ($node) { | ||
| 141 | my $name = $node->name; | ||
| 142 | die "Term::Fabulous::Layout: widget '$name' does not accept key=value properties (got: " . join( ', ', map { $_->[0] } $node->props->@* ) . ")" | ||
| 143 | if $node->props->@*; | ||
| 144 | |||
| 145 | my @args = $node->args->@*; | ||
| 146 | return undef unless @args; | ||
| 147 | die "Term::Fabulous::Layout: widget '$name' takes at most one argument, a string id" unless @args == 1 && $args[0]->is_string; | ||
| 148 | return $args[0]->value; | ||
| 149 | } | ||
| 150 | |||
| 151 | method _build_widget ($node) { | ||
| 152 | my $name = $node->name; | ||
| 153 | my $class = $required_modules->{$name} // die "Term::Fabulous::Layout: unknown widget '$name'; declare it with 'use Module::Name as $name'"; | ||
| 154 | my $id = _widget_id($node); | ||
| 155 | |||
| 156 | my $widget; | ||
| 157 | try { | ||
| 158 | $widget = $class->new( id => $id ); | ||
| 159 | $widget->apply_layout_node($node); | ||
| 160 | } | ||
| 161 | catch ($error) { | ||
| 162 | die "Term::Fabulous::Layout: cannot build widget '$name'" . ( defined $id ? " \"$id\"" : '' ) . ": $error"; | ||
| 163 | } | ||
| 164 | |||
| 165 | foreach my $child ( $node->children->@* ) { | ||
| 166 | next unless Term::Fabulous::Role::CanParseLayout::is_widget_node_name( $child->name ); # properties are parsed by the widget itself | ||
| 167 | die "Term::Fabulous::Layout: widget '$name' cannot contain child widgets (found '" . $child->name . "')" | ||
| 168 | unless $widget->can('add_child'); | ||
| 169 | $widget->add_child( $self->_build_widget($child) ); | ||
| 170 | } | ||
| 171 | return $widget; | ||
| 172 | } | ||
| 173 | |||
| 174 | method build () { | ||
| 175 | $root_widget //= $self->_build_widget($_root_node); | ||
| 176 | return $root_widget; | ||
| 177 | } | ||
| 178 | |||
| 179 | method walk_nodes ($code) { | ||
| 180 | my @nodes = ( $raw->nodes->@* ); | ||
| 181 | while (@nodes) { | ||
| 182 | my $node = shift @nodes; | ||
| 183 | $code->($node); | ||
| 184 | push @nodes, $node->children->@* if $node->children; | ||
| 185 | } | ||
| 186 | return; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; | ||
| 191 | |||
| 192 | __END__ |