From 1b0ae8408c134414698a6ac4a9bf4895fddbc007 Mon Sep 17 00:00:00 2001 From: Nikita Shubin Date: Fri, 29 Mar 2024 13:12:44 +0300 Subject: [PATCH] hugolib: Add support for gitpurism Add possibility to extract and use tags and categories coming from git notes. NOTE: I have no idea of what this is, just mimicking some stuff found here. Signed-off-by: Nikita Shubin --- hugolib/content_map_page.go | 19 +++++++++++++++++++ hugolib/hugo_sites.go | 37 +++++++++++++++++++++++++++++++++++++ hugolib/page.go | 4 ++++ hugolib/page__common.go | 1 + 4 files changed, 61 insertions(+) diff --git a/hugolib/content_map_page.go b/hugolib/content_map_page.go index 7954c606..dec8c7dd 100644 --- a/hugolib/content_map_page.go +++ b/hugolib/content_map_page.go @@ -147,6 +147,13 @@ func (m *pageMap) newPageFromContentNode(n *contentNode, parentBucket *pagesMapB } ps.markdownTitleInfo = mi + // Append taxonomies and categories + gpi, err := s.h.gitPurismInfoForPage(ps) + if err != nil { + return nil, errors.Wrap(err, "failed to load GitPurism data") + } + ps.gitPurismInfo = gpi + r, err := content() if err != nil { return nil, err @@ -176,6 +183,18 @@ func (m *pageMap) newPageFromContentNode(n *contentNode, parentBucket *pagesMapB return nil, ps.wrapError(err) } + if (metaProvider.params["tags"] != nil) { + metaProvider.params["tags"] = append(metaProvider.params["tags"].([]string), gpi.Tags...) + } else { + metaProvider.params["tags"] = gpi.Tags + } + + if (metaProvider.params["categories"] != nil) { + metaProvider.params["categories"] = append(metaProvider.params["tags"].([]string), gpi.Categories...) + } else { + metaProvider.params["categories"] = gpi.Categories + } + if err := metaProvider.applyDefaultValues(n); err != nil { return nil, err } diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index b69cd61c..76c0b9e1 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -83,6 +83,8 @@ type HugoSites struct { markdownTitleInfo *markdownTitleInfo + gitPurismInfo *gitPurismInfo + // As loaded from the /data dirs data map[string]any @@ -184,6 +186,7 @@ type hugoSitesInit struct { gitInfo *lazy.Init markdownTitleInfo *lazy.Init + gitPurismInfo *lazy.Init // Maps page translations. translations *lazy.Init @@ -194,6 +197,7 @@ func (h *hugoSitesInit) Reset() { h.layouts.Reset() h.gitInfo.Reset() h.markdownTitleInfo.Reset() + h.gitPurismInfo.Reset() h.translations.Reset() } @@ -242,6 +246,18 @@ func (h *HugoSites) markdownTitleInfoForPage(p page.Page) (*MarkdownTitleInfo, e return h.markdownTitleInfo.forPage(p), nil } +func (h *HugoSites) gitPurismInfoForPage(p page.Page) (*GitPurismInfo, error) { + if _, err := h.init.gitPurismInfo.Do(); err != nil { + return nil, err + } + + if h.gitPurismInfo == nil { + return nil, nil + } + + return h.gitPurismInfo.forPage(p), nil +} + func (h *HugoSites) siteInfos() page.Sites { infos := make(page.Sites, len(h.Sites)) for i, site := range h.Sites { @@ -372,6 +388,7 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) { layouts: lazy.New(), gitInfo: lazy.New(), markdownTitleInfo: lazy.New(), + gitPurismInfo: lazy.New(), translations: lazy.New(), }, } @@ -423,6 +440,14 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) { return nil, nil }) + h.init.gitPurismInfo.Add(func() (any, error) { + err := h.loadGitPurismInfo() + if err != nil { + return nil, errors.Wrap(err, "failed to load MarkdownTitle info") + } + return nil, nil + }) + for _, s := range sites { s.h = h } @@ -483,6 +508,18 @@ func (h *HugoSites) loadMarkdownTitleInfo() error { return nil } +func (h *HugoSites) loadGitPurismInfo() error { + if h.Cfg.GetBool("enableGitPurismInfo") { + gpi, err := newGitPurismInfo(h.Cfg) + if err != nil { + h.Log.Errorln("Failed to read GitPurismInfo:", err) + } else { + h.gitPurismInfo = gpi + } + } + return nil +} + func (l configLoader) applyDeps(cfg deps.DepsCfg, sites ...*Site) error { if cfg.TemplateProvider == nil { cfg.TemplateProvider = tplimpl.DefaultTemplateProvider diff --git a/hugolib/page.go b/hugolib/page.go index 5fae15ee..077d378e 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -164,6 +164,10 @@ func (p *pageState) MarkdownTitleInfo() *MarkdownTitleInfo { return p.markdownTitleInfo } +func (p *pageState) GitPurismInfo() *GitPurismInfo { + return p.gitPurismInfo +} + // GetTerms gets the terms defined on this page in the given taxonomy. // The pages returned will be ordered according to the front matter. func (p *pageState) GetTerms(taxonomy string) page.Pages { diff --git a/hugolib/page__common.go b/hugolib/page__common.go index b0ab4489..25c215ba 100644 --- a/hugolib/page__common.go +++ b/hugolib/page__common.go @@ -107,6 +107,7 @@ type pageCommon struct { codeowners []string markdownTitleInfo *MarkdownTitleInfo + gitPurismInfo *GitPurismInfo // Positional navigation posNextPrev *nextPrev -- 2.30.2