~srivathsan/blaze

daf9e252145df156731d2310b383c5d6eb659ead — Srivathsan Murali 3 years ago 7c2c724
Parse template from markdown content
2 files changed, 24 insertions(+), 2 deletions(-)

M builder/page.go
M builder/templates.go
M builder/page.go => builder/page.go +5 -2
@@ 75,14 75,17 @@ func ParsePage(relativePath string, config *config.BlazeConfig) (*Page, error) {
		return nil, err
	}
	page.Template = fmt.Sprintf("%s%s", page.Template, page.Extension)
	page.outputPath = filepath.Join(
		config.OutputDir(), changeExtension(relativePath, page.Extension))
	url := fmt.Sprintf("/%s", changeExtension(relativePath, page.Extension))
	page.outputPath = filepath.Join(config.OutputDir(), url)
	if filepath.Base(url) == "index.html" {
		url = filepath.Dir(url)
	}
	page.Url = url
	page.ListsPath = filepath.Join(config.ContentDir(), page.ListsPath)
	page.Content, err = ParseTemplateString(page.Site, page.Content, page)
	if err != nil {
		return nil, err
	}

	return page, nil
}

M builder/templates.go => builder/templates.go +19 -0
@@ 61,6 61,25 @@ func getTemplateFuncs(config *config.BlazeConfig) template.FuncMap {
	}
}

func ParseTemplateString(
	config *config.BlazeConfig, templateString string,
	data interface{}) (string, error) {
	temp, err := template.New("page_content").
		Funcs(getTemplateFuncs(config)).Parse(templateString)
	if err != nil {
		log.Println("Parse tempalte string:", err)
		return "", err
	}

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

	return outString.String(), nil
}

func ParseTemplate(config *config.BlazeConfig, templateName string, data interface{}) ([]byte, error) {
	templateFile := path.Join(config.TemplateDir(), templateName)