Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested keys in yfm_keys (Markdown) #392

Closed
rtsisyk opened this issue Dec 14, 2022 · 4 comments
Closed

Support nested keys in yfm_keys (Markdown) #392

rtsisyk opened this issue Dec 14, 2022 · 4 comments

Comments

@rtsisyk
Copy link

rtsisyk commented Dec 14, 2022

Thanks for the great tool. It would be nice to add support for nested keys for --yfm_keys option:

title: "My title"
extra:
  menu_title: Home <!-- not supported currently

I took a look at implementation, but the code is too complex for me to patch it quickly. Please help.

sub do_hash {
my ( $self, $blockref, $hash, $indent, $ctx, $yfm_keys, $yfm_skip_array ) = @_;
foreach my $name ( sort keys %$hash ) {
my $el = $hash->{$name};
my $header = ( ' ' x $indent ) . YAML::Tiny::_dump_scalar( "dummy", $name, 1 ) . ":";
my $type = ref $el;
if ( !$type ) {
my %keys = %{$yfm_keys};
if ( ( not %keys ) || $keys{$name} ) { # either no key is provided, or the key we need is also provided
my $translation = $self->translate( $el, $blockref, "YAML Front Matter:$ctx $name", "wrap" => 0 );
if ( $el =~ /^\[.*\]$/ ) { # Do not quote the lists
$self->pushline( $header . " $translation\n" );
} else {
# add extra quotes to the parameter, as a protection to the extra chars that the translator could add
$self->pushline( $header . ' ' . format_scalar($translation) . "\n" );
}
} else {
# Work around a bug in YAML::Tiny that quotes numbers
# See https://github.com/Perl-Toolchain-Gang/YAML-Tiny#additional-perl-specific-notes
if ( Scalar::Util::looks_like_number($el) ) {
$self->pushline("$header $el\n");
} elsif ( $el =~ /^\[.*\]$/ ) { # Do not quote the lists either
$self->pushline("$header $el\n");
} else {
$self->pushline( $header . ' ' . YAML::Tiny::_dump_scalar( "dummy", $el ) . "\n" );
}
}
} elsif ( $type eq 'ARRAY' ) {
if (@$el) {
$self->pushline( $header . "\n" );
do_array( $self, $blockref, $el, $indent + 1, "$ctx $name", $yfm_keys, $yfm_skip_array );
} else {
$self->pushline( $header . " []\n" );
}
} elsif ( $type eq 'HASH' ) {
if ( keys %$el ) {
$self->pushline( $header . "\n" );
do_hash( $self, $blockref, $el, $indent + 1, "$ctx $name", $yfm_keys, $yfm_skip_array );
} else {
$self->pushline( $header . " {}\n" );
}
} else {
die "YAML $type references not supported";
}
}
}
}

@mquinson
Copy link
Owner

Hello,

I have the feeling that this can be achieved with -o 'yfm_paths=extra menu_title' (and my commits of earlier today). Could you please test whether it's doing what you want and tell us?

Thanks,
Mt

@mquinson
Copy link
Owner

I'm rather sure that my change is actually fixing your problem, so I'm closing this issue. Please feel free to reopen if I'm off track.

@rtsisyk
Copy link
Author

rtsisyk commented Feb 21, 2023

I'm rather sure that my change is actually fixing your problem, so I'm closing this issue. Please feel free to reopen if I'm off track.

Yeah, -o 'yfm_paths=extra menu_title' does work. Thanks a lot!

@zygisk-topjohnu
Copy link

Press C button for 5 Second.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants