github.com/fighterlyt/hugo@v0.47.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() map[string]interface{} {
    14  		docs := make(map[string]interface{})
    15  
    16  		docs["formats"] = DefaultFormats
    17  		docs["layouts"] = createLayoutExamples()
    18  		return docs
    19  	}
    20  
    21  	docshelper.AddDocProvider("output", docsProvider)
    22  }
    23  
    24  func createLayoutExamples() interface{} {
    25  
    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  		{"Single page in \"posts\" section with layout set", LayoutDescriptor{Kind: "page", Type: "posts", Layout: demoLayout}, HTMLFormat},
    48  		{"AMP single page", LayoutDescriptor{Kind: "page", Type: "posts"}, AMPFormat},
    49  		{"AMP single page, French language", LayoutDescriptor{Kind: "page", Type: "posts", Lang: "fr"}, AMPFormat},
    50  		// All section or typeless pages gets "page" as type
    51  		{"Home page", LayoutDescriptor{Kind: "home", Type: "page"}, HTMLFormat},
    52  		{"Home page with type set", LayoutDescriptor{Kind: "home", Type: demoType}, HTMLFormat},
    53  		{"Home page with layout set", LayoutDescriptor{Kind: "home", Type: "page", Layout: demoLayout}, HTMLFormat},
    54  		{`AMP home, French language"`, LayoutDescriptor{Kind: "home", Type: "page", Lang: "fr"}, AMPFormat},
    55  		{"JSON home", LayoutDescriptor{Kind: "home", Type: "page"}, JSONFormat},
    56  		{"RSS home", LayoutDescriptor{Kind: "home", Type: "page"}, RSSFormat},
    57  		{"RSS section posts", LayoutDescriptor{Kind: "section", Type: "posts"}, RSSFormat},
    58  		{"Taxonomy list in categories", LayoutDescriptor{Kind: "taxonomy", Type: "categories", Section: "category"}, RSSFormat},
    59  		{"Taxonomy terms in categories", LayoutDescriptor{Kind: "taxonomyTerm", Type: "categories", Section: "category"}, RSSFormat},
    60  		{"Section list for \"posts\" section", LayoutDescriptor{Kind: "section", Type: "posts", Section: "posts"}, HTMLFormat},
    61  		{"Section list for \"posts\" section with type set to \"blog\"", LayoutDescriptor{Kind: "section", Type: "blog", Section: "posts"}, HTMLFormat},
    62  		{"Section list for \"posts\" section with layout set to \"demoLayout\"", LayoutDescriptor{Kind: "section", Layout: demoLayout, Section: "posts"}, HTMLFormat},
    63  
    64  		{"Taxonomy list in categories", LayoutDescriptor{Kind: "taxonomy", Type: "categories", Section: "category"}, HTMLFormat},
    65  		{"Taxonomy term in categories", LayoutDescriptor{Kind: "taxonomyTerm", Type: "categories", Section: "category"}, HTMLFormat},
    66  	} {
    67  
    68  		l := NewLayoutHandler()
    69  		layouts, _ := l.For(example.d, example.f)
    70  
    71  		basicExamples = append(basicExamples, Example{
    72  			Example:      example.name,
    73  			Kind:         example.d.Kind,
    74  			OutputFormat: example.f.Name,
    75  			Suffix:       example.f.MediaType.Suffix(),
    76  			Layouts:      makeLayoutsPresentable(layouts)})
    77  	}
    78  
    79  	return basicExamples
    80  
    81  }
    82  
    83  func makeLayoutsPresentable(l []string) []string {
    84  	var filtered []string
    85  	for _, ll := range l {
    86  		if strings.Contains(ll, "page/") {
    87  			// This is a valid lookup, but it's more confusing than useful.
    88  			continue
    89  		}
    90  		ll = "layouts/" + strings.TrimPrefix(ll, "_text/")
    91  
    92  		if !strings.Contains(ll, "indexes") {
    93  			filtered = append(filtered, ll)
    94  		}
    95  	}
    96  
    97  	return filtered
    98  }