diff options
Diffstat (limited to 'docshelper/docs.go')
-rw-r--r-- | docshelper/docs.go | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/docshelper/docs.go b/docshelper/docs.go index 17e0ccd91..999e14d7d 100644 --- a/docshelper/docs.go +++ b/docshelper/docs.go @@ -15,37 +15,37 @@ // is of limited interest for the general Hugo user. package docshelper -import ( - "encoding/json" +type ( + DocProviderFunc = func() DocProvider + DocProvider map[string]map[string]interface{} ) -// DocProviders contains all DocProviders added to the system. -var DocProviders = make(map[string]DocProvider) +var docProviderFuncs []DocProviderFunc -// AddDocProvider adds or updates the DocProvider for a given name. -func AddDocProvider(name string, provider DocProvider) { - if prev, ok := DocProviders[name]; !ok { - DocProviders[name] = provider - } else { - DocProviders[name] = merge(prev, provider) - } +func AddDocProviderFunc(fn DocProviderFunc) { + docProviderFuncs = append(docProviderFuncs, fn) } -// DocProvider is used to save arbitrary JSON data -// used for the generation of the documentation. -type DocProvider func() map[string]interface{} +func GetDocProvider() DocProvider { + provider := make(DocProvider) + + for _, fn := range docProviderFuncs { + p := fn() + for k, v := range p { + if prev, found := provider[k]; !found { + provider[k] = v + } else { + merge(prev, v) + } + } + } -// MarshalJSON returns a JSON representation of the DocProvider. -func (d DocProvider) MarshalJSON() ([]byte, error) { - return json.MarshalIndent(d(), "", " ") + return provider } -func merge(a, b DocProvider) DocProvider { - next := a() - for k, v := range b() { - next[k] = v - } - return func() map[string]interface{} { - return next +// Shallow merge +func merge(dst, src map[string]interface{}) { + for k, v := range src { + dst[k] = v } } |