diff options
author | Thomas Voss <mail@thomasvoss.com> | 2023-09-11 05:10:54 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2023-09-11 05:10:54 +0200 |
commit | 8bbf31f9f779ae9958ff3324f0f7735aa366f7bc (patch) | |
tree | 0f6d6f70b2758df577dc8cd47e74b5e8636c1aba | |
parent | 977f794ab8b28d7462fd0e85eee7d4343f946d3a (diff) |
Add the ‘=’ node for whitespace trimming
-rw-r--r-- | formatter/formatter.go | 8 | ||||
-rw-r--r-- | gsp.5 | 69 | ||||
-rw-r--r-- | parser/parser.go | 14 |
3 files changed, 38 insertions, 53 deletions
diff --git a/formatter/formatter.go b/formatter/formatter.go index 157b0ab..af84ec3 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -42,6 +42,8 @@ func PrintAst(ast parser.AstNode) { } case parser.Tagless: printChildren(ast.Children) + case parser.TaglessTrim: + printChildrenTrim(ast.Children) } } @@ -93,6 +95,12 @@ func printText(s string) { } func printChildren(nodes []parser.AstNode) { + for _, n := range nodes { + PrintAst(n) + } +} + +func printChildrenTrim(nodes []parser.AstNode) { for i, n := range nodes { if i == 0 && n.Type == parser.Text { n.Text = trimLeftSpaces(n.Text) @@ -195,55 +195,6 @@ p {- } .Ed .Ss Whitespace control -The typical behavior of -.Nm -is to compact whitespace. -Here you can see a before-and-after of transpilation: -.Bd -literal -offset indent -Before - -foo { - bar{- Hello World } - baz{-Hello World} -} -.Ed -.Bd -literal -offset indent -After - -<foo><bar>Hello World</bar><baz>Hello World</baz></foo> -.Ed -.Pp -One exception to this use is when using embedded nodes. -If your literal text contains an embedded node, then whitespace around the node -is preserved: -.Bd -literal -offset indent -Before - -foo {- - Hello @bar{-there} world! -} -.Ed -.Bd -literal -offset indent -After - -<foo>Hello <bar>there</bar> world!</foo> -.Ed -.Pp -Therefore if you would like to remove the whitespace when working with literal -text, you need to manually compact your document: -.Bd -literal -offset indent -Before - -foo {- - Hello@bar{-there}world! -} -.Ed -.Bd -literal -offset indent -After - -<foo>Hello<bar>there</bar>world!</foo> -.Ed -.Pp Sometimes it is also useful to have a newline between nodes, especially when working with .Sq code @@ -269,6 +220,26 @@ After <code>bar</code> <code>baz</code></pre> .Ed +.Pp +Additionally, sometimes when using literal text with the +.Sq - +special node name, it can be nice to have a way to trim whitespace around the +text without having to minify your markup. +To achieve this, you can use the special equals +.Pq Sq = +node name: +.Bd -literal -offset indent +Before + +>foo {- Hello World } + bar {= Hello World } +.Ed +.Bd -literal -offset indent +After + +<foo> Hello World </foo> +<bar>Hello World</bar> +.Ed .Sh SEE ALSO .Xr gsp 1 .Pp diff --git a/parser/parser.go b/parser/parser.go index f55a135..0fc0037 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -14,6 +14,7 @@ type nodeType uint const ( Normal nodeType = iota Tagless + TaglessTrim Text ) @@ -66,8 +67,8 @@ func (reader *reader) parseNode() (node AstNode, err error) { } switch r { - case '-': - return reader.parseText() + case '-', '=': + return reader.parseText(r == '=') case '>': node.Newline = true if _, err = reader.readRune(); err != nil { @@ -153,13 +154,18 @@ func (reader *reader) parseNodeName() (string, error) { return sb.String(), nil } -func (reader *reader) parseText() (AstNode, error) { +func (reader *reader) parseText(trim bool) (AstNode, error) { if _, err := reader.readRune(); err != nil { return AstNode{}, err } sb := strings.Builder{} - node := AstNode{Type: Tagless} + node := AstNode{} + if trim { + node.Type = TaglessTrim + } else { + node.Type = Tagless + } loop: for { |