From 37563af4c7e82fab352f4ab9137f808058e483e2 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 13 Oct 2024 23:50:54 +0700 Subject: all: fix reading include file when parent path is absolute Previously, if the parent document is opened using absolute path and it contains include directive, the included file will fail to read because the parent path is joined with current working directory. --- document.go | 8 -------- element_include.go | 2 +- element_include_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ testdata/include.adoc | 3 +++ testdata/include_test.txt | 4 ++-- 5 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 element_include_test.go create mode 100644 testdata/include.adoc diff --git a/document.go b/document.go index 78eea2a..cfa3eeb 100644 --- a/document.go +++ b/document.go @@ -8,7 +8,6 @@ import ( "fmt" "io" "os" - "path/filepath" "strconv" "strings" "time" @@ -96,7 +95,6 @@ func newDocument() (doc *Document) { func Open(file string) (doc *Document, err error) { var ( fi os.FileInfo - wd string raw []byte ) @@ -110,13 +108,7 @@ func Open(file string) (doc *Document, err error) { return nil, fmt.Errorf(`Open %s: %w`, file, err) } - wd, err = os.Getwd() - if err != nil { - return nil, fmt.Errorf(`Open %s: %w`, file, err) - } - doc = newDocument() - doc.fpath = filepath.Join(wd, file) doc.file = file doc.Attributes.Entry[docAttrLastUpdateValue] = fi.ModTime().Round(time.Second).Format(`2006-01-02 15:04:05 Z0700`) diff --git a/element_include.go b/element_include.go index fc45b34..4b36314 100644 --- a/element_include.go +++ b/element_include.go @@ -44,7 +44,7 @@ func parseInclude(doc *Document, line []byte) (el *elementInclude) { el.attrs.parseElementAttribute(line[start : start+end+1]) path = applySubstitutions(doc, path) - el.fpath = filepath.Join(filepath.Dir(doc.fpath), string(path)) + el.fpath = filepath.Join(filepath.Dir(doc.file), string(path)) el.content, err = os.ReadFile(el.fpath) if err != nil { diff --git a/element_include_test.go b/element_include_test.go new file mode 100644 index 0000000..f337537 --- /dev/null +++ b/element_include_test.go @@ -0,0 +1,43 @@ +package asciidoctor + +import ( + "bytes" + "os" + "path/filepath" + "testing" + + "git.sr.ht/~shulhan/pakakeh.go/lib/test" +) + +func TestParseIncludeWithAbsolutePath(t *testing.T) { + var ( + tdata *test.Data + err error + ) + tdata, err = test.LoadData(`testdata/include_test.txt`) + if err != nil { + t.Fatal(err) + } + + var wd string + + wd, err = os.Getwd() + if err != nil { + t.Fatal(err) + } + + var fadoc = filepath.Join(wd, `testdata`, `include.adoc`) + var doc *Document + + doc, err = Open(fadoc) + if err != nil { + t.Fatal(err) + } + + var got bytes.Buffer + + doc.ToHTMLEmbedded(&got) + + var exp = string(tdata.Output[`include`]) + test.Assert(t, `ParseIncludeWithAbsolutePath`, exp, got.String()) +} diff --git a/testdata/include.adoc b/testdata/include.adoc new file mode 100644 index 0000000..5c6c772 --- /dev/null +++ b/testdata/include.adoc @@ -0,0 +1,3 @@ +include::_includes/list_desc_00.adoc[] + +include::_includes/list_desc_01.adoc[] diff --git a/testdata/include_test.txt b/testdata/include_test.txt index ba8a0bb..eca5c6d 100644 --- a/testdata/include_test.txt +++ b/testdata/include_test.txt @@ -1,12 +1,12 @@ Test include directive with list description that contains open block. ->>> list_description +>>> include include::testdata/_includes/list_desc_00.adoc[] include::testdata/_includes/list_desc_01.adoc[] -<<< list_description +<<< include
-- cgit v1.3