Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions asciidoc/render/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
20 changes: 12 additions & 8 deletions asciidoc/render/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("}")
}
11 changes: 8 additions & 3 deletions asciidoc/render/target.go
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
10 changes: 6 additions & 4 deletions asciidoc/render/unwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
26 changes: 15 additions & 11 deletions asciidoc/render/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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() {
Expand Down
Loading