github.com/gohugoio/hugo@v0.88.1/output/docshelper.go (about)

     1  package output
     2  
     3  import (
     4  	"strings"
     5  
     6  	//	"fmt"
     7  
     8  	"github.com/gohugoio/hugo/docshelper"
     9  )
    10  
    11  // This is is just some helpers used to create some JSON used in the Hugo docs.
    12  func init() {
    13  	docsProvider := func() docshelper.DocProvider {
    14  		return docshelper.DocProvider{
    15  			"output": map[string]interface{}{
    16  				"formats": DefaultFormats,
    17  				"layouts": createLayoutExamples(),
    18  			},
    19  		}
    20  	}
    21  
    22  	docshelper.AddDocProviderFunc(docsProvider)
    23  }
    24  
    25  func createLayoutExamples() interface{} {
    26  	type Example struct {
    27  		Example      string
    28  		Kind         string
    29  		OutputFormat string
    30  		Suffix       string
    31  		Layouts      []string `json:"Template Lookup Order"`
    32  	}
    33  
    34  	var (
    35  		basicExamples []Example
    36  		demoLayout    = "demolayout"
    37  		demoType      = "demotype"
    38  	)
    39  
    40  	for _, example := range []struct {
    41  		name string
    42  		d    LayoutDescriptor
    43  		f    Format
    44  	}{
    45  		// Taxonomy output.LayoutDescriptor={categories category taxonomy en  false Type Section
    46  		{"Single page in \"posts\" section", LayoutDescriptor{Kind: "page", Type: "posts"}, HTMLFormat},
    47  		{"Base template for single page in \"posts\" section", LayoutDescriptor{Baseof: true, Kind: "page", Type: "posts"}, HTMLFormat},
    48  		{"Single page in \"posts\" section with layout set", LayoutDescriptor{Kind: "page", Type: "posts", Layout: demoLayout}, HTMLFormat},
    49  		{"Base template for single page in \"posts\" section with layout set", LayoutDescriptor{Baseof: true, Kind: "page", Type: "posts", Layout: demoLayout}, HTMLFormat},
    50  		{"AMP single page", LayoutDescriptor{Kind: "page", Type: "posts"}, AMPFormat},
    51  		{"AMP single page, French language", LayoutDescriptor{Kind: "page", Type: "posts", Lang: "fr"}, AMPFormat},
    52  		// All section or typeless pages gets "page" as type
    53  		{"Home page", LayoutDescriptor{Kind: "home", Type: "page"}, HTMLFormat},
    54  		{"Base template for home page", LayoutDescriptor{Baseof: true, Kind: "home", Type: "page"}, HTMLFormat},
    55  		{"Home page with type set", LayoutDescriptor{Kind: "home", Type: demoType}, HTMLFormat},
    56  		{"Base template for home page with type set", LayoutDescriptor{Baseof: true, Kind: "home", Type: demoType}, HTMLFormat},
    57  		{"Home page with layout set", LayoutDescriptor{Kind: "home", Type: "page", Layout: demoLayout}, HTMLFormat},
    58  		{"AMP home, French language", LayoutDescriptor{Kind: "home", Type: "page", Lang: "fr"}, AMPFormat},
    59  		{"JSON home", LayoutDescriptor{Kind: "home", Type: "page"}, JSONFormat},
    60  		{"RSS home", LayoutDescriptor{Kind: "home", Type: "page"}, RSSFormat},
    61  		{"RSS section posts", LayoutDescriptor{Kind: "section", Type: "posts"}, RSSFormat},
    62  		{"Taxonomy in categories", LayoutDescriptor{Kind: "taxonomy", Type: "categories", Section: "category"}, RSSFormat},
    63  		{"Term in categories", LayoutDescriptor{Kind: "term", Type: "categories", Section: "category"}, RSSFormat},
    64  		{"Section list for \"posts\" section", LayoutDescriptor{Kind: "section", Type: "posts", Section: "posts"}, HTMLFormat},
    65  		{"Section list for \"posts\" section with type set to \"blog\"", LayoutDescriptor{Kind: "section", Type: "blog", Section: "posts"}, HTMLFormat},
    66  		{"Section list for \"posts\" section with layout set to \"demoLayout\"", LayoutDescriptor{Kind: "section", Layout: demoLayout, Section: "posts"}, HTMLFormat},
    67  
    68  		{"Taxonomy list in categories", LayoutDescriptor{Kind: "taxonomy", Type: "categories", Section: "category"}, HTMLFormat},
    69  		{"Taxonomy term in categories", LayoutDescriptor{Kind: "term", Type: "categories", Section: "category"}, HTMLFormat},
    70  	} {
    71  
    72  		l := NewLayoutHandler()
    73  		layouts, _ := l.For(example.d, example.f)
    74  
    75  		basicExamples = append(basicExamples, Example{
    76  			Example:      example.name,
    77  			Kind:         example.d.Kind,
    78  			OutputFormat: example.f.Name,
    79  			Suffix:       example.f.MediaType.FirstSuffix.Suffix,
    80  			Layouts:      makeLayoutsPresentable(layouts),
    81  		})
    82  	}
    83  
    84  	return basicExamples
    85  }
    86  
    87  func makeLayoutsPresentable(l []string) []string {
    88  	var filtered []string
    89  	for _, ll := range l {
    90  		if strings.Contains(ll, "page/") {
    91  			// This is a valid lookup, but it's more confusing than useful.
    92  			continue
    93  		}
    94  		ll = "layouts/" + strings.TrimPrefix(ll, "_text/")
    95  
    96  		if !strings.Contains(ll, "indexes") {
    97  			filtered = append(filtered, ll)
    98  		}
    99  	}
   100  
   101  	return filtered
   102  }