diff --git a/asciidoc/render/attributes.go b/asciidoc/render/attributes.go index f83713d0..717e6c2c 100644 --- a/asciidoc/render/attributes.go +++ b/asciidoc/render/attributes.go @@ -299,6 +299,10 @@ func quoteAttributeValue(val any) (string, error) { return escapeQuotes(val.Value), nil case asciidoc.AttributeReference: return "{" + val.Name() + "}", nil + case *asciidoc.Counter: + var sb strings.Builder + renderCounterText(&sb, val) + return sb.String(), nil case asciidoc.Elements: var sb strings.Builder for _, a := range val { @@ -322,6 +326,8 @@ func renderNakedAttributeValue(cxt Target, val any) (err error) { cxt.WriteRune('{') cxt.WriteString(val.Name()) cxt.WriteRune('}') + case *asciidoc.Counter: + renderCounterText(cxt, val) case asciidoc.Elements: for _, a := range val { err = renderNakedAttributeValue(cxt, a) diff --git a/asciidoc/render/macro.go b/asciidoc/render/macro.go index afc18d3b..fc954865 100644 --- a/asciidoc/render/macro.go +++ b/asciidoc/render/macro.go @@ -25,16 +25,20 @@ func renderFileInclude(cxt Target, el *asciidoc.FileInclude) (err error) { func renderCounter(cxt Target, el *asciidoc.Counter) { cxt.StartBlock() - cxt.WriteString("{counter") + renderCounterText(cxt, el) + cxt.EndBlock() +} + +func renderCounterText(tb TextBuffer, el *asciidoc.Counter) { + tb.WriteString("{counter") if !el.Display.Visible() { - cxt.WriteRune('2') + tb.WriteRune('2') } - cxt.WriteRune(':') - cxt.WriteString(el.Name) + tb.WriteRune(':') + tb.WriteString(el.Name) if len(el.InitialValue) > 0 { - cxt.WriteRune(':') - cxt.WriteString(el.InitialValue) + tb.WriteRune(':') + tb.WriteString(el.InitialValue) } - cxt.WriteString("}") - cxt.EndBlock() + tb.WriteString("}") } diff --git a/asciidoc/render/target.go b/asciidoc/render/target.go index c37f6633..9dc48b87 100644 --- a/asciidoc/render/target.go +++ b/asciidoc/render/target.go @@ -1,10 +1,15 @@ package render +type TextBuffer interface { + WriteRune(r rune) (int, error) + WriteString(s string) (int, error) + String() string +} + type Target interface { + TextBuffer + EnsureNewLine() - WriteRune(r rune) - WriteString(s string) - String() string EnableWrap() DisableWrap() FlushWrap() diff --git a/asciidoc/render/unwrap.go b/asciidoc/render/unwrap.go index c61c7706..08faf15e 100644 --- a/asciidoc/render/unwrap.go +++ b/asciidoc/render/unwrap.go @@ -18,17 +18,19 @@ func NewUnwrappedTarget(parent context.Context) Target { } } -func (o *unwrappedTarget) WriteString(s string) { +func (o *unwrappedTarget) WriteString(s string) (n int, err error) { rs := []rune(s) if len(rs) > 0 { o.lastRune = rs[len(rs)-1] - o.out.WriteString(s) + n, err = o.out.WriteString(s) } + return } -func (o *unwrappedTarget) WriteRune(r rune) { - o.out.WriteRune(r) +func (o *unwrappedTarget) WriteRune(r rune) (n int, err error) { o.lastRune = r + n, err = o.out.WriteRune(r) + return } func (o *unwrappedTarget) EnsureNewLine() { diff --git a/asciidoc/render/wrap.go b/asciidoc/render/wrap.go index 65535606..e905b7e5 100644 --- a/asciidoc/render/wrap.go +++ b/asciidoc/render/wrap.go @@ -44,7 +44,7 @@ func NewWrappedTarget(parent context.Context, wrapLength int) Target { } } -func (o *wrappedTarget) WriteString(s string) { +func (o *wrappedTarget) WriteString(s string) (n int, err error) { if len(s) == 0 { return } @@ -56,22 +56,24 @@ func (o *wrappedTarget) WriteString(s string) { return } if o.currentBlock != nil { - o.currentBlock.WriteString(s) + n, err = o.currentBlock.WriteString(s) o.lastBlockRune, _ = utf8.DecodeLastRuneInString(s) return } } - o.writeWrappedText(s) + n = o.writeWrappedText(s) + return } -func (o *wrappedTarget) writeWrappedText(s string) { +func (o *wrappedTarget) writeWrappedText(s string) (n int) { for _, r := range s { - o.writeRune(r) + n += o.writeRune(r) } + return } -func (o *wrappedTarget) writeRune(r rune) { +func (o *wrappedTarget) writeRune(r rune) (n int) { index := len(o.out) switch r { case '\r': // We just strip these out as we go @@ -111,7 +113,7 @@ func (o *wrappedTarget) writeRune(r rune) { o.lastSpace = index } } - + n = utf8.RuneLen(r) o.out = utf8.AppendRune(o.out, r) o.lastRune = r if o.indented { @@ -130,7 +132,7 @@ func (o *wrappedTarget) writeRune(r rune) { o.lastNewline = o.lastSpace o.lastInsertedNewline = o.lastSpace o.lastSpace = -1 - + return } func (o *wrappedTarget) writeBlockText(s string) { @@ -150,7 +152,7 @@ func (o *wrappedTarget) insertNewLine() { o.lastRune = '\n' } -func (o *wrappedTarget) WriteRune(r rune) { +func (o *wrappedTarget) WriteRune(r rune) (n int, err error) { if o.disableWrapCount > 0 { switch r { case '\r': // We just strip these out as we go @@ -163,16 +165,18 @@ func (o *wrappedTarget) WriteRune(r rune) { o.lastSpace = len(o.out) } } + n = utf8.RuneLen(r) o.out = utf8.AppendRune(o.out, r) o.lastRune = r return } if o.currentBlock != nil { - o.currentBlock.WriteRune(r) + n, err = o.currentBlock.WriteRune(r) o.lastBlockRune = r return } - o.writeRune(r) + n = o.writeRune(r) + return } func (o *wrappedTarget) EnsureNewLine() {