Skip to content

Commit

Permalink
fix(parser/renderer): use main file path to start inclusions (#429)
Browse files Browse the repository at this point in the history
use the base dir of the top/root parent as the starting dir
when processing file inclusions

modify matchers to support optional filename

fixes #424

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Oct 26, 2019
1 parent ee0d57b commit 5bc6ab4
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 74 deletions.
6 changes: 3 additions & 3 deletions libasciidoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func ConvertFileToHTML(ctx context.Context, filename string, output io.Writer, o
return nil, errors.Wrapf(err, "error opening %s", filename)
}
defer file.Close()
return ConvertToHTML(ctx, file, output, options...)
return ConvertToHTML(ctx, filename, file, output, options...)
}

// ConvertToHTML converts the content of the given reader `r` into a full HTML document, written in the given writer `output`.
// Returns an error if a problem occurred
func ConvertToHTML(ctx context.Context, r io.Reader, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
func ConvertToHTML(ctx context.Context, filename string, r io.Reader, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
log.Debugf("parsing the asciidoc source...")
doc, err := parser.ParseDocument("", r)
doc, err := parser.ParseDocument(filename, r)
if err != nil {
return nil, errors.Wrapf(err, "error while parsing the document")
}
Expand Down
23 changes: 23 additions & 0 deletions libasciidoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ a paragraph with _italic content_`
Expect(source).To(RenderHTML5Body(expectedContent))
Expect(source).To(RenderHTML5Title(expectedTitle))
})

It("should include adoc file without leveloffset from local file", func() {
source := "include::test/includes/grandchild-include.adoc[]"
expected := `<div class="paragraph">
<p>first line of grandchild</p>
</div>
<div class="paragraph">
<p>last line of grandchild</p>
</div>`
Expect(source).To(RenderHTML5Body(expected, WithFilename("foo.adoc")))
})

It("should include adoc file without leveloffset from relative file", func() {
source := "include::../test/includes/grandchild-include.adoc[]"
expected := `<div class="paragraph">
<p>first line of grandchild</p>
</div>
<div class="paragraph">
<p>last line of grandchild</p>
</div>`

Expect(source).To(RenderHTML5Body(expected, WithFilename("tmp/foo.adoc")))
})
})

Context("complete Document ", func() {
Expand Down
6 changes: 4 additions & 2 deletions pkg/parser/file_inclusion.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ func init() {

func parseFileToInclude(filename string, incl types.FileInclusion, attrs types.DocumentAttributes, opts ...Option) (types.PreflightDocument, error) {
path := incl.Location.Resolve(attrs)
log.Debugf("parsing '%s'...", path)
currentDir := filepath.Dir(filename)
log.Debugf("parsing '%s' from '%s' (%s)", path, currentDir, filename)
log.Debugf("file inclusion attributes: %s", spew.Sdump(incl.Attributes))
f, absPath, done, err := open(path)
f, absPath, done, err := open(filepath.Join(currentDir, path))
defer done()
if err != nil {
return invalidFileErrMsg(filename, path, incl.RawText, err)
Expand Down Expand Up @@ -211,6 +212,7 @@ func open(path string) (*os.File, string, func(), error) {
return nil, "", func() {}, err
}
absPath, err := filepath.Abs(path)
log.Debugf("file path: %s", absPath)
if err != nil {
return nil, "", func() {
log.Debugf("restoring current working dir to: %s", wd)
Expand Down
Loading

0 comments on commit 5bc6ab4

Please sign in to comment.