Skip to content

TIL-7 Tags into pages #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 23 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -217,13 +233,15 @@ 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)
}

page := pages.NewPage(title, tDir)

err = page.Open(defaultEditor)
if err != nil {
src.Info("Failed to open editor")
src.Defeat(err)
}

Expand All @@ -249,26 +267,14 @@ 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 {
pageSet := []*pages.Page{}

tDir, err := src.GetTargetDir(src.GlobalConfig, targetDirFlag, true)
if err != nil {
src.Info("Failed to get target dir (4)")
src.Defeat(err)
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand Down
44 changes: 44 additions & 0 deletions pages/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -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 := "<!-- TAGS:START -->"
tagsEndStr := "<!-- TAGS:END -->"

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)
}

Expand Down Expand Up @@ -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)
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func GetConfigFilePath() (string, error) {
func makeConfigDir() {
cDir, err := getConfigDir()
if err != nil {
Info("Failed to make config dir")
Defeat(err)
}

Expand All @@ -120,6 +121,7 @@ func makeConfigDir() {
func makeConfigFile() {
cPath, err := GetConfigFilePath()
if err != nil {
Info("Failed to get config file path")
Defeat(err)
}

Expand All @@ -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)
}
}
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down
15 changes: 15 additions & 0 deletions src/target_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package src

import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -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)))
}
}