diff options
| author | Shulhan <ms@kilabit.info> | 2025-02-16 15:43:05 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2025-02-16 15:43:05 +0700 |
| commit | 5aac8b7676140dcb803fc1614334a92698e35429 (patch) | |
| tree | e9066a340db4fd39ce92cb023589c132add70724 | |
| parent | 8daebbd6b41177ee1e0de79257f162aa4b486766 (diff) | |
| download | asciidoctor-go-5aac8b7676140dcb803fc1614334a92698e35429.tar.xz | |
all: move setting attribute to Document
Some of attribute may depends on property of the Document.
| -rw-r--r-- | document.go | 37 | ||||
| -rw-r--r-- | document_attribute.go | 37 | ||||
| -rw-r--r-- | document_attribute_test.go | 100 | ||||
| -rw-r--r-- | document_parser.go | 4 | ||||
| -rw-r--r-- | document_test.go | 88 | ||||
| -rw-r--r-- | element.go | 2 |
6 files changed, 128 insertions, 140 deletions
diff --git a/document.go b/document.go index 29d589e..2ddb3fc 100644 --- a/document.go +++ b/document.go @@ -274,6 +274,43 @@ func (doc *Document) haveHeader() bool { return false } +// setAttribute store the document attribute val by its key. +func (doc *Document) setAttribute(key, val string) (err error) { + if key[0] == '!' { + key = strings.TrimSpace(key[1:]) + delete(doc.Attributes.Entry, key) + return nil + } + var n = len(key) + if key[n-1] == '!' { + key = strings.TrimSpace(key[:n-1]) + delete(doc.Attributes.Entry, key) + return nil + } + + val = strings.TrimSpace(val) + + switch key { + case docAttrLevelOffset: + var offset int64 + offset, err = strconv.ParseInt(val, 10, 32) + if err != nil { + return fmt.Errorf(`Document: setAttribute: %s invalid value %q`, key, val) + } + if val[0] == '+' || val[0] == '-' { + doc.Attributes.LevelOffset += int(offset) + } else { + doc.Attributes.LevelOffset = int(offset) + } + doc.Attributes.Entry[key] = val + + default: + doc.Attributes.Entry[key] = val + } + + return nil +} + func (doc *Document) toHTMLBody(buf *bytes.Buffer, withHeaderFooter bool) { var ( ok bool diff --git a/document_attribute.go b/document_attribute.go index ec86de1..c9f8374 100644 --- a/document_attribute.go +++ b/document_attribute.go @@ -3,12 +3,6 @@ package asciidoctor -import ( - "fmt" - "strconv" - "strings" -) - // List of document attribute. const ( DocAttrAuthor = `author` // May contain the first author full name only. @@ -81,34 +75,3 @@ func newDocumentAttribute() DocumentAttribute { }, } } - -func (docAttr *DocumentAttribute) apply(key, val string) (err error) { - if key[0] == '!' { - key = strings.TrimSpace(key[1:]) - delete(docAttr.Entry, key) - return nil - } - var n = len(key) - if key[n-1] == '!' { - key = strings.TrimSpace(key[:n-1]) - delete(docAttr.Entry, key) - return nil - } - - if key == docAttrLevelOffset { - var offset int64 - offset, err = strconv.ParseInt(val, 10, 32) - if err != nil { - return fmt.Errorf(`DocumentAttribute: %s: invalid value %q`, key, val) - } - if val[0] == '+' || val[0] == '-' { - docAttr.LevelOffset += int(offset) - goto valid - } - docAttr.LevelOffset = int(offset) - } - -valid: - docAttr.Entry[key] = val - return nil -} diff --git a/document_attribute_test.go b/document_attribute_test.go deleted file mode 100644 index cec898e..0000000 --- a/document_attribute_test.go +++ /dev/null @@ -1,100 +0,0 @@ -// SPDX-FileCopyrightText: 2024 M. Shulhan <ms@kilabit.info> -// SPDX-License-Identifier: GPL-3.0-or-later - -package asciidoctor - -import ( - "testing" - - "git.sr.ht/~shulhan/pakakeh.go/lib/test" -) - -func TestDocumentAttributeApply(t *testing.T) { - type testCase struct { - desc string - key string - val string - expError string - exp DocumentAttribute - } - - var docAttr = DocumentAttribute{ - Entry: map[string]string{ - `key1`: ``, - `key2`: ``, - }, - } - - var listCase = []testCase{{ - key: `key3`, - exp: docAttr, - }, { - desc: `prefix negation`, - key: `!key1`, - exp: DocumentAttribute{ - Entry: map[string]string{ - `key2`: ``, - `key3`: ``, - }, - }, - }, { - desc: `suffix negation`, - key: `key2!`, - exp: DocumentAttribute{ - Entry: map[string]string{ - `key3`: ``, - }, - }, - }, { - desc: `leveloffset +`, - key: docAttrLevelOffset, - val: `+2`, - exp: DocumentAttribute{ - Entry: map[string]string{ - `key3`: ``, - `leveloffset`: `+2`, - }, - LevelOffset: 2, - }, - }, { - desc: `leveloffset -`, - key: docAttrLevelOffset, - val: `-2`, - exp: DocumentAttribute{ - Entry: map[string]string{ - `key3`: ``, - `leveloffset`: `-2`, - }, - LevelOffset: 0, - }, - }, { - desc: `leveloffset`, - key: docAttrLevelOffset, - val: `1`, - exp: DocumentAttribute{ - Entry: map[string]string{ - `key3`: ``, - `leveloffset`: `1`, - }, - LevelOffset: 1, - }, - }, { - desc: `leveloffset: invalid`, - key: docAttrLevelOffset, - val: `*1`, - expError: `DocumentAttribute: leveloffset: invalid value "*1"`, - }} - - var ( - tc testCase - err error - ) - for _, tc = range listCase { - err = docAttr.apply(tc.key, tc.val) - if err != nil { - test.Assert(t, `apply: `+tc.desc, tc.expError, err.Error()) - continue - } - test.Assert(t, `apply: `+tc.desc, tc.exp, docAttr) - } -} diff --git a/document_parser.go b/document_parser.go index 74c14b6..ca167e6 100644 --- a/document_parser.go +++ b/document_parser.go @@ -398,7 +398,7 @@ func (docp *documentParser) parseBlock(parent *element, term int) { } el.Attrs[key] = value } else { - _ = docp.doc.Attributes.apply(key, value) + _ = docp.doc.setAttribute(key, value) parent.addChild(&element{ kind: docp.kind, key: key, @@ -750,7 +750,7 @@ func (docp *documentParser) parseHeader() { var key, value string key, value, ok = docp.parseAttribute(line, false) if ok { - _ = docp.doc.Attributes.apply(key, value) + _ = docp.doc.setAttribute(key, value) } line = nil continue diff --git a/document_test.go b/document_test.go index 0c160c5..54c5def 100644 --- a/document_test.go +++ b/document_test.go @@ -101,3 +101,91 @@ func TestParse_document_title(t *testing.T) { test.Assert(t, `String`, c.expString, got.Title.String()) } } + +func TestDocumentSetAttribute(t *testing.T) { + type testCase struct { + desc string + key string + val string + expError string + exp DocumentAttribute + } + + var doc = newDocument() + clear(doc.Attributes.Entry) + doc.Attributes.Entry[`key1`] = `` + doc.Attributes.Entry[`key2`] = `` + + var listCase = []testCase{{ + key: `key3`, + exp: doc.Attributes, + }, { + desc: `prefix negation`, + key: `!key1`, + exp: DocumentAttribute{ + Entry: map[string]string{ + `key2`: ``, + `key3`: ``, + }, + }, + }, { + desc: `suffix negation`, + key: `key2!`, + exp: DocumentAttribute{ + Entry: map[string]string{ + `key3`: ``, + }, + }, + }, { + desc: `leveloffset +`, + key: docAttrLevelOffset, + val: `+2`, + exp: DocumentAttribute{ + Entry: map[string]string{ + `key3`: ``, + `leveloffset`: `+2`, + }, + LevelOffset: 2, + }, + }, { + desc: `leveloffset -`, + key: docAttrLevelOffset, + val: `-2`, + exp: DocumentAttribute{ + Entry: map[string]string{ + `key3`: ``, + `leveloffset`: `-2`, + }, + LevelOffset: 0, + }, + }, { + desc: `leveloffset`, + key: docAttrLevelOffset, + val: `1`, + exp: DocumentAttribute{ + Entry: map[string]string{ + `key3`: ``, + `leveloffset`: `1`, + }, + LevelOffset: 1, + }, + }, { + desc: `leveloffset: invalid`, + key: docAttrLevelOffset, + val: `*1`, + expError: `Document: setAttribute: leveloffset invalid value "*1"`, + }} + + var ( + tc testCase + err error + ) + for _, tc = range listCase { + err = doc.setAttribute(tc.key, tc.val) + if err != nil { + test.Assert(t, tc.desc+` error`, tc.expError, err.Error()) + continue + } + test.Assert(t, tc.desc, tc.exp, doc.Attributes) + } +} @@ -708,7 +708,7 @@ func (el *element) setStyleAdmonition(admName string) { func (el *element) toHTML(doc *Document, w io.Writer) { switch el.kind { case lineKindAttribute: - _ = doc.Attributes.apply(el.key, el.value) + _ = doc.setAttribute(el.key, el.value) case elKindCrossReference: var ( |
