Skip to content

Commit 5a4de35

Browse files
committed
Merge branch 'main' into zap
2 parents edbdaa9 + 2127752 commit 5a4de35

76 files changed

Lines changed: 1235 additions & 606 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/build.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Build and Test
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Set up Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version-file: 'go.mod'
19+
20+
- name: Build
21+
run: go build -v ./...
22+
23+
- name: Test
24+
run: go test -v ./...

asciidoc/counter.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ type Counter struct {
66

77
Name string
88
InitialValue string
9-
Display bool
9+
Display CounterVisibility
1010
}
1111

12-
func NewCounter(name string, initialValue any, display bool) *Counter {
12+
func NewCounter(name string, initialValue any, display CounterVisibility) *Counter {
1313
iv, _ := initialValue.(string)
1414
return &Counter{Name: name, InitialValue: iv, Display: display}
1515
}
@@ -44,6 +44,17 @@ const (
4444
CounterTypeLowerCase
4545
)
4646

47+
type CounterVisibility uint8
48+
49+
const (
50+
CounterVisibilityHidden CounterVisibility = iota
51+
CounterVisibilityVisible
52+
)
53+
54+
func (cb CounterVisibility) Visible() bool {
55+
return cb == CounterVisibilityVisible
56+
}
57+
4758
type CounterState struct {
4859
CounterType CounterType
4960
Value int

asciidoc/describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func Describe(el Element) string {
164164
if el.InitialValue != "" {
165165
s += fmt.Sprintf(" (initial value: %s)", el.InitialValue)
166166
}
167-
if el.Display {
167+
if el.Display.Visible() {
168168
s += " (display)"
169169
}
170170
return s

asciidoc/overlay/overlay.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ func preparseFile(cxt OverlayContext, pps *overlayFileState, d *asciidoc.Documen
123123
}
124124
remove = true
125125
case *asciidoc.IfDef:
126-
suppressStack.Push(&conditionalBlock{suppress: suppress, open: el})
126+
suppressStack.Push(&conditionalBlock{suppress: suppress, open: el, addToCell: addToCell})
127127
suppress = suppress || !el.Eval(pps)
128128
remove = true
129-
addToCell = el.Inline
129+
addToCell = el.Inline || isContentIfDef(parent, index)
130130
case *asciidoc.IfNDef:
131-
suppressStack.Push(&conditionalBlock{suppress: suppress, open: el})
131+
suppressStack.Push(&conditionalBlock{suppress: suppress, open: el, addToCell: addToCell})
132132
suppress = suppress || !el.Eval(pps)
133133
remove = true
134-
addToCell = el.Inline
134+
addToCell = el.Inline || isContentIfDef(parent, index)
135135
case *asciidoc.IfEval:
136-
suppressStack.Push(&conditionalBlock{suppress: suppress, open: el})
136+
suppressStack.Push(&conditionalBlock{suppress: suppress, open: el, addToCell: addToCell})
137137
if !suppress {
138138
var include bool
139139
include, err = el.Eval(pps)
@@ -144,7 +144,7 @@ func preparseFile(cxt OverlayContext, pps *overlayFileState, d *asciidoc.Documen
144144
suppress = !include
145145
}
146146
remove = true
147-
addToCell = el.Inline
147+
addToCell = el.Inline || isContentIfDef(parent, index)
148148
case *asciidoc.IfDefBlock, *asciidoc.IfNDefBlock, *asciidoc.IfEvalBlock:
149149
err = fmt.Errorf("unexpected type in preparse: %T", el)
150150
should = parse.SearchShouldStop
@@ -163,7 +163,7 @@ func preparseFile(cxt OverlayContext, pps *overlayFileState, d *asciidoc.Documen
163163
}
164164
suppress = cb.suppress
165165
remove = true
166-
addToCell = false
166+
addToCell = cb.addToCell
167167
case *asciidoc.InlineIfDef:
168168
if !suppress {
169169
if el.Eval(pps) {
@@ -365,3 +365,26 @@ func parseLevelOffset(el asciidoc.Parent, elements asciidoc.Elements) (leveloffs
365365
leveloffset, relative, err = text.ParseRelativeNumber(val)
366366
return
367367
}
368+
369+
func isContentIfDef(parent asciidoc.ParentElement, index int) bool {
370+
if _, ok := parent.(*asciidoc.Table); !ok {
371+
return false
372+
}
373+
children := parent.Children()
374+
depth := 0
375+
for i := index + 1; i < len(children); i++ {
376+
child := children[i]
377+
switch child.(type) {
378+
case *asciidoc.IfDef, *asciidoc.IfNDef, *asciidoc.IfEval:
379+
depth++
380+
case *asciidoc.EndIf:
381+
if depth == 0 {
382+
return true
383+
}
384+
depth--
385+
case *asciidoc.TableRow:
386+
return false
387+
}
388+
}
389+
return false
390+
}

asciidoc/overlay/state.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
)
88

99
type conditionalBlock struct {
10-
open asciidoc.Element
11-
suppress bool
10+
open asciidoc.Element
11+
suppress bool
12+
addToCell bool
1213
}
1314

1415
type overlayState struct {

asciidoc/parse/asciidoc.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

asciidoc/render/attributes.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,19 @@ const (
3030
AttributeFilterFloat
3131
)
3232

33+
type attributeRenderType uint8
34+
35+
const (
36+
attributeRenderTypeBlock attributeRenderType = iota
37+
attributeRenderTypeInline
38+
)
39+
3340
func shouldRenderAttributeType(at AttributeFilter, include AttributeFilter, exclude AttributeFilter) bool {
3441
return ((at & include) == at) && ((at & exclude) != at)
3542
}
3643

37-
func renderAttributes(cxt Target, attributes []asciidoc.Attribute, inline bool) error {
38-
_, err := renderSelectAttributes(cxt, attributes, AttributeFilterAll, AttributeFilterNone, inline)
44+
func renderAttributes(cxt Target, attributes []asciidoc.Attribute, renderType attributeRenderType) error {
45+
_, err := renderSelectAttributes(cxt, attributes, AttributeFilterAll, AttributeFilterNone, renderType)
3946
return err
4047
}
4148

@@ -67,12 +74,12 @@ func getAttributeType(name asciidoc.AttributeName) AttributeFilter {
6774
return AttributeFilterNone
6875
}
6976

70-
func renderSelectAttributes(cxt Target, attributes []asciidoc.Attribute, include AttributeFilter, exclude AttributeFilter, inline bool) (n int, err error) {
77+
func renderSelectAttributes(cxt Target, attributes []asciidoc.Attribute, include AttributeFilter, exclude AttributeFilter, renderType attributeRenderType) (n int, err error) {
7178
if len(attributes) == 0 {
7279
return
7380
}
7481

75-
type attributeClass uint32
82+
type attributeClass uint8
7683

7784
const (
7885
attributeClassNone attributeClass = iota
@@ -136,7 +143,7 @@ func renderSelectAttributes(cxt Target, attributes []asciidoc.Attribute, include
136143
continue
137144
}
138145
for _, ta := range anchors {
139-
err = renderAttributeAnchor(cxt, ta, include, exclude, inline)
146+
err = renderAttributeAnchor(cxt, ta, include, exclude, renderType)
140147
if err != nil {
141148
return
142149
}
@@ -170,7 +177,7 @@ func renderSelectAttributes(cxt Target, attributes []asciidoc.Attribute, include
170177
if len(filtered) == 0 {
171178
continue
172179
}
173-
if !inline {
180+
if renderType != attributeRenderTypeInline {
174181
cxt.EnsureNewLine()
175182
}
176183
cxt.FlushWrap()
@@ -216,7 +223,7 @@ func renderSelectAttributes(cxt Target, attributes []asciidoc.Attribute, include
216223
n++
217224
}
218225
cxt.WriteString("]")
219-
if !inline {
226+
if renderType != attributeRenderTypeInline {
220227
cxt.WriteString("\n")
221228
}
222229
cxt.EndBlock()
@@ -229,10 +236,10 @@ func renderSelectAttributes(cxt Target, attributes []asciidoc.Attribute, include
229236
return
230237
}
231238

232-
func renderAttributeAnchor(cxt Target, anchor *asciidoc.AnchorAttribute, include AttributeFilter, exclude AttributeFilter, inline bool) (err error) {
239+
func renderAttributeAnchor(cxt Target, anchor *asciidoc.AnchorAttribute, include AttributeFilter, exclude AttributeFilter, renderType attributeRenderType) (err error) {
233240
id := anchor.ID
234241
if len(id) > 0 && shouldRenderAttributeType(AttributeFilterID, include, exclude) {
235-
if !inline {
242+
if renderType != attributeRenderTypeInline {
236243
cxt.EnsureNewLine()
237244
}
238245
cxt.FlushWrap()
@@ -255,7 +262,7 @@ func renderAttributeAnchor(cxt Target, anchor *asciidoc.AnchorAttribute, include
255262
}
256263
}
257264
cxt.WriteString("]]")
258-
if !inline {
265+
if renderType != attributeRenderTypeInline {
259266
cxt.WriteRune('\n')
260267
}
261268
cxt.EndBlock()

asciidoc/render/delimited.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "github.com/project-chip/alchemy/asciidoc"
55
func renderDelimitedLines(cxt Target, el asciidoc.HasLines, delimiter asciidoc.Delimiter) (err error) {
66
cxt.FlushWrap()
77
if ae, ok := el.(asciidoc.Attributable); ok {
8-
err = renderAttributes(cxt, ae.Attributes(), false)
8+
err = renderAttributes(cxt, ae.Attributes(), attributeRenderTypeBlock)
99
if err != nil {
1010
return
1111
}
@@ -24,7 +24,7 @@ func renderDelimitedLines(cxt Target, el asciidoc.HasLines, delimiter asciidoc.D
2424
func renderDelimitedElements(cxt Target, el asciidoc.ParentElement, delimiter asciidoc.Delimiter) (err error) {
2525
cxt.FlushWrap()
2626
if ae, ok := el.(asciidoc.Attributable); ok {
27-
err = renderAttributes(cxt, ae.Attributes(), false)
27+
err = renderAttributes(cxt, ae.Attributes(), attributeRenderTypeBlock)
2828
if err != nil {
2929
return
3030
}
@@ -44,7 +44,7 @@ func renderDelimitedElements(cxt Target, el asciidoc.ParentElement, delimiter as
4444

4545
func renderFencedBlock(cxt Target, el *asciidoc.FencedBlock) (err error) {
4646
cxt.FlushWrap()
47-
err = renderAttributes(cxt, el.Attributes(), false)
47+
err = renderAttributes(cxt, el.Attributes(), attributeRenderTypeBlock)
4848
if err != nil {
4949
return
5050
}

asciidoc/render/link.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func renderLink(cxt Target, il *asciidoc.Link) (err error) {
1212
return
1313
}
1414

15-
err = renderAttributes(cxt, il.Attributes(), true)
15+
err = renderAttributes(cxt, il.Attributes(), attributeRenderTypeInline)
1616
cxt.EndBlock()
1717
return
1818
}
@@ -26,15 +26,15 @@ func renderLinkMacro(cxt Target, il *asciidoc.LinkMacro) (err error) {
2626
return
2727
}
2828

29-
err = renderAttributes(cxt, il.Attributes(), true)
29+
err = renderAttributes(cxt, il.Attributes(), attributeRenderTypeInline)
3030
cxt.EndBlock()
3131
return
3232
}
3333

3434
func renderImageBlock(cxt Target, ib *asciidoc.BlockImage) (err error) {
3535
cxt.FlushWrap()
3636
cxt.EnsureNewLine()
37-
_, err = renderSelectAttributes(cxt, ib.Attributes(), AttributeFilterID|AttributeFilterTitle, AttributeFilterNone, false)
37+
_, err = renderSelectAttributes(cxt, ib.Attributes(), AttributeFilterID|AttributeFilterTitle, AttributeFilterNone, attributeRenderTypeBlock)
3838
if err != nil {
3939
return
4040
}
@@ -46,7 +46,7 @@ func renderImageBlock(cxt Target, ib *asciidoc.BlockImage) (err error) {
4646
return
4747
}
4848
var count int
49-
count, err = renderSelectAttributes(cxt, ib.Attributes(), AttributeFilterAll, AttributeFilterID|AttributeFilterTitle|AttributeFilterCols, true)
49+
count, err = renderSelectAttributes(cxt, ib.Attributes(), AttributeFilterAll, AttributeFilterID|AttributeFilterTitle|AttributeFilterCols, attributeRenderTypeInline)
5050
if err != nil {
5151
return
5252
}
@@ -61,7 +61,7 @@ func renderImageBlock(cxt Target, ib *asciidoc.BlockImage) (err error) {
6161
func renderInlineImage(cxt Target, ib *asciidoc.InlineImage) (err error) {
6262
cxt.FlushWrap()
6363
cxt.EnsureNewLine()
64-
_, err = renderSelectAttributes(cxt, ib.Attributes(), AttributeFilterID|AttributeFilterTitle, AttributeFilterNone, false)
64+
_, err = renderSelectAttributes(cxt, ib.Attributes(), AttributeFilterID|AttributeFilterTitle, AttributeFilterNone, attributeRenderTypeBlock)
6565
if err != nil {
6666
return
6767
}
@@ -73,7 +73,7 @@ func renderInlineImage(cxt Target, ib *asciidoc.InlineImage) (err error) {
7373
return
7474
}
7575
var count int
76-
count, err = renderSelectAttributes(cxt, ib.Attributes(), AttributeFilterAll, AttributeFilterID|AttributeFilterTitle|AttributeFilterCols, true)
76+
count, err = renderSelectAttributes(cxt, ib.Attributes(), AttributeFilterAll, AttributeFilterID|AttributeFilterTitle|AttributeFilterCols, attributeRenderTypeInline)
7777
if err != nil {
7878
return
7979
}

asciidoc/render/list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func renderOrderedListElement(cxt Target, el *asciidoc.OrderedListItem) (err err
88
cxt.DisableWrap()
99
cxt.EnsureNewLine()
1010

11-
err = renderAttributes(cxt, el.Attributes(), false)
11+
err = renderAttributes(cxt, el.Attributes(), attributeRenderTypeBlock)
1212
if err != nil {
1313
return
1414
}
@@ -25,7 +25,7 @@ func renderUnorderedListElement(cxt Target, el *asciidoc.UnorderedListItem) (err
2525
cxt.DisableWrap()
2626
cxt.EnsureNewLine()
2727

28-
err = renderAttributes(cxt, el.Attributes(), false)
28+
err = renderAttributes(cxt, el.Attributes(), attributeRenderTypeBlock)
2929
if err != nil {
3030
return
3131
}
@@ -39,7 +39,7 @@ func renderUnorderedListElement(cxt Target, el *asciidoc.UnorderedListItem) (err
3939

4040
func renderDescriptionListItem(cxt Target, el *asciidoc.DescriptionListItem) (err error) {
4141
cxt.FlushWrap()
42-
err = renderAttributes(cxt, el.Attributes(), false)
42+
err = renderAttributes(cxt, el.Attributes(), attributeRenderTypeBlock)
4343
if err != nil {
4444
return
4545
}

0 commit comments

Comments
 (0)