コード例 #1
0
ファイル: jedie.go プロジェクト: NovemberFoxtrot/jedie
func (cfg *config) convertFile(src, dst string) error {
	dir := filepath.Dir(dst)
	_, err := os.Stat(dir)
	if err != nil {
		err = os.MkdirAll(filepath.Dir(dst), 0755)
		if err != nil {
			return err
		}
	}
	ext := filepath.Ext(src)
	if isConvertable(src) {
		if isMarkdown(src) {
			dst = dst[0:len(dst)-len(filepath.Ext(dst))] + ".html"
		}

		vars := pongo.Context{"content": ""}
		for {
			for k, v := range cfg.vars {
				vars[k] = v
			}
			pageVars := pongo.Context{}
			content, err := parseFile(src, pageVars)
			if err != nil {
				return err
			}
			for k, v := range pageVars {
				vars[k] = v
			}
			vars["post"] = pongo.Context{
				"date":  cfg.toDate(src),
				"url":   cfg.toPostUrl(src),
				"title": str(vars["title"]),
			}
			vars["page"] = pongo.Context{
				"date":  cfg.toDate(src),
				"url":   cfg.toPostUrl(src),
				"title": str(vars["title"]),
			}
			if content != "" {
				ps := new(string)
				*ps = content
				// TODO The variables must be hidden for the each posts/pages?
				//old := cfg.vars
				//cfg.vars = vars
				tpl, err := pongo.FromString(str(vars["layout"]), ps, include(cfg, vars))
				//cfg.vars = old
				if err == nil {
					output, err := tpl.Execute(&vars)
					if err == nil && output != nil {
						content = *output
					} else {
						return err
					}
				} else {
					return err
				}
			}

			if isMarkdown(src) {
				renderer := blackfriday.HtmlRenderer(0, "", "")
				vars["content"] = string(blackfriday.Markdown([]byte(content), renderer, extensions))
			} else {
				vars["content"] = content
			}
			if str(vars["layout"]) == "" || str(vars["layout"]) == "nil" {
				break
			}
			src = filepath.ToSlash(filepath.Join(cfg.layouts, str(vars["layout"])+".html"))
			ext = filepath.Ext(src)
			content = str(vars["content"])
			vars["content"] = content
			vars["layout"] = ""
		}

		err = ioutil.WriteFile(dst, []byte(str(vars["content"])), 0644)
	} else {
		switch ext {
		case ".yml", ".go", ".exe":
			return nil
		}
		_, err = copyFile(src, dst)
	}
	return err
}
コード例 #2
0
ファイル: jedie.go プロジェクト: koron/jedie
func (cfg *config) convertFile(src, dst string) error {
	var err error
	ext := filepath.Ext(src)
	switch ext {
	case ".yml", ".go", ".exe":
		return nil
	case ".html", ".md", ".mkd":
		dst = dst[0:len(dst)-len(ext)] + ".html"
		fi, err := os.Stat(src)
		if err != nil {
			return err
		}

		vars := pongo.Context{"content": ""}
		for {
			for k, v := range cfg.vars {
				vars[k] = v
			}
			content, err := parseFile(src, vars)
			if err != nil {
				// TODO Really?
				break
			}
			vars["post"] = pongo.Context{
				"date":  fi.ModTime(),
				"url":   cfg.toUrl(src),
				"title": vars["title"],
			}
			// FIXME Why pongo returns pointer of string?
			if content != "" {
				ps := new(string)
				*ps = content
				//old := cfg.vars
				//cfg.vars = vars
				tpl, err := pongo.FromString(str(vars["layout"]), ps, include(cfg, vars))
				//cfg.vars = old
				if err == nil {
					output, err := tpl.Execute(&vars)
					if err == nil && output != nil {
						content = *output
					} else {
						println(err.Error())
						return err
					}
				} else {
					return err
				}
			}

			if ext == ".md" || ext == ".mkd" {
				renderer := blackfriday.HtmlRenderer(0, "", "")
				vars["content"] = string(blackfriday.Markdown([]byte(content), renderer, extensions))
			} else {
				vars["content"] = content
			}
			if vars["layout"] == "" {
				break
			}
			src = filepath.ToSlash(filepath.Join(cfg.source, "_layouts", str(vars["layout"])+".html"))
			ext = filepath.Ext(src)
			content = str(vars["content"])
			vars = pongo.Context{"content": content}
		}

		return ioutil.WriteFile(dst, []byte(str(vars["content"])), 0644)
	}
	_, err = copyFile(src, dst)
	return err
}