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

NAS-132993 / 25.04 / Improve documentation appearance #15231

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions src/middlewared_docs/docs/_static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,16 @@ ul.search li { background: none; }
#json-schema h4 { display: none; }
#json-schema .breadcrumbs { display: none; }
#json-schema .no-additional { display: none; }
#json-schema .json-default-value {
font-size: 75%;
padding: .25em .4em;
border: 2px solid #28a745;
border-radius: .25rem;
font-weight: 700;
}
#json-schema .json-default-value .value {
font-family: 'Consolas', 'Menlo', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace;
font-weight: 400;
margin-top: .25em;
white-space: pre;
}
21 changes: 20 additions & 1 deletion src/middlewared_docs/generate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ def _generate_method_schemas_html(self, method: APIDumpMethod):

soup = BeautifulSoup(html)

# Elements like `Each item of this array must be:`
for h4 in soup.find_all("h4"):
if h4.text.endswith(":"):
h4["style"] = "display: block;"
h5 = soup.new_tag("h5")
h5.string = h4.text
h4.replace_with(h5)

for h5 in soup.find("div", {"id": "Call_parameters"}).find().find_all("h5", recursive=False):
if m := re.match("Item at ([0-9]+) must be:", h5.text):
Expand All @@ -121,6 +124,22 @@ def _generate_method_schemas_html(self, method: APIDumpMethod):
name = next_sibling.find("h4").text
h5.string = f"Parameter {number}: {name}"

# Multi-line default values (usually, non-trivial JSON arrays/objects)
for default_value in soup.find_all("span", class_="default-value"):
value = default_value.text.split(": ", 1)[1]
if len(value) > 40 and value.startswith(("[", "{")):
try:
value_decoded = json.loads(value)
except ValueError:
continue

new_default_value_value = soup.new_tag("div", **{"class": "value"})
new_default_value_value.string = json.dumps(value_decoded, indent=2)
new_default_value = soup.new_tag("div", **{"class": "json-default-value"})
new_default_value.string = "Default:"
new_default_value.insert(1, new_default_value_value)
default_value.replace_with(new_default_value)

return soup.find("body").decode_contents()
finally:
os.unlink(json_path)
Expand Down
Loading