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

@ -15,9 +15,19 @@ package main
import "github.com/go-xmlfmt/xmlfmt"
func main() {
xml1 := `<root><this><is>a</is><test /><message><org><cn>Some org-or-other</cn><ph>Wouldnt you like to know</ph></org><contact><fn>Pat</fn><ln>Califia</ln></contact></message></this></root>`
xmlfmt.NL = "\n"
xml1 := `<root><this><is>a</is><test /><message><!-- with comment --><org><cn>Some org-or-other</cn><ph>Wouldnt you like to know</ph></org><contact><fn>Pat</fn><ln>Califia</ln></contact></message></this></root>`
x := xmlfmt.FormatXML(xml1, "\t", " ")
print(x)
// If the XML Comments have nested tags in them
xml1 = `<book> <author>Fred</author>
<!--
<price>20</price><currency>USD</currency>
-->
<isbn>23456</isbn> </book>`
x = xmlfmt.FormatXML(xml1, "", " ", true)
print(x)
}
```
@ -47,10 +57,25 @@ Output:
</message>
</this>
</root>
<book>
<author>Fred
</author>
<!-- <price>20</price><currency>USD</currency> -->
<isbn>23456
</isbn>
</book>
```
There is no XML decoding and encoding involved, only pure regular expression matching and replacing. So it is much faster than going through decoding and encoding procedures. Moreover, the exact XML source string is preserved, instead of being changed by the encoder. This is why this package exists in the first place.
Note that
- the XML is mainly used in Windows environments, thus the default line ending is in Windows' `CRLF` format. To change the default line ending, see the above sample code (first line).
- the case of XML comments nested within XML comments is ***not*** supported. Please avoid them or use any other tools to correct them before using this package.
- don't turn on the `nestedTagsInComments` parameter blindly, as the code has become 10+ times more complicated because of it.
## Command
To use it on command line, check out [xmlfmt](https://github.com/AntonioSun/xmlfmt):
@ -59,7 +84,8 @@ To use it on command line, check out [xmlfmt](https://github.com/AntonioSun/xmlf
```
$ xmlfmt
XML Formatter
built on 2019-12-08
Version 1.1.0 built on 2021-12-06
Copyright (C) 2021, Antonio Sun
The xmlfmt will format the XML string without rewriting the document
@ -69,6 +95,42 @@ Options:
-f, --file *The xml file to read from (or stdin)
-p, --prefix each element begins on a new line and this prefix
-i, --indent[= ] indent string for nested elements
-n, --nested nested tags in comments
$ xmlfmt -f https://pastebin.com/raw/z3euQ5PR
<root>
<this>
<is>a
</is>
<test />
<message>
<!-- with comment -->
<org>
<cn>Some org-or-other
</cn>
<ph>Wouldnt you like to know
</ph>
</org>
<contact>
<fn>Pat
</fn>
<ln>Califia
</ln>
</contact>
</message>
</this>
</root>
$ xmlfmt -f https://pastebin.com/raw/Zs0qy0qz -n
<book>
<author>Fred
</author>
<!-- <price>20</price><currency>USD</currency> -->
<isbn>23456
</isbn>
</book>
```

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