diff --git a/doc/lib/cpp-tagfiles.js b/doc/lib/cpp-tagfiles.js index f8e0938ec..cf19a7031 100644 --- a/doc/lib/cpp-tagfiles.js +++ b/doc/lib/cpp-tagfiles.js @@ -91,13 +91,18 @@ class CppTagfilesExtension { const defaultTagfilesDir = path.join(__dirname, 'cpp_tagfiles'); this.tagfiles.push({ file: path.join(defaultTagfilesDir, 'cppreference-doxygen-web.tag.xml'), - baseUrl: 'https://en.cppreference.com/w/' + baseUrl: 'https://en.cppreference.com/w/', + component: null }) - // Iterate files in playbook. + // Iterate files in playbook const playbookFiles = this.config.files || [] playbookFiles.forEach(tagfile => { - this.tagfiles.push({file: path.join(playbook.dir, tagfile.file), baseUrl: tagfile.baseUrl}) + this.tagfiles.push({ + file: path.join(playbook.dir, tagfile.file), + baseUrl: tagfile.baseUrl, + component: null + }) }) // Look for tagfiles set in the components @@ -107,13 +112,21 @@ class CppTagfilesExtension { const {descriptor, worktree} = origin let tagFilesConfig = descriptor?.ext?.cppTagfiles?.files || [] for (const tagfile of tagFilesConfig) { - this.tagfiles.push({file: path.join(worktree, tagfile.file), baseUrl: tagfile.baseUrl}) + this.tagfiles.push({ + file: path.join(worktree, tagfile.file), + baseUrl: tagfile.baseUrl, + component: descriptor.name + }) } } } - // Remove duplicates - this.tagfiles = this.tagfiles.filter((tagfile, index, self) => self.findIndex(t => t.file === tagfile.file) === index) + // Remove duplicates considering both the file and the component + this.tagfiles = this.tagfiles.filter((tagfile, index, self) => + index === self.findIndex((t) => ( + t.file === tagfile.file && t.component === tagfile.component + ))) + this.tagfiles.reverse() this.logger.info(this.tagfiles, 'tagfiles') // Read the tagfiles @@ -125,7 +138,7 @@ class CppTagfilesExtension { allowBooleanAttributes: true }); this.tagfiles.forEach(tagfile => { - const {file, baseUrl} = tagfile + const {file} = tagfile if (!fs.existsSync(file)) { this.logger.error(`Tagfile not found: ${file}`) return @@ -156,7 +169,8 @@ class CppTagfilesExtension { registry.inlineMacro('cpp', function () { const self = this; self.process(function (parent, target, attr) { - const link = extensionSelf.getSymbolLink(he.decode(target)) + const component = parent.document.getAttributes()['page-component-name'] || null + const link = extensionSelf.getSymbolLink(he.decode(target), component) if (link) { return self.createInline(parent, 'quoted', link, {type: 'monospaced'}) } @@ -172,9 +186,10 @@ class CppTagfilesExtension { * Gets the URL for a symbol. * * @param symbol - The name of the symbol. + * @param component - The Antora component the symbol belongs to. * @returns {undefined|string} The URL for the symbol, or undefined if the symbol is not found. */ - getSymbolLink(symbol) { + getSymbolLink(symbol, component) { if (CppTagfilesExtension.isFundamentalType(symbol)) { return `https://en.cppreference.com/w/cpp/language/types[${symbol},window=_blank]` } @@ -184,9 +199,12 @@ class CppTagfilesExtension { symbol.startsWith('<') && symbol.endsWith('>'); if (isIncludeFile) { for (const tagfile of this.tagfiles) { + if (tagfile.component !== undefined && tagfile.component !== component) { + continue + } const filename = CppTagfilesExtension.getFileFilename(tagfile.doc['tagfile'], symbol) if (filename) { - return `https://en.cppreference.com/w/${filename}[${he.encode(symbol)},window="_blank"]` + return `${tagfile.baseUrl}${filename}[${he.encode(symbol)},window="_blank"]` } } return undefined @@ -196,6 +214,9 @@ class CppTagfilesExtension { if (!symbol.includes('<')) { // Handle symbols that are not templates for (const tagfile of this.tagfiles) { + if (tagfile.component !== undefined && tagfile.component !== component) { + continue + } const filename = CppTagfilesExtension.getSymbolFilename(tagfile.doc['tagfile'], symbol, '') if (filename !== undefined) { return `${tagfile.baseUrl}${filename}[${he.encode(symbol)},window="_blank"]` @@ -205,7 +226,7 @@ class CppTagfilesExtension { // Handle symbols that are templates const {mainSymbolName, templateParameters, rest} = CppTagfilesExtension.splitCppSymbol(symbol) let fullLink = '' - const mainLink = this.getSymbolLink(mainSymbolName) + const mainLink = this.getSymbolLink(mainSymbolName, component) if (mainLink) { fullLink = mainLink } else { @@ -214,7 +235,7 @@ class CppTagfilesExtension { fullLink += he.encode('<') let isFirst = true for (const templateParameter of templateParameters) { - const templateParameterLink = this.getSymbolLink(templateParameter) + const templateParameterLink = this.getSymbolLink(templateParameter, component) if (!isFirst) { fullLink += he.encode(', ') } @@ -228,7 +249,7 @@ class CppTagfilesExtension { fullLink += he.encode('>') if (rest.startsWith('::')) { fullLink += he.encode('::') - const restLink = this.getSymbolLink(mainSymbolName + rest) + const restLink = this.getSymbolLink(mainSymbolName + rest, component) if (restLink) { fullLink += restLink } else {