diff options
author | Thomas Voss <mail@thomasvoss.com> | 2023-11-01 16:38:49 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2023-11-01 16:38:49 +0100 |
commit | 1f83fa89d8585b1fa59df7a400157f8df93ff681 (patch) | |
tree | dc80afa3ea2f17f76705e7a4b695f41386320845 | |
parent | eced3e62c922ee121429759eaa5d4956eb17085d (diff) |
Remove the ‘>’ prefixv3.0.0
The rationale was explained on the mailing list[1].
[1]: https://paste.thomasvoss.com/353
-rw-r--r-- | formatter/formatter.go | 4 | ||||
-rw-r--r-- | formatter/formatter_test.go | 14 | ||||
-rw-r--r-- | gsp.5 | 84 | ||||
-rw-r--r-- | parser/parser.go | 9 | ||||
-rw-r--r-- | parser/parser_test.go | 10 |
5 files changed, 66 insertions, 55 deletions
diff --git a/formatter/formatter.go b/formatter/formatter.go index 8bd1c4d..526de5a 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -35,10 +35,6 @@ func PrintAst(ast parser.AstNode) { printChildren(ast.Children) fmt.Printf("</%s>", ast.Text) } - - if ast.Newline { - fmt.Print("\n") - } case parser.Tagless: printChildren(ast.Children) case parser.TaglessTrim: diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index c5f0b83..4fe8035 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -35,14 +35,14 @@ func restoreAndCapture() string { func TestPrintAst(t *testing.T) { s := ` html lang="en" { - >head attr { - >title {- + head attr { + title {- My Website } meta .→{} x="y"{} } - >body { - >div #some-id {} + body { + div #some-id {} div key="val" .class-1 .class-2 { p {- This is some @em{-emphatic} text } } @@ -52,11 +52,7 @@ func TestPrintAst(t *testing.T) { }` result := `<html lang="en"><head attr><title> My Website - </title> -<meta class="→{}" x="y"></head> -<body><div id="some-id"> -<div class="class-1 class-2" key="val"><p> This is some <em>emphatic</em> text </p></div><tags key="Some long value"></body> -</html>` + </title><meta class="→{}" x="y"></head><body><div id="some-id"><div class="class-1 class-2" key="val"><p> This is some <em>emphatic</em> text </p></div><tags key="Some long value"></body></html>` // Write the source to a temp file r := strings.NewReader(s) @@ -1,4 +1,4 @@ -.Dd $Mdocdate: October 28 2023 $ +.Dd $Mdocdate: November 1 2023 $ .Dt GSP 5 .Os .Sh NAME @@ -219,51 +219,77 @@ p {- } .Ed .Ss Whitespace control -Sometimes it is also useful to have a newline between nodes, especially when -working with -.Sq code -tags nested within a -.Sq pre -tag. -To specify that you want a newline to be placed after a node, you can prefix the -node name with a greater-than symbol -.Pq Sq > : +By default GSP transpiled to HTML will be automatically minified with the +exception of literal text whose whitespace is untouched. +Sometimes though, we want to have proper control over whitespace. +The first trick to manual whitespace control is to make use of the special node +name +.Sq = . +It acts identially to the special +.Sq - +node, except it removes all leading- and trailing whitespace: .Bd -literal -offset indent Before -pre { - >code {-foo} - >code {-bar} - code {-baz} +p {= Hello World + } -.Ed -.Bd -literal -offset indent + After -<pre><code>foo</code> -<code>bar</code> -<code>baz</code></pre> +<p>Hello World</p> .Ed .Pp -Additionally, sometimes when using literal text with the +This can be useful for trimming whitespace, but sometimes we want to preserve +it. +This is especially crucial with HTML +.Ql <pre> +tags for which whitespace is not squashed. +We can get around this issue by making use of the fact that the special .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: +node does not trim whitespace. +The following is an example of how not to display two seperate lines in a +.Ql <pre> +tag: .Bd -literal -offset indent Before ->foo {- Hello World } - bar {= Hello World } +pre { + code{-Foo} + code{-Bar} +} + +After + +<pre><code>Foo</code><code>Bar</code></pre> .Ed +.Pp +Instead, you can do the following: .Bd -literal -offset indent +Before + +pre {- + @code{-Foo} + @code{-Bar} +} + After -<foo> Hello World </foo> -<bar>Hello World</bar> +<pre> + <code>Foo</code> + <code>Bar</code> +</pre> .Ed +.Pp +If you would like to have the whitespace between the opening- and closing +.Ql pre +tags and the inner +.Ql code +tags removed, you can use the +.Sq = +node instead of the +.Sq - +node. .Sh SEE ALSO .Xr gsp 1 .Pp diff --git a/parser/parser.go b/parser/parser.go index b333506..7d69e30 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -32,7 +32,6 @@ type AstNode struct { Text string Attrs []Attr Children []AstNode - Newline bool } // ParseFile reads and parses a GSP-formatted text file and returns a GSP AST. @@ -73,14 +72,8 @@ func (reader *reader) parseNode() (node AstNode, err error) { return } - switch r { - case '-', '=': + if r == '-' || r == '=' { return reader.parseText(r == '=') - case '>': - node.Newline = true - if _, err = reader.readRune(); err != nil { - return - } } if node.Text, err = reader.parseNodeName(); err != nil { diff --git a/parser/parser_test.go b/parser/parser_test.go index 1851307..66a76bf 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -11,14 +11,14 @@ import ( func TestParseFile(t *testing.T) { s := ` html lang="en" { - >head attr { - >title {- + head attr { + title {- My Website } meta .→{} x="y"{} } - >body { - >div #some-id {} + body { + div #some-id {} div key="val" .class-1 .class-2 { p {- This is some @em{-emphatic} text } } @@ -39,7 +39,7 @@ func TestParseFile(t *testing.T) { s = fmt.Sprintf("%+v", ast) result := `{Type:1 Text: Attrs:[] Children:[{Type:0 Text:html Attrs:[{Key:lang Value:en}] Children:[{Type:0 Text:head Attrs:[{Key:attr Value:}] Children:[{Type:0 Text:title Attrs:[] Children:[{Type:1 Text: Attrs:[] Children:[{Type:3 Text: My Website - Attrs:[] Children:[] Newline:false}] Newline:false}] Newline:true} {Type:0 Text:meta Attrs:[{Key:class Value:→{}} {Key:x Value:y}] Children:[] Newline:false}] Newline:true} {Type:0 Text:body Attrs:[] Children:[{Type:0 Text:div Attrs:[{Key:id Value:some-id}] Children:[] Newline:true} {Type:0 Text:div Attrs:[{Key:key Value:val} {Key:class Value:class-1} {Key:class Value:class-2}] Children:[{Type:0 Text:p Attrs:[] Children:[{Type:1 Text: Attrs:[] Children:[{Type:3 Text: This is some Attrs:[] Children:[] Newline:false} {Type:0 Text:em Attrs:[] Children:[{Type:1 Text: Attrs:[] Children:[{Type:3 Text:emphatic Attrs:[] Children:[] Newline:false}] Newline:false}] Newline:false} {Type:3 Text: text Attrs:[] Children:[] Newline:false}] Newline:false}] Newline:false}] Newline:false} {Type:0 Text:tags Attrs:[{Key:key Value:Some long value}] Children:[] Newline:false}] Newline:true}] Newline:false}] Newline:false}` + Attrs:[] Children:[]}]}]} {Type:0 Text:meta Attrs:[{Key:class Value:→{}} {Key:x Value:y}] Children:[]}]} {Type:0 Text:body Attrs:[] Children:[{Type:0 Text:div Attrs:[{Key:id Value:some-id}] Children:[]} {Type:0 Text:div Attrs:[{Key:key Value:val} {Key:class Value:class-1} {Key:class Value:class-2}] Children:[{Type:0 Text:p Attrs:[] Children:[{Type:1 Text: Attrs:[] Children:[{Type:3 Text: This is some Attrs:[] Children:[]} {Type:0 Text:em Attrs:[] Children:[{Type:1 Text: Attrs:[] Children:[{Type:3 Text:emphatic Attrs:[] Children:[]}]}]} {Type:3 Text: text Attrs:[] Children:[]}]}]}]} {Type:0 Text:tags Attrs:[{Key:key Value:Some long value}] Children:[]}]}]}]}` if s != result { t.Fatalf("ParseFile() parsed unexpected AST ‘%s’", s) } |