@@ -145,10 +145,7 @@ func main() {
145
145
Defeat (errors .New (errNoTitle ))
146
146
}
147
147
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 )
152
149
153
150
Victory (statusDone )
154
151
}
@@ -484,54 +481,21 @@ func buildTagPages(pages []*Page) *TagMap {
484
481
return tagMap
485
482
}
486
483
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
- "---\n date: %s\n title: %s\n tags: %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 ) {
503
485
tDir , err := getTargetDir (globalConfig , true )
504
486
if err != nil {
505
487
Defeat (err )
506
488
}
507
489
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 )
527
491
528
- cmd := exec .Command (editor , filePath )
529
- err = cmd .Run ()
492
+ err = open (page )
530
493
if err != nil {
531
494
Defeat (err )
532
495
}
533
496
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 )
535
499
}
536
500
537
501
// determineCommitMessage figures out which commit message to save the repo with
@@ -591,6 +555,20 @@ func loadPages() []*Page {
591
555
return pages
592
556
}
593
557
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
+
594
572
// pagesToHTMLUnorderedList creates the unordered list of page links that appear
595
573
// on the index and tag pages
596
574
func pagesToHTMLUnorderedList (pages []* Page ) string {
@@ -770,6 +748,27 @@ type Page struct {
770
748
Title string `yaml:"title"`
771
749
}
772
750
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
+
773
772
// CreatedAt returns a time instance representing when the page was created
774
773
func (page * Page ) CreatedAt () time.Time {
775
774
date , err := time .Parse (time .RFC3339 , page .Date )
@@ -789,6 +788,16 @@ func (page *Page) CreatedMonth() time.Month {
789
788
return page .CreatedAt ().Month ()
790
789
}
791
790
791
+ // FrontMatter returns the front-matter of the page
792
+ func (page * Page ) FrontMatter () string {
793
+ return fmt .Sprintf (
794
+ "---\n date: %s\n title: %s\n tags: %s\n ---\n \n " ,
795
+ page .Date ,
796
+ page .Title ,
797
+ page .TagsStr ,
798
+ )
799
+ }
800
+
792
801
// IsContentPage returns true if the page is a valid entry page, false if it is not
793
802
func (page * Page ) IsContentPage () bool {
794
803
return page .Title != ""
@@ -809,6 +818,17 @@ func (page *Page) PrettyDate() string {
809
818
return page .CreatedAt ().Format ("Jan 02, 2006" )
810
819
}
811
820
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
+
812
832
// Tags returns a slice of tags assigned to this page
813
833
func (page * Page ) Tags () []* Tag {
814
834
tags := []* Tag {}
0 commit comments