Skip to content

Commit c2fff9c

Browse files
More extraction into better groupings
Signed-off-by: Chris Cummer <[email protected]>
1 parent c34f37d commit c2fff9c

File tree

5 files changed

+110
-88
lines changed

5 files changed

+110
-88
lines changed

main.go

+32-50
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import (
77
"io/ioutil"
88
"log"
99
"os"
10-
"os/exec"
1110
"path/filepath"
1211
"strings"
1312
"sync"
1413
"time"
1514

16-
"github.com/ericaro/frontmatter"
1715
"github.com/go-git/go-git/v5"
1816
"github.com/go-git/go-git/v5/plumbing/object"
1917
"github.com/olebedev/config"
18+
"github.com/senorprogrammer/til/pages"
2019
"github.com/senorprogrammer/til/src"
2120
)
2221

@@ -60,6 +59,8 @@ func init() {
6059
flag.StringVar(&targetDirFlag, "target", "", "specifies the target directory key")
6160
}
6261

62+
/* -------------------- Main -------------------- */
63+
6364
func main() {
6465
flag.Parse()
6566

@@ -112,11 +113,12 @@ func main() {
112113
func buildContent() {
113114
pages := loadPages()
114115
tagMap := buildTagPages(pages)
116+
115117
buildIndexPage(pages, tagMap)
116118
}
117119

118120
// buildIndexPage creates the main index.md page that is the root of the site
119-
func buildIndexPage(pages []*src.Page, tagMap *src.TagMap) {
121+
func buildIndexPage(pageSet []*pages.Page, tagMap *pages.TagMap) {
120122
src.Info(statusIdxBuild)
121123

122124
content := ""
@@ -135,7 +137,7 @@ func buildIndexPage(pages []*src.Page, tagMap *src.TagMap) {
135137
content += "\n"
136138

137139
// Write the page list into the middle of the page
138-
content += pagesToHTMLUnorderedList(pages)
140+
content += pagesToHTMLUnorderedList(pageSet)
139141
content += "\n"
140142

141143
// Write the footer content into the bottom of the index
@@ -151,7 +153,7 @@ func buildIndexPage(pages []*src.Page, tagMap *src.TagMap) {
151153
filePath := fmt.Sprintf(
152154
"%s/index.%s",
153155
tDir,
154-
src.FileExtension,
156+
pages.FileExtension,
155157
)
156158

157159
err = ioutil.WriteFile(filePath, []byte(content), 0644)
@@ -163,10 +165,10 @@ func buildIndexPage(pages []*src.Page, tagMap *src.TagMap) {
163165
}
164166

165167
// buildTagPages creates the tag pages, with links to posts tagged with those names
166-
func buildTagPages(pages []*src.Page) *src.TagMap {
168+
func buildTagPages(pageSet []*pages.Page) *pages.TagMap {
167169
src.Info(statusTagBuild)
168170

169-
tagMap := src.NewTagMap(pages)
171+
tagMap := pages.NewTagMap(pageSet)
170172

171173
var wGroup sync.WaitGroup
172174

@@ -195,7 +197,7 @@ func buildTagPages(pages []*src.Page) *src.TagMap {
195197
"%s/%s.%s",
196198
tDir,
197199
tagName,
198-
src.FileExtension,
200+
pages.FileExtension,
199201
)
200202

201203
err = ioutil.WriteFile(filePath, []byte(content), 0644)
@@ -218,9 +220,9 @@ func createNewPage(title string) {
218220
src.Defeat(err)
219221
}
220222

221-
page := src.NewPage(title, tDir)
223+
page := pages.NewPage(title, tDir)
222224

223-
err = open(page)
225+
err = page.Open(defaultEditor)
224226
if err != nil {
225227
src.Defeat(err)
226228
}
@@ -262,8 +264,8 @@ func listTargetDirectories(cfg *config.Config) {
262264

263265
// loadPages reads the page files from disk (in reverse chronological order) and
264266
// creates Page instances from them
265-
func loadPages() []*src.Page {
266-
pages := []*src.Page{}
267+
func loadPages() []*pages.Page {
268+
pageSet := []*pages.Page{}
267269

268270
tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, true)
269271
if err != nil {
@@ -274,39 +276,39 @@ func loadPages() []*src.Page {
274276
fmt.Sprintf(
275277
"%s/*.%s",
276278
tDir,
277-
src.FileExtension,
279+
pages.FileExtension,
278280
),
279281
)
280282

281283
for i := len(filePaths) - 1; i >= 0; i-- {
282-
page := readPage(filePaths[i])
283-
pages = append(pages, page)
284+
page := pages.PageFromFilePath(filePaths[i])
285+
pageSet = append(pageSet, page)
284286
}
285287

286-
return pages
288+
return pageSet
287289
}
288290

289-
// open tll the OS to open the newly-created page in the editor (as specified in the config)
290-
// If there's no editor explicitly defined by the user, tell the OS to try and open it
291-
func open(page *src.Page) error {
292-
editor := src.GlobalConfig.UString("editor", defaultEditor)
293-
if editor == "" {
294-
editor = defaultEditor
295-
}
291+
// // open tll the OS to open the newly-created page in the editor (as specified in the config)
292+
// // If there's no editor explicitly defined by the user, tell the OS to try and open it
293+
// func open(page *src.Page) error {
294+
// editor := src.GlobalConfig.UString("editor", defaultEditor)
295+
// if editor == "" {
296+
// editor = defaultEditor
297+
// }
296298

297-
cmd := exec.Command(editor, page.FilePath)
298-
err := cmd.Run()
299+
// cmd := exec.Command(editor, page.FilePath)
300+
// err := cmd.Run()
299301

300-
return err
301-
}
302+
// return err
303+
// }
302304

303305
// pagesToHTMLUnorderedList creates the unordered list of page links that appear
304306
// on the index and tag pages
305-
func pagesToHTMLUnorderedList(pages []*src.Page) string {
307+
func pagesToHTMLUnorderedList(pageSet []*pages.Page) string {
306308
content := ""
307-
prevPage := &src.Page{}
309+
prevPage := &pages.Page{}
308310

309-
for _, page := range pages {
311+
for _, page := range pageSet {
310312
if !page.IsContentPage() {
311313
continue
312314
}
@@ -353,26 +355,6 @@ func push() {
353355
}
354356
}
355357

356-
// readPage reads the contents of the page and unmarshals it into the Page struct,
357-
// making the page's internal frontmatter programmatically accessible
358-
func readPage(filePath string) *src.Page {
359-
page := new(src.Page)
360-
361-
data, err := ioutil.ReadFile(filePath)
362-
if err != nil {
363-
src.Defeat(err)
364-
}
365-
366-
err = frontmatter.Unmarshal(data, page)
367-
if err != nil {
368-
src.Defeat(err)
369-
}
370-
371-
page.FilePath = filePath
372-
373-
return page
374-
}
375-
376358
// https://github.com/go-git/go-git/blob/master/_examples/commit/main.go
377359
func save(commitMsg string) {
378360
src.Info(statusRepoSave)

src/page.go renamed to pages/page.go

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
package src
1+
package pages
22

33
import (
44
"fmt"
55
"io/ioutil"
6+
"os/exec"
67
"path/filepath"
78
"strings"
89
"time"
10+
11+
"github.com/ericaro/frontmatter"
12+
"github.com/senorprogrammer/til/src"
913
)
1014

1115
const (
@@ -46,6 +50,25 @@ func NewPage(title string, targetDir string) *Page {
4650
return page
4751
}
4852

53+
// PageFromFilePath creates and returns a Page instance from a file path
54+
func PageFromFilePath(filePath string) *Page {
55+
page := new(Page)
56+
57+
data, err := ioutil.ReadFile(filePath)
58+
if err != nil {
59+
src.Defeat(err)
60+
}
61+
62+
err = frontmatter.Unmarshal(data, page)
63+
if err != nil {
64+
src.Defeat(err)
65+
}
66+
67+
page.FilePath = filePath
68+
69+
return page
70+
}
71+
4972
// CreatedAt returns a time instance representing when the page was created
5073
func (page *Page) CreatedAt() time.Time {
5174
date, err := time.Parse(time.RFC3339, page.Date)
@@ -90,6 +113,20 @@ func (page *Page) Link() string {
90113
)
91114
}
92115

116+
// Open tll the OS to open the newly-created page in the editor (as specified in the config)
117+
// If there's no editor explicitly defined by the user, tell the OS to try and open it
118+
func (page *Page) Open(defaultEditor string) error {
119+
editor := src.GlobalConfig.UString("editor", defaultEditor)
120+
if editor == "" {
121+
editor = defaultEditor
122+
}
123+
124+
cmd := exec.Command(editor, page.FilePath)
125+
err := cmd.Run()
126+
127+
return err
128+
}
129+
93130
// PrettyDate returns a human-friendly representation of the CreatedAt date
94131
func (page *Page) PrettyDate() string {
95132
return page.CreatedAt().Format("Jan 02, 2006")
@@ -102,7 +139,7 @@ func (page *Page) Save() {
102139

103140
err := ioutil.WriteFile(page.FilePath, []byte(pageSrc), 0644)
104141
if err != nil {
105-
Defeat(err)
142+
src.Defeat(err)
106143
}
107144
}
108145

src/tag.go renamed to pages/tag.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package src
1+
package pages
22

33
import (
44
"fmt"

src/tag_map.go renamed to pages/tag_map.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
package src
1+
package pages
22

3-
import "sort"
3+
import (
4+
"sort"
5+
)
46

57
// TagMap is a map of tag name to Tag instance
68
type TagMap struct {
79
Tags map[string][]*Tag
810
}
911

1012
// NewTagMap creates and returns an instance of TagMap
11-
func NewTagMap(pages []*Page) *TagMap {
13+
func NewTagMap(pageSet []*Page) *TagMap {
1214
tm := &TagMap{
1315
Tags: make(map[string][]*Tag),
1416
}
1517

16-
tm.BuildFromPages(pages)
18+
tm.BuildFromPages(pageSet)
1719

1820
return tm
1921
}

0 commit comments

Comments
 (0)