-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Archetype fields are parsed/sorted upon instantiations #452
Comments
Yeah, the problem is that Hugo isn't just copying the contents of the You're not the first person to complain about it, and I think you're right,
|
I’ve just been looking at this for the last hour or so, and I’m not sure it’s going to be easy to do. The order of keys is determined by the front matter creator. If the front matter were a specific type rather than Strictly speaking, this isn’t even a matter of the underlying libraries, it’s a deliberate design decision of Go itself for |
It's not surprising that no solution was found as that is the proper behavior and not a bug, according to YAML specs. To do otherwise would be to not follow the specs. In these situations, a sequence is to be used. http://yaml.org/spec/1.2/spec.html#id2765608 I also feel I need to address the 'blaming' of Go for this as this is a misunderstanding of maps and hashes, from a technical perspective. Since maps are hash backed and hashes have no order themselves and it's impossible to infer order from hashes, e.g. sorting files by checksums would not produce anything useful. This is not something unique to Go, which is why YAML specs say the same thing. Go not supporting ordering of map entries and their solution, to use a more appropriate data-strucuture(s) when ordering is required, or provide methods to provide ordered output, seems reasonable. I much prefer it to the way other languages allow developers to rely on a side-effect that they shouldn't, ordering of hashes. It leads to numerous problems; Chef's re-ordering of recipes and Salt's re-ordering of their state files, both to the bane of developers writing those files who expect that the code they write will have their order preserved, are two examples that have occurred in recent years. This bug-report is in the same class, imo, and a result of the same misunderstanding of maps as data strucutures. |
@mohae Go originally supported insertion-ordered maps, but the core language designers changed that and changed map iteration to always use a pseudorandom starting hash for iteration so that it would be impossible to depend on that. You’re right that hashtables are inherently unordered, but it is possible to spec your maps such that they support insertion ordering. The folks behind Go simply chose not to do so (and to make it that much harder to do so). I don’t blame them—it’s a perfectly cromulent decision to make. It’s just an observation that even if the YAML spec said nothing about ordering, Go makes it very difficult to do otherwise. When I was looking at the solution, I was looking from the perspective of writing an All that clear, I think that this just has to be closed because it is pathologically unfixable from a Go perspective (unless, again, Hugo were to switch to streaming parsers and perform the bulk of the parsing itself into an |
What I could like a little better than the current situation, and should be easy to implement, is to add some kind of sorting prior to writing the front matter to file. Even alphabetic would be better than today's shuffle. But also maybe weigh some fields higher than others, title, date? I have been a little bit annoyed about this when browsing my content on GitHub. EDIT IN: I notice Hugo uses a string map also internally, which makes the above NOT so easy to implement. But there are sorted map implementations out there. |
Does it really need to parse the parameters at that point? Couldn't it just parse them to ensure validity, then copy the original into the new post? It shouldn't be too hard to do a simple string insertion of the date or any other auto-injected parameters. |
@derekperkins I haven't looked closely into this one, but the parsing is delegated to different frameworks (YAML, TOML, JSON) -- when Hugo gets the data it is a Go map, and there has been no guaranteed iteration order in those maps since Go 1. |
@derekperkins, @bjornerik is correct and what I found with my quick investigation and where I noted that we would still need to write custom encoders and decoders to ensure an order to the map. You also can’t not parse the archetype front matter, because the archetype may be coming from the theme that you’re using and you may have indicated that you prefer a different front matter format than what the archetype indicated (with the |
This is what I came up with to solve the problem.
When I create a new post, this is what comes out. Comments are stripped out and the map is alphabetized, but the grouping solves most of my issues.
|
I noticed this discrepancy while going through the documentation, and did some testing. With the example archetypes/default.md of:
Hugo v0.11 (
whereas Hugo v0.12 and above (up to HEAD of v0.13-DEV) gives:
I couldn't get So, regardless of the root cause, to the end users, it is a regression: At least for TOML, It used to keep the proper order in v0.11, but now it sorts all the variables alphabetically and messes things up. I haven't looked into it any deeper, though I wonder if a commit in Hugo causes this, or whether an external library changed. Just my 2 cents. :-) |
That doesn’t look right, @anthonyfok. As you indicated, if you have this archetype: +++
tags = ["x", "y"]
categories = ["x", "y"]
+++ If Hugo v0.11 (hugo_0.11_amd64_linux new post/test.md) gives, +++
categories = ["x", "y"]
tags = ["x", "y"]
title = "test"
date = 2015-01-14T02:26:19Z
+++ Notice that As I indicated, this is something that the Go team has gone out of its way to ensure That said, I did a little more digging, and the folks behind YAML have possibly done us a favour if we use |
I stand corrected. Thank you for your detailed explanation, @halostatue. (I didn't even read my tests correctly. Note to self: Next time, read the whole thread before speaking.) Thanks for digging deeper into this: the new yaml.MapSlice in yaml.v2 sounds interesting indeed. Cheers, |
Just to clarify, the reason Hugo needs to parse and re-write the archetype files is just to insert the date and title fields, correct? I know it's hack, but why not simply insert those strings into the file contents right after the |
@dimo414 you got a point. It would make the code a little more complex, but doable I guess. Well tested PRs are welcome. |
This commit removes the fragile front matter decoding, and takes the provided archetype file as-is and processes it as a template. This also means that we no longer will attempt to fill in default values for `title` and `date`. The upside is that it is now easy to create these values in a dynamic way: ```toml +++ title = {{ .Name | title }} date = {{ .Date }} draft = true +++ ``` You can currently use all of Hugo's template funcs, but the data context is currently very shallow: * `.Type` gives the archetype kind provided * `.Name` gives the target file name without extension. * `.Path` gives the target file name * `.Date` gives the current time as RFC3339 formatted string The above will probably be extended in gohugoio#1629. Fixes gohugoio#452 Updates gohugoio#1629
This commit removes the fragile front matter decoding, and takes the provided archetype file as-is and processes it as a template. This also means that we no longer will attempt to fill in default values for `title` and `date`. The upside is that it is now easy to create these values in a dynamic way: ```toml +++ title = {{ .BaseFileName | title }} date = {{ .Date }} draft = true +++ ``` You can currently use all of Hugo's template funcs, but the data context is currently very shallow: * `.Type` gives the archetype kind provided * `.Name` gives the target file name without extension. * `.Path` gives the target file name * `.Date` gives the current time as RFC3339 formatted string The above will probably be extended in #1629. Fixes #452 Updates #1629
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I've created
archetypes/post.md
containing some fields. Most of them are just there to remind me that I should fill them in:After
hugo new post/foo.md
, it turned out that hugo unhelpfully sorted all of the fields:Not really helpful :(
The text was updated successfully, but these errors were encountered: