diff options
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | document.go | 23 | ||||
| -rw-r--r-- | document_attribute.go | 1 | ||||
| -rw-r--r-- | element_include.go | 10 | ||||
| -rw-r--r-- | testdata/include.adoc | 4 |
5 files changed, 32 insertions, 7 deletions
@@ -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[] |
