-
Notifications
You must be signed in to change notification settings - Fork 37
Fix rst to trlc parser #464
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
base: main
Are you sure you want to change the base?
Changes from all commits
a051825
ff719bd
60b00b6
3241f43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,7 +63,10 @@ def _rst_to_trlc_impl(ctx): | |
| """Convert each .rst source file to a .trlc file via the Python converter.""" | ||
| outs = [] | ||
| for src in ctx.files.srcs: | ||
| out = ctx.actions.declare_file(src.basename[:-4] + ".trlc", sibling = src) | ||
| # `src` may come from a different repository (E.g.: a cross-repo RST label) | ||
| # Nest under the target's own name so two rst_to_trlc targets in the | ||
| # same package converting same-named files don't clash on the output path. | ||
| out = ctx.actions.declare_file(ctx.label.name + "/" + src.basename[:-4] + ".trlc") | ||
|
Contributor
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. srcs = ["a/x.rst", "b/x.rst"] in one target still collides on /x.trlc |
||
| outs.append(out) | ||
|
|
||
| args = ctx.actions.args() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,6 +83,7 @@ | |
| _RE_MARKUP = re.compile(r"\*\*?(.*?)\*\*?") | ||
| _RE_DIRECTIVE = re.compile(r"^\.\.\s+([\w]+)::\s*(.*)") | ||
| _RE_FIELD = re.compile(r"^\s+:([\w]+):\s*(.*)") # noqa: E501 | ||
| _RE_NEEDS_FILTER = re.compile(r"(\s*\[[^\[\]]*\])+\s*$") | ||
|
|
||
| _TRLC_HEADER = """\ | ||
| /******************************************************************************** | ||
|
|
@@ -136,8 +137,19 @@ def _escape(text: str) -> str: | |
|
|
||
|
|
||
| def _collect_refs(fields: dict[str, str]) -> list[str]: | ||
| """Extract all cross-reference IDs from relationship fields.""" | ||
| return [r.strip() for k in _REF_FIELDS if k in fields for r in fields[k].split(",") if r.strip()] | ||
| """Extract all cross-reference IDs from relationship fields. | ||
|
|
||
| Strips one or more trailing sphinx-needs filter expressions (E.g.: | ||
| [version==1] or [version==1][status==valid]), which are not | ||
| valid in TRLC reference syntax. | ||
| """ | ||
| return [ | ||
| _RE_NEEDS_FILTER.sub("", r.strip()).strip() | ||
|
Contributor
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. Line 150 splits before line 147 strips. Split on , happens before filter strip. req_001[version==1, status==x] → ['req_001[version==1', 'status==x]']. sphinx-needs filters with commas are legal ([tags in ["a","b"]]). Strip filters then split, or split only on top-level commas. |
||
| for k in _REF_FIELDS | ||
| if k in fields | ||
| for r in fields[k].split(",") | ||
| if r.strip() | ||
|
Contributor
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. if r.strip() guards the pre-substitution value. {"satisfies": "[version==1]"} → [''] → renders derived_from = [Pkg.@1]. Filter after the sub, not before |
||
| ] | ||
|
|
||
|
|
||
| def parse_directives(content: str) -> list[dict[str, Any]]: | ||
|
|
@@ -154,6 +166,14 @@ def parse_directives(content: str) -> list[dict[str, Any]]: | |
| directive, title = m.group(1), m.group(2).strip() | ||
| i += 1 | ||
|
|
||
| # RST directive arguments may wrap onto continuation lines (docutils | ||
| # allows this); consume any such lines here before the field list, | ||
| # otherwise they get mis-parsed as body text and the real :id: (and | ||
| # other fields) on the following lines are missed entirely. | ||
| while i < len(lines) and lines[i].strip() and not _RE_FIELD.match(lines[i]): | ||
|
Contributor
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. continuation loop has no indentation guard: while i < len(lines) and lines[i].strip() and lines[i][0].isspace() and not _RE_FIELD.match(lines[i]): |
||
| title += " " + lines[i].strip() | ||
| i += 1 | ||
|
|
||
| fields, i = _collect_fields(lines, i) | ||
| raw_body, i = _collect_body(lines, i) | ||
| body = _RE_MARKUP.sub(r"\1", raw_body).strip() | ||
|
|
||
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.
No tests for starlark changes