Skip to content

Commit e34a79a

Browse files
TIL-7 Tags into pages
Signed-off-by: Chris Cummer <[email protected]>
1 parent 4886087 commit e34a79a

File tree

1 file changed

+85
-13
lines changed

1 file changed

+85
-13
lines changed

til.go

+85-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12+
"regexp"
1213
"sort"
1314
"strings"
1415
"sync"
@@ -383,25 +384,41 @@ func getTargetDir(cfg *config.Config, withDocsDir bool) (string, error) {
383384

384385
func buildContent() {
385386
pages := loadPages()
387+
buildContentPages(pages)
386388
tagMap := buildTagPages(pages)
387389
buildIndexPage(pages, tagMap)
388390
}
389391

392+
// buildContentPages loops through all the pages and tells them to save themselves
393+
// to disk. This process writes any auto-generated content into the pages
394+
func buildContentPages(pages []*Page) {
395+
for _, page := range pages {
396+
if page.IsContentPage() {
397+
page.AppendTagsToContent()
398+
page.Save()
399+
}
400+
}
401+
}
402+
390403
// buildIndexPage creates the main index.md page that is the root of the site
391404
func buildIndexPage(pages []*Page, tagMap *TagMap) {
392405
Info(statusIdxBuild)
393406

394407
content := ""
395408

396409
// Write the tag list into the top of the index
397-
for _, tag := range tagMap.SortedTagNames() {
398-
content += fmt.Sprintf(
399-
"[%s](%s), ",
400-
tag,
401-
fmt.Sprintf("./%s", tag),
402-
)
410+
tagLinks := []string{}
411+
412+
for _, tagName := range tagMap.SortedTagNames() {
413+
tags := tagMap.Get(tagName)
414+
if len(tags) > 0 {
415+
tagLinks = append(tagLinks, tags[0].Link())
416+
}
403417
}
404418

419+
content += strings.Join(tagLinks, ", ")
420+
content += "\n"
421+
405422
// Write the page list into the middle of the page
406423
content += pagesToHTMLUnorderedList(pages)
407424
content += "\n"
@@ -690,13 +707,6 @@ func save(commitMsg string) {
690707
Defeat(errors.New(errConfigValueRead))
691708
}
692709

693-
// if commitMsg == "" {
694-
// // The incoming commitMsg is optional (if it is set, it probably came in
695-
// // via command line args on -save). If it isn't set, we use the default
696-
// // from the config file instead
697-
// commitMsg = defaultCommitMsg
698-
// }
699-
700710
commit, err := w.Commit(commitMsg, &git.CommitOptions{
701711
Author: &object.Signature{
702712
Name: defaultCommitName,
@@ -766,6 +776,47 @@ type Page struct {
766776
Title string `yaml:"title"`
767777
}
768778

779+
// AppendTagsToContent programatically modifies the page content to save auto-included
780+
// content like the tag list
781+
func (page *Page) AppendTagsToContent() bool {
782+
if len(page.Tags()) == 0 {
783+
return true
784+
}
785+
786+
tagLinks := []string{}
787+
for _, tag := range page.Tags() {
788+
if tag.Link() != "" {
789+
tagLinks = append(tagLinks, tag.Link())
790+
}
791+
}
792+
793+
tagList := strings.Join(tagLinks, ", ")
794+
795+
// Tags
796+
tagsStartStr := "<!-- TAGS:START -->"
797+
tagsEndStr := "<!-- TAGS:END -->"
798+
799+
re := fmt.Sprintf("`%s\n.*\n%s`", tagsStartStr, tagsEndStr)
800+
rg := regexp.MustCompile(re)
801+
802+
newContent := rg.ReplaceAllString(page.Content, tagList)
803+
804+
if page.Content != newContent {
805+
// Swap the old content with the new content
806+
page.Content = newContent
807+
} else {
808+
// Append the tag list to the end of the page
809+
page.Content += fmt.Sprintf(
810+
"\n%s\n%s\n%s\n",
811+
tagsStartStr,
812+
tagList,
813+
tagsEndStr,
814+
)
815+
}
816+
817+
return true
818+
}
819+
769820
// CreatedAt returns a time instance representing when the page was created
770821
func (page *Page) CreatedAt() time.Time {
771822
date, err := time.Parse(time.RFC3339, page.Date)
@@ -805,6 +856,14 @@ func (page *Page) PrettyDate() string {
805856
return page.CreatedAt().Format("Jan 02, 2006")
806857
}
807858

859+
// Save writes the page to disk
860+
func (page *Page) Save() error {
861+
Info(page.FilePath)
862+
863+
err := ioutil.WriteFile(page.FilePath, []byte(page.Content), 0644)
864+
return err
865+
}
866+
808867
// Tags returns a slice of tags assigned to this page
809868
func (page *Page) Tags() []*Tag {
810869
tags := []*Tag{}
@@ -845,6 +904,19 @@ func (tag *Tag) IsValid() bool {
845904
return tag.Name != ""
846905
}
847906

907+
// Link returns a link string suitable for embedding in a Markdown page
908+
func (tag *Tag) Link() string {
909+
if tag.Name == "" {
910+
return ""
911+
}
912+
913+
return fmt.Sprintf(
914+
"[%s](%s)",
915+
tag.Name,
916+
fmt.Sprintf("./%s", tag.Name),
917+
)
918+
}
919+
848920
/* -------------------- TagMap -------------------- */
849921

850922
// TagMap is a map of tag name to Tag instance

0 commit comments

Comments
 (0)