-
Notifications
You must be signed in to change notification settings - Fork 64
/
Xref.ts
203 lines (181 loc) · 5.75 KB
/
Xref.ts
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
import type Spec from './Spec';
import type { Context } from './Context';
import type * as Biblio from './Biblio';
import type Clause from './Clause';
import Builder from './Builder';
import * as utils from './utils';
/*@internal*/
export default class Xref extends Builder {
namespace: string;
href: string;
aoid: string;
clause: Clause | null;
id: string;
entry: Biblio.BiblioEntry | undefined;
static elements = ['EMU-XREF'];
constructor(
spec: Spec,
node: HTMLElement,
clause: Clause | null,
namespace: string,
href: string,
aoid: string
) {
super(spec, node);
this.namespace = namespace;
this.href = href;
this.aoid = aoid;
this.clause = clause;
this.id = node.getAttribute('id')!;
}
static enter({ node, spec, clauseStack }: Context) {
const href = node.getAttribute('href')!;
const aoid = node.getAttribute('aoid')!;
const parentClause = clauseStack[clauseStack.length - 1];
let namespace: string;
if (node.hasAttribute('namespace')) {
namespace = node.getAttribute('namespace')!;
} else {
namespace = parentClause ? parentClause.namespace : spec.namespace;
}
if (href && aoid) {
utils.logWarning("xref can't have both href and aoid.");
return;
}
if (!href && !aoid) {
utils.logWarning('xref has no href or aoid.');
console.log(node.outerHTML);
return;
}
const xref = new Xref(spec, node, parentClause, namespace, href, aoid);
spec._xrefs.push(xref);
}
build() {
const spec = this.spec;
const href = this.href;
const node = this.node;
const aoid = this.aoid;
const namespace = this.namespace;
if (href) {
if (href[0] !== '#') {
utils.logWarning(
'xref to anything other than a fragment id is not supported (is ' +
href +
'). Try href="#sec-id" instead.'
);
return;
}
const id = href.slice(1);
this.entry = spec.biblio.byId(id);
if (!this.entry) {
utils.logWarning("can't find clause, production, note or example with id " + href);
return;
}
switch (this.entry.type) {
case 'clause':
buildClauseLink(node, this.entry);
break;
case 'production':
buildProductionLink(node, this.entry);
break;
case 'example':
buildFigureLink(spec, this.clause, node, this.entry, 'Example');
break;
case 'note':
buildFigureLink(spec, this.clause, node, this.entry, 'Note');
break;
case 'table':
buildFigureLink(spec, this.clause, node, this.entry, 'Table');
break;
case 'figure':
buildFigureLink(spec, this.clause, node, this.entry, 'Figure');
break;
case 'term':
buildTermLink(node, this.entry);
break;
default:
utils.logWarning('found unknown biblio entry (this is a bug, please file it)');
}
} else if (aoid) {
this.entry = spec.biblio.byAoid(aoid, namespace);
if (this.entry) {
buildAOLink(node, this.entry);
return;
}
utils.logWarning("can't find abstract op with aoid " + aoid + ' in namespace ' + namespace);
}
}
}
function buildClauseLink(xref: Element, entry: Biblio.ClauseBiblioEntry) {
if (xref.textContent!.trim() === '') {
if (xref.hasAttribute('title')) {
// titleHTML might not be present from older biblio files.
xref.innerHTML = buildXrefLink(entry, entry.titleHTML || entry.title);
} else {
xref.innerHTML = buildXrefLink(entry, entry.number);
}
} else {
xref.innerHTML = buildXrefLink(entry, xref.innerHTML);
}
}
function buildProductionLink(xref: Element, entry: Biblio.ProductionBiblioEntry) {
if (xref.textContent!.trim() === '') {
xref.innerHTML = buildXrefLink(entry, '<emu-nt>' + entry.name + '</emu-nt>');
} else {
xref.innerHTML = buildXrefLink(entry, xref.innerHTML);
}
}
function buildAOLink(xref: Element, entry: Biblio.BiblioEntry) {
if (xref.textContent!.trim() === '') {
xref.innerHTML = buildXrefLink(entry, xref.getAttribute('aoid'));
} else {
xref.innerHTML = buildXrefLink(entry, xref.innerHTML);
}
}
function buildTermLink(xref: Element, entry: Biblio.TermBiblioEntry) {
if (xref.textContent!.trim() === '') {
xref.innerHTML = buildXrefLink(entry, entry.term);
} else {
xref.innerHTML = buildXrefLink(entry, xref.innerHTML);
}
}
function buildFigureLink(
spec: Spec,
parentClause: Clause | null,
xref: Element,
entry: Biblio.FigureBiblioEntry,
type: string
) {
if (xref.textContent!.trim() === '') {
if (entry.clauseId) {
// first need to find the associated clause
const clauseEntry = spec.biblio.byId(entry.clauseId);
if (clauseEntry.type !== 'clause') {
utils.logWarning('could not find parent clause for ' + type + ' id ' + entry.id);
return;
}
if (parentClause && parentClause.id === clauseEntry.id) {
xref.innerHTML = buildXrefLink(entry, type + ' ' + entry.number);
} else {
if (xref.hasAttribute('title')) {
xref.innerHTML = buildXrefLink(
entry,
clauseEntry.title + ' ' + type + ' ' + entry.number
);
} else {
xref.innerHTML = buildXrefLink(
entry,
clauseEntry.number + ' ' + type + ' ' + entry.number
);
}
}
} else {
xref.innerHTML = buildXrefLink(entry, type + ' ' + entry.number);
}
} else {
xref.innerHTML = buildXrefLink(entry, xref.innerHTML);
}
}
function buildXrefLink(entry: Biblio.BiblioEntry, contents: string | number | undefined | null) {
return '<a href="' + entry.location + '#' + (entry.id || entry.refId) + '">' + contents + '</a>';
}