Skip to content

fix(schema): improve docs parsing #2850

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

Merged
merged 5 commits into from
Feb 2, 2023
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fetch-schema-data:
update-schema-data:
# Parse docs
bin/parse_docs.py .tmp/aws-sam-developer-guide/doc_source > schema_source/docs.json
bin/parse_docs.py --cfn --with-title .tmp/aws-cloudformation-user-guide/doc_source > schema_source/cloudformation-docs.json
bin/parse_docs.py --cfn .tmp/aws-cloudformation-user-guide/doc_source > schema_source/cloudformation-docs.json

# Add CloudFormation docs to CloudFormation schema
python bin/add_docs_cfn_schema.py --schema .tmp/cloudformation.schema.json --docs schema_source/cloudformation-docs.json > schema_source/cloudformation.schema.json
Expand Down
14 changes: 9 additions & 5 deletions bin/parse_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ def parse(s: str) -> Iterator[Tuple[str, str]]:
"""Parse an AWS docs page in Markdown format, yielding each property."""
# Prevent from parsing return values accidentally
with suppress(ValueError):
s = s[: s.index("Return Values")]
s = s[: s.index("# Return Value")]
with suppress(ValueError):
s = s[: s.index("# Return value")]
parts = s.split("\n\n")
for part in parts:
match = re.match(r"^\s*`(\w+)`\s+<a", part)
if match:
yield match.group(1), part.strip()
yield match.group(1), part


# TODO: Change in the docs instead?
Expand Down Expand Up @@ -64,14 +66,13 @@ def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("dir", type=Path)
parser.add_argument("--cfn", action="store_true")
parser.add_argument("--with-title", help="use doc title instead of filename as key", action="store_true")
args = parser.parse_args()

props: Dict[str, Dict[str, str]] = {}
for path in args.dir.glob("*.md"):
text = path.read_text()
title = stringbetween(text, "# ", "<a")
page = title if args.with_title else path.stem
page = title if args.cfn else path.stem
for name, description in parse(text):
if page not in props:
props[page] = {}
Expand All @@ -83,7 +84,10 @@ def main() -> None:
else "https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/"
)
description = convert_to_full_path(description, prefix)
props[page][name] = description
# Assume properties (what we care about) at top, so skip if already exists
if name in props[page]:
continue
props[page][name] = description.strip()

print(
json.dumps(
Expand Down
Loading