Beispiel #1
0
func (p *Package) HTMLDecl(node interface{}) (template.HTML, error) {
	cfg := printer.Config{
		HTML:     true,
		Tabwidth: 8,
		Linker:   p,
	}
	var buf bytes.Buffer
	err := cfg.Fprint(&buf, p.fset, node)
	s := buf.String()
	if strings.HasPrefix(s, "<span class=\"token\">const</span>") {
		s = valueRe.ReplaceAllString(s, "<span id=\""+constPrefix+"${1}\">${1}</span>${2}")
	} else if strings.HasPrefix(s, "<span class=\"token\">var</span>") {
		s = valueRe.ReplaceAllString(s, "<span id=\""+varPrefix+"${1}\">${1}</span>${2}")
	}
	return template.HTML(s), err
}
Beispiel #2
0
func (e *Example) HTML() (template.HTML, error) {
	cfg := printer.Config{
		HTML:     true,
		Tabwidth: 8,
		Linker:   e.pkg,
	}
	var buf bytes.Buffer
	var node interface{}
	if e.example.Play != nil {
		node = e.example.Play
	} else {
		node = e.example.Code
		if bk, ok := node.(*ast.BlockStmt); ok {
			node = bk.List
		}
	}
	err := cfg.Fprint(&buf, e.fset, node)
	return template.HTML(buf.String()), err
}