github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/docgen/api/config.go (about)

     1  package docgen
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type config struct {
     9  	// Path to scan for documentation to render
    10  	SourcePath string `yaml:"SourcePath"`
    11  
    12  	// File extension of the source documents
    13  	// (anything with this extension will be read by docgen)
    14  	SourceExt string `yaml:"SourceExt"`
    15  
    16  	// Categories, templates, etc
    17  	Categories categories `yaml:"Categories"`
    18  
    19  	renderedCategories map[string][]string
    20  	renderedDocuments  map[string]map[string][]string
    21  }
    22  
    23  // Config is the global configuration for docgen
    24  var Config = new(config)
    25  
    26  type categories []category
    27  
    28  func (c *categories) ByID(catID string) *category {
    29  	for i := range *c {
    30  		if (*c)[i].ID == catID {
    31  			return &(*c)[i]
    32  		}
    33  	}
    34  	panic(fmt.Sprintf("no category found with ID '%s'", catID))
    35  }
    36  
    37  // ReadConfig loads a config file from disk
    38  func ReadConfig(path string) (err error) {
    39  	defer func() {
    40  		// Write a stack trace on error
    41  		if !Panic {
    42  			if r := recover(); r != nil {
    43  				err = fmt.Errorf("%s", r)
    44  			}
    45  		}
    46  	}()
    47  
    48  	parseSourceFile(path, Config)
    49  
    50  	for cat := range Config.Categories {
    51  		for i := range Config.Categories[cat].Templates {
    52  			updateConfig(&Config.Categories[cat].Templates[i], cat, i)
    53  		}
    54  	}
    55  
    56  	Config.renderedCategories = make(map[string][]string)
    57  	Config.renderedDocuments = make(map[string]map[string][]string)
    58  
    59  	return
    60  }
    61  
    62  func updateConfig(t *templates, cat int, i int) {
    63  	t.ref = &Config.Categories[cat]
    64  	t.index = i
    65  
    66  	if !strings.HasSuffix(t.OutputPath, "/") {
    67  		t.OutputPath += "/"
    68  	}
    69  
    70  	t.docTemplate = readTemplate(t.DocumentTemplate)
    71  	t.catTemplate = readTemplate(t.CategoryTemplate)
    72  }