~srivathsan/blaze

89dffa80e4dfae164cfd7a38bed8e374eb82e464 — Srivathsan Murali 3 years ago f74b675
parse url formats using template
2 files changed, 69 insertions(+), 16 deletions(-)

M builder/page.go
M builder/templates.go
M builder/page.go => builder/page.go +24 -16
@@ 14,18 14,20 @@ import (
)

type Page struct {
	Site       *config.BlazeConfig
	Title      string    `yaml:"title"`
	Template   string    `yaml:"template"`
	Date       time.Time `yaml:"date"`
	Draft      bool      `yaml:"draft"`
	ListsPath  string    `yaml:"lists-path"`
	Content    string    `fm:"content"`
	Extension  string
	Url        string
	HTML       []byte
	sourcePath string
	outputPath string
	Site         *config.BlazeConfig
	Title        string    `yaml:"title"`
	Template     string    `yaml:"template"`
	Date         time.Time `yaml:"date"`
	Draft        bool      `yaml:"draft"`
	ListsPath    string    `yaml:"lists-path"`
	Content      string    `fm:"content"`
	UrlFormat    string    `yaml:"url-format"`
	Extension    string
	Url          string
	HTML         []byte
	sourcePath   string
	outputPath   string
	relativePath string
}

func CreatePage(path string, config *config.BlazeConfig) error {


@@ 59,9 61,10 @@ func changeExtension(_path, new_ext string) string {
func ParsePage(relativePath string, config *config.BlazeConfig) (*Page, error) {
	log.Println("Parsing path:", relativePath)
	page := &Page{
		sourcePath: filepath.Join(config.ContentDir(), relativePath),
		Site:       config,
		Draft:      false,
		sourcePath:   filepath.Join(config.ContentDir(), relativePath),
		relativePath: relativePath,
		Site:         config,
		Draft:        false,
	}

	buf, err := ioutil.ReadFile(page.sourcePath)


@@ 81,8 84,13 @@ func ParsePage(relativePath string, config *config.BlazeConfig) (*Page, error) {
	}

	url := fmt.Sprintf("/%s", changeExtension(relativePath, page.Extension))
	if page.UrlFormat != "" {
		url, err = ParseUrlFormat(page)
		if err != nil {
			return nil, err
		}
	}
	page.outputPath = filepath.Join(config.OutputDir(), url)

	if filepath.Base(url) == "index.html" {
		url = filepath.Dir(url)
	}

M builder/templates.go => builder/templates.go +45 -0
@@ 107,3 107,48 @@ func ParseTemplate(config *config.BlazeConfig, templateName string, data interfa

	return outString.Bytes(), nil
}

func getUrlTemplates(p *Page) template.FuncMap {
	return template.FuncMap{
		"Y": func() string {
			return fmt.Sprintf("%d", p.Date.Year())
		},
		"y": func() string {
			return fmt.Sprintf("%d", p.Date.Year()/100)
		},
		"m": func() string {
			return fmt.Sprintf("%02d", p.Date.Month())
		},
		"d": func() string {
			return fmt.Sprintf("%02d", p.Date.Day())
		},
		"F": func() string {
			return fmt.Sprintf("%d-%02d-%02d",
				p.Date.Year(), p.Date.Month(), p.Date.Day())
		},
		"Title": func() string {
			return p.Title
		},
		"Path": func() string {
			return p.relativePath
		},
	}
}

func ParseUrlFormat(page *Page) (string, error) {
	temp, err := template.New("url format").
		Funcs(getUrlTemplates(page)).Parse(page.UrlFormat)
	if err != nil {
		log.Println("Parse tempalte string:", err)
		return "", err
	}

	var outString bytes.Buffer
	if err := temp.Execute(&outString, page); err != nil {
		log.Println("Parse Template:", err)
		return "", err
	}

	return changeExtension(outString.String(), page.Extension), nil

}