I’m iterating over the frontmatter of a few data files to get all licenses that are used. Since some licenses can appear twice or more, I only want to store them once.
A hash-table is needed for the representation, because the key should be a modified version of the value. Look here
The frontmatters:
// #1 Frontmatter
license = "GPL"
// #2 Frontmatter
license = "GPL"
// #3 Frontmatter
license = "MIT"
// #4 Frontmatter
license = "Unlicense License"
// and so on
// The (pseudo-) code to get all licenses:
{{ $hTable := map[string]string{} }}
{{ $key := "" }}
{{ range .Data.Pages }}
{{ $key = (lower (replace .Params.license " " "")) }} // Make the variable's value lowercase and remove all spaces
{{ $hTable[$key] = .Params.license }}
{{ end }}
// The expected output
// $hTable["mit"] -> MIT
// $hTable["gpl"] -> GPL
// $hTable[unlicenselicense] -> Unlicense License
Good idea @bep, but the problem I see is that I need to range over the hash-table later on and the licenses will be mixed up with other (possible) key/value-pairs.
I’m experimenting with a jQuery plugin for filtering all Hugo themes based on their theme.toml for the theme gallery. A <select>-element should list all licenses that are used by the themes. This works so far except that I have doubled entries for the options. Therefore I need something like a set which contains each license only once.
The key of the hash-table is the CSS-class that the jQuery-plugin uses to filter the elements. The value represents the option in the select-element.
What you really need is a way to check if you have printed a license before. The licenses are already in front matter. Loop the pages, if not printed, print and add to scratch.