Skip to content

Commit

Permalink
docs: support tagfiles for a single component
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Jul 23, 2024
1 parent 65d3732 commit d0b923a
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions doc/lib/cpp-tagfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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'})
}
Expand All @@ -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]`
}
Expand All @@ -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
Expand All @@ -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"]`
Expand All @@ -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 {
Expand All @@ -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(', ')
}
Expand All @@ -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 {
Expand Down

0 comments on commit d0b923a

Please sign in to comment.