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 concealed index terms #475

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
121 changes: 121 additions & 0 deletions pkg/parser/concealed_index_terms_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package parser_test

import (
"github.com/bytesparadise/libasciidoc/pkg/types"
. "github.com/bytesparadise/libasciidoc/testsupport"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("concealed index terms", func() {

Context("draft document", func() {

It("index term in existing paragraph line", func() {
source := `a paragraph with an index term (((index, term, here))).`
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: [][]interface{}{
{
types.StringElement{
Content: "a paragraph with an index term ",
},
types.ConceleadIndexTerm{
Term1: "index",
Term2: "term",
Term3: "here",
},
types.StringElement{
Content: ".",
},
},
},
},
},
}
Expect(source).To(BecomeDraftDocument(expected))
})

It("index term in single paragraph line", func() {
source := `(((index, term)))
a paragraph with an index term.`
expected := types.DraftDocument{
Blocks: []interface{}{
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: [][]interface{}{
{
types.ConceleadIndexTerm{
Term1: "index",
Term2: "term",
},
},
{
types.StringElement{
Content: "a paragraph with an index term.",
},
},
},
},
},
}
Expect(source).To(BecomeDraftDocument(expected))
})
})

Context("final document", func() {

It("index term in existing paragraph line", func() {
source := `a paragraph with an index term (((index, term, here))).`
expected := types.Document{
Attributes: types.DocumentAttributes{},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Elements: []interface{}{
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: [][]interface{}{
{
types.StringElement{
Content: "a paragraph with an index term ",
},
types.StringElement{
Content: ".",
},
},
},
},
},
}
Expect(source).To(BecomeDocument(expected))
})

It("index term in single paragraph line", func() {
source := `(((index, term)))
a paragraph with an index term.`
expected := types.Document{
Attributes: types.DocumentAttributes{},
ElementReferences: types.ElementReferences{},
Footnotes: types.Footnotes{},
FootnoteReferences: types.FootnoteReferences{},
Elements: []interface{}{
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: [][]interface{}{
{
types.StringElement{
Content: "a paragraph with an index term.",
},
},
},
},
},
}
Expect(source).To(BecomeDocument(expected))
})
})
})
20 changes: 11 additions & 9 deletions pkg/parser/document_processing_filter_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ elements:
}

// AllMatchers all the matchers needed to remove the unneeded blocks/elements from the final document
var allMatchers = []filterMatcher{blankLineMatcher, emptyPreambleMatcher, documentAttributeMatcher, singleLineCommentMatcher, commentBlockMatcher}
var allMatchers = []filterMatcher{blankLineMatcher, emptyPreambleMatcher, documentAttributeMatcher, singleLineCommentMatcher, commentBlockMatcher, concealedIndexTermMatcher}

// filterMatcher returns true if the given element is to be filtered out
type filterMatcher func(element interface{}) bool
Expand All @@ -87,8 +87,8 @@ var emptyPreambleMatcher filterMatcher = func(element interface{}) bool {

// blankLineMatcher filters the element if it is a blank line
var blankLineMatcher filterMatcher = func(element interface{}) bool {
_, result := element.(types.BlankLine)
return result
_, ok := element.(types.BlankLine)
return ok
}

// documentAttributeMatcher filters the element if it is a DocumentAttributeDeclaration,
Expand All @@ -104,12 +104,8 @@ var documentAttributeMatcher filterMatcher = func(element interface{}) bool {

// singleLineCommentMatcher filters the element if it is a SingleLineComment
var singleLineCommentMatcher filterMatcher = func(element interface{}) bool {
switch element.(type) {
case types.SingleLineComment:
return true
default:
return false
}
_, ok := element.(types.SingleLineComment)
return ok
}

// commentBlockMatcher filters the element if it is a DelimitedBlock of kind 'Comment'
Expand All @@ -121,3 +117,9 @@ var commentBlockMatcher filterMatcher = func(element interface{}) bool {
return false
}
}

// concealedIndexTermMatcher filters the element if it is a ConcealedIndexTerm
var concealedIndexTermMatcher filterMatcher = func(element interface{}) bool {
_, ok := element.(types.ConceleadIndexTerm)
return ok
}
Loading