|
9 | 9 | "os"
|
10 | 10 | "os/exec"
|
11 | 11 | "path/filepath"
|
| 12 | + "regexp" |
12 | 13 | "sort"
|
13 | 14 | "strings"
|
14 | 15 | "sync"
|
@@ -383,25 +384,41 @@ func getTargetDir(cfg *config.Config, withDocsDir bool) (string, error) {
|
383 | 384 |
|
384 | 385 | func buildContent() {
|
385 | 386 | pages := loadPages()
|
| 387 | + buildContentPages(pages) |
386 | 388 | tagMap := buildTagPages(pages)
|
387 | 389 | buildIndexPage(pages, tagMap)
|
388 | 390 | }
|
389 | 391 |
|
| 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 | + |
390 | 403 | // buildIndexPage creates the main index.md page that is the root of the site
|
391 | 404 | func buildIndexPage(pages []*Page, tagMap *TagMap) {
|
392 | 405 | Info(statusIdxBuild)
|
393 | 406 |
|
394 | 407 | content := ""
|
395 | 408 |
|
396 | 409 | // 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 | + } |
403 | 417 | }
|
404 | 418 |
|
| 419 | + content += strings.Join(tagLinks, ", ") |
| 420 | + content += "\n" |
| 421 | + |
405 | 422 | // Write the page list into the middle of the page
|
406 | 423 | content += pagesToHTMLUnorderedList(pages)
|
407 | 424 | content += "\n"
|
@@ -690,13 +707,6 @@ func save(commitMsg string) {
|
690 | 707 | Defeat(errors.New(errConfigValueRead))
|
691 | 708 | }
|
692 | 709 |
|
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 |
| - |
700 | 710 | commit, err := w.Commit(commitMsg, &git.CommitOptions{
|
701 | 711 | Author: &object.Signature{
|
702 | 712 | Name: defaultCommitName,
|
@@ -766,6 +776,47 @@ type Page struct {
|
766 | 776 | Title string `yaml:"title"`
|
767 | 777 | }
|
768 | 778 |
|
| 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 | + |
769 | 820 | // CreatedAt returns a time instance representing when the page was created
|
770 | 821 | func (page *Page) CreatedAt() time.Time {
|
771 | 822 | date, err := time.Parse(time.RFC3339, page.Date)
|
@@ -805,6 +856,14 @@ func (page *Page) PrettyDate() string {
|
805 | 856 | return page.CreatedAt().Format("Jan 02, 2006")
|
806 | 857 | }
|
807 | 858 |
|
| 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 | + |
808 | 867 | // Tags returns a slice of tags assigned to this page
|
809 | 868 | func (page *Page) Tags() []*Tag {
|
810 | 869 | tags := []*Tag{}
|
@@ -845,6 +904,19 @@ func (tag *Tag) IsValid() bool {
|
845 | 904 | return tag.Name != ""
|
846 | 905 | }
|
847 | 906 |
|
| 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 | + |
848 | 920 | /* -------------------- TagMap -------------------- */
|
849 | 921 |
|
850 | 922 | // TagMap is a map of tag name to Tag instance
|
|
0 commit comments