[ only. */
const linked = (node, { type = false, keywords = true } = {}) => {
if (!node) return '';
const parts = [];
for (const n of kids(node)) {
if (isText(n)) {
const raw = type ? normalizeTypeText(n['#text']) : n['#text'];
const t = escapeHtml(raw);
parts.push(keywords ? highlightKeywords(t) : t);
} else if (tagOf(n) === 'ref') {
parts.push(link(attr(n, 'refid'), escapeHtml(textOf(n))));
} else if (tagOf(n) === 'sp') {
parts.push(' ');
} else {
parts.push(linked(n, { type, keywords }));
}
}
return parts.join('').replace(/\s+/g, ' ').trim();
};
const plain = (node, type = false) => {
const t = textOf(node).replace(/\s+/g, ' ').trim();
return type ? normalizeTypeText(t) : t;
};
const renderInline = (n) => {
if (isText(n)) return escapeHtml(n['#text']);
const t = tagOf(n);
if (ENTITIES[t] !== undefined) return ENTITIES[t];
switch (t) {
case 'sp':
return ' ';
case 'ref':
return link(attr(n, 'refid'), kids(n).map(renderInline).join(''));
case 'computeroutput':
return `]${kids(n).map(renderInline).join('')}`;
case 'emphasis':
return `${kids(n).map(renderInline).join('')}`;
case 'bold':
return `${kids(n).map(renderInline).join('')}`;
case 's':
case 'strike':
case 'del':
return `${kids(n).map(renderInline).join('')}`;
case 'underline':
case 'ins':
return `${kids(n).map(renderInline).join('')}`;
case 'subscript':
return `${kids(n).map(renderInline).join('')}`;
case 'superscript':
return `${kids(n).map(renderInline).join('')}`;
case 'small':
return `${kids(n).map(renderInline).join('')}`;
case 'ulink':
return `${kids(n).map(renderInline).join('')}`;
case 'linebreak':
return '
';
case 'nonbreakablespace':
return ' ';
case 'anchor':
return '';
case 'formula':
return `${escapeHtml(textOf(n))}`;
case 'emoji':
return attr(n, 'unicode') ?? '';
case 'image':
case 'htmlonly':
case 'manonly':
case 'xmlonly':
case 'rtfonly':
case 'latexonly':
case 'docbookonly':
case 'dot':
case 'msc':
case 'plantuml':
case 'indexentry':
return '';
default:
return kids(n).map(renderInline).join('');
}
};
const codeOf = (n) =>
children(n, 'codeline')
.map((cl) => textOf(cl))
.join('\n');
const unwrapP = (html) => {
const m = /^([\s\S]*)<\/p>$/.exec(html);
return m && !m[1].includes('
') ? m[1] : html;
};
/** Renders a description into HTML. Parameters, return values and notes go into `out`. */
const renderBlocks = (nodes, out) => {
let html = '';
for (const n of nodes) {
if (isText(n)) {
if (n['#text'].trim()) html += `
${escapeHtml(n['#text'].trim())}
`;
continue;
}
const t = tagOf(n);
if (t === 'para') html += renderPara(n, out);
else if (t === 'sect1' || t === 'sect2' || t === 'sect3' || t === 'internal') {
const title = child(n, 'title');
if (title) html += `${kids(title).map(renderInline).join('')}
`;
html += renderBlocks(
kids(n).filter((k) => tagOf(k) !== 'title'),
out,
);
} else html += renderPara({ para: [n] }, out);
}
return html;
};
const listItems = (n, out) =>
children(n, 'listitem')
.map((li) => `${unwrapP(renderBlocks(kids(li), out).trim())}`)
.join('');
const renderPara = (para, out) => {
let html = '';
let inline = [];
const flush = () => {
const text = inline.join('').replace(/\s+/g, ' ').trim();
if (text) html += `${text}
`;
inline = [];
};
for (const n of kids(para)) {
if (isText(n)) {
inline.push(escapeHtml(n['#text']));
continue;
}
const t = tagOf(n);
switch (t) {
case 'parameterlist': {
flush();
const kind = attr(n, 'kind');
for (const item of children(n, 'parameteritem')) {
const names = children(child(item, 'parameternamelist'), 'parametername').map((pn) => ({
name: textOf(pn).trim(),
dir: attr(pn, 'direction'),
}));
const desc = unwrapP(renderBlocks(kids(child(item, 'parameterdescription')), out).trim());
for (const { name, dir } of names) {
const entry = { name, dir, html: desc };
if (kind === 'templateparam') out.tparams.push(entry);
else if (kind === 'retval') out.retvals.push(entry);
else if (kind === 'exception') out.throws.push(entry);
else out.params.push(entry);
}
}
break;
}
case 'simplesect': {
flush();
const kind = attr(n, 'kind');
const body = renderBlocks(
kids(n).filter((k) => tagOf(k) !== 'title'),
out,
).trim();
if (kind === 'return') out.returns = out.returns ? `${out.returns} ${unwrapP(body)}` : unwrapP(body);
else if (kind === 'see') out.see.push(unwrapP(body));
else if (kind === 'since') out.since = textOf(n).trim();
else if (kind === 'note' || kind === 'remark' || kind === 'attention' || kind === 'important')
html += `Note${body}
`;
else if (kind === 'warning') html += `Caution${body}
`;
else if (kind === 'pre') html += `Precondition: ${unwrapP(body)}
`;
else if (kind === 'post') html += `Postcondition: ${unwrapP(body)}
`;
else if (kind === 'invariant') html += `Invariant: ${unwrapP(body)}
`;
else if (kind === 'par') {
const title = child(n, 'title');
const label = title ? kids(title).map(renderInline).join('').trim() : '';
html += `${label || 'Note'}${body}
`;
}
// author, authors, version, date, copyright: not shown
break;
}
case 'xrefsect': {
flush();
const title = textOf(child(n, 'xreftitle')).trim();
const body = renderBlocks(kids(child(n, 'xrefdescription')), out).trim();
if (/deprecated/i.test(title)) out.deprecated = unwrapP(body) || 'Deprecated.';
else html += `${escapeHtml(title)}${body}
`;
break;
}
case 'itemizedlist':
flush();
html += ``;
break;
case 'orderedlist':
flush();
html += `${listItems(n, out)}
`;
break;
case 'programlisting':
flush();
html += `${escapeHtml(codeOf(n))}
`;
break;
case 'verbatim':
case 'preformatted':
flush();
html += `${escapeHtml(textOf(n))}
`;
break;
case 'heading':
flush();
html += `${kids(n).map(renderInline).join('')}
`;
break;
case 'blockquote':
flush();
html += `${renderBlocks(kids(n), out)}
`;
break;
case 'hruler':
flush();
html += '
';
break;
case 'table': {
flush();
const rows = children(n, 'row')
.map((r) => {
const cells = children(r, 'entry')
.map((e) => {
const tag = attr(e, 'thead') === 'yes' ? 'th' : 'td';
return `<${tag}>${unwrapP(renderBlocks(kids(e), out).trim())}${tag}>`;
})
.join('');
return `${cells}
`;
})
.join('');
html += ``;
break;
}
case 'variablelist': {
flush();
let dl = '';
for (const k of kids(n)) {
if (tagOf(k) === 'varlistentry') dl += `${kids(child(k, 'term')).map(renderInline).join('')}`;
if (tagOf(k) === 'listitem') dl += `${unwrapP(renderBlocks(kids(k), out).trim())}`;
}
html += `${dl}
`;
break;
}
case 'para':
flush();
html += renderPara(n, out);
break;
default:
inline.push(renderInline(n));
}
}
flush();
return html;
};
const newOut = () => ({ params: [], tparams: [], retvals: [], throws: [], returns: '', see: [], since: '', deprecated: '' });
const describe = (node) => {
const out = newOut();
const briefNode = child(node, 'briefdescription');
const brief = briefNode
? children(briefNode, 'para')
.map((p) => unwrapP(renderPara(p, out)))
.join(' ')
.trim()
: '';
const detailNode = child(node, 'detaileddescription');
const detail = detailNode ? renderBlocks(kids(detailNode), out).trim() : '';
const documented =
!!brief ||
!!detail ||
out.params.some((p) => p.html) ||
!!out.returns ||
out.retvals.length > 0 ||
!!out.deprecated;
return { brief, detail, ...out, documented };
};
const tparamList = (node) => {
const tpl = child(node, 'templateparamlist');
if (!tpl) return null;
const ps = children(tpl, 'param').map((p) => {
const type = linked(child(p, 'type'), { type: true });
const decl = textOf(child(p, 'declname')).trim();
const defval = child(p, 'defval');
let s = type;
if (decl && !new RegExp(`\\b${decl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}$`).test(plain(child(p, 'type')))) s += ` ${escapeHtml(decl)}`;
if (defval) s += ` = ${linked(defval, { type: true })}`;
return s;
});
return `template<${ps.join(', ')}>`;
};
/** The text after the parameter list in an argsstring, e.g. "const", "noexcept=default". */
const qualifiersOf = (args) => {
const s = args ?? '';
const start = s.indexOf('(');
if (start < 0) return s.trim();
let depth = 0;
for (let i = start; i < s.length; i++) {
if (s[i] === '(') depth++;
else if (s[i] === ')') {
depth--;
if (depth === 0) return s.slice(i + 1).trim();
}
}
return '';
};
const formatQualifiers = (q) =>
q
.replace(/\s*=\s*(default|delete|0)\s*$/, ' = $1')
.replace(/\s+/g, ' ')
.trim();
const signature = (m) => {
const node = m.node;
const name = `${escapeHtml(m.name)}`;
const tpl = tparamList(node);
const typeHtml = linked(child(node, 'type'), { type: true });
const typeText = plain(child(node, 'type'), true);
// Doxygen keeps some specifiers in the type text; do not repeat them.
const typeWords = new Set(typeText.split(/\s+/).slice(0, 4));
const pre = [];
if (m.kind === 'friend' && !typeWords.has('friend')) pre.push('friend');
if (m.static && !typeWords.has('static')) pre.push('static');
if (m.constexpr && !typeWords.has('constexpr')) pre.push('constexpr');
if (m.explicit && !typeWords.has('explicit')) pre.push('explicit');
if ((m.virt === 'virtual' || m.virt === 'pure-virtual') && !typeWords.has('virtual')) pre.push('virtual');
const preHtml = pre.map((p) => `${p} `).join('');
const argsstring = textOf(child(node, 'argsstring'));
let body;
let text;
if (m.kind === 'function' || (m.kind === 'friend' && argsstring.includes('('))) {
const params = children(node, 'param').map((p) => {
const ptype = linked(child(p, 'type'), { type: true });
const decl = textOf(child(p, 'declname')).trim();
const arr = textOf(child(p, 'array')).trim();
const defval = child(p, 'defval');
let s = ptype;
if (decl) s += `${ptype ? ' ' : ''}${escapeHtml(decl)}`;
if (arr) s += escapeHtml(arr);
if (defval) s += ` = ${linked(defval)}`;
return s;
});
const paramText = children(node, 'param').map((p) => {
let s = plain(child(p, 'type'), true);
const decl = textOf(child(p, 'declname')).trim();
if (decl) s += ` ${decl}`;
if (child(p, 'defval')) s += ` = ${plain(child(p, 'defval'))}`;
return s;
});
const qual = formatQualifiers(qualifiersOf(argsstring));
const qualHtml = qual ? ' ' + highlightKeywords(escapeHtml(qual)) : '';
const oneLine = `${pre.join(' ')} ${typeText} ${m.name}(${paramText.join(', ')}) ${qual}`.trim();
const multi = oneLine.length > 78 && params.length > 1;
const paramsHtml = multi ? `\n ${params.join(',\n ')}\n` : params.join(', ');
// "void" as the only parameter type with no name means no parameters.
body = `${preHtml}${typeHtml ? typeHtml + ' ' : ''}${name}(${paramsHtml})${qualHtml}`;
text = `${pre.length ? pre.join(' ') + ' ' : ''}${typeText ? typeText + ' ' : ''}${m.name}(${paramText.join(', ')})${qual ? ' ' + qual : ''}`;
} else if (m.kind === 'variable') {
const init = child(node, 'initializer');
const initHtml = init ? ' ' + linked(init) : '';
body = `${preHtml}${typeHtml} ${name}${escapeHtml(argsstring)}${initHtml}`;
text = `${pre.join(' ')} ${typeText} ${m.name}${argsstring}${init ? ' ' + plain(init) : ''}`.trim();
} else if (m.kind === 'typedef') {
const def = textOf(child(node, 'definition')).trim();
if (def.startsWith('using')) {
body = `using ${name} = ${typeHtml}`;
text = `using ${m.name} = ${typeText}`;
} else {
body = `typedef ${typeHtml} ${name}${escapeHtml(argsstring)}`;
text = `typedef ${typeText} ${m.name}${argsstring}`;
}
} else if (m.kind === 'enum') {
const under = typeHtml ? ` : ${typeHtml}` : '';
body = `enum${m.strong ? ' class' : ''} ${name}${under}`;
text = `enum${m.strong ? ' class' : ''} ${m.name}${typeText ? ' : ' + typeText : ''}`;
} else if (m.kind === 'friend') {
body = `friend ${typeHtml} ${name}`;
text = `friend ${typeText} ${m.name}`;
} else if (m.kind === 'define') {
body = `#define ${name}${escapeHtml(argsstring)}`;
text = `#define ${m.name}${argsstring}`;
} else {
body = `${typeHtml} ${name}${escapeHtml(argsstring)}`;
text = `${typeText} ${m.name}${argsstring}`;
}
return { html: tpl ? `${tpl}\n${body}` : body, text: text.replace(/\s+/g, ' ').trim(), template: !!tpl };
};
// ---------- Pass 3: render public records ----------
for (const m of members.values()) {
const sig = signature(m);
m.sigHtml = sig.html;
m.sigText = sig.text;
m.template = sig.template;
const t = targets.get(m.id);
if (t) t.sig = sig.text;
}
const renderMember = (m) => {
Object.assign(m, describe(m.node));
if (m.kind === 'enum') {
m.values = children(m.node, 'enumvalue').map((ev) => {
const out = newOut();
const b = child(ev, 'briefdescription');
const d = child(ev, 'detaileddescription');
const brief = b ? children(b, 'para').map((p) => unwrapP(renderPara(p, out))).join(' ').trim() : '';
const detail = d ? renderBlocks(kids(d), out).trim() : '';
return {
name: textOf(child(ev, 'name')).trim(),
anchor: anchorFor(textOf(child(ev, 'name')).trim()),
init: plain(child(ev, 'initializer')).replace(/^=\s*/, ''),
html: [brief, unwrapP(detail)].filter(Boolean).join(' '),
};
});
}
};
for (const rec of compounds.values()) {
if (!rec.public) continue;
Object.assign(rec, describe(rec.node));
rec.templ = tparamList(rec.node);
rec.final = attr(rec.node, 'final') === 'yes';
rec.abstract = attr(rec.node, 'abstract') === 'yes';
rec.bases = children(rec.node, 'basecompoundref').map((b) => ({
html: attr(b, 'refid') ? link(attr(b, 'refid'), escapeHtml(textOf(b).trim())) : escapeHtml(textOf(b).trim()),
prot: attr(b, 'prot'),
virt: attr(b, 'virt'),
}));
rec.derived = children(rec.node, 'derivedcompoundref').map((b) => ({
html: attr(b, 'refid') ? link(attr(b, 'refid'), escapeHtml(textOf(b).trim())) : escapeHtml(textOf(b).trim()),
id: attr(b, 'refid'),
}));
const list = rec.kind === 'namespace' ? rec.publicMembers : rec.members.filter((m) => m.prot !== 'private');
for (const m of list) renderMember(m);
rec.visible = list;
}
// Which types and members each file defines, for the source pages.
for (const rec of compounds.values()) {
if (rec.kind !== 'namespace' && files.has(rec.file)) files.get(rec.file).defines.push(rec);
}
return {
base,
publicHeaders,
sourceFiles,
compounds,
members,
files,
targets,
link,
/** Public classes, structs and unions. */
types: () => [...compounds.values()].filter((c) => c.public && c.kind !== 'namespace'),
/** Namespaces with a page. */
namespaces: () => [...compounds.values()].filter((c) => c.public && c.kind === 'namespace'),
/** Enums at namespace scope, which get their own pages. */
namespaceEnums: () =>
[...compounds.values()]
.filter((c) => c.public && c.kind === 'namespace')
.flatMap((ns) => ns.visible.filter((m) => m.kind === 'enum').map((e) => ({ ns, e }))),
compoundByName: (name) => [...compounds.values()].find((c) => c.name === name),
};
}
let cached;
let cachedKey;
/** The model, built once per build. */
export function getModel(opts) {
const key = JSON.stringify(opts);
if (!cached || cachedKey !== key) {
cached = buildModel(opts);
cachedKey = key;
}
return cached;
}