diff --git a/main.go b/main.go index 168b1c4..bbf2fce 100644 --- a/main.go +++ b/main.go @@ -74,7 +74,7 @@ func main() { "stickhandling", so here we are, abomination enshrined */ if listFlag { - listTargetDirectories(src.GlobalConfig) + src.ListTargetDirectories(src.GlobalConfig) src.Victory(statusDone) } @@ -112,11 +112,23 @@ func main() { func buildContent() { pages := loadPages() + buildContentPages(pages) tagMap := buildTagPages(pages) buildIndexPage(pages, tagMap) } +// buildContentPages loops through all the pages and tells them to save themselves +// to disk. This process writes any auto-generated content into the pages +func buildContentPages(pages []*pages.Page) { + for _, page := range pages { + if page.IsContentPage() { + page.AppendTagsToContent() + page.Save() + } + } +} + // buildIndexPage creates the main index.md page that is the root of the site func buildIndexPage(pageSet []*pages.Page, tagMap *pages.TagMap) { src.Info(statusIdxBuild) @@ -147,6 +159,7 @@ func buildIndexPage(pageSet []*pages.Page, tagMap *pages.TagMap) { // And write the file to disk tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, true) if err != nil { + src.Info("Failed to get target dir (1)") src.Defeat(err) } @@ -158,6 +171,7 @@ func buildIndexPage(pageSet []*pages.Page, tagMap *pages.TagMap) { err = ioutil.WriteFile(filePath, []byte(content), 0644) if err != nil { + src.Info("Failed to write file (1)") src.Defeat(err) } @@ -190,6 +204,7 @@ func buildTagPages(pageSet []*pages.Page) *pages.TagMap { // And write the file to disk tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, true) if err != nil { + src.Info("Failed to get target dir (2)") src.Defeat(err) } @@ -202,6 +217,7 @@ func buildTagPages(pageSet []*pages.Page) *pages.TagMap { err = ioutil.WriteFile(filePath, []byte(content), 0644) if err != nil { + src.Info("Failed to write file (2)") src.Defeat(err) } @@ -217,6 +233,7 @@ func buildTagPages(pageSet []*pages.Page) *pages.TagMap { func createNewPage(title string) { tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, true) if err != nil { + src.Info("Failed to get target dir (3)") src.Defeat(err) } @@ -224,6 +241,7 @@ func createNewPage(title string) { err = page.Open(defaultEditor) if err != nil { + src.Info("Failed to open editor") src.Defeat(err) } @@ -249,19 +267,6 @@ func determineCommitMessage(cfg *config.Config, args []string) string { return msg } -// listTargetDirectories writes the list of target directories in the configuration -// out to the terminal -func listTargetDirectories(cfg *config.Config) { - dirMap, err := cfg.Map("targetDirectories") - if err != nil { - src.Defeat(err) - } - - for key, dir := range dirMap { - src.Info(fmt.Sprintf("%6s\t%s\n", key, dir.(string))) - } -} - // loadPages reads the page files from disk (in reverse chronological order) and // creates Page instances from them func loadPages() []*pages.Page { @@ -269,6 +274,7 @@ func loadPages() []*pages.Page { tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, true) if err != nil { + src.Info("Failed to get target dir (4)") src.Defeat(err) } @@ -288,20 +294,6 @@ func loadPages() []*pages.Page { return pageSet } -// // open tll the OS to open the newly-created page in the editor (as specified in the config) -// // If there's no editor explicitly defined by the user, tell the OS to try and open it -// func open(page *src.Page) error { -// editor := src.GlobalConfig.UString("editor", defaultEditor) -// if editor == "" { -// editor = defaultEditor -// } - -// cmd := exec.Command(editor, page.FilePath) -// err := cmd.Run() - -// return err -// } - // pagesToHTMLUnorderedList creates the unordered list of page links that appear // on the index and tag pages func pagesToHTMLUnorderedList(pageSet []*pages.Page) string { @@ -341,16 +333,19 @@ func push() { tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, false) if err != nil { + src.Info("Failed to get target dir (5)") src.Defeat(err) } r, err := git.PlainOpen(tDir) if err != nil { + src.Info("Failed to plain open") src.Defeat(err) } err = r.Push(&git.PushOptions{}) if err != nil { + src.Info("Failed to git push") src.Defeat(err) } } diff --git a/pages/page.go b/pages/page.go index 7883718..e3cb40f 100644 --- a/pages/page.go +++ b/pages/page.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os/exec" "path/filepath" + "regexp" "strings" "time" @@ -50,17 +51,59 @@ func NewPage(title string, targetDir string) *Page { return page } +// AppendTagsToContent programatically modifies the page content to save auto-included +// content like the tag list +func (page *Page) AppendTagsToContent() { + if len(page.Tags()) == 0 { + return + } + + tagLinks := []string{} + for _, tag := range page.Tags() { + if tag.Link() != "" { + tagLinks = append(tagLinks, tag.Link()) + } + } + + tagList := strings.Join(tagLinks, ", ") + + // Tags + tagsStartStr := "" + tagsEndStr := "" + + re := fmt.Sprintf("`%s\n.*\n%s`", tagsStartStr, tagsEndStr) + rg := regexp.MustCompile(re) + + newContent := rg.ReplaceAllString(page.Content, tagList) + + if page.Content != newContent { + // Swap the old content with the new content and we're done + page.Content = newContent + return + } + + // Else append the tag list to the end of the page + page.Content += fmt.Sprintf( + "\n%s\n%s\n%s\n", + tagsStartStr, + tagList, + tagsEndStr, + ) +} + // PageFromFilePath creates and returns a Page instance from a file path func PageFromFilePath(filePath string) *Page { page := new(Page) data, err := ioutil.ReadFile(filePath) if err != nil { + src.Info("Failed to read file") src.Defeat(err) } err = frontmatter.Unmarshal(data, page) if err != nil { + src.Info(fmt.Sprintf("Failed to unmarshal: %s", filePath)) src.Defeat(err) } @@ -139,6 +182,7 @@ func (page *Page) Save() { err := ioutil.WriteFile(page.FilePath, []byte(pageSrc), 0644) if err != nil { + src.Info("Failed to write file") src.Defeat(err) } } diff --git a/src/configuration.go b/src/configuration.go index 5f7279f..1942cce 100644 --- a/src/configuration.go +++ b/src/configuration.go @@ -100,6 +100,7 @@ func GetConfigFilePath() (string, error) { func makeConfigDir() { cDir, err := getConfigDir() if err != nil { + Info("Failed to make config dir") Defeat(err) } @@ -120,6 +121,7 @@ func makeConfigDir() { func makeConfigFile() { cPath, err := GetConfigFilePath() if err != nil { + Info("Failed to get config file path") Defeat(err) } @@ -143,6 +145,7 @@ func makeConfigFile() { } else { // But wait, it's some kind of other error. What kind? // I dunno, but it's probably bad so die + Info("Something weird happened") Defeat(err) } } @@ -169,6 +172,7 @@ func makeConfigFile() { func readConfigFile() *config.Config { cPath, err := GetConfigFilePath() if err != nil { + Info("Failed to read config file path") Defeat(err) } @@ -178,6 +182,7 @@ func readConfigFile() *config.Config { cfg, err := config.ParseYamlFile(cPath) if err != nil { + Info("Failed to parse config yaml file") Defeat(err) } diff --git a/src/target_directory.go b/src/target_directory.go index a7a6c3c..2c78104 100644 --- a/src/target_directory.go +++ b/src/target_directory.go @@ -2,6 +2,7 @@ package src import ( "errors" + "fmt" "log" "os" "path/filepath" @@ -97,3 +98,17 @@ func GetTargetDir(cfg *config.Config, targetDirFlag string, withDocsDir bool) (s return filepath.Join(dir, tDir[1:], docsBit), nil } + +// ListTargetDirectories writes the list of target directories in the configuration +// out to the terminal +func ListTargetDirectories(cfg *config.Config) { + dirMap, err := cfg.Map("targetDirectories") + if err != nil { + Info("Failed to map target directories") + Defeat(err) + } + + for key, dir := range dirMap { + Info(fmt.Sprintf("%6s\t%s\n", key, dir.(string))) + } +}