From 8fd0f17b7bcca4725046ca82d3193b9986fe1faa Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 9 Sep 2023 23:03:57 +0200 Subject: Add primitive support for doctypes and XML --- formatter/formatter.go | 55 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 14 deletions(-) (limited to 'formatter') 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("") + } + 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) -- cgit v1.2.3