Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 4575ffe

Browse files
authored
feat: add support for enums (#374)
1 parent 39baecb commit 4575ffe

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

docfx_yaml/extension.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ def _parse_docstring_summary(summary):
400400
summary_parts = []
401401
attributes = []
402402
attribute_type_token = ":type:"
403+
enum_type_token = "Values:"
403404
keyword = name = description = var_type = ""
404405

405406
notice_open_tag = '<aside class="{notice_tag}">\n<b>{notice_name}:</b>'
@@ -486,7 +487,29 @@ def _parse_docstring_summary(summary):
486487

487488
# Parse keywords if found.
488489
# lstrip is added to parse code blocks that are not formatted well.
489-
if part.lstrip('\n').startswith('..'):
490+
if (potential_keyword := part.lstrip('\n')) and (
491+
potential_keyword.startswith('..') or
492+
potential_keyword.startswith(enum_type_token)
493+
):
494+
if enum_type_token in potential_keyword:
495+
# Handle the enum section starting with `Values:`
496+
parts = [split_part for split_part in part.split("\n") if split_part][1:]
497+
if not parts:
498+
continue
499+
tab_space = len(parts[0]) - len(parts[0].lstrip(" "))
500+
if tab_space == 0:
501+
raise ValueError("Content in the block should be indented."\
502+
f"Please check the docstring: \n{summary}")
503+
parts = "\n".join(
504+
[indent_code_left(part, tab_space) for part in parts]
505+
)
506+
summary_parts.append(
507+
"Enum values:\n\n```\n"
508+
f"{parts}"
509+
"\n```\n"
510+
)
511+
continue
512+
490513
try:
491514
keyword = extract_keyword(part.lstrip('\n'))
492515
except ValueError:
@@ -566,22 +589,22 @@ def _extract_docstring_info(summary_info, summary, name):
566589
':type': 'variables',
567590
':param': 'variables',
568591
':raises': 'exceptions',
569-
':raises:': 'exceptions'
592+
':raises:': 'exceptions',
570593
}
571-
594+
572595
initial_index = -1
573596
front_tag = '<xref'
574597
end_tag = '/xref>'
575598
end_len = len(end_tag)
576-
599+
577600
# Prevent GoogleDocstring crashing on custom types and parse all xrefs to normal
578601
if front_tag in parsed_text:
579602
type_pairs = []
580603
# Constant length for end of xref tag
581604
initial_index = max(0, parsed_text.find(front_tag))
582605

583606
summary_part = parsed_text[initial_index:]
584-
607+
585608
# Remove all occurrences of "<xref uid="uid">text</xref>"
586609
while front_tag in summary_part:
587610

@@ -611,12 +634,12 @@ def _extract_docstring_info(summary_info, summary, name):
611634
for pairs in type_pairs:
612635
original_type, safe_type = pairs[0], pairs[1]
613636
parsed_text = parsed_text.replace(original_type, safe_type)
614-
637+
615638
# Clean the string by cleaning newlines and backlashes, then split by white space.
616639
config = Config(napoleon_use_param=True, napoleon_use_rtype=True)
617640
# Convert Google style to reStructuredText
618641
parsed_text = str(GoogleDocstring(parsed_text, config))
619-
642+
620643
# Trim the top summary but maintain its formatting.
621644
indexes = []
622645
for types in var_types:
@@ -666,7 +689,7 @@ def _extract_docstring_info(summary_info, summary, name):
666689
while index <= len(parsed_text):
667690
word = parsed_text[index] if index < len(parsed_text) else ""
668691
# Check if we encountered specific words.
669-
if word in var_types or index == len(parsed_text):
692+
if word in var_types or index == len(parsed_text):
670693
# Finish processing previous section.
671694
if cur_type:
672695
if cur_type == ':type':
@@ -698,11 +721,11 @@ def _extract_docstring_info(summary_info, summary, name):
698721
# process further.
699722
if word not in var_types:
700723
raise ValueError(f"Encountered wrong formatting, please check docstring for {name}")
701-
724+
702725
# Reached end of string, break after finishing processing
703726
if index == len(parsed_text):
704727
break
705-
728+
706729
# Start processing for new section
707730
cur_type = word
708731
if cur_type in [':type', ':param', ':raises', ':raises:']:

0 commit comments

Comments
 (0)