Skip to content

Commit

Permalink
feat(parser): include document attribute when processing section 0 (#451
Browse files Browse the repository at this point in the history
)

get the "document attributes" by reading the beginning of the
first blocks of the draft document, and use them while processing
the section 0 title.

Fixes #447

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Dec 22, 2019
1 parent 41b223c commit 0826b73
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 193 deletions.
5 changes: 5 additions & 0 deletions pkg/parser/document_preprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"

"github.com/bytesparadise/libasciidoc/pkg/types"
"github.com/davecgh/go-spew/spew"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -32,6 +33,10 @@ func parseDraftDocument(filename string, r io.Reader, levelOffsets []levelOffset
return types.DraftDocument{}, err
}
doc.Blocks = blocks
if log.IsLevelEnabled(log.DebugLevel) {
log.Debug("draf document:")
spew.Dump(doc)
}
return doc, nil
}

Expand Down
11 changes: 10 additions & 1 deletion pkg/parser/document_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ func ParseDocument(filename string, r io.Reader, opts ...Option) (types.Document
}
}

// also, add all front-matter values
// also, add all front-matter key/values
for k, v := range draftDoc.FrontMatter.Content {
if v, ok := v.(string); ok {
attrs[k] = v
}
}

// also, add all DocumentAttributeDeclaration at the top of the document
documentAttributes := draftDoc.DocumentAttributes()
for k, v := range documentAttributes {
if v, ok := v.(string); ok {
attrs[k] = v
}
}

// apply document attribute substitutions and re-parse paragraphs that were affected
blocks, err := ApplyDocumentAttributeSubstitutions(draftDoc.Blocks, attrs)
if err != nil {
Expand Down
162 changes: 78 additions & 84 deletions pkg/parser/file_inclusion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,99 +14,93 @@ import (
log "github.com/sirupsen/logrus"
)

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

DescribeTable("'FileLocation' pattern",
func(filename string, expected interface{}) {
reader := strings.NewReader(filename)
actual, err := parser.ParseReader(filename, reader, parser.Entrypoint("FileLocation"))
Expect(err).ToNot(HaveOccurred())
GinkgoT().Log("actual result: %s", spew.Sdump(actual))
GinkgoT().Log("expected result: %s", spew.Sdump(expected))
Expect(actual).To(Equal(expected))
var _ = DescribeTable("'FileLocation' pattern",
func(filename string, expected interface{}) {
reader := strings.NewReader(filename)
actual, err := parser.ParseReader(filename, reader, parser.Entrypoint("FileLocation"))
Expect(err).ToNot(HaveOccurred())
GinkgoT().Log("actual result: %s", spew.Sdump(actual))
GinkgoT().Log("expected result: %s", spew.Sdump(expected))
Expect(actual).To(Equal(expected))
},
Entry("'chapter'", "chapter", types.Location{
Elements: []interface{}{
types.StringElement{
Content: "chapter",
},
},
Entry("'chapter'", "chapter", types.Location{
Elements: []interface{}{
types.StringElement{
Content: "chapter",
},
}),
Entry("'chapter.adoc'", "chapter.adoc", types.Location{
Elements: []interface{}{
types.StringElement{
Content: "chapter.adoc",
},
}),
Entry("'chapter.adoc'", "chapter.adoc", types.Location{
Elements: []interface{}{
types.StringElement{
Content: "chapter.adoc",
},
},
}),
Entry("'chapter-a.adoc'", "chapter-a.adoc", types.Location{
Elements: []interface{}{
types.StringElement{
Content: "chapter-a.adoc",
},
}),
Entry("'chapter-a.adoc'", "chapter-a.adoc", types.Location{
Elements: []interface{}{
types.StringElement{
Content: "chapter-a.adoc",
},
},
}),
Entry("'chapter_a.adoc'", "chapter_a.adoc", types.Location{
Elements: []interface{}{
types.StringElement{
Content: "chapter_a.adoc",
},
}),
Entry("'chapter_a.adoc'", "chapter_a.adoc", types.Location{
Elements: []interface{}{
types.StringElement{
Content: "chapter_a.adoc",
},
},
}),
Entry("'../../test/includes/chapter_a.adoc'", "../../test/includes/chapter_a.adoc", types.Location{
Elements: []interface{}{
types.StringElement{
Content: "../../test/includes/chapter_a.adoc",
},
}),
Entry("'../../test/includes/chapter_a.adoc'", "../../test/includes/chapter_a.adoc", types.Location{
Elements: []interface{}{
types.StringElement{
Content: "../../test/includes/chapter_a.adoc",
},
},
}),
Entry("'chapter-{foo}.adoc'", "chapter-{foo}.adoc", types.Location{
Elements: []interface{}{
types.StringElement{
Content: "chapter-",
},
}),
Entry("'chapter-{foo}.adoc'", "chapter-{foo}.adoc", types.Location{
Elements: []interface{}{
types.StringElement{
Content: "chapter-",
},
types.DocumentAttributeSubstitution{
Name: "foo",
},
types.StringElement{
Content: ".adoc",
},
types.DocumentAttributeSubstitution{
Name: "foo",
},
}),
Entry("'{includedir}/chapter-{foo}.adoc'", "{includedir}/chapter-{foo}.adoc", types.Location{
Elements: []interface{}{
types.DocumentAttributeSubstitution{
Name: "includedir",
},
types.StringElement{
Content: "/chapter-",
},
types.DocumentAttributeSubstitution{
Name: "foo",
},
types.StringElement{
Content: ".adoc",
},
types.StringElement{
Content: ".adoc",
},
}),
)
})

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),
)
})
}),
Entry("'{includedir}/chapter-{foo}.adoc'", "{includedir}/chapter-{foo}.adoc", types.Location{
Elements: []interface{}{
types.DocumentAttributeSubstitution{
Name: "includedir",
},
types.StringElement{
Content: "/chapter-",
},
types.DocumentAttributeSubstitution{
Name: "foo",
},
types.StringElement{
Content: ".adoc",
},
},
}),
)

var _ = 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 - draft with preprocessing", func() {

Expand Down
4 changes: 0 additions & 4 deletions pkg/parser/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ image::images/bar.png[]`
Elements: []interface{}{
types.StringElement{Content: "images/foo.png"},
},
// Resolved: "images/foo.png",
},
},
},
Expand Down Expand Up @@ -228,7 +227,6 @@ image::{imagesdir}/foo.png[]`
Elements: []interface{}{
types.StringElement{Content: "./path/to/images/foo.png"},
},
// Resolved: "./path/to/images/foo.png",
},
},
},
Expand Down Expand Up @@ -668,7 +666,6 @@ image::{imagesdir}/foo.png[]`
Elements: []interface{}{
types.StringElement{Content: "images/foo.png"},
},
// Resolved: "images/foo.png",
},
},
},
Expand Down Expand Up @@ -706,7 +703,6 @@ an image:{imagesdir}/foo.png[].`
Elements: []interface{}{
types.StringElement{Content: "./path/to/images/foo.png"},
},
// Resolved: "./path/to/images/foo.png",
},
},
types.StringElement{Content: "."},
Expand Down
Loading

0 comments on commit 0826b73

Please sign in to comment.