diff options
Diffstat (limited to 'formatter')
-rw-r--r-- | formatter/formatter.go | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/formatter/formatter.go b/formatter/formatter.go index 7d44891..738581d 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -7,6 +7,8 @@ import ( "git.thomasvoss.com/gsp/parser" ) +var xml = false + var stringEscapes = map[rune]string{ '"': """, '&': "&", @@ -19,6 +21,24 @@ func PrintHtml(ast parser.AstNode) { return } + if ast.Type == parser.DocType || ast.Type == parser.XmlDocType { + if ast.Type == parser.DocType { + fmt.Print("<!DOCTYPE") + } else { + xml = true + fmt.Print("<?xml") + } + + for _, a := range ast.Attrs { + printAttr(a) + } + + if ast.Type == parser.XmlDocType { + fmt.Print("?") + } + fmt.Print(">") + } + if ast.Type == parser.Normal { fmt.Printf("<%s", ast.Text) @@ -44,22 +64,14 @@ func PrintHtml(ast parser.AstNode) { } for _, a := range notClasses { - fmt.Printf(" %s", a.Key) - if a.Value == "" { - continue - } - fmt.Print("=\"") - for _, r := range a.Value { - if v, ok := stringEscapes[r]; ok { - fmt.Print(v) - } else { - fmt.Printf("%c", r) - } - } - fmt.Print("\"") + printAttr(a) } - fmt.Print(">") + if xml && len(ast.Children) == 0 { + fmt.Print("/>") + } else { + fmt.Print(">") + } } if len(ast.Children) == 0 { @@ -85,6 +97,21 @@ func PrintHtml(ast parser.AstNode) { } } +func printAttr(a parser.Attr) { + fmt.Printf(" %s", a.Key) + if a.Value != "" { + fmt.Print("=\"") + for _, r := range a.Value { + if v, ok := stringEscapes[r]; ok { + fmt.Print(v) + } else { + fmt.Printf("%c", r) + } + } + fmt.Print("\"") + } +} + func trimLeftSpaces(s string) string { i := 0 rs := []rune(s) |