github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/create/skeletons/skeletons.go (about) 1 // Copyright 2023 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 package skeletons 15 16 import ( 17 "bytes" 18 "embed" 19 "errors" 20 "io/fs" 21 "path/filepath" 22 "strings" 23 24 "github.com/gohugoio/hugo/helpers" 25 "github.com/gohugoio/hugo/parser" 26 "github.com/gohugoio/hugo/parser/metadecoders" 27 "github.com/spf13/afero" 28 ) 29 30 //go:embed all:site/* 31 var siteFs embed.FS 32 33 //go:embed all:theme/* 34 var themeFs embed.FS 35 36 // CreateTheme creates a theme skeleton. 37 func CreateTheme(createpath string, sourceFs afero.Fs) error { 38 if exists, _ := helpers.Exists(createpath, sourceFs); exists { 39 return errors.New(createpath + " already exists") 40 } 41 return copyFiles(createpath, sourceFs, themeFs) 42 } 43 44 // CreateSite creates a site skeleton. 45 func CreateSite(createpath string, sourceFs afero.Fs, force bool, format string) error { 46 format = strings.ToLower(format) 47 if exists, _ := helpers.Exists(createpath, sourceFs); exists { 48 if isDir, _ := helpers.IsDir(createpath, sourceFs); !isDir { 49 return errors.New(createpath + " already exists but not a directory") 50 } 51 52 isEmpty, _ := helpers.IsEmpty(createpath, sourceFs) 53 54 switch { 55 case !isEmpty && !force: 56 return errors.New(createpath + " already exists and is not empty. See --force.") 57 case !isEmpty && force: 58 var all []string 59 fs.WalkDir(siteFs, ".", func(path string, d fs.DirEntry, err error) error { 60 if d.IsDir() && path != "." { 61 all = append(all, path) 62 } 63 return nil 64 }) 65 all = append(all, filepath.Join(createpath, "hugo."+format)) 66 for _, path := range all { 67 if exists, _ := helpers.Exists(path, sourceFs); exists { 68 return errors.New(path + " already exists") 69 } 70 } 71 } 72 } 73 74 err := newSiteCreateConfig(sourceFs, createpath, format) 75 if err != nil { 76 return err 77 } 78 79 return copyFiles(createpath, sourceFs, siteFs) 80 } 81 82 func copyFiles(createpath string, sourceFs afero.Fs, skeleton embed.FS) error { 83 return fs.WalkDir(skeleton, ".", func(path string, d fs.DirEntry, err error) error { 84 _, slug, _ := strings.Cut(path, "/") 85 if d.IsDir() { 86 return sourceFs.MkdirAll(filepath.Join(createpath, slug), 0777) 87 } else { 88 if filepath.Base(path) != ".gitkeep" { 89 data, _ := fs.ReadFile(skeleton, path) 90 return helpers.WriteToDisk(filepath.Join(createpath, slug), bytes.NewReader(data), sourceFs) 91 } 92 return nil 93 } 94 }) 95 } 96 97 func newSiteCreateConfig(fs afero.Fs, createpath string, format string) (err error) { 98 in := map[string]string{ 99 "baseURL": "https://example.org/", 100 "title": "My New Hugo Site", 101 "languageCode": "en-us", 102 } 103 104 var buf bytes.Buffer 105 err = parser.InterfaceToConfig(in, metadecoders.FormatFromString(format), &buf) 106 if err != nil { 107 return err 108 } 109 110 return helpers.WriteToDisk(filepath.Join(createpath, "hugo."+format), &buf, fs) 111 }