Skip to content

Commit

Permalink
added @Bool (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
samlotti committed Jan 14, 2023
2 parents 9bfda83 + f32eabd commit e1f8bd4
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 2 deletions.
14 changes: 14 additions & 0 deletions blipUtil/RtsTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ func (t *BlipUtil) HasError(ctx context.Context) bool {
return ctx.Value("errors") != nil
}

func (t *BlipUtil) WriteBool(w io.Writer, val bool) {
s := "true"
if !val {
s = "false"
}
_, err := w.Write([]byte(s))
if err != nil {
if err != nil {
log.Println(fmt.Sprintf("blip had error writing: %s\n", err))
}
panic(err)
}
}

func (t *BlipUtil) WriteInt(w io.Writer, val int) {
_, err := w.Write([]byte(strconv.Itoa(val)))
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions docs/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ This should never be used for user generated content.
#### @int= ... @ and @int64= ... @
The value must be int or int64 types and will for formatted using iToA conversion.

#### @bool= ... @
The value must be a bool and will render as true or false

#### @text ... @end
Writes the content to the output. This is only needed if there is some @ signs in the content and don't want to escape with @@.
Note that between commands in the template the content is written to the output.
Expand Down
2 changes: 1 addition & 1 deletion internal/blip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
)

var Version = "0.8.9"
var Version = "0.8.10"
var Name = "Blip Template Compiler"

type BlipOptions struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
LITERAL = "LITERAL"
ARG = "@arg" // Literal will be the remainder of the line
CONTEXT = "@context" // Context variable expected
ATDisplayBool = "@bool=" // write integer
ATDisplayInt = "@int=" // write integer
ATDisplayInt64 = "@int64=" // write integer
ATDisplay = "@=" // Literal will be up to the eol/eof or next @ @= name @
Expand Down Expand Up @@ -347,6 +348,9 @@ func (l *Lexer) pickCommand() Token {
// tkn = l.newTokenStr(ILLEGAL, fmt.Sprintf("@ invalid in the : %s", tkn.Type))
//}
advance = true
case "@bool=":
tkn = l.newTokenStr(ATDisplayBool, l.readTilStrSingleLine([]rune{'@'}))
advance = true
case "@int=":
tkn = l.newTokenStr(ATDisplayInt, l.readTilStrSingleLine([]rune{'@'}))
advance = true
Expand Down
3 changes: 3 additions & 0 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
NODE_TEXT
NODE_DISPLAY
NODE_DISPLAY_RAW
NODE_DISPLAY_BOOL
NODE_DISPLAY_INT
NODE_DISPLAY_INT64
NODE_TOKEN_RAW
Expand Down Expand Up @@ -182,6 +183,8 @@ func (p *Parser) parseNode(node ast, isRoot bool, terminators []TokenType) *Toke
node.addChild(newAst(node, NODE_DISPLAY, token))
case ATDisplayInt:
node.addChild(newAst(node, NODE_DISPLAY_INT, token))
case ATDisplayBool:
node.addChild(newAst(node, NODE_DISPLAY_BOOL, token))
case ATDisplayInt64:
node.addChild(newAst(node, NODE_DISPLAY_INT64, token))
case ATDisplayUnsafe:
Expand Down
3 changes: 2 additions & 1 deletion internal/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func TestParserText(t *testing.T) {
<tr><td>"abc"</td></tr>
@end
@arg test string
@bool= true @
`
lex := NewLexer(sample, "TestLexer1")
parser := New(lex)
Expand Down Expand Up @@ -196,5 +197,5 @@ func TestParserText(t *testing.T) {
// @code is not touched
assert.True(t, strings.Contains(result, "\\n<tr><td>@code</td></tr>\\n"))
assert.True(t, strings.Contains(result, "<tr><td>\\\"abc\\\"</td></tr>\\n"))

assert.True(t, strings.Contains(result, "si.WriteBool(w, true )"))
}
2 changes: 2 additions & 0 deletions internal/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ func (r *Render) writeBody(node ast, depth int, o io.Writer, opt *BlipOptions) {
// si.WriteStr(w, game.Opponent)
r.wStr(o, fmt.Sprintf("%ssi.WriteStrSafe(w, %s, escaper)\n", tabs, r.addSlashes(base.token.Literal)))
// f.Sp "si.Write(w, indexpage1)")
case NODE_DISPLAY_BOOL:
r.wStr(o, fmt.Sprintf("%ssi.WriteBool(w, %s)\n", tabs, r.addSlashes(base.token.Literal)))
case NODE_DISPLAY_INT:
r.wStr(o, fmt.Sprintf("%ssi.WriteInt(w, %s)\n", tabs, r.addSlashes(base.token.Literal)))
case NODE_DISPLAY_INT64:
Expand Down

0 comments on commit e1f8bd4

Please sign in to comment.