Skip to content

Commit 112857b

Browse files
Page creation is now the providence of the Page struct
Signed-off-by: Chris Cummer <[email protected]>
1 parent d561c42 commit 112857b

File tree

1 file changed

+62
-42
lines changed

1 file changed

+62
-42
lines changed

til.go

+62-42
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,7 @@ func main() {
145145
Defeat(errors.New(errNoTitle))
146146
}
147147

148-
pagePath := createNewPage(title)
149-
150-
// Write the pagePath to the console. This makes it easy to know which file we just created
151-
Info(pagePath)
148+
createNewPage(title)
152149

153150
Victory(statusDone)
154151
}
@@ -484,54 +481,21 @@ func buildTagPages(pages []*Page) *TagMap {
484481
return tagMap
485482
}
486483

487-
func createNewPage(title string) string {
488-
date := time.Now()
489-
pathDate := date.Format(ghFriendlyDateFormat)
490-
491-
// Front matter lives at the top of the generated file and contains bits of info about the page
492-
// This is loosely based on the format Hugo uses
493-
frontMatter := fmt.Sprintf(
494-
"---\ndate: %s\ntitle: %s\ntags: %s\n---\n\n",
495-
date.Format(time.RFC3339),
496-
title,
497-
"",
498-
)
499-
500-
content := frontMatter + fmt.Sprintf("# %s\n\n\n", title)
501-
502-
// Write out the stub file, explode if we can't do that
484+
func createNewPage(title string) {
503485
tDir, err := getTargetDir(globalConfig, true)
504486
if err != nil {
505487
Defeat(err)
506488
}
507489

508-
filePath := fmt.Sprintf(
509-
"%s/%s-%s.%s",
510-
tDir,
511-
pathDate,
512-
strings.ReplaceAll(strings.ToLower(title), " ", "-"),
513-
fileExtension,
514-
)
515-
516-
err = ioutil.WriteFile(filePath, []byte(content), 0644)
517-
if err != nil {
518-
Defeat(err)
519-
}
520-
521-
// Tell the OS to open the newly-created page in the editor (as specified in the config)
522-
// If there's no editor explicitly defined by the user, tell the OS to try and open it
523-
editor := globalConfig.UString("editor", defaultEditor)
524-
if editor == "" {
525-
editor = defaultEditor
526-
}
490+
page := NewPage(title, tDir)
527491

528-
cmd := exec.Command(editor, filePath)
529-
err = cmd.Run()
492+
err = open(page)
530493
if err != nil {
531494
Defeat(err)
532495
}
533496

534-
return filePath
497+
// Write the page path to the console. This makes it easy to know which file we just created
498+
Info(page.FilePath)
535499
}
536500

537501
// determineCommitMessage figures out which commit message to save the repo with
@@ -591,6 +555,20 @@ func loadPages() []*Page {
591555
return pages
592556
}
593557

558+
// open tll the OS to open the newly-created page in the editor (as specified in the config)
559+
// If there's no editor explicitly defined by the user, tell the OS to try and open it
560+
func open(page *Page) error {
561+
editor := globalConfig.UString("editor", defaultEditor)
562+
if editor == "" {
563+
editor = defaultEditor
564+
}
565+
566+
cmd := exec.Command(editor, page.FilePath)
567+
err := cmd.Run()
568+
569+
return err
570+
}
571+
594572
// pagesToHTMLUnorderedList creates the unordered list of page links that appear
595573
// on the index and tag pages
596574
func pagesToHTMLUnorderedList(pages []*Page) string {
@@ -770,6 +748,27 @@ type Page struct {
770748
Title string `yaml:"title"`
771749
}
772750

751+
// NewPage creates and returns an instance of page
752+
func NewPage(title string, targetDir string) *Page {
753+
date := time.Now()
754+
755+
page := &Page{
756+
Date: date.Format(time.RFC3339),
757+
FilePath: fmt.Sprintf(
758+
"%s/%s-%s.%s",
759+
targetDir,
760+
date.Format(ghFriendlyDateFormat),
761+
strings.ReplaceAll(strings.ToLower(title), " ", "-"),
762+
fileExtension,
763+
),
764+
Title: title,
765+
}
766+
767+
page.Save()
768+
769+
return page
770+
}
771+
773772
// CreatedAt returns a time instance representing when the page was created
774773
func (page *Page) CreatedAt() time.Time {
775774
date, err := time.Parse(time.RFC3339, page.Date)
@@ -789,6 +788,16 @@ func (page *Page) CreatedMonth() time.Month {
789788
return page.CreatedAt().Month()
790789
}
791790

791+
// FrontMatter returns the front-matter of the page
792+
func (page *Page) FrontMatter() string {
793+
return fmt.Sprintf(
794+
"---\ndate: %s\ntitle: %s\ntags: %s\n---\n\n",
795+
page.Date,
796+
page.Title,
797+
page.TagsStr,
798+
)
799+
}
800+
792801
// IsContentPage returns true if the page is a valid entry page, false if it is not
793802
func (page *Page) IsContentPage() bool {
794803
return page.Title != ""
@@ -809,6 +818,17 @@ func (page *Page) PrettyDate() string {
809818
return page.CreatedAt().Format("Jan 02, 2006")
810819
}
811820

821+
// Save writes the content of the page to file
822+
func (page *Page) Save() {
823+
pageSrc := page.FrontMatter()
824+
pageSrc += fmt.Sprintf("# %s\n\n", page.Title)
825+
826+
err := ioutil.WriteFile(page.FilePath, []byte(pageSrc), 0644)
827+
if err != nil {
828+
Defeat(err)
829+
}
830+
}
831+
812832
// Tags returns a slice of tags assigned to this page
813833
func (page *Page) Tags() []*Tag {
814834
tags := []*Tag{}

0 commit comments

Comments
 (0)