From e5984f3343830aaab8f50a2a51982fa8f33eec8b Mon Sep 17 00:00:00 2001 From: Arnau Casau Date: Fri, 5 Jan 2024 19:09:58 +0100 Subject: [PATCH 1/3] Refactor recursive search --- scripts/lib/sphinx/sphinxHtmlToMarkdown.ts | 62 ++++++++-------------- 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts b/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts index 5dc83d4b3ae..33f62e95087 100644 --- a/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts +++ b/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts @@ -81,15 +81,13 @@ export async function sphinxHtmlToMarkdown(options: { $child.find(".viewcode-link").closest("a").remove(); const id = $dl.find("dt").attr("id") || ""; - if (child.name === "dt" && $dl.hasClass("class")) { - if (!meta.python_api_type) { - meta.python_api_type = "class"; - meta.python_api_name = id; - } + const python_type = getPythonApiType($dl); - findByText($, $main, "em.property", "class").remove(); - return `

${$child.html()}

`; - } else if (child.name === "dt" && $dl.hasClass("property")) { + if (child.name !== "dt" || !python_type) { + return `
${$child.html()}
`; + } + + if (python_type == "property") { if (!meta.python_api_type) { meta.python_api_type = "property"; meta.python_api_name = id; @@ -103,25 +101,9 @@ export async function sphinxHtmlToMarkdown(options: { const signature = $child.find("em").text()?.replace(/^:\s+/, ""); if (signature.trim().length === 0) return; return `

${signature}

`; - } else if (child.name === "dt" && $dl.hasClass("method")) { - if (!meta.python_api_type) { - meta.python_api_type = "method"; - meta.python_api_name = id; - if (id) { - $dl.siblings("h1").text(getLastPartFromFullIdentifier(id)); - } - } else { - // Inline methods - if (id) { - $(`

${getLastPartFromFullIdentifier(id)}

`).insertBefore( - $dl, - ); - } - } + } - findByText($, $main, "em.property", "method").remove(); - return `

${$child.html()}

`; - } else if (child.name === "dt" && $dl.hasClass("attribute")) { + if (python_type == "attribute") { if (!meta.python_api_type) { meta.python_api_type = "attribute"; meta.python_api_name = id; @@ -164,24 +146,26 @@ export async function sphinxHtmlToMarkdown(options: { } return output.join("\n"); } - } else if (child.name === "dt" && $dl.hasClass("function")) { - if (!meta.python_api_type) { - meta.python_api_type = "function"; - meta.python_api_name = id; - } - findByText($, $main, "em.property", "function").remove(); - return `

${$child.html()}

`; - } else if (child.name === "dt" && $dl.hasClass("exception")) { + } + + if (python_type == "method" && id) { if (!meta.python_api_type) { - meta.python_api_type = "exception"; - meta.python_api_name = id; + $dl.siblings("h1").text(getLastPartFromFullIdentifier(id)); + } else { + // Inline methods + $(`

${getLastPartFromFullIdentifier(id)}

`).insertBefore( + $dl, + ); } + } - findByText($, $main, "em.property", "exception").remove(); - return `

${$child.html()}

`; + if (!meta.python_api_type) { + meta.python_api_type = python_type; + meta.python_api_name = id; } - return `
${$child.html()}
`; + findByText($, $main, "em.property", `${python_type}`).remove(); + return `

${$child.html()}

`; }) .join("\n"); From aa3e46c45a3bf4b79c10454345101126b6012920 Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Mon, 8 Jan 2024 09:46:35 -0500 Subject: [PATCH 2/3] Attempt 2 --- scripts/lib/sphinx/sphinxHtmlToMarkdown.ts | 118 +++++++++++---------- 1 file changed, 64 insertions(+), 54 deletions(-) diff --git a/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts b/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts index 33f62e95087..9ebd78ed2e1 100644 --- a/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts +++ b/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts @@ -87,14 +87,20 @@ export async function sphinxHtmlToMarkdown(options: { return `
${$child.html()}
`; } - if (python_type == "property") { - if (!meta.python_api_type) { - meta.python_api_type = "property"; - meta.python_api_name = id; + const priorPythonApiType = meta.python_api_type; + if (!priorPythonApiType) { + meta.python_api_type = python_type; + meta.python_api_name = id; + } - if (id) { - $dl.siblings("h1").text(getLastPartFromFullIdentifier(id)); - } + if (python_type == "class") { + findByText($, $main, "em.property", "class").remove(); + return `

${$child.html()}`; + } + + if (python_type == "property") { + if (!priorPythonApiType && id) { + $dl.siblings("h1").text(getLastPartFromFullIdentifier(id)); } findByText($, $main, "em.property", "property").remove(); @@ -103,11 +109,24 @@ export async function sphinxHtmlToMarkdown(options: { return `

${signature}

`; } - if (python_type == "attribute") { - if (!meta.python_api_type) { - meta.python_api_type = "attribute"; - meta.python_api_name = id; + if (python_type == "method") { + if (id) { + if (!priorPythonApiType) { + $dl.siblings("h1").text(getLastPartFromFullIdentifier(id)); + } else { + // Inline methods + $(`

${getLastPartFromFullIdentifier(id)}

`).insertBefore( + $dl, + ); + } + } + + findByText($, $main, "em.property", "method").remove(); + return `

${$child.html()}`; + } + if (python_type == "attribute") { + if (!priorPythonApiType) { if (id) { $dl.siblings("h1").text(getLastPartFromFullIdentifier(id)); } @@ -116,56 +135,47 @@ export async function sphinxHtmlToMarkdown(options: { const signature = $child.find("em").text()?.replace(/^:\s+/, ""); if (signature.trim().length === 0) return; return `

${signature}

`; - } else { - // The attribute is embedded on the class - const text = $child.text(); - const equalIndex = text.indexOf("="); - const colonIndex = text.indexOf(":"); - let name = text; - let type: string | undefined; - let value: string | undefined; - if (colonIndex > 0 && equalIndex > 0) { - name = text.substring(0, colonIndex); - type = text.substring(colonIndex + 1, equalIndex); - value = text.substring(equalIndex); - } else if (colonIndex > 0) { - name = text.substring(0, colonIndex); - type = text.substring(colonIndex + 1); - } else if (equalIndex > 0) { - name = text.substring(0, equalIndex); - value = text.substring(equalIndex); - } - const output = [ - `

${name}

`, - ]; - if (type) { - output.push(`

${type}

`); - } - if (value) { - output.push(`

${value}

`); - } - return output.join("\n"); } - } - if (python_type == "method" && id) { - if (!meta.python_api_type) { - $dl.siblings("h1").text(getLastPartFromFullIdentifier(id)); - } else { - // Inline methods - $(`

${getLastPartFromFullIdentifier(id)}

`).insertBefore( - $dl, - ); + // Else, the attribute is embedded on the class + const text = $child.text(); + const equalIndex = text.indexOf("="); + const colonIndex = text.indexOf(":"); + let name = text; + let type: string | undefined; + let value: string | undefined; + if (colonIndex > 0 && equalIndex > 0) { + name = text.substring(0, colonIndex); + type = text.substring(colonIndex + 1, equalIndex); + value = text.substring(equalIndex); + } else if (colonIndex > 0) { + name = text.substring(0, colonIndex); + type = text.substring(colonIndex + 1); + } else if (equalIndex > 0) { + name = text.substring(0, equalIndex); + value = text.substring(equalIndex); + } + const output = [`

${name}

`]; + if (type) { + output.push(`

${type}

`); } + if (value) { + output.push(`

${value}

`); + } + return output.join("\n"); } - if (!meta.python_api_type) { - meta.python_api_type = python_type; - meta.python_api_name = id; + if (python_type === "function") { + findByText($, $main, "em.property", "function").remove(); + return `

${$child.html()}`; + } + + if (python_type === "exception") { + findByText($, $main, "em.property", "exception").remove(); + return `

${$child.html()}`; } - findByText($, $main, "em.property", `${python_type}`).remove(); - return `

${$child.html()}

`; + throw new Error(`Unhandled Python type: ${python_type}`); }) .join("\n"); From 208e2d0997931860627e54ce76a4a8882163aa77 Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Mon, 8 Jan 2024 09:49:57 -0500 Subject: [PATCH 3/3] Fix bad return statements --- scripts/lib/sphinx/sphinxHtmlToMarkdown.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts b/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts index 9ebd78ed2e1..9abcf3bf6d0 100644 --- a/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts +++ b/scripts/lib/sphinx/sphinxHtmlToMarkdown.ts @@ -95,7 +95,7 @@ export async function sphinxHtmlToMarkdown(options: { if (python_type == "class") { findByText($, $main, "em.property", "class").remove(); - return `

${$child.html()}`; + return `

${$child.html()}

`; } if (python_type == "property") { @@ -122,7 +122,7 @@ export async function sphinxHtmlToMarkdown(options: { } findByText($, $main, "em.property", "method").remove(); - return `

${$child.html()}`; + return `

${$child.html()}

`; } if (python_type == "attribute") { @@ -167,12 +167,12 @@ export async function sphinxHtmlToMarkdown(options: { if (python_type === "function") { findByText($, $main, "em.property", "function").remove(); - return `

${$child.html()}`; + return `

${$child.html()}

`; } if (python_type === "exception") { findByText($, $main, "em.property", "exception").remove(); - return `

${$child.html()}`; + return `

${$child.html()}

`; } throw new Error(`Unhandled Python type: ${python_type}`);