コード例 #1
0
ファイル: payload.go プロジェクト: newblue/neyo
func LoadLayouts(root string, theme string) map[string]Mapper {
	layouts := make(map[string]Mapper)
	var layout Mapper
	Log(DEBUG, "Load %sthemes/%s/layouts/", root, theme)
	layout_path := filepath.Join(root, "themes", theme, "/layouts/")
	filepath.Walk(layout_path, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}
		filename := filepath.Base(path)
		if strings.HasPrefix(filename, ".") {
			return nil
		}

		f, err := os.Open(path)
		if err != nil {
			return err
		}
		defer f.Close()
		buf := make([]byte, 3)
		_, err = f.Read(buf)
		if err != nil {
			return err
		}
		f.Seek(0, os.SEEK_SET)
		if string(buf) == "---" {
			layout, err = ReadMuPage(path)
			if err != nil {
				return err
			}
			tpl, err := mustache.Parse(bytes.NewBufferString(layout["_content"].(*DocContent).Source))
			if err != nil {
				Log(ERROR, "Bad layout %s %s", path, err)
				return err
			}
			layout["_content"].(*DocContent).TPL = tpl
			layout["_content"].(*DocContent).Source = ""
		} else {
			layout = make(map[string]interface{})
			tpl, err := mustache.Parse(f)
			if err != nil {
				return err
			}
			layout["_content"] = &DocContent{"", "", tpl}
		}
		layoutName := filename[0 : len(filename)-len(filepath.Ext(filename))]
		layouts[layoutName] = layout
		Log(DEBUG, "Load layout: %s", layoutName)
		return nil
	})
	return layouts
}
コード例 #2
0
ファイル: payload.go プロジェクト: xushiwei/gor
func LoadLayouts(theme string) map[string]Mapper {
	layouts := make(map[string]Mapper)
	var layout Mapper
	filepath.Walk("themes/"+theme+"/layouts/", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}
		filename := filepath.Base(path)
		if strings.HasPrefix(filename, ".") {
			return nil
		}

		f, err := os.Open(path)
		if err != nil {
			return err
		}
		defer f.Close()
		buf := make([]byte, 3)
		_, err = f.Read(buf)
		if err != nil {
			return err
		}
		f.Seek(0, os.SEEK_SET)
		if string(buf) == "---" {
			layout, err = ReadMuPage(path)
			if err != nil {
				return err
			}
			tpl, err := mustache.Parse(bytes.NewBufferString(layout["_content"].(*DocContent).Source))
			if err != nil {
				log.Println("Bad Layout", path, err)
				return err
			}
			layout["_content"].(*DocContent).TPL = tpl
			layout["_content"].(*DocContent).Source = ""
		} else {
			layout = make(map[string]interface{})
			tpl, err := mustache.Parse(f)
			if err != nil {
				return err
			}
			layout["_content"] = &DocContent{"", "", tpl}
		}
		layouts[filename[0:len(filename)-len(filepath.Ext(filename))]] = layout
		return nil
	})
	return layouts
}