Skip to content

Commit

Permalink
feat(parser): support concealed index terms (#475)
Browse files Browse the repository at this point in the history
support the `(((` syntax in paragraphs, but discard
the element from the final document, so no rendering
is done.

Fixes #473

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Jan 12, 2020
1 parent 940da7f commit d295e1b
Show file tree
Hide file tree
Showing 6 changed files with 2,181 additions and 1,841 deletions.
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

0 comments on commit d295e1b

Please sign in to comment.