Skip to content

Commit 81072e6

Browse files
committed
Incorporate feedback
1 parent 061ab42 commit 81072e6

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ test("addLanguageClassToCodeBlocks()", () => {
285285
// as with this example. Also this name is misleading. Really what we are doing here is dealing with RST's `.. parsed-literal ::`
286286
// (https://docutils.sourceforge.io/docs/ref/rst/directives.html#parsed-literal-block), which we tend to use to put code literals
287287
// along with gate text representations like the above.
288-
addLanguageClassToCodeBlocks(doc1.$, doc1.$main, false);
288+
addLanguageClassToCodeBlocks(doc1.$, doc1.$main, { isCApi: false });
289289
doc1.expectHtml(`<p><strong>Circuit symbol:</strong></p>
290290
<div class="highlight-default notranslate"><div class="highlight"><pre><code class="language-python"><span></span> ┌──────────┐
291291
q_0: ┤ U(ϴ,φ,λ) ├
@@ -302,7 +302,7 @@ test("addLanguageClassToCodeBlocks()", () => {
302302
</pre>
303303
</div>
304304
</div>`);
305-
addLanguageClassToCodeBlocks(doc2.$, doc2.$main, false);
305+
addLanguageClassToCodeBlocks(doc2.$, doc2.$main, { isCApi: false });
306306
doc2.expectHtml(`<div class="highlight-default notranslate">
307307
<div class="highlight">
308308
<pre><code class="language-python"><span></span><span class="kn">from</span> <span class="nn">qiskit_ibm_runtime.options</span> <span class="kn">import</span> <span class="n">Options</span>
@@ -400,7 +400,7 @@ test.describe("maybeSetModuleMetadata()", () => {
400400
const html = `<h1>Hello</h1>`;
401401
const meta: Metadata = {};
402402
const doc = CheerioDoc.load(html);
403-
maybeSetModuleMetadata(doc.$, doc.$main, meta, false);
403+
maybeSetModuleMetadata(doc.$, doc.$main, meta, { isCApi: false });
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+
maybeSetModuleMetadata(doc.$, doc.$main, meta, { isCApi });
416416
doc.expectHtml(html);
417417
expect(meta).toEqual({
418418
apiType: "module",
@@ -515,7 +515,7 @@ backends may not have this attribute.</p>
515515
`;
516516
const doc = CheerioDoc.load(html);
517517
const meta: Metadata = {};
518-
await processMembersAndSetMeta(doc.$, doc.$main, meta, false);
518+
await processMembersAndSetMeta(doc.$, doc.$main, meta, { isCApi: false });
519519
doc.expectHtml(`<h1>least_busy</h1>
520520
<div><function id="qiskit_ibm_provider.least_busy" attributetypehint="undefined" attributevalue="undefined" isdedicatedpage="true" github="../_modules/qiskit_ibm_provider.html#least_busy" signature="least_busy(backends)¶" modifiers="" extrasignatures="[]">
521521
@@ -575,7 +575,7 @@ particular error, which subclasses both <a class="reference internal" href="#qis
575575
`;
576576
const doc = CheerioDoc.load(html);
577577
const meta: Metadata = {};
578-
await processMembersAndSetMeta(doc.$, doc.$main, meta, false);
578+
await processMembersAndSetMeta(doc.$, doc.$main, meta, { isCApi: false });
579579
doc.expectHtml(`<span class="target" id="module-qiskit.exceptions"><span id="qiskit-exceptions"></span></span><section id="top-level-exceptions-qiskit-exceptions">
580580
<h1>Top-level exceptions (<a class="reference internal" href="#module-qiskit.exceptions" title="qiskit.exceptions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">qiskit.exceptions</span></code></a>)<a class="headerlink" href="#top-level-exceptions-qiskit-exceptions" title="Permalink to this heading">¶</a></h1>
581581
<p>All Qiskit-related errors raised by Qiskit are subclasses of the base:</p>
@@ -621,7 +621,7 @@ marked as builtins since they are not actually present in any include file this
621621
`;
622622
const doc = CheerioDoc.load(html);
623623
const meta: Metadata = { apiType: "module", apiName: "my_module" };
624-
await processMembersAndSetMeta(doc.$, doc.$main, meta, false);
624+
await processMembersAndSetMeta(doc.$, doc.$main, meta, { isCApi: false });
625625
doc.expectHtml(`
626626
<h3 data-header-type="attribute-header">qiskit.qasm2.LEGACY_CUSTOM_INSTRUCTIONS¶</h3><div><attribute id="qiskit.qasm2.LEGACY_CUSTOM_INSTRUCTIONS" attributetypehint="" attributevalue="" isdedicatedpage="undefined" github="undefined" signature="" modifiers="" extrasignatures="[]">
627627
@@ -661,7 +661,7 @@ marked as builtins since they are not actually present in any include file this
661661
</dd></dl>`;
662662
const doc = CheerioDoc.load(html);
663663
const meta: Metadata = {};
664-
await processMembersAndSetMeta(doc.$, doc.$main, meta, true);
664+
await processMembersAndSetMeta(doc.$, doc.$main, meta, { isCApi: true });
665665
doc.expectHtml(`<h3 data-header-type=\"method-header\">qk_obs_identity</h3><div><function id=\"qk_obs_identity\" attributetypehint=\"undefined\" attributevalue=\"undefined\" isdedicatedpage=\"undefined\" github=\"undefined\" signature=\"QkSparseObservable *qk_obs_identity(uint32_t num_qubits)¶\" modifiers=\"\" extrasignatures=\"[]\">
666666
667667
<div><p>Construct the identity observable.</p>

scripts/js/lib/api/processHtml.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ export type ProcessedHtml = {
2727
isReleaseNotes: boolean;
2828
};
2929

30-
export async function processHtml(options: {
30+
interface ProcessHtmlOptions {
3131
html: string;
3232
fileName: string;
3333
imageDestination: string;
3434
determineGithubUrl: (fileName: string) => string;
3535
releaseNotesTitle: string;
3636
hasSeparateReleaseNotes: boolean;
3737
isCApi: boolean;
38-
}): Promise<ProcessedHtml> {
38+
}
39+
40+
export async function processHtml(options: ProcessHtmlOptions): Promise<ProcessedHtml> {
3941
const {
4042
html,
4143
fileName,
@@ -68,7 +70,7 @@ export async function processHtml(options: {
6870
removeDownloadSourceCode($main);
6971
removeMatplotlibFigCaptions($main);
7072
handleSphinxDesignCards($, $main);
71-
addLanguageClassToCodeBlocks($, $main, isCApi);
73+
addLanguageClassToCodeBlocks($, $main, options);
7274
replaceViewcodeLinksWithGitHub($, $main, determineGithubUrl);
7375
updateRedirectedExternalLinks($, $main, externalRedirects);
7476
convertRubricsToHeaders($, $main);
@@ -78,8 +80,8 @@ export async function processHtml(options: {
7880
preserveMathBlockWhitespace($, $main);
7981

8082
const meta: Metadata = {};
81-
await processMembersAndSetMeta($, $main, meta, isCApi);
82-
maybeSetModuleMetadata($, $main, meta, isCApi);
83+
await processMembersAndSetMeta($, $main, meta, options);
84+
maybeSetModuleMetadata($, $main, meta, options);
8385
if (meta.apiType === "module") {
8486
updateModuleHeadings($, $main);
8587
}
@@ -183,8 +185,8 @@ export function handleSphinxDesignCards(
183185
});
184186
}
185187

186-
function detectLanguage($pre: Cheerio<any>, isCApi: boolean): string | null {
187-
const defaultLanguage = isCApi ? "c" : "python";
188+
function detectLanguage($pre: Cheerio<any>, options: { isCApi: boolean }): string | null {
189+
const defaultLanguage = options.isCApi ? "c" : "python";
188190
// Two levels up from `pre` should have class `highlight-<language>`
189191
const detectedLanguage = $pre
190192
.parent()
@@ -201,11 +203,11 @@ function detectLanguage($pre: Cheerio<any>, isCApi: boolean): string | null {
201203
export function addLanguageClassToCodeBlocks(
202204
$: CheerioAPI,
203205
$main: Cheerio<any>,
204-
isCApi: boolean,
206+
options: { isCApi: boolean },
205207
): void {
206208
$main.find("pre").each((_, pre) => {
207209
const $pre = $(pre);
208-
const language = detectLanguage($pre, isCApi);
210+
const language = detectLanguage($pre, options);
209211
const languageClass = language ? `language-${language}` : "";
210212
$pre.replaceWith(
211213
`<pre><code class="${languageClass}">${$pre.html()}</code></pre>`,
@@ -339,7 +341,7 @@ export async function processMembersAndSetMeta(
339341
$: CheerioAPI,
340342
$main: Cheerio<any>,
341343
meta: Metadata,
342-
isCApi: boolean,
344+
options: { isCApi: boolean },
343345
): Promise<void> {
344346
let continueMapMembers = true;
345347
while (continueMapMembers) {
@@ -359,10 +361,11 @@ export async function processMembersAndSetMeta(
359361

360362
const $dl = $(dl);
361363
const id =
362-
(isCApi
364+
(options.isCApi
363365
? // IDs in the C API have a bunch of extra information (e.g.
364366
// `_CPPv415qk_obs_free8uint32_t`) whereas we just want to display the
365-
// function name (e.g. `qk_obs_free`).
367+
// function name (e.g. `qk_obs_free`). This is safe because C does not
368+
// have function overloading so the function name is unique.
366369
$dl.find("span.sig-name.descname").text()
367370
: $dl.find("dt").attr("id")) || "";
368371
const apiType = getApiType($dl);
@@ -411,9 +414,9 @@ export function maybeSetModuleMetadata(
411414
$: CheerioAPI,
412415
$main: Cheerio<any>,
413416
meta: Metadata,
414-
isCApi: boolean,
417+
options: { isCApi: boolean },
415418
): void {
416-
const modulePrefix = isCApi ? "group__" : "module-";
419+
const modulePrefix = options.isCApi ? "group__" : "module-";
417420
const moduleIdWithPrefix = $main
418421
.find("span, section, div.section")
419422
.toArray()

0 commit comments

Comments
 (0)