-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_inline_reference.go
More file actions
297 lines (267 loc) · 11.7 KB
/
Copy pathbuilder_inline_reference.go
File metadata and controls
297 lines (267 loc) · 11.7 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package marksplice
import (
"fmt"
"strings"
"github.com/zoster81/marksplice/internal/splice"
)
type typedInlineReferenceForm uint8
const (
typedInlineReferenceFull typedInlineReferenceForm = iota
typedInlineReferenceCollapsed
typedInlineReferenceShortcut
)
type typedInlineReferenceScope uint8
const (
typedInlineReferencePriorExact typedInlineReferenceScope = iota
typedInlineReferenceDeferredExact
typedInlineReferenceAvailableNormalized
)
type typedInlineReferenceDefinition struct {
label string
destination string
title string
hasTitle bool
}
// ReferenceLinkInline returns one conservative full reference-link construction value.
// The exact reference label must identify one already-appended top-level reference
// definition in the destination DocumentBuilder when the value is appended. It permits
// the same reviewed bounded structured-inline label children as direct links.
func ReferenceLinkInline(reference string, label ...Inline) Inline {
return newTypedReferenceLinkOrImage(inlineConstructionReferenceLink, typedInlineReferenceFull, typedInlineReferencePriorExact, reference, label)
}
// ForwardReferenceLinkInline returns one full reference-link construction value
// resolved only against one explicitly deferred top-level definition.
func ForwardReferenceLinkInline(reference string, label ...Inline) Inline {
return newTypedReferenceLinkOrImage(inlineConstructionReferenceLink, typedInlineReferenceFull, typedInlineReferenceDeferredExact, reference, label)
}
// CollapsedReferenceLinkInline returns one `[label][]` construction value. The
// emitted label must resolve to exactly one available normalized definition.
func CollapsedReferenceLinkInline(label ...Inline) Inline {
return newTypedReferenceLinkOrImage(inlineConstructionReferenceLink, typedInlineReferenceCollapsed, typedInlineReferenceAvailableNormalized, "", label)
}
// ShortcutReferenceLinkInline returns one `[label]` construction value. The
// emitted label must resolve to exactly one available normalized definition.
func ShortcutReferenceLinkInline(label ...Inline) Inline {
return newTypedReferenceLinkOrImage(inlineConstructionReferenceLink, typedInlineReferenceShortcut, typedInlineReferenceAvailableNormalized, "", label)
}
// ReferenceImageInline returns one conservative full reference-image construction value.
// It follows the same exact-definition and structured-alt requirements as ReferenceLinkInline.
func ReferenceImageInline(reference string, alt ...Inline) Inline {
return newTypedReferenceLinkOrImage(inlineConstructionReferenceImage, typedInlineReferenceFull, typedInlineReferencePriorExact, reference, alt)
}
// ForwardReferenceImageInline returns one full reference-image construction
// value resolved only against one explicitly deferred top-level definition.
func ForwardReferenceImageInline(reference string, alt ...Inline) Inline {
return newTypedReferenceLinkOrImage(inlineConstructionReferenceImage, typedInlineReferenceFull, typedInlineReferenceDeferredExact, reference, alt)
}
// CollapsedReferenceImageInline returns one `![alt][]` construction value.
func CollapsedReferenceImageInline(alt ...Inline) Inline {
return newTypedReferenceLinkOrImage(inlineConstructionReferenceImage, typedInlineReferenceCollapsed, typedInlineReferenceAvailableNormalized, "", alt)
}
// ShortcutReferenceImageInline returns one `![alt]` construction value.
func ShortcutReferenceImageInline(alt ...Inline) Inline {
return newTypedReferenceLinkOrImage(inlineConstructionReferenceImage, typedInlineReferenceShortcut, typedInlineReferenceAvailableNormalized, "", alt)
}
func newTypedReferenceLinkOrImage(kind inlineConstructionKind, form typedInlineReferenceForm, scope typedInlineReferenceScope, reference string, content []Inline) Inline {
return Inline{
kind: kind,
reference: reference,
referenceForm: form,
referenceScope: scope,
children: cloneTypedInlineConstruction(content),
}
}
func writeTypedInlineReferenceLinkOrImage(output *strings.Builder, inline Inline, context typedInlineWriteContext) error {
if len(inline.children) == 0 {
return fmt.Errorf("reference link or image label is empty")
}
kind, syntaxStart := typedInlineReferenceOwner(output, inline)
structured := !typedInlineChildrenAreText(inline.children)
proofIndex := appendTypedInlineReferenceOwnerProof(context, kind, structured)
output.WriteByte('[')
labelStart := output.Len()
childContext := context
childContext.policy = typedInlineStructuredNested
childContext.parent = proofIndex
for _, child := range inline.children {
if err := writeTypedInlineConstruction(output, child, childContext); err != nil {
return err
}
}
labelEnd := output.Len()
reference, definition, form, err := resolveTypedInlineReference(inline, output.String()[labelStart:labelEnd], context)
if err != nil {
return err
}
referenceRange := writeTypedInlineReferenceSuffix(output, form, reference)
if structured {
(*context.hierarchy)[proofIndex].SyntaxRange = splice.Range{Start: syntaxStart, End: output.Len()}
(*context.hierarchy)[proofIndex].ContentRange = splice.Range{Start: labelStart, End: labelEnd}
}
*context.referenceExpected = append(*context.referenceExpected, splice.ConstructionReferenceInlineExpectation{
Kind: kind,
Form: form,
SyntaxRange: splice.Range{Start: syntaxStart, End: output.Len()},
LabelRange: splice.Range{Start: labelStart, End: labelEnd},
ReferenceRange: referenceRange,
Reference: reference,
Destination: definition.destination,
Title: definition.title,
HasTitle: definition.hasTitle,
StructuredLabel: structured,
})
return nil
}
func typedInlineReferenceOwner(output *strings.Builder, inline Inline) (splice.Kind, int) {
kind := splice.KindInlineLink
syntaxStart := output.Len()
if inline.kind == inlineConstructionReferenceImage {
kind = splice.KindImage
output.WriteByte('!')
}
return kind, syntaxStart
}
func appendTypedInlineReferenceOwnerProof(context typedInlineWriteContext, kind splice.Kind, structured bool) int {
if !structured {
return -1
}
index := len(*context.hierarchy)
*context.hierarchy = append(*context.hierarchy, splice.ConstructionInlineExpectation{
Kind: kind,
Parent: context.parent,
})
return index
}
func resolveTypedInlineReference(inline Inline, labelSource string, context typedInlineWriteContext) (string, typedInlineReferenceDefinition, splice.ConstructionReferenceInlineForm, error) {
form, ok := spliceTypedInlineReferenceForm(inline.referenceForm)
if !ok {
return "", typedInlineReferenceDefinition{}, form, fmt.Errorf("unsupported typed reference form")
}
if inline.referenceForm != typedInlineReferenceFull {
definition, err := resolveNormalizedTypedInlineReference(labelSource, availableTypedInlineReferenceDefinitions(context))
return labelSource, definition, form, err
}
if err := validateTypedInlineReference(inline.reference); err != nil {
return "", typedInlineReferenceDefinition{}, form, err
}
definition, err := resolveExactTypedInlineReference(inline.reference, inline.referenceScope, context)
return inline.reference, definition, form, err
}
func resolveExactTypedInlineReference(reference string, scope typedInlineReferenceScope, context typedInlineWriteContext) (typedInlineReferenceDefinition, error) {
switch scope {
case typedInlineReferencePriorExact:
return resolveExactTypedInlineReferenceFrom(reference, context.referenceDefinitions, context.referenceDefinitions)
case typedInlineReferenceDeferredExact:
return resolveExactTypedInlineReferenceFrom(reference, context.deferredReferenceDefinitions, availableTypedInlineReferenceDefinitions(context))
default:
return typedInlineReferenceDefinition{}, fmt.Errorf("unsupported typed reference scope")
}
}
func resolveExactTypedInlineReferenceFrom(reference string, candidates, universe []typedInlineReferenceDefinition) (typedInlineReferenceDefinition, error) {
var exact typedInlineReferenceDefinition
count := 0
for _, definition := range candidates {
if definition.label != reference {
continue
}
exact = definition
count++
}
if count != 1 {
return typedInlineReferenceDefinition{}, fmt.Errorf("reference %q must match exactly one definition in its required scope", reference)
}
resolved, err := resolveNormalizedTypedInlineReference(reference, universe)
if err != nil {
return typedInlineReferenceDefinition{}, err
}
if resolved != exact {
return typedInlineReferenceDefinition{}, fmt.Errorf("reference %q normalized resolution changed", reference)
}
return exact, nil
}
func resolveNormalizedTypedInlineReference(reference string, definitions []typedInlineReferenceDefinition) (typedInlineReferenceDefinition, error) {
resolved, err := splice.ResolveConstructionReference(reference, spliceTypedInlineReferenceDefinitions(definitions))
if err != nil {
return typedInlineReferenceDefinition{}, err
}
return typedInlineReferenceDefinition{
label: resolved.Label,
destination: resolved.Destination,
title: resolved.Title,
hasTitle: resolved.HasTitle,
}, nil
}
func typedInlineReferenceDefinitions(blocks []constructionBlock) []typedInlineReferenceDefinition {
result := make([]typedInlineReferenceDefinition, 0)
for _, block := range blocks {
if block.kind != constructionReferenceDefinition {
continue
}
result = append(result, typedInlineReferenceDefinition{
label: block.label,
destination: block.destination,
title: block.title,
hasTitle: block.hasTitle,
})
}
return result
}
func availableTypedInlineReferenceDefinitions(context typedInlineWriteContext) []typedInlineReferenceDefinition {
definitions := make([]typedInlineReferenceDefinition, 0, len(context.referenceDefinitions)+len(context.deferredReferenceDefinitions))
definitions = append(definitions, context.referenceDefinitions...)
definitions = append(definitions, context.deferredReferenceDefinitions...)
return definitions
}
func spliceTypedInlineReferenceDefinitions(definitions []typedInlineReferenceDefinition) []splice.ConstructionReferenceDefinition {
converted := make([]splice.ConstructionReferenceDefinition, len(definitions))
for index, definition := range definitions {
converted[index] = splice.ConstructionReferenceDefinition{
Label: definition.label,
Destination: definition.destination,
Title: definition.title,
HasTitle: definition.hasTitle,
}
}
return converted
}
func spliceTypedInlineReferenceForm(form typedInlineReferenceForm) (splice.ConstructionReferenceInlineForm, bool) {
switch form {
case typedInlineReferenceFull:
return splice.ConstructionReferenceInlineFull, true
case typedInlineReferenceCollapsed:
return splice.ConstructionReferenceInlineCollapsed, true
case typedInlineReferenceShortcut:
return splice.ConstructionReferenceInlineShortcut, true
default:
return splice.ConstructionReferenceInlineFull, false
}
}
func writeTypedInlineReferenceSuffix(output *strings.Builder, form splice.ConstructionReferenceInlineForm, reference string) splice.Range {
switch form {
case splice.ConstructionReferenceInlineFull:
output.WriteString("][")
start := output.Len()
output.WriteString(reference)
end := output.Len()
output.WriteByte(']')
return splice.Range{Start: start, End: end}
case splice.ConstructionReferenceInlineCollapsed:
output.WriteString("][")
position := output.Len()
output.WriteByte(']')
return splice.Range{Start: position, End: position}
default:
output.WriteByte(']')
position := output.Len()
return splice.Range{Start: position, End: position}
}
}
func validateTypedInlineReference(reference string) error {
if err := validateTypedInlineText(reference); err != nil {
return fmt.Errorf("reference label %v", err)
}
if strings.ContainsAny(reference, "[]\\&") {
return fmt.Errorf("reference label requires escaping or entity interpretation")
}
return nil
}