From 5d9bf2ed001c2becc0eac0fdf407d101b11ebe4d Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Mon, 30 Dec 2024 11:02:39 +0000 Subject: [PATCH 1/2] Ensure every site configures markup This avoids needing to notice and play whack-a-mole when things aren't configured as we expect. Also, add a --fix mode which will try to fix things for you. --- .../cmd/site-consistency/hugo_toml_structs.go | 34 +++++++++ tooling/go/cmd/site-consistency/main.go | 74 ++++++++++++++++++- tooling/go/go.mod | 4 +- tooling/go/go.sum | 2 + 4 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 tooling/go/cmd/site-consistency/hugo_toml_structs.go diff --git a/tooling/go/cmd/site-consistency/hugo_toml_structs.go b/tooling/go/cmd/site-consistency/hugo_toml_structs.go new file mode 100644 index 000000000..5b000be91 --- /dev/null +++ b/tooling/go/cmd/site-consistency/hugo_toml_structs.go @@ -0,0 +1,34 @@ +package main + +type hugoToml struct { + Markup *hugoTomlMarkup `toml:"markup"` +} + +type hugoTomlMarkup struct { + TableOfContents *hugoTomlTableOfContents `toml:"tableOfContents"` + Goldmark *hugoTomlGoldmark `toml:"goldmark"` +} + +type hugoTomlTableOfContents struct { + Endlevel *int `toml:"endLevel"` + Ordered *bool `toml:"ordered"` + StartLevel *int `toml:"startLevel"` +} + +type hugoTomlGoldmark struct { + Renderer *hugoTomlGoldmarkRenderer `toml:"renderer"` + Parser *hugoTomlGoldmarkParser `toml:"parser"` +} + +type hugoTomlGoldmarkRenderer struct { + Unsafe *bool `toml:"unsafe"` +} + +type hugoTomlGoldmarkParser struct { + Attribute *hugoTomlGoldmarkParserAttribute `toml:"attribute"` +} + +type hugoTomlGoldmarkParserAttribute struct { + Block *bool `toml:"block"` + Title *bool `toml:"title"` +} diff --git a/tooling/go/cmd/site-consistency/main.go b/tooling/go/cmd/site-consistency/main.go index d1d4a3857..e913764db 100644 --- a/tooling/go/cmd/site-consistency/main.go +++ b/tooling/go/cmd/site-consistency/main.go @@ -8,12 +8,17 @@ import ( "log" "os" "path/filepath" + "reflect" "sort" + + "github.com/BurntSushi/toml" ) func main() { var rootDirectory string + var fix bool flag.StringVar(&rootDirectory, "root-dir", ".", "Root directory to search for hugo.toml files in") + flag.BoolVar(&fix, "fix", false, "Fix problems, rather than reporting them") flag.Parse() var directoriesToCheck []string @@ -35,10 +40,14 @@ func main() { var errors []string for _, directoryToCheck := range directoriesToCheck { for _, expectedSymlink := range []string{"config", "deploy-netlify.sh", "netlify.toml"} { - if errorToReport, ok := checkForParentSymlink(directoryToCheck, expectedSymlink); !ok { + if errorToReport, ok := checkForParentSymlink(directoryToCheck, expectedSymlink, fix); !ok { errors = append(errors, errorToReport) } } + + if errorToReport := checkForMarkupConfig(filepath.Join(directoryToCheck, "hugo.toml"), fix); errorToReport != nil { + errors = append(errors, errorToReport.Error()) + } } for _, errorToReport := range errors { @@ -51,13 +60,19 @@ func main() { // checkForParentSymlink checks that a symlink exists in the directory with expectedLinkName pointing at something in the parent directory with the same name. // It returns a bool indicating whether things were correct, and if the bool is false, returns a non-empty string describing the problem suitable for displaying to a user. -func checkForParentSymlink(directoryToCheck, expectedLinkName string) (string, bool) { +func checkForParentSymlink(directoryToCheck, expectedLinkName string, fix bool) (string, bool) { expectedDestination := filepath.Join("..", "tooling", "common-config", expectedLinkName) path := filepath.Join(directoryToCheck, expectedLinkName) fileInfo, err := os.Lstat(path) if err != nil { reason := fmt.Sprintf("an error occurred looking it up: %v", err) if errors.Is(err, fs.ErrNotExist) { + if fix { + if err := os.Symlink(expectedDestination, path); err != nil { + panic(fmt.Sprintf("Failed to create symlink %s: %v", path, err)) + } + return "", true + } reason = "it didn't exist" } return formatError(path, expectedDestination, reason), false @@ -78,3 +93,58 @@ func checkForParentSymlink(directoryToCheck, expectedLinkName string) (string, b func formatError(path, expectedDestination, reason string) string { return fmt.Sprintf("Expected %s to be a symlink pointing at %s but %s", path, expectedDestination, reason) } + +func checkForMarkupConfig(path string, fix bool) error { + var config hugoToml + bytes, err := os.ReadFile(path) + if err != nil { + return fmt.Errorf("failed to read %s: %w", path, err) + } + if err := toml.Unmarshal(bytes, &config); err != nil { + return fmt.Errorf("failed to decode %s as toml: %w", path, err) + } + + want := &hugoTomlMarkup{ + TableOfContents: &hugoTomlTableOfContents{ + Endlevel: ptr(2), + Ordered: ptr(true), + StartLevel: ptr(2), + }, + Goldmark: &hugoTomlGoldmark{ + Renderer: &hugoTomlGoldmarkRenderer{ + Unsafe: ptr(true), + }, + Parser: &hugoTomlGoldmarkParser{ + Attribute: &hugoTomlGoldmarkParserAttribute{ + Block: ptr(true), + Title: ptr(true), + }, + }, + }, + } + if !reflect.DeepEqual(config.Markup, want) { + wantConfig := hugoToml{ + Markup: want, + } + marshalledWant, err := toml.Marshal(wantConfig) + if err != nil { + panic(fmt.Sprintf("failed to marshal known-good toml: %v", err)) + } + if fix && config.Markup == nil { + var out []byte + out = append(out, bytes...) + out = append(out, '\n', '\n') + out = append(out, marshalledWant...) + if err := os.WriteFile(path, out, 0644); err != nil { + panic(fmt.Sprintf("failed to fix %s: %v", path, err)) + } + return nil + } + return fmt.Errorf("%s had wrong or missing [markup] section. Add this:\n%s", path, string(marshalledWant)) + } + return nil +} + +func ptr[T any](v T) *T { + return &v +} diff --git a/tooling/go/go.mod b/tooling/go/go.mod index 05fb562a2..0e61fd6ff 100644 --- a/tooling/go/go.mod +++ b/tooling/go/go.mod @@ -3,13 +3,13 @@ module github.com/CodeYourFuture/curriculum/tooling/go go 1.21.5 require ( + github.com/BurntSushi/toml v1.4.0 + github.com/adrg/frontmatter v0.2.0 github.com/stretchr/testify v1.8.4 golang.org/x/mod v0.14.0 ) require ( - github.com/BurntSushi/toml v0.3.1 // indirect - github.com/adrg/frontmatter v0.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v2 v2.3.0 // indirect diff --git a/tooling/go/go.sum b/tooling/go/go.sum index 51a6c4938..36ba7a3fd 100644 --- a/tooling/go/go.sum +++ b/tooling/go/go.sum @@ -1,5 +1,7 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= +github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4= github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= From 4073615749b193405c1b25a7439927125aa867b3 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Mon, 30 Dec 2024 11:03:07 +0000 Subject: [PATCH 2/2] Configure markup in all sites --- common-content/hugo.toml | 15 ++++++++++++++- common-docs/hugo.toml | 24 ++++++++++++++---------- common-theme/hugo.toml | 14 ++++++++++++++ org-cyf-guides/hugo.toml | 15 ++++++++++++++- org-cyf-how-this-works/hugo.toml | 14 ++++++++++++++ org-cyf-itd/hugo.toml | 20 ++++++++++++-------- org-cyf-launch/hugo.toml | 20 ++++++++++++-------- org-cyf-piscine/hugo.toml | 20 ++++++++++++-------- org-cyf-sdc/hugo.toml | 19 ++++++++++++------- org-cyf-theme/hugo.toml | 14 ++++++++++++++ org-cyf-tracks/hugo.toml | 14 ++++++++++++++ org-cyf/hugo.toml | 14 ++++++++++++++ 12 files changed, 160 insertions(+), 43 deletions(-) diff --git a/common-content/hugo.toml b/common-content/hugo.toml index d24b83be8..49f5df1ae 100644 --- a/common-content/hugo.toml +++ b/common-content/hugo.toml @@ -2,4 +2,17 @@ [module] [module.hugoVersion] extended = true - min = "0.116.0" \ No newline at end of file + min = "0.116.0" + +[markup] + [markup.tableOfContents] + endLevel = 2 + ordered = true + startLevel = 2 + [markup.goldmark] + [markup.goldmark.renderer] + unsafe = true + [markup.goldmark.parser] + [markup.goldmark.parser.attribute] + block = true + title = true diff --git a/common-docs/hugo.toml b/common-docs/hugo.toml index 6a6dfbc52..08e194a01 100644 --- a/common-docs/hugo.toml +++ b/common-docs/hugo.toml @@ -63,20 +63,24 @@ maxAge="6h" getenv = ["^HUGO_CURRICULUM_GITHUB_BEARER_TOKEN$"] +theme = "common-theme" + +#this makes the section nav go the right way +# because of this 'unexpected behaviour' https://gohugo.io/methods/page/nextinsection/ +[page] + nextPrevInSectionSortOrder = 'asc' + nextPrevSortOrder = 'asc' + + [markup] -# I've configured markdown so you don't have to [markup.tableOfContents] endLevel = 2 ordered = true startLevel = 2 + [markup.goldmark] [markup.goldmark.renderer] -# Enable HTML codeblocks, e.g. for
blocks unsafe = true - -theme = "common-theme" - -#this makes the section nav go the right way -# because of this 'unexpected behaviour' https://gohugo.io/methods/page/nextinsection/ -[page] - nextPrevInSectionSortOrder = 'asc' - nextPrevSortOrder = 'asc' \ No newline at end of file + [markup.goldmark.parser] + [markup.goldmark.parser.attribute] + block = true + title = true diff --git a/common-theme/hugo.toml b/common-theme/hugo.toml index e3c68a4b4..4df59fd02 100644 --- a/common-theme/hugo.toml +++ b/common-theme/hugo.toml @@ -59,3 +59,17 @@ getenv = ["^HUGO_CURRICULUM_GITHUB_BEARER_TOKEN$"] [module.hugoVersion] extended = true min = "0.133.0" + + +[markup] + [markup.tableOfContents] + endLevel = 2 + ordered = true + startLevel = 2 + [markup.goldmark] + [markup.goldmark.renderer] + unsafe = true + [markup.goldmark.parser] + [markup.goldmark.parser.attribute] + block = true + title = true diff --git a/org-cyf-guides/hugo.toml b/org-cyf-guides/hugo.toml index f19000f5d..cc6f38e79 100644 --- a/org-cyf-guides/hugo.toml +++ b/org-cyf-guides/hugo.toml @@ -9,4 +9,17 @@ baseURL = "https://guides.codeyourfuture.io/" # because of this 'unexpected behaviour' https://gohugo.io/methods/page/nextinsection/ [page] nextPrevInSectionSortOrder = 'asc' - nextPrevSortOrder = 'asc' \ No newline at end of file + nextPrevSortOrder = 'asc' + +[markup] + [markup.tableOfContents] + endLevel = 2 + ordered = true + startLevel = 2 + [markup.goldmark] + [markup.goldmark.renderer] + unsafe = true + [markup.goldmark.parser] + [markup.goldmark.parser.attribute] + block = true + title = true diff --git a/org-cyf-how-this-works/hugo.toml b/org-cyf-how-this-works/hugo.toml index db62f7cce..75a4849db 100644 --- a/org-cyf-how-this-works/hugo.toml +++ b/org-cyf-how-this-works/hugo.toml @@ -5,3 +5,17 @@ baseURL = "https://curriculum.codeyourfuture.io/" [module] [[module.imports]] path = "github.com/CodeYourFuture/curriculum/org-cyf-theme" + + +[markup] + [markup.tableOfContents] + endLevel = 2 + ordered = true + startLevel = 2 + [markup.goldmark] + [markup.goldmark.renderer] + unsafe = true + [markup.goldmark.parser] + [markup.goldmark.parser.attribute] + block = true + title = true diff --git a/org-cyf-itd/hugo.toml b/org-cyf-itd/hugo.toml index 6208a1e87..7370685a4 100644 --- a/org-cyf-itd/hugo.toml +++ b/org-cyf-itd/hugo.toml @@ -19,18 +19,22 @@ description="Meet the world of tech in 30 days" weight = 3 url = "https://codeyourfuture.io/donate/" +#this makes the section nav go the right way +# because of this 'unexpected behaviour' https://gohugo.io/methods/page/nextinsection/ +[page] + nextPrevInSectionSortOrder = 'asc' + nextPrevSortOrder = 'asc' + + [markup] -# I've configured markdown so you don't have to [markup.tableOfContents] endLevel = 2 ordered = true startLevel = 2 + [markup.goldmark] [markup.goldmark.renderer] -# Enable HTML codeblocks, e.g. for
blocks unsafe = true - -#this makes the section nav go the right way -# because of this 'unexpected behaviour' https://gohugo.io/methods/page/nextinsection/ -[page] - nextPrevInSectionSortOrder = 'asc' - nextPrevSortOrder = 'asc' \ No newline at end of file + [markup.goldmark.parser] + [markup.goldmark.parser.attribute] + block = true + title = true diff --git a/org-cyf-launch/hugo.toml b/org-cyf-launch/hugo.toml index 44b506e83..6c09e6e94 100644 --- a/org-cyf-launch/hugo.toml +++ b/org-cyf-launch/hugo.toml @@ -21,18 +21,22 @@ baseURL = "https://launch.codeyourfuture.io/" target = "content/pd/blocks" +#this makes the section nav go the right way +# because of this 'unexpected behaviour' https://gohugo.io/methods/page/nextinsection/ +[page] + nextPrevInSectionSortOrder = 'asc' + nextPrevSortOrder = 'asc' + + [markup] -# I've configured markdown so you don't have to [markup.tableOfContents] endLevel = 2 ordered = true startLevel = 2 + [markup.goldmark] [markup.goldmark.renderer] -# Enable HTML codeblocks, e.g. for
blocks unsafe = true - -#this makes the section nav go the right way -# because of this 'unexpected behaviour' https://gohugo.io/methods/page/nextinsection/ -[page] - nextPrevInSectionSortOrder = 'asc' - nextPrevSortOrder = 'asc' \ No newline at end of file + [markup.goldmark.parser] + [markup.goldmark.parser.attribute] + block = true + title = true diff --git a/org-cyf-piscine/hugo.toml b/org-cyf-piscine/hugo.toml index fbee95829..e04b76352 100644 --- a/org-cyf-piscine/hugo.toml +++ b/org-cyf-piscine/hugo.toml @@ -7,18 +7,22 @@ title = "CYF Piscine" [params] googleFonts="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@300;500;700&family=Inter:wght@400;600&display=swap" +#this makes the section nav go the right way +# because of this 'unexpected behaviour' https://gohugo.io/methods/page/nextinsection/ +[page] + nextPrevInSectionSortOrder = 'asc' + nextPrevSortOrder = 'asc' + + [markup] -# I've configured markdown so you don't have to [markup.tableOfContents] endLevel = 2 ordered = true startLevel = 2 + [markup.goldmark] [markup.goldmark.renderer] -# Enable HTML codeblocks, e.g. for
blocks unsafe = true - -#this makes the section nav go the right way -# because of this 'unexpected behaviour' https://gohugo.io/methods/page/nextinsection/ -[page] - nextPrevInSectionSortOrder = 'asc' - nextPrevSortOrder = 'asc' \ No newline at end of file + [markup.goldmark.parser] + [markup.goldmark.parser.attribute] + block = true + title = true diff --git a/org-cyf-sdc/hugo.toml b/org-cyf-sdc/hugo.toml index 44c3b5a66..1585e1d6f 100644 --- a/org-cyf-sdc/hugo.toml +++ b/org-cyf-sdc/hugo.toml @@ -24,17 +24,22 @@ baseURL = "https://sdc.codeyourfuture.io/" [params] googleFonts="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@300;500;700&family=Inter:wght@400;600&display=swap" +#this makes the section nav go the right way +# because of this 'unexpected behaviour' https://gohugo.io/methods/page/nextinsection/ +[page] + nextPrevInSectionSortOrder = 'asc' + nextPrevSortOrder = 'asc' + + [markup] -# I've configured markdown so you don't have to [markup.tableOfContents] endLevel = 2 ordered = true startLevel = 2 + [markup.goldmark] [markup.goldmark.renderer] -# Enable HTML codeblocks, e.g. for
blocks unsafe = true -#this makes the section nav go the right way -# because of this 'unexpected behaviour' https://gohugo.io/methods/page/nextinsection/ -[page] - nextPrevInSectionSortOrder = 'asc' - nextPrevSortOrder = 'asc' + [markup.goldmark.parser] + [markup.goldmark.parser.attribute] + block = true + title = true diff --git a/org-cyf-theme/hugo.toml b/org-cyf-theme/hugo.toml index 7e8fdfa2c..d0b24abd5 100644 --- a/org-cyf-theme/hugo.toml +++ b/org-cyf-theme/hugo.toml @@ -64,3 +64,17 @@ maxAge = 0 # Allow accessing a GitHub bearer token to avoid rate limits when doing HTTP fetches to the GitHub API. # This can be generated at https://github.com/settings/tokens?type=beta and needs read-only access to all public CYF GitHub repos. getenv = ["^HUGO_CURRICULUM_GITHUB_BEARER_TOKEN$"] + + +[markup] + [markup.tableOfContents] + endLevel = 2 + ordered = true + startLevel = 2 + [markup.goldmark] + [markup.goldmark.renderer] + unsafe = true + [markup.goldmark.parser] + [markup.goldmark.parser.attribute] + block = true + title = true diff --git a/org-cyf-tracks/hugo.toml b/org-cyf-tracks/hugo.toml index dee1b1de5..2d182c294 100644 --- a/org-cyf-tracks/hugo.toml +++ b/org-cyf-tracks/hugo.toml @@ -23,3 +23,17 @@ baseURL = "https://tracks.codeyourfuture.io/" [taxonomies] track_kind = "track_kinds" + + +[markup] + [markup.tableOfContents] + endLevel = 2 + ordered = true + startLevel = 2 + [markup.goldmark] + [markup.goldmark.renderer] + unsafe = true + [markup.goldmark.parser] + [markup.goldmark.parser.attribute] + block = true + title = true diff --git a/org-cyf/hugo.toml b/org-cyf/hugo.toml index b419d4dab..4c02e9bcc 100644 --- a/org-cyf/hugo.toml +++ b/org-cyf/hugo.toml @@ -64,3 +64,17 @@ target = "content/guides" [page] nextPrevInSectionSortOrder = 'asc' nextPrevSortOrder = 'asc' + + +[markup] + [markup.tableOfContents] + endLevel = 2 + ordered = true + startLevel = 2 + [markup.goldmark] + [markup.goldmark.renderer] + unsafe = true + [markup.goldmark.parser] + [markup.goldmark.parser.attribute] + block = true + title = true