github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/output/docshelper.go (about) 1 package output 2 3 import ( 4 "strings" 5 6 // "fmt" 7 8 "github.com/gohugoio/hugo/docshelper" 9 "github.com/gohugoio/hugo/output/layouts" 10 ) 11 12 // This is is just some helpers used to create some JSON used in the Hugo docs. 13 func init() { 14 docsProvider := func() docshelper.DocProvider { 15 return docshelper.DocProvider{ 16 "output": map[string]any{ 17 "layouts": createLayoutExamples(), 18 }, 19 } 20 } 21 22 docshelper.AddDocProviderFunc(docsProvider) 23 } 24 25 func createLayoutExamples() any { 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 layouts.LayoutDescriptor 43 }{ 44 45 // Taxonomy layouts.LayoutDescriptor={categories category taxonomy en false Type Section 46 {"Single page in \"posts\" section", layouts.LayoutDescriptor{Kind: "page", Type: "posts", OutputFormatName: "html", Suffix: "html"}}, 47 {"Base template for single page in \"posts\" section", layouts.LayoutDescriptor{Baseof: true, Kind: "page", Type: "posts", OutputFormatName: "html", Suffix: "html"}}, 48 {"Single page in \"posts\" section with layout set to \"demolayout\"", layouts.LayoutDescriptor{Kind: "page", Type: "posts", Layout: demoLayout, OutputFormatName: "html", Suffix: "html"}}, 49 {"Base template for single page in \"posts\" section with layout set to \"demolayout\"", layouts.LayoutDescriptor{Baseof: true, Kind: "page", Type: "posts", Layout: demoLayout, OutputFormatName: "html", Suffix: "html"}}, 50 {"AMP single page", layouts.LayoutDescriptor{Kind: "page", Type: "posts", OutputFormatName: "amp", Suffix: "html"}}, 51 {"AMP single page, French language", layouts.LayoutDescriptor{Kind: "page", Type: "posts", Lang: "fr", OutputFormatName: "html", Suffix: "html"}}, 52 // Typeless pages get "page" as type 53 {"Home page", layouts.LayoutDescriptor{Kind: "home", Type: "page", OutputFormatName: "html", Suffix: "html"}}, 54 {"Base template for home page", layouts.LayoutDescriptor{Baseof: true, Kind: "home", Type: "page", OutputFormatName: "html", Suffix: "html"}}, 55 {"Home page with type set to \"demotype\"", layouts.LayoutDescriptor{Kind: "home", Type: demoType, OutputFormatName: "html", Suffix: "html"}}, 56 {"Base template for home page with type set to \"demotype\"", layouts.LayoutDescriptor{Baseof: true, Kind: "home", Type: demoType, OutputFormatName: "html", Suffix: "html"}}, 57 {"Home page with layout set to \"demolayout\"", layouts.LayoutDescriptor{Kind: "home", Type: "page", Layout: demoLayout, OutputFormatName: "html", Suffix: "html"}}, 58 {"AMP home, French language", layouts.LayoutDescriptor{Kind: "home", Type: "page", Lang: "fr", OutputFormatName: "amp", Suffix: "html"}}, 59 {"JSON home", layouts.LayoutDescriptor{Kind: "home", Type: "page", OutputFormatName: "json", Suffix: "json"}}, 60 {"RSS home", layouts.LayoutDescriptor{Kind: "home", Type: "page", OutputFormatName: "rss", Suffix: "xml"}}, 61 62 {"Section list for \"posts\"", layouts.LayoutDescriptor{Kind: "section", Type: "posts", Section: "posts", OutputFormatName: "html", Suffix: "html"}}, 63 {"Section list for \"posts\" with type set to \"blog\"", layouts.LayoutDescriptor{Kind: "section", Type: "blog", Section: "posts", OutputFormatName: "html", Suffix: "html"}}, 64 {"Section list for \"posts\" with layout set to \"demolayout\"", layouts.LayoutDescriptor{Kind: "section", Layout: demoLayout, Section: "posts", OutputFormatName: "html", Suffix: "html"}}, 65 {"Section list for \"posts\"", layouts.LayoutDescriptor{Kind: "section", Type: "posts", OutputFormatName: "rss", Suffix: "xml"}}, 66 67 {"Taxonomy list for \"categories\"", layouts.LayoutDescriptor{Kind: "taxonomy", Type: "categories", Section: "category", OutputFormatName: "html", Suffix: "html"}}, 68 {"Taxonomy list for \"categories\"", layouts.LayoutDescriptor{Kind: "taxonomy", Type: "categories", Section: "category", OutputFormatName: "rss", Suffix: "xml"}}, 69 70 {"Term list for \"categories\"", layouts.LayoutDescriptor{Kind: "term", Type: "categories", Section: "category", OutputFormatName: "html", Suffix: "html"}}, 71 {"Term list for \"categories\"", layouts.LayoutDescriptor{Kind: "term", Type: "categories", Section: "category", OutputFormatName: "rss", Suffix: "xml"}}, 72 } { 73 74 l := layouts.NewLayoutHandler() 75 layouts, _ := l.For(example.d) 76 77 basicExamples = append(basicExamples, Example{ 78 Example: example.name, 79 Kind: example.d.Kind, 80 OutputFormat: example.d.OutputFormatName, 81 Suffix: example.d.Suffix, 82 Layouts: makeLayoutsPresentable(layouts), 83 }) 84 } 85 86 return basicExamples 87 } 88 89 func makeLayoutsPresentable(l []string) []string { 90 var filtered []string 91 for _, ll := range l { 92 if strings.Contains(ll, "page/") { 93 // This is a valid lookup, but it's more confusing than useful. 94 continue 95 } 96 ll = "layouts/" + strings.TrimPrefix(ll, "_text/") 97 98 if !strings.Contains(ll, "indexes") { 99 filtered = append(filtered, ll) 100 } 101 } 102 103 return filtered 104 }