Exemplo n.º 1
0
Arquivo: fs.go Projeto: romanoff/ahc
func (self *Fs) readTemplate(c *component.Component, basePath string) error {
	filepath := basePath + ".tmpl"
	if _, err := os.Stat(filepath); err != nil {
		return nil
	}
	content, err := ioutil.ReadFile(filepath)
	if err != nil {
		return errors.New(fmt.Sprintf("Error while reading tmpl file: %v", filepath))
	}
	template := &component.Template{Content: stripClasses(string(content))}
	c.Template = template
	return nil
}
Exemplo n.º 2
0
Arquivo: fs.go Projeto: romanoff/ahc
func (self *Fs) readHtml(c *component.Component, basePath string) error {
	filepath := basePath + ".html"
	if _, err := os.Stat(filepath); err != nil {
		return nil
	}
	content, err := ioutil.ReadFile(filepath)
	document := xmlx.New()
	document.LoadBytes(content, nil)
	html := document.Root.Children[0]
	for _, attr := range html.Attributes {
		if attr.Name.Local == "namespace" {
			c.Namespace = attr.Value
		}
		if attr.Name.Local == "default_param" {
			c.DefaultParam = attr.Value
		}
		if attr.Name.Local == "require" {
			c.Requires = []string{attr.Value}
		}
	}
	for _, node := range html.Children {
		if node.Type != xmlx.NT_ELEMENT {
			continue
		}
		if node.Name.Local == "style" {
			c.Css = getXmlNodesContent(node.Children)
		}
		if node.Name.Local == "schema" {
			content := getXmlNodesContent(node.Children)
			lines := strings.Split(content, "\n")
			schemaContent := []byte{}
			for _, line := range lines {
				schemaContent = append(schemaContent, []byte(strings.Replace(line, "    ", "", 1))...)
			}
			schema, err := self.ParseSchema(schemaContent, filepath)
			if err != nil {
				return err
			}
			c.Schema = schema
		}
		if node.Name.Local == "template" {
			c.Template = &component.Template{Content: stripClasses(getXmlNodesContent(node.Children))}
		}
	}
	if err != nil {
		return errors.New(fmt.Sprintf("Error while reading html file: %v", filepath))
	}
	return nil
}