diff options
author | 2021-12-12 15:47:51 +0100 | |
---|---|---|
committer | 2021-12-12 15:47:51 +0100 | |
commit | 67ac8db190eb82a7758746fb021fa3014f4241b7 (patch) | |
tree | 4a4124ad8f0ee9ec8858b109dd0bcc2e567fc144 /vendor/github.com/go-xmlfmt/xmlfmt/xmlfmt.go | |
parent | upstep dependencies (#339) (diff) | |
download | gotosocial-67ac8db190eb82a7758746fb021fa3014f4241b7.tar.xz |
Upstep Go dependencies (#340)
* Upstep Go dependencies
* tiny linter fix
* Tidy
Diffstat (limited to 'vendor/github.com/go-xmlfmt/xmlfmt/xmlfmt.go')
-rw-r--r-- | vendor/github.com/go-xmlfmt/xmlfmt/xmlfmt.go | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/vendor/github.com/go-xmlfmt/xmlfmt/xmlfmt.go b/vendor/github.com/go-xmlfmt/xmlfmt/xmlfmt.go index b744f5b35..8b5a9e422 100644 --- a/vendor/github.com/go-xmlfmt/xmlfmt/xmlfmt.go +++ b/vendor/github.com/go-xmlfmt/xmlfmt/xmlfmt.go @@ -1,12 +1,13 @@ //////////////////////////////////////////////////////////////////////////// // Porgram: xmlfmt.go // Purpose: Go XML Beautify from XML string using pure string manipulation -// Authors: Antonio Sun (c) 2016-2019, All rights reserved +// Authors: Antonio Sun (c) 2016-2021, All rights reserved //////////////////////////////////////////////////////////////////////////// package xmlfmt import ( + "html" "regexp" "strings" ) @@ -17,12 +18,32 @@ var ( NL = "\r\n" ) -// FormatXML will (purly) reformat the XML string in a readable way, without any rewriting/altering the structure -func FormatXML(xmls, prefix, indent string) string { +// FormatXML will (purly) reformat the XML string in a readable way, without any rewriting/altering the structure. +// If your XML Comments have nested tags in them, or you're not 100% sure otherwise, pass `true` as the third parameter to this function. But don't turn it on blindly, as the code has become ten times more complicated because of it. +func FormatXML(xmls, prefix, indent string, nestedTagsInComments ...bool) string { + nestedTagsInComment := false + if len(nestedTagsInComments) > 0 { + nestedTagsInComment = nestedTagsInComments[0] + } + reXmlComments := regexp.MustCompile(`(?s)(<!--)(.*?)(-->)`) src := regexp.MustCompile(`(?s)>\s+<`).ReplaceAllString(xmls, "><") - + if nestedTagsInComment { + src = reXmlComments.ReplaceAllStringFunc(src, func(m string) string { + parts := reXmlComments.FindStringSubmatch(m) + p2 := regexp.MustCompile(`\r*\n`).ReplaceAllString(parts[2], " ") + return parts[1] + html.EscapeString(p2) + parts[3] + }) + } rf := replaceTag(prefix, indent) - return (prefix + reg.ReplaceAllStringFunc(src, rf)) + r := prefix + reg.ReplaceAllStringFunc(src, rf) + if nestedTagsInComment { + r = reXmlComments.ReplaceAllStringFunc(r, func(m string) string { + parts := reXmlComments.FindStringSubmatch(m) + return parts[1] + html.UnescapeString(parts[2]) + parts[3] + }) + } + + return r } // replaceTag returns a closure function to do 's/(?<=>)\s+(?=<)//g; s(<(/?)([^>]+?)(/?)>)($indent+=$3?0:$1?-1:1;"<$1$2$3>"."\n".(" "x$indent))ge' as in Perl |