-
Notifications
You must be signed in to change notification settings - Fork 136
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
Improve parsing & formatting of help page descriptions #680
Open
lgarber-akamai
wants to merge
3
commits into
linode:dev
Choose a base branch
from
lgarber-akamai:fix/malformed-arg-desc
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+68
−28
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,15 @@ | |
import functools | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes in this file originated in #639, which provides a bit more context for why they were made. |
||
import re | ||
from html import unescape | ||
from typing import List, Tuple | ||
from typing import List, Optional, Tuple | ||
|
||
# Sentence delimiter, split on a period followed by any type of | ||
# whitespace (space, new line, tab, etc.) | ||
REGEX_SENTENCE_DELIMITER = re.compile(r"\W(?:\s|$)") | ||
REGEX_SENTENCE_DELIMITER = re.compile(r"\.(?:\s|$)", flags=re.M) | ||
|
||
# Matches on pattern __prefix__ at the beginning of a description | ||
# or after a comma | ||
REGEX_TECHDOCS_PREFIX = re.compile(r"(?:, |\A)__([\w-]+)__") | ||
REGEX_TECHDOCS_PREFIX = re.compile(r"(?:, |\A)__([^_]+)__") | ||
|
||
# Matches on pattern [link title](https://.../) | ||
REGEX_MARKDOWN_LINK = re.compile(r"\[(?P<text>.*?)]\((?P<link>.*?)\)") | ||
|
@@ -121,23 +121,35 @@ def get_short_description(description: str) -> str: | |
:rtype: set | ||
""" | ||
|
||
target_lines = description.splitlines() | ||
relevant_lines = None | ||
|
||
for i, line in enumerate(target_lines): | ||
def __simplify(sentence: str) -> Optional[str]: | ||
# Edge case for descriptions starting with a note | ||
if line.lower().startswith("__note__"): | ||
continue | ||
if sentence.lower().startswith("__note__"): | ||
return None | ||
|
||
sentence = strip_techdocs_prefixes(sentence) | ||
|
||
relevant_lines = target_lines[i:] | ||
break | ||
# Check that the sentence still has content after stripping prefixes | ||
if len(sentence) < 2: | ||
return None | ||
|
||
if relevant_lines is None: | ||
return sentence + "." | ||
|
||
# Find the first relevant sentence | ||
result = next( | ||
simplified | ||
for simplified in iter( | ||
__simplify(sentence) | ||
for sentence in REGEX_SENTENCE_DELIMITER.split(description) | ||
) | ||
if simplified is not None | ||
) | ||
|
||
if result is None: | ||
raise ValueError( | ||
f"description does not contain any relevant lines: {description}", | ||
) | ||
|
||
return REGEX_SENTENCE_DELIMITER.split("\n".join(relevant_lines), 1)[0] + "." | ||
return result | ||
|
||
|
||
def strip_techdocs_prefixes(description: str) -> str: | ||
|
@@ -150,14 +162,10 @@ def strip_techdocs_prefixes(description: str) -> str: | |
:returns: The stripped description | ||
:rtype: str | ||
""" | ||
result_description = REGEX_TECHDOCS_PREFIX.sub( | ||
"", description.lstrip() | ||
).lstrip() | ||
|
||
return result_description | ||
return REGEX_TECHDOCS_PREFIX.sub("", description.lstrip()).lstrip() | ||
|
||
|
||
def process_arg_description(description: str) -> Tuple[str, str]: | ||
def simplify_description(description: str) -> Tuple[str, str]: | ||
""" | ||
Processes the given raw request argument description into one suitable | ||
for help pages, etc. | ||
|
@@ -173,12 +181,12 @@ def process_arg_description(description: str) -> Tuple[str, str]: | |
return "", "" | ||
|
||
result = get_short_description(description) | ||
result = strip_techdocs_prefixes(result) | ||
result = result.replace("\n", " ").replace("\r", " ") | ||
|
||
description, links = extract_markdown_links(result) | ||
# NOTE: Links should only be separated from Rich Markdown links | ||
result_no_links, links = extract_markdown_links(result) | ||
|
||
if len(links) > 0: | ||
description += f" See: {'; '.join(links)}" | ||
result_no_links += f" See: {'; '.join(links)}" | ||
|
||
return unescape(markdown_to_rich_markup(description)), unescape(description) | ||
return unescape(markdown_to_rich_markup(result_no_links)), unescape(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't consume
OpenAPIOperation(...).description
currently but I figured it'd be good to future-proof 🙂