Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use early returns for sphinxHtmlToMarkdown member processing #590

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 59 additions & 65 deletions scripts/lib/sphinx/sphinxHtmlToMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,38 +81,40 @@ 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);

if (child.name !== "dt" || !python_type) {
return `<div>${$child.html()}</div>`;
}

const priorPythonApiType = meta.python_api_type;
if (!priorPythonApiType) {
meta.python_api_type = python_type;
meta.python_api_name = id;
}

if (python_type == "class") {
findByText($, $main, "em.property", "class").remove();
return `<span class="target" id="${id}"/><p><code>${$child.html()}</code></p>`;
} else if (child.name === "dt" && $dl.hasClass("property")) {
if (!meta.python_api_type) {
meta.python_api_type = "property";
meta.python_api_name = id;
}

if (id) {
$dl.siblings("h1").text(getLastPartFromFullIdentifier(id));
}
if (python_type == "property") {
if (!priorPythonApiType && id) {
$dl.siblings("h1").text(getLastPartFromFullIdentifier(id));
}

findByText($, $main, "em.property", "property").remove();
const signature = $child.find("em").text()?.replace(/^:\s+/, "");
if (signature.trim().length === 0) return;
return `<span class="target" id='${id}'/><p><code>${signature}</code></p>`;
} 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) {
}

if (python_type == "method") {
if (id) {
if (!priorPythonApiType) {
$dl.siblings("h1").text(getLastPartFromFullIdentifier(id));
}
} else {
// Inline methods
if (id) {
} else {
// Inline methods
$(`<h3>${getLastPartFromFullIdentifier(id)}</h3>`).insertBefore(
$dl,
);
Expand All @@ -121,11 +123,10 @@ export async function sphinxHtmlToMarkdown(options: {

findByText($, $main, "em.property", "method").remove();
return `<span class="target" id='${id}'/><p><code>${$child.html()}</code></p>`;
} else if (child.name === "dt" && $dl.hasClass("attribute")) {
if (!meta.python_api_type) {
meta.python_api_type = "attribute";
meta.python_api_name = id;
}

if (python_type == "attribute") {
if (!priorPythonApiType) {
if (id) {
$dl.siblings("h1").text(getLastPartFromFullIdentifier(id));
}
Expand All @@ -134,54 +135,47 @@ export async function sphinxHtmlToMarkdown(options: {
const signature = $child.find("em").text()?.replace(/^:\s+/, "");
if (signature.trim().length === 0) return;
return `<span class="target" id='${id}'/><p><code>${signature}</code></p>`;
} 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 = [
`<span class="target" id='${id}'/><h3>${name}</h3>`,
];
if (type) {
output.push(`<p><code>${type}</code></p>`);
}
if (value) {
output.push(`<p><code>${value}</code></p>`);
}
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;

// 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 = [`<span class="target" id='${id}'/><h3>${name}</h3>`];
if (type) {
output.push(`<p><code>${type}</code></p>`);
}
if (value) {
output.push(`<p><code>${value}</code></p>`);
}
return output.join("\n");
}

if (python_type === "function") {
findByText($, $main, "em.property", "function").remove();
return `<span class="target" id="${id}"/><p><code>${$child.html()}</code></p>`;
} else if (child.name === "dt" && $dl.hasClass("exception")) {
if (!meta.python_api_type) {
meta.python_api_type = "exception";
meta.python_api_name = id;
}
}

if (python_type === "exception") {
findByText($, $main, "em.property", "exception").remove();
return `<span class="target" id='${id}'/><p><code>${$child.html()}</code></p>`;
return `<span class="target" id="${id}"/><p><code>${$child.html()}</code></p>`;
}

return `<div>${$child.html()}</div>`;
throw new Error(`Unhandled Python type: ${python_type}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

})
.join("\n");

Expand Down