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

Boolean values in metadata are printed as "true"/null in $meta-json$ #6650

Closed
zackw opened this issue Aug 30, 2020 · 4 comments
Closed

Boolean values in metadata are printed as "true"/null in $meta-json$ #6650

zackw opened this issue Aug 30, 2020 · 4 comments

Comments

@zackw
Copy link

zackw commented Aug 30, 2020

Boolean values in frontmatter, or added to the metadata by a filter, are reported in $meta-json$ using the string "true" for true values and JSON null for false values. It would be better if they were reported as JSON booleans.

Demo:

$ cat > demo.md <<EOF
---
bool_a: true
bool_b: false
title: title
...
EOF

$ cat > demo-template.html5 <<EOF
$meta-json$
EOF

$ pandoc -t html5 -s --template ./demo-template.html5 < demo.md
{"bool_a":"true","title":"title","bool_b":null}

$ cat > demo-filter.lua <<EOF
function Meta (data)
  data.bool_a = true
  data.bool_b = false
  return data
end
EOF

$ pandoc -s -t html5 --metadata title="title" \
  --template ./demo-template.html5 --lua-filter ./demo-filter.lua < /dev/null
{"bool_a":"true","title":"title","bool_b":null}

Desired output from both pandoc commands is

{"bool_a":true,"title":"title","bool_b":false}

Pandoc version information:

pandoc 2.9.2.1
Compiled with pandoc-types 1.20, texmath 0.12.0.2, skylighting 0.8.5

Possibly related to #6630 and #6388.

N.B. in addition to being a minor headache for downstream consumers of the JSON metadata, this problem confused me into thinking I had a bug in my filter, not creating a new metadata entry correctly or something like that.

@jgm
Copy link
Owner

jgm commented Aug 30, 2020

Currently we render the metadata to a doctemplates Context and take the JSON from this.
The problem is that the Val type underlying Context doesn't have Booleans. (They're not really needed in the template system.) It might be worth adding them, but in any case that's what would be needed for this and the related issue #6388.

@argent0
Copy link
Contributor

argent0 commented Sep 11, 2020

Hi, I'm really new to pandoc code:

From Text.DocTemplate, I see that Val a is used to store a JSON like structure where all the "endpoints" are Doc a.

Browsing the code I get to src/Text/Pandoc/Writers/Shared.hs's function metaValueToVal. In that function the line

113: metaValueToVal _ _ (MetaBool False) = return NullVal

maps false to NullVal a Nothing-like constructor for Val a.

But the previous line maps:

112: metaValueToVal _ _ (MetaBool True) = return $ SimpleVal "true"

So I went ahead and did the same with the False values. I.e.

113: metaValueToVal _ _ (MetaBool False) = return $ SimpleVal "false"

And got the expected result both in this case and #6630

All tests passing in my repository ($ stack test)

Is this fix valid?

@jgm
Copy link
Owner

jgm commented Sep 11, 2020

I think the problem is that if you set it to SimpleVal "false", then template tests will pass (because they're checking for an empty value). So if you had a template with

$if(foo)$
blah
$endif$

then, if I'm not mistaken, you'd get output blah with foo set to SimpleVal "false" (untested and haven't looked back at the code, so you might try it).

@jgm
Copy link
Owner

jgm commented Dec 17, 2020

This issue is now fixed by the use of doctemplates 0.9.

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

No branches or pull requests

3 participants