From 34a421761567ad0e711673c6e4ef63d9c1840d5a Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 16 Feb 2025 15:46:20 +0700 Subject: all: support document attribute "docdir" The "docdir" attribute contains the full path of the directory that contains the source document. By default it is set to the directory where the Document resided. --- README.md | 1 + document.go | 23 ++++++++++++++++++++--- document_attribute.go | 1 + element_include.go | 10 +++++++--- testdata/include.adoc | 4 +++- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0c5dcf8..561a897 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ Supported document attribute references, * `author(_x)` * `authorinitials(_x)` +* `docdir` * `doctitle` * `email(_x)` * `firstname(_x)` diff --git a/document.go b/document.go index 2ddb3fc..aa89434 100644 --- a/document.go +++ b/document.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "os" + "path/filepath" "strconv" "strings" "time" @@ -42,8 +43,13 @@ type Document struct { Revision Revision - file string - fpath string + file string + + // docdir contains the directory where file located. + // This is the default value when ":docdir:" attribute is set and + // empty. + docdir string + rawAuthors string rawRevision string tocPosition string @@ -110,7 +116,11 @@ func Open(file string) (doc *Document, err error) { doc = newDocument() doc.file = file - doc.Attributes.Entry[docAttrLastUpdateValue] = fi.ModTime().Round(time.Second).Format(`2006-01-02 15:04:05 Z0700`) + doc.docdir = filepath.Dir(file) + + var modTime = fi.ModTime().Round(time.Second).Format(`2006-01-02 15:04:05 Z0700`) + doc.Attributes.Entry[docAttrLastUpdateValue] = modTime + doc.Attributes.Entry[docAttrDocdir] = doc.docdir parse(doc, raw) @@ -304,6 +314,13 @@ func (doc *Document) setAttribute(key, val string) (err error) { } doc.Attributes.Entry[key] = val + case docAttrDocdir: + if val == `` { + doc.Attributes.Entry[key] = doc.docdir + } else { + doc.Attributes.Entry[key] = val + } + default: doc.Attributes.Entry[key] = val } diff --git a/document_attribute.go b/document_attribute.go index c9f8374..9419084 100644 --- a/document_attribute.go +++ b/document_attribute.go @@ -12,6 +12,7 @@ const ( DocAttrKeywords = `keywords` docAttrAuthorInitials = `authorinitials` + docAttrDocdir = `docdir` docAttrDocTitle = `doctitle` docAttrEmail = attrValueEmail docAttrFirstName = `firstname` diff --git a/element_include.go b/element_include.go index 4b36314..187348b 100644 --- a/element_include.go +++ b/element_include.go @@ -43,9 +43,13 @@ 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.file), string(path)) - + var newPath = applySubstitutions(doc, path) + if bytes.Contains(path, []byte(docAttrDocdir)) { + el.fpath = string(newPath) + } else { + el.fpath = filepath.Join(doc.docdir, string(newPath)) + } + log.Printf(`parseInclude: fpath: %s`, el.fpath) el.content, err = os.ReadFile(el.fpath) if err != nil { log.Printf(`parseInclude %q: %s`, el.fpath, err) diff --git a/testdata/include.adoc b/testdata/include.adoc index 5c6c772..09b4f7a 100644 --- a/testdata/include.adoc +++ b/testdata/include.adoc @@ -1,3 +1,5 @@ += Include + include::_includes/list_desc_00.adoc[] -include::_includes/list_desc_01.adoc[] +include::{docdir}/_includes/list_desc_01.adoc[] -- cgit v1.3