Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser): support for single-line ifdef/ifndef directives #899

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/parser/document_preprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ func preprocess(ctx *ParseContext, source io.Reader) (string, error) {
t, _ := e.RawText()
b.WriteString(t)
case types.ConditionalInclusion:
b.enabled = c.push(ctx, e)
if content, ok := e.SingleLineContent(); ok {
if e.Eval(ctx.attributes.allAttributes()) {
b.WriteString(content)
}
} else {
b.enabled = c.push(ctx, e)
}
case *types.EndOfCondition:
b.enabled = c.pop()
default:
Expand Down
54 changes: 54 additions & 0 deletions pkg/parser/document_preprocessing_conditionals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,33 @@ cookie content (2)
closing content`
Expect(PreparseDocument(source)).To(Equal(expected))
})

Context("single-line", func() {

It("with attribute not defined", func() {
source := `* some content
ifdef::cookie[* conditional content]
* more content`
expected := `* some content
* more content`

Expect(PreparseDocument(source)).To(Equal(expected))
})

It("with attribute defined", func() {
source := `:cookie:

* some content
ifdef::cookie[* conditional content]
* more content`
expected := `:cookie:

* some content
* conditional content
* more content`
Expect(PreparseDocument(source)).To(Equal(expected))
})
})
})

Context("ifndef", func() {
Expand Down Expand Up @@ -197,6 +224,33 @@ cookie content (2)
closing content`
Expect(PreparseDocument(source)).To(Equal(expected))
})

Context("single-line", func() {

It("with attribute not defined", func() {
source := `* some content
ifndef::cookie[* conditional content]
* more content`
expected := `* some content
* conditional content
* more content`

Expect(PreparseDocument(source)).To(Equal(expected))
})

It("with attribute defined", func() {
source := `:cookie:

* some content
ifndef::cookie[* conditional content]
* more content`
expected := `:cookie:

* some content
* more content`
Expect(PreparseDocument(source)).To(Equal(expected))
})
})
})

Context("ifeval", func() {
Expand Down
Loading