Skip to content

Commit

Permalink
feat(parser): do not further processing inclusions in non-asciidoc files
Browse files Browse the repository at this point in the history
do not process 'include' macros in non-asciidoc files that are included
in the parent document.

also, move the 'IsAsciidoc' func from 'types' to 'parser' pkg.

fixes bytesparadise#380

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed Oct 29, 2019
1 parent bfff288 commit 128e18e
Show file tree
Hide file tree
Showing 11 changed files with 3,209 additions and 3,041 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ generate: prebuild-checks
generate-optimized:
@echo "generating the parser (optimized)..."
@pigeon -optimize-parser \
-alternate-entrypoints PreflightDocument,PreflightDocumentWithinDelimitedBlock,DocumentBlock,InlineElementsWithoutSubtitution,FileLocation,IncludedFileLine \
-alternate-entrypoints PreflightAsciidocDocument,PreflightAsciidocDocumentWithinDelimitedBlock,PreflightTextDocument,DocumentBlock,InlineElementsWithoutSubtitution,FileLocation,IncludedFileLine \
-o ./pkg/parser/parser.go ./pkg/parser/parser.peg

.PHONY: test
Expand Down
4 changes: 2 additions & 2 deletions pkg/parser/document_preprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const LevelOffset ContextKey = "leveloffset"

// ParsePreflightDocument parses a document's content and applies the preprocessing directives (file inclusions)
func ParsePreflightDocument(filename string, r io.Reader, opts ...Option) (types.PreflightDocument, error) {
opts = append(opts, Entrypoint("PreflightDocument"))
opts = append(opts, Entrypoint("PreflightAsciidocDocument"))
return parsePreflightDocument(filename, r, "", opts...)
}

Expand Down Expand Up @@ -56,7 +56,7 @@ func parseElements(filename string, elements []interface{}, attrs types.Document
case types.DelimitedBlock:
elmts, err := parseElements(filename, e.Elements, attrs, levelOffset,
// use a new var to avoid overridding the current one which needs to stay as-is for the rest of the doc parsing
append(opts, Entrypoint("PreflightDocumentWithinDelimitedBlock"))...)
append(opts, Entrypoint("PreflightAsciidocDocumentWithinDelimitedBlock"))...)
if err != nil {
return nil, err
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/parser/file_inclusion.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func parseFileToInclude(filename string, incl types.FileInclusion, attrs types.D
}
// parse the content, and returns the corresponding elements
levelOffset := incl.Attributes.GetAsString(types.AttrLevelOffset)
if !IsAsciidoc(absPath) {
opts = append(opts, Entrypoint("PreflightTextDocument"))
}

return parsePreflightDocument(absPath, content, levelOffset, opts...)
}

Expand Down Expand Up @@ -251,3 +255,9 @@ func open(path string) (*os.File, string, func(), error) {
}
}, nil
}

// IsAsciidoc returns true if the file to include is an asciidoc file (based on the file location extension)
func IsAsciidoc(path string) bool {
ext := filepath.Ext(path)
return ext == ".asciidoc" || ext == ".adoc" || ext == ".ad" || ext == ".asc" || ext == ".txt"
}
58 changes: 58 additions & 0 deletions pkg/parser/file_inclusion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ var _ = Describe("file location", func() {
)
})

var _ = Describe("file inclusions", func() {

DescribeTable("check asciidoc file",
func(path string, expectation bool) {
Expect(parser.IsAsciidoc(path)).To(Equal(expectation))
},
Entry("foo.adoc", "foo.adoc", true),
Entry("foo.asc", "foo.asc", true),
Entry("foo.ad", "foo.ad", true),
Entry("foo.asciidoc", "foo.asciidoc", true),
Entry("foo.txt", "foo.txt", true),
Entry("foo.csv", "foo.csv", false),
Entry("foo.go", "foo.go", false),
)
})

var _ = Describe("file inclusions - preflight with preprocessing", func() {

It("should include adoc file without leveloffset from local file", func() {
Expand Down Expand Up @@ -266,6 +282,48 @@ include::{includedir}/chapter-a.adoc[]`
Expect(source).To(BecomePreflightDocument(expected))
})

It("should not further process with non-asciidoc files", func() {
source := `:includedir: ../../test/includes
include::{includedir}/include.foo[]`
expected := types.PreflightDocument{
Blocks: []interface{}{
types.DocumentAttributeDeclaration{
Name: "includedir",
Value: "../../test/includes",
},
types.BlankLine{},
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: []types.InlineElements{
{
types.QuotedText{
Kind: types.Bold,
Elements: types.InlineElements{
types.StringElement{
Content: "some strong content",
},
},
},
},
},
},
types.BlankLine{},
types.Paragraph{
Attributes: types.ElementAttributes{},
Lines: []types.InlineElements{
{
types.StringElement{
Content: "include::hello_world.go.txt[]",
},
},
},
},
},
}
Expect(source).To(BecomePreflightDocument(expected, WithFilename("foo.bar"))) // parent doc may not need to be a '.adoc'
})

Context("file inclusions in delimited blocks", func() {

It("should include adoc file within fenced block", func() {
Expand Down
Loading

0 comments on commit 128e18e

Please sign in to comment.