Upstep Go dependencies (#340)

* Upstep Go dependencies

* tiny linter fix

* Tidy
This commit is contained in:
tobi
2021-12-12 15:47:51 +01:00
committed by GitHub
parent 5506a5ecbe
commit 67ac8db190
160 changed files with 248601 additions and 232400 deletions

View File

@ -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