예제 #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
}