@@ -7,16 +7,15 @@ import (
7
7
"io/ioutil"
8
8
"log"
9
9
"os"
10
- "os/exec"
11
10
"path/filepath"
12
11
"strings"
13
12
"sync"
14
13
"time"
15
14
16
- "github.com/ericaro/frontmatter"
17
15
"github.com/go-git/go-git/v5"
18
16
"github.com/go-git/go-git/v5/plumbing/object"
19
17
"github.com/olebedev/config"
18
+ "github.com/senorprogrammer/til/pages"
20
19
"github.com/senorprogrammer/til/src"
21
20
)
22
21
@@ -60,6 +59,8 @@ func init() {
60
59
flag .StringVar (& targetDirFlag , "target" , "" , "specifies the target directory key" )
61
60
}
62
61
62
+ /* -------------------- Main -------------------- */
63
+
63
64
func main () {
64
65
flag .Parse ()
65
66
@@ -112,11 +113,12 @@ func main() {
112
113
func buildContent () {
113
114
pages := loadPages ()
114
115
tagMap := buildTagPages (pages )
116
+
115
117
buildIndexPage (pages , tagMap )
116
118
}
117
119
118
120
// 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 ) {
120
122
src .Info (statusIdxBuild )
121
123
122
124
content := ""
@@ -135,7 +137,7 @@ func buildIndexPage(pages []*src.Page, tagMap *src.TagMap) {
135
137
content += "\n "
136
138
137
139
// Write the page list into the middle of the page
138
- content += pagesToHTMLUnorderedList (pages )
140
+ content += pagesToHTMLUnorderedList (pageSet )
139
141
content += "\n "
140
142
141
143
// Write the footer content into the bottom of the index
@@ -151,7 +153,7 @@ func buildIndexPage(pages []*src.Page, tagMap *src.TagMap) {
151
153
filePath := fmt .Sprintf (
152
154
"%s/index.%s" ,
153
155
tDir ,
154
- src .FileExtension ,
156
+ pages .FileExtension ,
155
157
)
156
158
157
159
err = ioutil .WriteFile (filePath , []byte (content ), 0644 )
@@ -163,10 +165,10 @@ func buildIndexPage(pages []*src.Page, tagMap *src.TagMap) {
163
165
}
164
166
165
167
// 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 {
167
169
src .Info (statusTagBuild )
168
170
169
- tagMap := src .NewTagMap (pages )
171
+ tagMap := pages .NewTagMap (pageSet )
170
172
171
173
var wGroup sync.WaitGroup
172
174
@@ -195,7 +197,7 @@ func buildTagPages(pages []*src.Page) *src.TagMap {
195
197
"%s/%s.%s" ,
196
198
tDir ,
197
199
tagName ,
198
- src .FileExtension ,
200
+ pages .FileExtension ,
199
201
)
200
202
201
203
err = ioutil .WriteFile (filePath , []byte (content ), 0644 )
@@ -218,9 +220,9 @@ func createNewPage(title string) {
218
220
src .Defeat (err )
219
221
}
220
222
221
- page := src .NewPage (title , tDir )
223
+ page := pages .NewPage (title , tDir )
222
224
223
- err = open ( page )
225
+ err = page . Open ( defaultEditor )
224
226
if err != nil {
225
227
src .Defeat (err )
226
228
}
@@ -262,8 +264,8 @@ func listTargetDirectories(cfg *config.Config) {
262
264
263
265
// loadPages reads the page files from disk (in reverse chronological order) and
264
266
// creates Page instances from them
265
- func loadPages () []* src .Page {
266
- pages := []* src .Page {}
267
+ func loadPages () []* pages .Page {
268
+ pageSet := []* pages .Page {}
267
269
268
270
tDir , err := src .GetTargetDir (src .GlobalConfig , targetDirFlag , true )
269
271
if err != nil {
@@ -274,39 +276,39 @@ func loadPages() []*src.Page {
274
276
fmt .Sprintf (
275
277
"%s/*.%s" ,
276
278
tDir ,
277
- src .FileExtension ,
279
+ pages .FileExtension ,
278
280
),
279
281
)
280
282
281
283
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 )
284
286
}
285
287
286
- return pages
288
+ return pageSet
287
289
}
288
290
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
+ // }
296
298
297
- cmd := exec .Command (editor , page .FilePath )
298
- err := cmd .Run ()
299
+ // cmd := exec.Command(editor, page.FilePath)
300
+ // err := cmd.Run()
299
301
300
- return err
301
- }
302
+ // return err
303
+ // }
302
304
303
305
// pagesToHTMLUnorderedList creates the unordered list of page links that appear
304
306
// on the index and tag pages
305
- func pagesToHTMLUnorderedList (pages []* src .Page ) string {
307
+ func pagesToHTMLUnorderedList (pageSet []* pages .Page ) string {
306
308
content := ""
307
- prevPage := & src .Page {}
309
+ prevPage := & pages .Page {}
308
310
309
- for _ , page := range pages {
311
+ for _ , page := range pageSet {
310
312
if ! page .IsContentPage () {
311
313
continue
312
314
}
@@ -353,26 +355,6 @@ func push() {
353
355
}
354
356
}
355
357
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
-
376
358
// https://github.com/go-git/go-git/blob/master/_examples/commit/main.go
377
359
func save (commitMsg string ) {
378
360
src .Info (statusRepoSave )
0 commit comments