-
Notifications
You must be signed in to change notification settings - Fork 23
/
cross_reference.go
103 lines (96 loc) · 2.74 KB
/
cross_reference.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
package sgml
import (
"path/filepath"
"strings"
"github.com/bytesparadise/libasciidoc/pkg/types"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func (r *sgmlRenderer) renderInternalCrossReference(ctx *context, xref *types.InternalCrossReference) (string, error) {
if log.IsLevelEnabled(log.DebugLevel) {
log.Debugf("rendering cross reference with ID: %s", spew.Sdump(xref.ID))
}
var label string
xrefID, ok := xref.ID.(string)
if !ok {
return "", errors.Errorf("unable to process internal cross reference: invalid ID: '%v'", xref.ID)
}
if xrefLabel, ok := xref.Label.(string); ok {
label = xrefLabel
} else if target, found := ctx.elementReferences[xrefID]; found {
switch t := target.(type) {
case string:
label = t
case []interface{}:
// render as usual except for links as plain text (since the cross reference is already displayed as a link)
buff := &strings.Builder{}
for _, e := range t {
switch e := e.(type) {
case *types.InlineLink:
renderedElement, err := RenderPlainText(e)
if err != nil {
return "", err
}
buff.WriteString(renderedElement)
default:
renderedElement, err := r.renderElement(ctx, e)
if err != nil {
return "", err
}
buff.WriteString(renderedElement)
}
}
label = buff.String()
default:
return "", errors.Errorf("unable to process internal cross reference to element of type %T", target)
}
} else {
label = "[" + xrefID + "]"
}
return r.execute(r.internalCrossReference, struct {
Href string
Label string
}{
Href: xrefID,
Label: label,
})
}
func (r *sgmlRenderer) renderExternalCrossReference(ctx *context, xref *types.ExternalCrossReference) (string, error) {
// log.Debugf("rendering cross reference with ID: %s", xref.Location)
var label string
var err error
switch l := xref.Attributes[types.AttrXRefLabel].(type) {
case string:
label = l
case []interface{}:
if label, err = r.renderInlineElements(ctx, l); err != nil {
return "", err
}
default:
label = defaultXrefLabel(xref)
}
return r.execute(r.externalCrossReference, struct {
Href string
Label string
}{
Href: getCrossReferenceLocation(xref),
Label: label,
})
}
func defaultXrefLabel(xref *types.ExternalCrossReference) string {
loc := xref.Location.ToDisplayString()
ext := filepath.Ext(loc)
if ext == "" {
return "[" + loc + "]" // internal references are within brackets
}
return loc[:len(loc)-len(ext)] + ".html"
}
func getCrossReferenceLocation(xref *types.ExternalCrossReference) string {
loc := xref.Location.ToDisplayString()
ext := filepath.Ext(loc)
if ext == "" { // internal reference
return "#" + loc
}
return loc[:len(loc)-len(ext)] + ".html" // TODO output extension
}