github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/tpl/tplimpl/embedded/generate/generate.go (about)

     1  // Copyright 2019 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  //go:generate go run generate.go
    15  
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"io/ioutil"
    21  	"log"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  )
    26  
    27  func main() {
    28  	templateFolder := filepath.Join("..", "templates")
    29  
    30  	templatePath := filepath.Join(".", templateFolder)
    31  
    32  	file, err := os.Create("../templates.autogen.go")
    33  	if err != nil {
    34  		log.Fatal(err)
    35  	}
    36  	defer file.Close()
    37  
    38  	var nameValues []string
    39  
    40  	err = filepath.Walk(templatePath, func(path string, info os.FileInfo, err error) error {
    41  		if err != nil {
    42  			return err
    43  		}
    44  
    45  		if info.IsDir() {
    46  			return nil
    47  		}
    48  		if strings.HasPrefix(info.Name(), ".") {
    49  			return nil
    50  		}
    51  
    52  		templateName := filepath.ToSlash(strings.TrimPrefix(path, templateFolder+string(os.PathSeparator)))
    53  
    54  		templateContent, err := ioutil.ReadFile(path)
    55  		if err != nil {
    56  			return err
    57  		}
    58  
    59  		nameValues = append(nameValues, nameValue(templateName, string(templateContent)))
    60  
    61  		return nil
    62  	})
    63  
    64  	if err != nil {
    65  		log.Fatal(err)
    66  	}
    67  
    68  	fmt.Fprint(file, `// Copyright 2019 The Hugo Authors. All rights reserved.
    69  //
    70  // Licensed under the Apache License, Version 2.0 (the "License");
    71  // you may not use this file except in compliance with the License.
    72  // You may obtain a copy of the License at
    73  // http://www.apache.org/licenses/LICENSE-2.0
    74  //
    75  // Unless required by applicable law or agreed to in writing, software
    76  // distributed under the License is distributed on an "AS IS" BASIS,
    77  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    78  // See the License for the specific language governing permissions and
    79  // limitations under the License.
    80  
    81  // This file is autogenerated.
    82  
    83  // Package embedded defines the internal templates that Hugo provides.
    84  package embedded
    85  
    86  // EmbeddedTemplates represents all embedded templates.
    87  var EmbeddedTemplates = [][2]string{
    88  `)
    89  
    90  	for _, v := range nameValues {
    91  		fmt.Fprint(file, "	", v, ",\n")
    92  	}
    93  	fmt.Fprint(file, "}\n")
    94  }
    95  
    96  func nameValue(name, value string) string {
    97  	return fmt.Sprintf("{`%s`, `%s`}", name, value)
    98  }