github.com/SDLMoe/hugo@v0.47.1/tpl/tplimpl/embedded/generate/generate.go (about)

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