Repository navigation
bug(pipelines): recognise CommonMark fence info strings in the ingest parser #324
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
Open
divyanshkulshrestha23
wants to merge
7
commits into
kubeflow:main
Choose a base branch
from
divyanshkulshrestha23:fix/fence-info-strings
base: main
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.
+137
−2
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
57da853
bug(pipelines): recognise CommonMark fence info strings in the ingest…
divyanshkulshrestha23 f23764b
bug(pipelines): document the fence opener pattern
divyanshkulshrestha23 7a8a6ef
bug(pipelines): allow backticks in tilde fence info strings
divyanshkulshrestha23 471825e
bug(pipelines): close fences only on a line of fence characters
divyanshkulshrestha23 f1149bf
bug(pipelines): only treat a pipe right after the fence as a table cell
divyanshkulshrestha23 e90cb63
bug(pipelines): document the closing-fence rule and cover other-chara…
divyanshkulshrestha23 034864b
bug(pipelines): keep the whole fence marker in the opener match
divyanshkulshrestha23 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """Tests for fenced code block detection in the canonical ingest parser (#322).""" | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| ROOT = Path(__file__).resolve().parents[1] / "docs-agent-mcp" | ||
| for path in (ROOT / "pipelines", ROOT / "pipelines" / "utils"): | ||
| if str(path) not in sys.path: | ||
| sys.path.insert(0, str(path)) | ||
|
|
||
| import canonical_rag_ingest # noqa: E402 | ||
|
|
||
|
|
||
| def blocks(text): | ||
| """Parse text and return (block_type, content) pairs in document order.""" | ||
| doc = canonical_rag_ingest.parse_canonical_document(text) | ||
| return [(b.block_type, b.content) for s in doc.sections for b in s.blocks] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "opening_fence", | ||
| [ | ||
| "```python", # baseline: already recognised | ||
| "``` python", # space before the language | ||
| "```shell-session", # hyphen in the language | ||
| "```c++", # non-word characters in the language | ||
| "```yaml {linenos=true}", # Hugo-style fence attributes | ||
| ], | ||
| ) | ||
| def test_opening_fence_variants_are_parsed_as_code(opening_fence): | ||
| text = f"Intro:\n\n{opening_fence}\ndef f():\n return 1\n```\n\nAfter.\n" | ||
|
|
||
| result = blocks(text) | ||
|
|
||
| assert [kind for kind, _ in result] == ["prose", "code_fence", "prose"] | ||
| assert " return 1" in result[1][1] # indentation preserved | ||
| assert result[2][1] == "After." | ||
|
|
||
|
|
||
| def test_unrecognised_fence_does_not_invert_the_rest_of_the_page(): | ||
| text = "Intro:\n\n``` python\ndef f():\n return 1\n```\n\nAfter.\n\n```bash\nls\n```\n" | ||
|
|
||
| result = blocks(text) | ||
|
|
||
| assert [kind for kind, _ in result] == ["prose", "code_fence", "prose", "code_fence"] | ||
| assert result[2][1] == "After." | ||
| assert "ls" in result[3][1] | ||
|
|
||
|
|
||
| def test_comment_lines_inside_code_are_not_headings(): | ||
| text = "# Title\n\n``` python\n# a comment, not a heading\nx = 1\n```\n" | ||
|
|
||
| doc = canonical_rag_ingest.parse_canonical_document(text) | ||
|
|
||
| headings = [s.heading for s in doc.sections] | ||
| assert "a comment, not a heading" not in headings | ||
|
|
||
|
|
||
| def test_tilde_fence_info_string_may_contain_backticks(): | ||
| match = canonical_rag_ingest.FENCE_START_RE.match("~~~sh title=`example`") | ||
| assert match is not None | ||
| assert match.group(2) == "sh" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "line", | ||
| [ | ||
| "```inline code```", # backtick info strings cannot contain backticks | ||
| "``` |", # closing fence inside a converted HTML table row | ||
| ], | ||
| ) | ||
| def test_non_fence_lines_are_not_openers(line): | ||
| assert canonical_rag_ingest.FENCE_START_RE.match(line) is None | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "text", | ||
| [ | ||
| "````markdown\n```bash\nls\n```\n````\n\nAfter.\n", # shorter fence inside a longer one | ||
| "```python\nx = 1\n```bash\ny = 2\n```\n\nAfter.\n", # info string on a would-be closer | ||
| ], | ||
| ) | ||
| def test_closing_fence_contains_only_fence_characters(text): | ||
| result = blocks(text) | ||
|
|
||
| assert [kind for kind, _ in result] == ["code_fence", "prose"] | ||
| assert result[1][1] == "After." |
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.
Uh oh!
There was an error while loading. Please reload this page.