From 1f83fa89d8585b1fa59df7a400157f8df93ff681 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Wed, 1 Nov 2023 16:38:49 +0100 Subject: Remove the ‘>’ prefix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The rationale was explained on the mailing list[1]. [1]: https://paste.thomasvoss.com/353 --- formatter/formatter.go | 4 --- formatter/formatter_test.go | 14 +++----- gsp.5 | 84 +++++++++++++++++++++++++++++---------------- parser/parser.go | 9 +---- 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("", 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 := ` My Website - - -
-

This is some emphatic text

-` +

This is some emphatic text

` // Write the source to a temp file r := strings.NewReader(s) diff --git a/gsp.5 b/gsp.5 index 0c0570e..41b458c 100644 --- a/gsp.5 +++ b/gsp.5 @@ -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 -
foo
-bar
-baz
+

Hello World

.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
+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 
+tag:
 .Bd -literal -offset indent
 Before
 
->foo {- Hello World  }
- bar {= Hello World  }
+pre {
+	code{-Foo}
+	code{-Bar}
+}
+
+After
+
+
FooBar
.Ed +.Pp +Instead, you can do the following: .Bd -literal -offset indent +Before + +pre {- + @code{-Foo} + @code{-Bar} +} + After - Hello World -Hello World +
+  Foo
+  Bar
+
.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) } -- cgit v1.2.3