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

Support semantic markup for roles #9

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions changelogs/fragments/9-semantic-markup-roles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
minor_changes:
- "Add support for semantic markup in roles (https://github.com/ansible-community/antsibull-docs-parser/pull/9)."
breaking_changes:
gotmax23 marked this conversation as resolved.
Show resolved Hide resolved
- "Modify ``LinkProvider.plugin_option_like_link`` signature to include optional string argument ``entrypoint`` after ``plugin`` (https://github.com/ansible-community/antsibull-docs-parser/pull/9)."
gotmax23 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions src/antsibull_docs_parser/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class CodePart(NamedTuple):

class OptionNamePart(NamedTuple):
plugin: t.Optional[PluginIdentifier]
entrypoint: t.Optional[str] # present iff plugin.type == 'role'
gotmax23 marked this conversation as resolved.
Show resolved Hide resolved
link: t.List[str]
name: str
value: t.Optional[str]
Expand All @@ -112,6 +113,7 @@ class EnvVariablePart(NamedTuple):

class ReturnValuePart(NamedTuple):
plugin: t.Optional[PluginIdentifier]
entrypoint: t.Optional[str] # present iff plugin.type == 'role'
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
link: t.List[str]
name: str
value: t.Optional[str]
Expand Down
13 changes: 11 additions & 2 deletions src/antsibull_docs_parser/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def plugin_link( # pylint:disable=no-self-use
def plugin_option_like_link( # pylint:disable=no-self-use
self,
plugin: dom.PluginIdentifier, # pylint:disable=unused-argument
entrypoint: t.Optional[str], # pylint:disable=unused-argument
# pylint:disable-next=unused-argument
what: "t.Union[t.Literal['option'], t.Literal['retval']]",
# pylint:disable-next=unused-argument
Expand Down Expand Up @@ -174,7 +175,11 @@ def process_option_name(self, part: dom.OptionNamePart) -> None:
url = None
if part.plugin:
url = self.link_provider.plugin_option_like_link(
part.plugin, "option", part.link, part.plugin == self.current_plugin
part.plugin,
part.entrypoint,
"option",
part.link,
part.plugin == self.current_plugin,
)
self.destination.append(self.formatter.format_option_name(part, url))

Expand All @@ -189,7 +194,11 @@ def process_return_value(self, part: dom.ReturnValuePart) -> None:
url = None
if part.plugin:
url = self.link_provider.plugin_option_like_link(
part.plugin, "retval", part.link, part.plugin == self.current_plugin
part.plugin,
part.entrypoint,
"retval",
part.link,
part.plugin == self.current_plugin,
)
self.destination.append(self.formatter.format_return_value(part, url))

Expand Down
31 changes: 26 additions & 5 deletions src/antsibull_docs_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def _is_plugin_type(text: str) -> bool:

class Context(t.NamedTuple):
current_plugin: t.Optional[dom.PluginIdentifier] = None
role_entrypoint: t.Optional[str] = None


class CommandParser(abc.ABC):
Expand Down Expand Up @@ -146,10 +147,13 @@ def parse(self, parameters: t.List[str], context: Context) -> dom.AnyPart:
def _parse_option_like(
text: str,
context: Context,
) -> t.Tuple[t.Optional[dom.PluginIdentifier], t.List[str], str, t.Optional[str]]:
) -> t.Tuple[
t.Optional[dom.PluginIdentifier], t.Optional[str], t.List[str], str, t.Optional[str]
]:
value = None
if "=" in text:
text, value = text.split("=", 1)
entrypoint: t.Optional[str] = None
m = _FQCN_TYPE_PREFIX_RE.match(text)
if m:
plugin_fqcn = m.group(1)
Expand All @@ -165,10 +169,19 @@ def _parse_option_like(
text = text[len(_IGNORE_MARKER) :]
else:
plugin_identifier = context.current_plugin
entrypoint = context.role_entrypoint
if plugin_identifier is not None and plugin_identifier.type == "role":
idx = text.find(":")
if idx >= 0:
entrypoint = text[:idx]
text = text[idx + 1 :]
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
if entrypoint is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be

Suggested change
if entrypoint is None:
if not entrypoint:

? I assume you'd also want a value like :abc (text[:idx] would be an empty string) to cause an error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know whether ansible-core allows empty entrypoints. (Considering how flexible roles are in general, I wouldn't be surprised if it does.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to merge this as-is for now. If it turns out that ansible-core does not allow these we can stricten the parsing rules later.

raise ValueError("Role reference is missing entrypoint")
if ":" in text or "#" in text:
raise ValueError(f'Invalid option/return value name "{text}"')
return (
plugin_identifier,
entrypoint,
_ARRAY_STUB_RE.sub("", text).split("."),
text,
value,
Expand Down Expand Up @@ -212,17 +225,25 @@ def __init__(self):
super().__init__("O", 1, escaped_arguments=True)

def parse(self, parameters: t.List[str], context: Context) -> dom.AnyPart:
plugin, link, name, value = _parse_option_like(parameters[0], context)
return dom.OptionNamePart(plugin=plugin, link=link, name=name, value=value)
plugin, entrypoint, link, name, value = _parse_option_like(
parameters[0], context
)
return dom.OptionNamePart(
plugin=plugin, entrypoint=entrypoint, link=link, name=name, value=value
)


class _ReturnValue(CommandParserEx):
def __init__(self):
super().__init__("RV", 1, escaped_arguments=True)

def parse(self, parameters: t.List[str], context: Context) -> dom.AnyPart:
plugin, link, name, value = _parse_option_like(parameters[0], context)
return dom.ReturnValuePart(plugin=plugin, link=link, name=name, value=value)
plugin, entrypoint, link, name, value = _parse_option_like(
parameters[0], context
)
return dom.ReturnValuePart(
plugin=plugin, entrypoint=entrypoint, link=link, name=name, value=value
)


_COMMANDS = [
Expand Down
4 changes: 4 additions & 0 deletions src/antsibull_docs_parser/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def _format_option_like(
result.append("#")
result.append(plugin.type)
result.append(":")
entrypoint = part.entrypoint
if entrypoint is not None:
result.append(entrypoint)
result.append(":")
result.append(part.name)
value = part.value
if value is not None:
Expand Down
Loading