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

     1  package docgen
     2  
     3  import (
     4  	"sort"
     5  	"text/template"
     6  )
     7  
     8  // category is the groupings for documents
     9  type category struct {
    10  	ID string `yaml:"ID"`
    11  
    12  	// Name of the category
    13  	Title string `yaml:"Title"`
    14  
    15  	// Description of the category
    16  	Description string `yaml:"Description"`
    17  
    18  	Templates []templates `yaml:"Templates"`
    19  }
    20  
    21  type templates struct {
    22  	// OutputPath to write the rendered documents
    23  	OutputPath string `yaml:"OutputPath"`
    24  
    25  	// CategoryFile is the file name (and path relative to OutputPath) of the
    26  	// category "index" file
    27  	CategoryFile string `yaml:"CategoryFile"`
    28  
    29  	// OutputExt is the file extension of the rendered documents
    30  	// (this is not applied to the category file)
    31  	OutputExt string `yaml:"OutputExt"`
    32  
    33  	// Document template for the documents
    34  	DocumentTemplate string `yaml:"DocumentTemplate"`
    35  
    36  	// Category template for the category (like an index.html type page)
    37  	CategoryTemplate string `yaml:"CategoryTemplate"`
    38  
    39  	docTemplate *template.Template
    40  	catTemplate *template.Template
    41  	ref         *category
    42  	index       int
    43  }
    44  
    45  // CategoryPath is the file name and path to write the category index file to
    46  func (t templates) CategoryFilePath() string {
    47  	return t.OutputPath + t.CategoryFile
    48  }
    49  
    50  func (t templates) CategoryValues(docs documents) *categoryValues {
    51  	var (
    52  		dv sortableDocumentValues
    53  		dt sortableDocumentDateTime
    54  	)
    55  
    56  	for i := range docs {
    57  		if docs[i].CategoryID == t.ref.ID {
    58  			dv = append(dv, t.DocumentValues(&docs[i], docs, true))
    59  			dt = append(dt, t.DocumentValues(&docs[i], docs, true))
    60  		}
    61  	}
    62  
    63  	sort.Sort(dv)
    64  	sort.Sort(dt)
    65  
    66  	return &categoryValues{
    67  		ID:          t.ref.ID,
    68  		Title:       t.ref.Title,
    69  		Description: t.ref.Description,
    70  		Documents:   dv,
    71  		DateTime:    dt,
    72  	}
    73  }
    74  
    75  type categoryValues struct {
    76  	ID          string
    77  	Title       string
    78  	Description string
    79  	Documents   []*documentValues
    80  	DateTime    []*documentValues
    81  }