You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For HaskAnything.com, I have an authors field. Sometimes, it has data like this:
authors: Beerend Lauwers
And sometimes, it has data like this:
authors:
- Beerend Lauwers
The context I pass to my templates can be simplified to the following:
templateCtx = metadataListField <> defaultContext
where metadataListField is a custom context I wrote myself that maps a YAML list field to a Hakyll list field, failing to yield a Hakyll field if no such YAML list field could be found:
----------------------------------------------------------------------------------| Map any list field to its metadata value, if presentmetadataListField::Contexta
metadataListField =Context$\k _ i ->do
values <- getMetadataListField (itemIdentifier i) k
case values ofJust vs ->do
listItems <-mapM makeItem vs
return$ListField (field "item" (return.itemBody)) listItems
Nothing-> empty
Problem
The problem I get is that if if have inconsistent YAML data (as explained above), referencing $authors$ in a template will result in an error:
Using YAML:
authors:
- Beerend Lauwers
[ERROR] Hakyll.Web.Template.applyTemplateWith: expected StringField but got ListField for expr authors
Trying to reference it as a list field with $for(authors)$ $item$ $endfor$ results in the opposite problem for another file:
Using YAML:
authors: Beerend Lauwers
[ERROR] Hakyll.Web.Template.applyTemplateWith: expected ListField but got StringField for expr authors
In the second file, the authors key could be interpreted as a string field or a list field (from Hakyll's perspective), but it is interpreted as a string field.
Solution
First off, perhaps it would be a good idea to automatically expose true YAML list fields as a Hakyll ListField by modifying metadataField to not always return a StringField, but a ListField as well. Here's the current definition:
--| Map any field to its metadata value, if presentmetadataField::Contexta
metadataField =Context$\k _ i ->do
value <- getMetadataField (itemIdentifier i) k
maybe empty (return.StringField) value
Second, perhaps we should document this caveat somewhere, or, even better, error out when we're parsing the YAML metadata of several files and notice an inconsistency like this.
The text was updated successfully, but these errors were encountered:
Yes, I think you are right. The reason for this behavior is that Hakyll hasn't supported YAML for too long -- it used to be just key: value Metadata.
In order to preserve backwards compatibility, I think we could add another primitive likemetadataField, but one which understands YAML and tries to do the right thing in most cases, also supporting $for$ loops and data inside that and so on. This could be called yamlMetadataField. What do you think?
Prelude
For HaskAnything.com, I have an
authors
field. Sometimes, it has data like this:And sometimes, it has data like this:
The context I pass to my templates can be simplified to the following:
where
metadataListField
is a custom context I wrote myself that maps a YAML list field to a Hakyll list field, failing to yield a Hakyll field if no such YAML list field could be found:Problem
The problem I get is that if if have inconsistent YAML data (as explained above), referencing
$authors$
in a template will result in an error:Using YAML:
Trying to reference it as a list field with
$for(authors)$ $item$ $endfor$
results in the opposite problem for another file:Using YAML:
In the second file, the
authors
key could be interpreted as a string field or a list field (from Hakyll's perspective), but it is interpreted as a string field.Solution
First off, perhaps it would be a good idea to automatically expose true YAML list fields as a Hakyll
ListField
by modifyingmetadataField
to not always return aStringField
, but aListField
as well. Here's the current definition:Second, perhaps we should document this caveat somewhere, or, even better, error out when we're parsing the YAML metadata of several files and notice an inconsistency like this.
The text was updated successfully, but these errors were encountered: