Skip to content

chore: filter CFN docs #2853

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 4 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
30 changes: 23 additions & 7 deletions bin/parse_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@
import argparse
import json
import re
from contextlib import suppress
from pathlib import Path
from typing import Dict, Iterator, Tuple


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 Value")]
with suppress(ValueError):
s = s[: s.index("# Return value")]
s = stringbetween(s, "#\s+Properties", "#\s+Return values")
if not s:
return
parts = s.split("\n\n")
for part in parts:
match = re.match(r"^\s*`(\w+)`\s+<a", part)
Expand Down Expand Up @@ -59,7 +57,18 @@ def convert_to_full_path(description: str, prefix: str) -> str:


def stringbetween(s: str, sep1: str, sep2: str) -> str:
return s.split(sep1, 1)[1].split(sep2, 1)[0]
"""
Return string between regexes. Case-insensitive. If sep2 doesn't match,
returns to end of string.
"""
start = re.search(sep1, s, re.IGNORECASE)
if not start:
return ""
s = s[start.end() :]
end = re.search(sep2, s, re.IGNORECASE)
if not end:
return s
return s[: end.start()]


def main() -> None:
Expand All @@ -71,7 +80,14 @@ def main() -> None:
props: Dict[str, Dict[str, str]] = {}
for path in args.dir.glob("*.md"):
text = path.read_text()
title = stringbetween(text, "# ", "<a")
title = stringbetween(text, r"#\s+", r"<a")
if not title:
raise Exception(f"{path} has no title")
# In CFN docs, always expect either `AWS::Foo::Bar`, or `AWS::Foo::Bar SomeProperty`,
# which maps to the definition names in GoFormation schema
# Tangentially related: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-resource-specification-format.html
if args.cfn and not re.match(r"^\w+::\w+::\w+( \w+)?$", title):
continue
page = title if args.cfn else path.stem
for name, description in parse(text):
if page not in props:
Expand Down
Loading