Skip to content

Commit 93b48b1

Browse files
committed
Fix C API metadata handling (#2861)
Currently, we're looking for an ID attribute starting with `group__` to work out if a page is a module or not. This isn't very robust because the `group__` ID only appears if there's a `doxygengroup` in the page, which isn't always the case. Instead, this PR treats all pages as modules except the index page and the release notes page, which we identify by their `<h1>`.
1 parent e8ebe96 commit 93b48b1

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

scripts/js/lib/api/processHtml.test.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
addLanguageClassToCodeBlocks,
1717
loadImages,
1818
handleSphinxDesignCards,
19-
maybeSetModuleMetadata,
19+
maybeSetPythonModuleMetadata,
2020
renameAllH1s,
2121
removeHtmlExtensionsInRelativeLinks,
2222
removeDownloadSourceCode,
@@ -395,12 +395,12 @@ test("handleFootnotes()", () => {
395395
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id2">1</a><span class="fn-bracket">]</span></span></aside></aside>`);
396396
});
397397

398-
test.describe("maybeSetModuleMetadata()", () => {
398+
test.describe("maybeSetPythonModuleMetadata()", () => {
399399
test("not a module", () => {
400400
const html = `<h1>Hello</h1>`;
401401
const meta: Metadata = {};
402402
const doc = CheerioDoc.load(html);
403-
maybeSetModuleMetadata(doc.$, doc.$main, meta, { isCApi: false });
403+
maybeSetPythonModuleMetadata(doc.$, doc.$main, meta);
404404
doc.expectHtml(html);
405405
expect(meta).toEqual({});
406406
});
@@ -412,7 +412,7 @@ test.describe("maybeSetModuleMetadata()", () => {
412412
): void => {
413413
const meta: Metadata = {};
414414
const doc = CheerioDoc.load(html);
415-
maybeSetModuleMetadata(doc.$, doc.$main, meta, { isCApi });
415+
maybeSetPythonModuleMetadata(doc.$, doc.$main, meta);
416416
doc.expectHtml(html);
417417
expect(meta).toEqual({
418418
apiType: "module",
@@ -441,14 +441,6 @@ test.describe("maybeSetModuleMetadata()", () => {
441441
"qiskit_ibm_provider.transpiler.passes.basis",
442442
);
443443
});
444-
445-
test("C API group", () => {
446-
checkModuleFound(
447-
`<section id="group__QkSparseObservable"><h1>Hello</h1></section>`,
448-
"QkSparseObservable",
449-
true,
450-
);
451-
});
452444
});
453445

454446
test.describe("processMembersAndSetMeta()", () => {

scripts/js/lib/api/processHtml.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ export async function processHtml(
4747
determineGithubUrl,
4848
releaseNotesTitle,
4949
hasSeparateReleaseNotes,
50-
isCApi,
5150
} = options;
5251
const $ = load(html);
5352
const $main = $(`[role='main']`);
5453

5554
const isReleaseNotes =
5655
fileName.endsWith("release_notes.html") ||
5756
fileName.endsWith("release-notes.html");
57+
const isIndex = fileName.endsWith("index.html");
5858
const images = loadImages(
5959
$,
6060
$main,
@@ -84,7 +84,9 @@ export async function processHtml(
8484

8585
const meta: Metadata = {};
8686
await processMembersAndSetMeta($, $main, meta, options);
87-
maybeSetModuleMetadata($, $main, meta, options);
87+
if (options.isCApi) maybeSetCMetadata($main, meta, isReleaseNotes, isIndex);
88+
else maybeSetPythonModuleMetadata($, $main, meta);
89+
8890
if (meta.apiType === "module") {
8991
updateModuleHeadings($, $main);
9092
}
@@ -435,13 +437,26 @@ export async function processMembersAndSetMeta(
435437
}
436438
}
437439

438-
export function maybeSetModuleMetadata(
440+
function maybeSetCMetadata(
441+
$main: Cheerio<any>,
442+
meta: Metadata,
443+
isReleaseNotes: boolean,
444+
isIndex: boolean,
445+
): void {
446+
// Every page in the C API should be displayed as a module except the index
447+
// and the release notes.
448+
if (isIndex || isReleaseNotes) return;
449+
const topHeadingText = $main.find("h1").first().text();
450+
meta.apiType = "module";
451+
meta.apiName = topHeadingText;
452+
}
453+
454+
export function maybeSetPythonModuleMetadata(
439455
$: CheerioAPI,
440456
$main: Cheerio<any>,
441457
meta: Metadata,
442-
options: { isCApi: boolean },
443458
): void {
444-
const modulePrefix = options.isCApi ? "group__" : "module-";
459+
const modulePrefix = "module-";
445460
const moduleIdWithPrefix = $main
446461
.find("span, section, div.section")
447462
.toArray()

0 commit comments

Comments
 (0)