diff options
author | Thomas Voss <mail@thomasvoss.com> | 2023-09-09 23:03:57 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2023-09-09 23:03:57 +0200 |
commit | 8fd0f17b7bcca4725046ca82d3193b9986fe1faa (patch) | |
tree | e0ab1680bba6bee3eb2cc653d88111b153fd396f /formatter/formatter.go | |
parent | 96aef4c6a473e380b027a7b9db87cbbbcbae1cba (diff) |
Add primitive support for doctypes and XML
Diffstat (limited to 'formatter/formatter.go')
-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) |