forked from bytesparadise/libasciidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
182 lines (155 loc) · 5.33 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package renderer
import (
"context"
"time"
"github.com/bytesparadise/libasciidoc/pkg/types"
log "github.com/sirupsen/logrus"
)
// Context is a custom implementation of the standard golang context.Context interface,
// which carries the types.Document which is being processed
type Context struct {
context context.Context
Document types.Document
options map[string]interface{}
}
// Wrap wraps the given `ctx` context into a new context which will contain the given `document` document.
func Wrap(ctx context.Context, document types.Document, options ...Option) *Context {
result := &Context{
context: ctx,
Document: document,
options: make(map[string]interface{}),
}
for _, option := range options {
option(result)
}
return result
}
const includeBlankLine string = "includeBlankLine"
// SetIncludeBlankLine sets the rendering context to include (or not) the blank lines
func (ctx *Context) SetIncludeBlankLine(b bool) bool {
var oldvalue bool
if v, ok := ctx.options[includeBlankLine].(bool); ok {
oldvalue = v
}
ctx.options[includeBlankLine] = b
log.Debugf("set '%s' context param to '%t' (was '%t' before)", includeBlankLine, b, oldvalue)
return oldvalue
}
// IncludeBlankLine indicates if blank lines should be rendered (default false)
func (ctx *Context) IncludeBlankLine() bool {
if b, found := ctx.options[includeBlankLine].(bool); found {
return b
}
// by default, ignore blank lines
return false
}
const withinDelimitedBlock string = "withinDelimitedBlock"
// SetWithinDelimitedBlock sets the rendering context to be within a delimited block
func (ctx *Context) SetWithinDelimitedBlock(b bool) bool {
log.Debugf("set rendering elements within a delimited block to `%t`", b)
var oldvalue bool
if v, ok := ctx.options[withinDelimitedBlock].(bool); ok {
oldvalue = v
}
log.Debugf("set '%s' context param to '%t' (was '%t' before)", withinDelimitedBlock, b, oldvalue)
ctx.options[withinDelimitedBlock] = b
return oldvalue
}
// WithinDelimitedBlock indicates if the current element to render is within a delimited block or not
func (ctx *Context) WithinDelimitedBlock() bool {
if b, found := ctx.options[withinDelimitedBlock].(bool); found {
log.Debugf("rendering elements within a delimited block? %t", b)
return b
}
// by default, consider not within a block
return false
}
const withinList string = "withinList"
// SetWithinList sets the rendering context to be within a list or a nest list
func (ctx *Context) SetWithinList(w bool) {
// log.Debugf("set rendering elements within a list to `%t`", w)
var counter int
var ok bool
if counter, ok = ctx.options[withinList].(int); ok {
// keep track of the depth of the list
if w {
counter++
} else {
counter--
}
} else {
if w {
counter = 1
} else {
counter = 0
}
}
// update the counter in the context
ctx.options[withinList] = counter
}
// WithinList indicates if the current element to render is within a list or not
func (ctx *Context) WithinList() bool {
if counter, found := ctx.options[withinList].(int); found {
// log.Debugf("rendering elements within a list? %t (%d)", (counter > 0), counter)
return counter > 0
}
// by default, ignore blank lines
return false
}
const tableCounter = "tableCounter"
// GetAndIncrementTableCounter returns the current value for the table counter after internally incrementing it.
func (ctx *Context) GetAndIncrementTableCounter() int {
return ctx.getAndIncrementCounter(tableCounter)
}
const imageCounter = "imageCounter"
// GetAndIncrementImageCounter returns the current value for the image counter after internally incrementing it.
func (ctx *Context) GetAndIncrementImageCounter() int {
return ctx.getAndIncrementCounter(imageCounter)
}
const exampleBlockCounter = "exampleBlockCounter"
// GetAndIncrementExampleBlockCounter returns the current value for the example block counter after internally incrementing it.
func (ctx *Context) GetAndIncrementExampleBlockCounter() int {
return ctx.getAndIncrementCounter(exampleBlockCounter)
}
// getAndIncrementCounter returns the current value for the counter after internally incrementing it.
func (ctx *Context) getAndIncrementCounter(counter string) int {
if _, found := ctx.options[counter]; !found {
ctx.options[counter] = 1
}
if c, ok := ctx.options[counter].(int); ok {
ctx.options[counter] = c + 1
return c
}
ctx.options[counter] = 1
log.Warnf("'%s' counter was set to a non-int value", counter)
return 1
}
const imagesdir = "imagesdir"
// GetImagesDir returns the value of the `imagesdir` attribute if it was set (as a string), empty string otherwise
func (ctx *Context) GetImagesDir() string {
if v := ctx.Document.Attributes[imagesdir]; v != nil {
if v, ok := v.(string); ok {
return v
}
}
return ""
}
// -----------------------
// context.Context methods
// -----------------------
// Deadline wrapper implementation of context.Context.Deadline()
func (ctx *Context) Deadline() (deadline time.Time, ok bool) {
return ctx.context.Deadline()
}
// Done wrapper implementation of context.Context.Done()
func (ctx *Context) Done() <-chan struct{} {
return ctx.context.Done()
}
// Err wrapper implementation of context.Context.Err()
func (ctx *Context) Err() error {
return ctx.context.Err()
}
// Value wrapper implementation of context.Context.Value(interface{})
func (ctx *Context) Value(key interface{}) interface{} {
return ctx.context.Value(key)
}