all repos — htmlformat @ c802f397618c8fb1f52254ebc0e0dd2345a828d1

Go package and CLI tool used to format HTML

make indent string configurable (default 2 spaces)

Alan Pearce
commit

c802f397618c8fb1f52254ebc0e0dd2345a828d1

parent

91ad1b65896167cf2f362e68e7faa44fc33ae8ad

3 files changed, 24 insertions(+), 16 deletions(-)

changed files
M cmd/htmlformat/main.gocmd/htmlformat/main.go
@@ -8,10 +8,15 @@
"alin.ovh/htmlformat" ) -var parseDocumentFlag = flag.Bool("document", false, "Set to true to parse a whole document") +var ( + parseDocumentFlag = flag.Bool("document", false, "Set to true to parse a whole document") + indentString = flag.String("indent", " ", "Set the indentation string") +) func main() { flag.Parse() + + htmlformat.IndentString = *indentString var err error if *parseDocumentFlag {
M format.goformat.go
@@ -12,6 +12,9 @@ "golang.org/x/net/html"
"golang.org/x/net/html/atom" ) +// IndentString is the string used for indentation. Use "\t" for tab. +var IndentString = " " + // Document formats a HTML document. func Document(w io.Writer, r io.Reader) (err error) { node, err := html.Parse(r)
@@ -141,7 +144,7 @@ t := scanner.Text()
if _, err = fmt.Fprintln(w); err != nil { return } - if err = printIndent(w, level+1); err != nil { + if err = printIndent(w, level); err != nil { return } if _, err = fmt.Fprint(w, t); err != nil {
@@ -248,6 +251,6 @@ return
} func printIndent(w io.Writer, level int) (err error) { - _, err = fmt.Fprint(w, strings.Repeat(" ", level)) + _, err = fmt.Fprint(w, strings.Repeat(IndentString, level)) return err }
M format_test.goformat_test.go
@@ -24,8 +24,8 @@ {
name: "html attribute escaping is normalized", input: `<ol> <li style="&amp;&#38;"> A </li> <li> B </li> </ol> `, expected: `<ol> - <li style="&amp;&amp;">A</li> - <li>B</li> + <li style="&amp;&amp;">A</li> + <li>B</li> </ol> `, },
@@ -33,8 +33,8 @@ {
name: "bare ampersands are escaped", input: `<ol> <li style="&"> A </li> <li> B </li> </ol> `, expected: `<ol> - <li style="&amp;">A</li> - <li>B</li> + <li style="&amp;">A</li> + <li>B</li> </ol> `, },
@@ -42,8 +42,8 @@ {
name: "html elements are indented", input: `<ol> <li class="name"> A </li> <li> B </li> </ol> `, expected: `<ol> - <li class="name">A</li> - <li>B</li> + <li class="name">A</li> + <li>B</li> </ol> `, },
@@ -65,9 +65,9 @@ {
name: "phrasing content element children are kept on the same line, including punctuation", input: `<ul><li><a href="http://example.com">Test</a>.</li></ul>`, expected: `<ul> - <li> - <a href="http://example.com">Test</a>. - </li> + <li> + <a href="http://example.com">Test</a>. + </li> </ul> `, },
@@ -89,8 +89,8 @@ {
name: "space after text node with punctuation is preserved", input: `<p><a href="https://example.com">Link</a>. <a href="https://example.org">Another</a></p>`, expected: `<p> - <a href="https://example.com">Link</a>. - <a href="https://example.org">Another</a> + <a href="https://example.com">Link</a>. + <a href="https://example.org">Another</a> </p> `, },
@@ -98,7 +98,7 @@ {
name: "spaces are not added around elements ending with punctuation", input: "<div><time>19:00</time>–<time>20:00</time></div>", expected: `<div> - <time>19:00</time>–<time>20:00</time> + <time>19:00</time>–<time>20:00</time> </div> `, },
@@ -130,7 +130,7 @@ }{
{ name: "HTML5 doctype is preserved", input: "<!doctype html><html><head></head><body></body></html>", - expected: "<!doctype html>\n<html>\n <head>\n </head> <body>\n </body>\n</html>\n", + expected: "<!doctype html>\n<html>\n <head>\n </head>\n <body>\n </body>\n</html>\n", }, }