func NewContent(kind, name string) (err error) { jww.INFO.Println("attempting to create ", name, "of", kind) location := FindArchetype(kind) var by []byte if location != "" { by, err = ioutil.ReadFile(location) if err != nil { jww.ERROR.Println(err) } } if location == "" || err != nil { by = []byte("+++\n title = \"title\"\n draft = true \n+++\n") } psr, err := parser.ReadFrom(bytes.NewReader(by)) if err != nil { return err } metadata, err := psr.Metadata() if err != nil { return err } newmetadata, err := cast.ToStringMapE(metadata) if err != nil { jww.ERROR.Println("Error processing archetype file:", location) return err } for k := range newmetadata { switch strings.ToLower(k) { case "date": newmetadata[k] = time.Now() case "title": newmetadata[k] = helpers.MakeTitle(helpers.Filename(name)) } } caseimatch := func(m map[string]interface{}, key string) bool { for k := range m { if strings.ToLower(k) == strings.ToLower(key) { return true } } return false } if newmetadata == nil { newmetadata = make(map[string]interface{}) } if !caseimatch(newmetadata, "date") { newmetadata["date"] = time.Now() } if !caseimatch(newmetadata, "title") { newmetadata["title"] = helpers.MakeTitle(helpers.Filename(name)) } page, err := hugolib.NewPage(name) if err != nil { return err } if x := parser.FormatSanitize(viper.GetString("MetaDataFormat")); x == "json" || x == "yaml" || x == "toml" { newmetadata["date"] = time.Now().Format(time.RFC3339) } //page.Dir = viper.GetString("sourceDir") page.SetSourceMetaData(newmetadata, parser.FormatToLeadRune(viper.GetString("MetaDataFormat"))) page.SetSourceContent(psr.Content()) if err = page.SafeSaveSourceAs(filepath.Join(viper.GetString("contentDir"), name)); err != nil { return } jww.FEEDBACK.Println(helpers.AbsPathify(filepath.Join(viper.GetString("contentDir"), name)), "created") editor := viper.GetString("NewContentEditor") if editor != "" { jww.FEEDBACK.Printf("Editing %s in %s.\n", name, editor) cmd := exec.Command(editor, path.Join(viper.GetString("contentDir"), name)) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err = cmd.Run(); err != nil { return } } return nil }
func (t *GoHTMLTemplate) loadTemplates(absPath string, prefix string) { walker := func(path string, fi os.FileInfo, err error) error { if err != nil { return nil } if fi.Mode()&os.ModeSymlink == os.ModeSymlink { link, err := filepath.EvalSymlinks(absPath) if err != nil { jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", absPath, err) return nil } linkfi, err := os.Stat(link) if err != nil { jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err) return nil } if !linkfi.Mode().IsRegular() { jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", absPath) } return nil } if !fi.IsDir() { if isDotFile(path) || isBackupFile(path) || isBaseTemplate(path) { return nil } tplName := t.GenerateTemplateNameFrom(absPath, path) if prefix != "" { tplName = strings.Trim(prefix, "/") + "/" + tplName } var baseTemplatePath string // ACE templates may have both a base and inner template. if filepath.Ext(path) == ".ace" && !strings.HasSuffix(filepath.Dir(path), "partials") { // This may be a view that shouldn't have base template // Have to look inside it to make sure needsBase, err := helpers.FileContains(path, aceTemplateInnerMarker, hugofs.OsFs) if err != nil { return err } if needsBase { // Look for base template in the follwing order: // 1. <current-path>/<template-name>-baseof.ace, e.g. list-baseof.ace. // 2. <current-path>/baseof.ace // 3. _default/<template-name>-baseof.ace, e.g. list-baseof.ace. // 4. _default/baseof.ace // 5. <themedir>/layouts/_default/<template-name>-baseof.ace // 6. <themedir>/layouts/_default/baseof.ace currBaseAceFilename := fmt.Sprintf("%s-%s", helpers.Filename(path), baseAceFilename) templateDir := filepath.Dir(path) themeDir := helpers.GetThemeDir() pathsToCheck := []string{ filepath.Join(templateDir, currBaseAceFilename), filepath.Join(templateDir, baseAceFilename), filepath.Join(absPath, "_default", currBaseAceFilename), filepath.Join(absPath, "_default", baseAceFilename), filepath.Join(themeDir, "layouts", "_default", currBaseAceFilename), filepath.Join(themeDir, "layouts", "_default", baseAceFilename), } for _, pathToCheck := range pathsToCheck { if ok, err := helpers.Exists(pathToCheck, hugofs.OsFs); err == nil && ok { baseTemplatePath = pathToCheck break } } } } t.AddTemplateFile(tplName, baseTemplatePath, path) } return nil } filepath.Walk(absPath, walker) }
// Filename without extension func (f *File) BaseFileName() string { return helpers.Filename(f.LogicalName()) }