rbtr-lang-rst 2026.9.0.dev1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rbtr_lang_rst-2026.9.0.dev1/LICENSE +21 -0
- rbtr_lang_rst-2026.9.0.dev1/PKG-INFO +68 -0
- rbtr_lang_rst-2026.9.0.dev1/README.md +42 -0
- rbtr_lang_rst-2026.9.0.dev1/pyproject.toml +64 -0
- rbtr_lang_rst-2026.9.0.dev1/pyproject.toml.orig +65 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/__init__.py +1 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/plugin.py +359 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/py.typed +0 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/references.scm +5 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/__init__.py +0 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/__snapshots__/test_samples/test_edges_match_snapshot.json +8 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/__snapshots__/test_samples/test_extraction_matches_snapshot.json +515 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/cases_extraction.py +89 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/samples/rst/config.rst +4 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/samples/rst/guide.rst +4 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/samples/rst/helpers.py +10 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/samples/rst/readme.rst +16 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/samples/rst/rst.rst +24 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/samples/rst/topics/detail.rst +4 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/samples/rst/topics/intro.rst +7 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/test_extraction.py +265 -0
- rbtr_lang_rst-2026.9.0.dev1/src/rbtr_lang_rst/tests/test_samples.py +85 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alejandro Giacometti
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rbtr-lang-rst
|
|
3
|
+
Version: 2026.9.0.dev1
|
|
4
|
+
Summary: rbtr — reStructuredText language plugin
|
|
5
|
+
Keywords: code-search,code-index,tree-sitter,static-analysis,semantic-search,developer-tools,rst,restructuredtext
|
|
6
|
+
Author: Alejandro Giacometti
|
|
7
|
+
Author-email: Alejandro Giacometti <alejandro.giacometti@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
15
|
+
Classifier: Topic :: Text Processing :: Indexing
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Dist: rbtr==2026.9.0.dev1
|
|
18
|
+
Requires-Dist: tree-sitter-rst
|
|
19
|
+
Requires-Python: >=3.13
|
|
20
|
+
Project-URL: Homepage, https://github.com/janrito/rbtr
|
|
21
|
+
Project-URL: Repository, https://github.com/janrito/rbtr
|
|
22
|
+
Project-URL: Documentation, https://github.com/janrito/rbtr/tree/main/packages/rbtr-lang-rst#readme
|
|
23
|
+
Project-URL: Issues, https://github.com/janrito/rbtr/issues
|
|
24
|
+
Project-URL: Changelog, https://github.com/janrito/rbtr/releases
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# rbtr-lang-rst
|
|
28
|
+
|
|
29
|
+
reStructuredText support for [rbtr]. A **default** plugin — installed
|
|
30
|
+
with rbtr itself (`pip install rbtr`).
|
|
31
|
+
|
|
32
|
+
[rbtr]: https://github.com/janrito/rbtr/tree/main/packages/rbtr#readme
|
|
33
|
+
|
|
34
|
+
## What it ingests
|
|
35
|
+
|
|
36
|
+
A custom chunker: it reconstructs the section hierarchy from the *order of
|
|
37
|
+
adornment characters* (RST has no fixed heading levels), each section a chunk
|
|
38
|
+
scoped by its ancestors. Cross-references are extracted as imports.
|
|
39
|
+
|
|
40
|
+
- **Sections** — headings (underline and overline adornments) → doc sections,
|
|
41
|
+
with `scope` the `::` chain of enclosing sections. A document with no
|
|
42
|
+
heading at all — a README, a changelog — gives each top-level block its own
|
|
43
|
+
section instead: paragraphs, lists, directives, literal blocks, link
|
|
44
|
+
targets.
|
|
45
|
+
- **References** — the `:func:` / `:class:` / `:meth:` / `:mod:` / `:doc:`
|
|
46
|
+
roles, `` `text <target>`_ `` references, and `.. toctree::` entries →
|
|
47
|
+
imports. External URLs and same-file anchors are skipped.
|
|
48
|
+
|
|
49
|
+
## Chunks produced
|
|
50
|
+
|
|
51
|
+
```rst
|
|
52
|
+
Title doc_section "Title"
|
|
53
|
+
=====
|
|
54
|
+
Section doc_section "Section", scope "Title"
|
|
55
|
+
-------
|
|
56
|
+
See :func:`helper` and :doc:`api/index`.
|
|
57
|
+
import {names: helper}; import {module: api/index}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Embedded / injected chunks
|
|
61
|
+
|
|
62
|
+
None. RST does not embed other languages.
|
|
63
|
+
|
|
64
|
+
## Grammar & dependencies
|
|
65
|
+
|
|
66
|
+
Uses the `tree-sitter-rst` grammar. No runtime dependency on other language
|
|
67
|
+
plugins; the test suite dev-depends on `rbtr-lang-python` to extract the
|
|
68
|
+
sample's `helpers.py` and snapshot the `.rst → .py` reference edges.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# rbtr-lang-rst
|
|
2
|
+
|
|
3
|
+
reStructuredText support for [rbtr]. A **default** plugin — installed
|
|
4
|
+
with rbtr itself (`pip install rbtr`).
|
|
5
|
+
|
|
6
|
+
[rbtr]: https://github.com/janrito/rbtr/tree/main/packages/rbtr#readme
|
|
7
|
+
|
|
8
|
+
## What it ingests
|
|
9
|
+
|
|
10
|
+
A custom chunker: it reconstructs the section hierarchy from the *order of
|
|
11
|
+
adornment characters* (RST has no fixed heading levels), each section a chunk
|
|
12
|
+
scoped by its ancestors. Cross-references are extracted as imports.
|
|
13
|
+
|
|
14
|
+
- **Sections** — headings (underline and overline adornments) → doc sections,
|
|
15
|
+
with `scope` the `::` chain of enclosing sections. A document with no
|
|
16
|
+
heading at all — a README, a changelog — gives each top-level block its own
|
|
17
|
+
section instead: paragraphs, lists, directives, literal blocks, link
|
|
18
|
+
targets.
|
|
19
|
+
- **References** — the `:func:` / `:class:` / `:meth:` / `:mod:` / `:doc:`
|
|
20
|
+
roles, `` `text <target>`_ `` references, and `.. toctree::` entries →
|
|
21
|
+
imports. External URLs and same-file anchors are skipped.
|
|
22
|
+
|
|
23
|
+
## Chunks produced
|
|
24
|
+
|
|
25
|
+
```rst
|
|
26
|
+
Title doc_section "Title"
|
|
27
|
+
=====
|
|
28
|
+
Section doc_section "Section", scope "Title"
|
|
29
|
+
-------
|
|
30
|
+
See :func:`helper` and :doc:`api/index`.
|
|
31
|
+
import {names: helper}; import {module: api/index}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Embedded / injected chunks
|
|
35
|
+
|
|
36
|
+
None. RST does not embed other languages.
|
|
37
|
+
|
|
38
|
+
## Grammar & dependencies
|
|
39
|
+
|
|
40
|
+
Uses the `tree-sitter-rst` grammar. No runtime dependency on other language
|
|
41
|
+
plugins; the test suite dev-depends on `rbtr-lang-python` to extract the
|
|
42
|
+
sample's `helpers.py` and snapshot the `.rst → .py` reference edges.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.11.26,<1"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[tool.uv.build-backend]
|
|
6
|
+
module-name = "rbtr_lang_rst"
|
|
7
|
+
|
|
8
|
+
[tool.uv.sources.rbtr]
|
|
9
|
+
workspace = true
|
|
10
|
+
|
|
11
|
+
[tool.uv.sources.rbtr-lang-python]
|
|
12
|
+
workspace = true
|
|
13
|
+
|
|
14
|
+
[project]
|
|
15
|
+
name = "rbtr-lang-rst"
|
|
16
|
+
version = "2026.9.0-dev1"
|
|
17
|
+
description = "rbtr — reStructuredText language plugin"
|
|
18
|
+
readme = "README.md"
|
|
19
|
+
license = "MIT"
|
|
20
|
+
license-files = ["LICENSE"]
|
|
21
|
+
keywords = [
|
|
22
|
+
"code-search",
|
|
23
|
+
"code-index",
|
|
24
|
+
"tree-sitter",
|
|
25
|
+
"static-analysis",
|
|
26
|
+
"semantic-search",
|
|
27
|
+
"developer-tools",
|
|
28
|
+
"rst",
|
|
29
|
+
"restructuredtext",
|
|
30
|
+
]
|
|
31
|
+
classifiers = [
|
|
32
|
+
"Development Status :: 4 - Beta",
|
|
33
|
+
"Intended Audience :: Developers",
|
|
34
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
35
|
+
"Programming Language :: Python :: 3.13",
|
|
36
|
+
"Topic :: Software Development :: Libraries",
|
|
37
|
+
"Topic :: Text Processing :: Indexing",
|
|
38
|
+
"Typing :: Typed",
|
|
39
|
+
]
|
|
40
|
+
requires-python = ">=3.13"
|
|
41
|
+
dependencies = [
|
|
42
|
+
"rbtr==2026.9.0-dev1",
|
|
43
|
+
"tree-sitter-rst",
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
[[project.authors]]
|
|
47
|
+
name = "Alejandro Giacometti"
|
|
48
|
+
email = "alejandro.giacometti@gmail.com"
|
|
49
|
+
|
|
50
|
+
[project.urls]
|
|
51
|
+
Homepage = "https://github.com/janrito/rbtr"
|
|
52
|
+
Repository = "https://github.com/janrito/rbtr"
|
|
53
|
+
Documentation = "https://github.com/janrito/rbtr/tree/main/packages/rbtr-lang-rst#readme"
|
|
54
|
+
Issues = "https://github.com/janrito/rbtr/issues"
|
|
55
|
+
Changelog = "https://github.com/janrito/rbtr/releases"
|
|
56
|
+
|
|
57
|
+
[project.entry-points."rbtr.languages"]
|
|
58
|
+
rst = "rbtr_lang_rst.plugin:rst"
|
|
59
|
+
|
|
60
|
+
[dependency-groups]
|
|
61
|
+
dev = [
|
|
62
|
+
"rbtr[test]",
|
|
63
|
+
"rbtr-lang-python",
|
|
64
|
+
]
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.11.26,<1"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[tool.uv.build-backend]
|
|
6
|
+
module-name = "rbtr_lang_rst"
|
|
7
|
+
# No source-exclude: the plugin's tests, samples, and snapshots ship with
|
|
8
|
+
# the wheel (they are the language's golden record), and references.scm
|
|
9
|
+
# ships as package data.
|
|
10
|
+
|
|
11
|
+
[project]
|
|
12
|
+
name = "rbtr-lang-rst"
|
|
13
|
+
version = "2026.9.0-dev1"
|
|
14
|
+
description = "rbtr — reStructuredText language plugin"
|
|
15
|
+
readme = "README.md"
|
|
16
|
+
license = "MIT"
|
|
17
|
+
license-files = ["LICENSE"]
|
|
18
|
+
authors = [
|
|
19
|
+
{ name = "Alejandro Giacometti", email = "alejandro.giacometti@gmail.com" },
|
|
20
|
+
]
|
|
21
|
+
keywords = [
|
|
22
|
+
"code-search",
|
|
23
|
+
"code-index",
|
|
24
|
+
"tree-sitter",
|
|
25
|
+
"static-analysis",
|
|
26
|
+
"semantic-search",
|
|
27
|
+
"developer-tools",
|
|
28
|
+
"rst",
|
|
29
|
+
"restructuredtext",
|
|
30
|
+
]
|
|
31
|
+
classifiers = [
|
|
32
|
+
"Development Status :: 4 - Beta",
|
|
33
|
+
"Intended Audience :: Developers",
|
|
34
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
35
|
+
"Programming Language :: Python :: 3.13",
|
|
36
|
+
"Topic :: Software Development :: Libraries",
|
|
37
|
+
"Topic :: Text Processing :: Indexing",
|
|
38
|
+
"Typing :: Typed",
|
|
39
|
+
]
|
|
40
|
+
requires-python = ">=3.13"
|
|
41
|
+
dependencies = [
|
|
42
|
+
"rbtr==2026.9.0-dev1",
|
|
43
|
+
"tree-sitter-rst",
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
[project.urls]
|
|
47
|
+
Homepage = "https://github.com/janrito/rbtr"
|
|
48
|
+
Repository = "https://github.com/janrito/rbtr"
|
|
49
|
+
Documentation = "https://github.com/janrito/rbtr/tree/main/packages/rbtr-lang-rst#readme"
|
|
50
|
+
Issues = "https://github.com/janrito/rbtr/issues"
|
|
51
|
+
Changelog = "https://github.com/janrito/rbtr/releases"
|
|
52
|
+
|
|
53
|
+
[project.entry-points."rbtr.languages"]
|
|
54
|
+
# The value resolves to the language's `LanguageRegistration` (named by its
|
|
55
|
+
# id) — see the rbtr ARCHITECTURE "External plugins" note.
|
|
56
|
+
rst = "rbtr_lang_rst.plugin:rst"
|
|
57
|
+
|
|
58
|
+
[tool.uv.sources]
|
|
59
|
+
rbtr = { workspace = true }
|
|
60
|
+
# The sample project includes a helpers.py the docs cross-reference, so the
|
|
61
|
+
# sample test needs the python plugin to extract it and snapshot the edges.
|
|
62
|
+
rbtr-lang-python = { workspace = true }
|
|
63
|
+
|
|
64
|
+
[dependency-groups]
|
|
65
|
+
dev = ["rbtr[test]", "rbtr-lang-python"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""reStructuredText language plugin package."""
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
"""RST language plugin.
|
|
2
|
+
|
|
3
|
+
Splits reStructuredText by heading hierarchy using tree-sitter.
|
|
4
|
+
RST sections are flat in the AST (title + adornment only), so
|
|
5
|
+
the chunker reconstructs hierarchy from adornment character
|
|
6
|
+
order (first seen = level 1, second = level 2, etc.).
|
|
7
|
+
|
|
8
|
+
Extracted chunks::
|
|
9
|
+
|
|
10
|
+
Title → doc_section "Title", scope ""
|
|
11
|
+
===== (content: heading through next heading)
|
|
12
|
+
|
|
13
|
+
Section A → doc_section "Section A", scope "Title"
|
|
14
|
+
--------- (adornment char "-" = level 2)
|
|
15
|
+
|
|
16
|
+
First paragraph. → doc_section "", scope "" (no heading)
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from collections.abc import Iterator
|
|
22
|
+
from typing import TYPE_CHECKING
|
|
23
|
+
|
|
24
|
+
from tree_sitter import Parser, Query, QueryCursor
|
|
25
|
+
|
|
26
|
+
from rbtr.domain.models import Chunk, ChunkKind, ImportMeta
|
|
27
|
+
from rbtr.languages.chunks import last_line
|
|
28
|
+
from rbtr.languages.registration import LanguageRegistration, load_query
|
|
29
|
+
|
|
30
|
+
if TYPE_CHECKING:
|
|
31
|
+
from tree_sitter import Language, Node, Range
|
|
32
|
+
|
|
33
|
+
# URL schemes that indicate external links.
|
|
34
|
+
_EXTERNAL_SCHEMES = ("http://", "https://", "mailto:", "ftp://")
|
|
35
|
+
|
|
36
|
+
# RST roles that reference a code symbol by name.
|
|
37
|
+
_SYMBOL_ROLES = frozenset({":func:", ":class:", ":meth:", ":attr:"})
|
|
38
|
+
# RST roles that reference a document/module by path.
|
|
39
|
+
_PATH_ROLES = frozenset({":doc:", ":mod:"})
|
|
40
|
+
|
|
41
|
+
# ── Chunking ─────────────────────────────────────────────────────────
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _rst_title_text(section: Node) -> str:
|
|
45
|
+
"""Extract the title text from an RST section node."""
|
|
46
|
+
for child in section.children:
|
|
47
|
+
if child.type == "title" and child.text:
|
|
48
|
+
return child.text.decode("utf-8", errors="replace")
|
|
49
|
+
return ""
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _rst_adornment_char(section: Node) -> str:
|
|
53
|
+
"""Return the adornment character of an RST section."""
|
|
54
|
+
for child in section.children:
|
|
55
|
+
if child.type == "adornment" and child.text:
|
|
56
|
+
return child.text.decode()[0]
|
|
57
|
+
return ""
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def chunk_rst(
|
|
61
|
+
file_path: str,
|
|
62
|
+
blob_sha: str,
|
|
63
|
+
content: str,
|
|
64
|
+
grammar: Language,
|
|
65
|
+
ranges: list[Range] | None = None,
|
|
66
|
+
) -> Iterator[Chunk]:
|
|
67
|
+
"""Split RST into sections, then extract its cross-references.
|
|
68
|
+
|
|
69
|
+
A document with headings is split by hierarchy, reconstructed from
|
|
70
|
+
adornment characters (first character seen = level 1, second = level
|
|
71
|
+
2, ...), each section running from its heading to the next
|
|
72
|
+
same-or-higher-level heading. A document with no heading has one
|
|
73
|
+
section per top-level block instead. References are extracted from
|
|
74
|
+
either.
|
|
75
|
+
"""
|
|
76
|
+
if not content.strip():
|
|
77
|
+
return
|
|
78
|
+
|
|
79
|
+
content_bytes = content.encode("utf-8")
|
|
80
|
+
parser = Parser(grammar)
|
|
81
|
+
if ranges is not None:
|
|
82
|
+
parser.included_ranges = ranges
|
|
83
|
+
tree = parser.parse(content_bytes)
|
|
84
|
+
root = tree.root_node
|
|
85
|
+
|
|
86
|
+
children = [c for c in root.children if c.is_named]
|
|
87
|
+
|
|
88
|
+
if any(c.type == "section" for c in children):
|
|
89
|
+
yield from _sectioned_chunks(children, root, content_bytes, file_path, blob_sha)
|
|
90
|
+
else:
|
|
91
|
+
yield from _block_chunks(children, content_bytes, file_path, blob_sha)
|
|
92
|
+
|
|
93
|
+
yield from _extract_references(root, grammar, content_bytes, file_path, blob_sha)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _block_chunks(
|
|
97
|
+
children: list[Node],
|
|
98
|
+
content_bytes: bytes,
|
|
99
|
+
file_path: str,
|
|
100
|
+
blob_sha: str,
|
|
101
|
+
) -> Iterator[Chunk]:
|
|
102
|
+
"""One section per top-level block, for a document with no headings.
|
|
103
|
+
|
|
104
|
+
Every block a document can open with is content: a paragraph, a
|
|
105
|
+
bullet or enumerated list, a definition list, a directive, a literal
|
|
106
|
+
block, a comment, a footnote, a link target. None carries a heading
|
|
107
|
+
to be named by, so each is anonymous and located by its lines.
|
|
108
|
+
"""
|
|
109
|
+
for child in children:
|
|
110
|
+
text = (
|
|
111
|
+
content_bytes[child.start_byte : child.end_byte]
|
|
112
|
+
.decode("utf-8", errors="replace")
|
|
113
|
+
.strip()
|
|
114
|
+
)
|
|
115
|
+
if not text:
|
|
116
|
+
continue
|
|
117
|
+
yield Chunk(
|
|
118
|
+
blob_sha=blob_sha,
|
|
119
|
+
file_path=file_path,
|
|
120
|
+
kind=ChunkKind.DOC_SECTION,
|
|
121
|
+
name="",
|
|
122
|
+
scope="",
|
|
123
|
+
content=text,
|
|
124
|
+
line_start=child.start_point[0] + 1,
|
|
125
|
+
line_end=last_line(child),
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _sectioned_chunks(
|
|
130
|
+
children: list[Node],
|
|
131
|
+
root: Node,
|
|
132
|
+
content_bytes: bytes,
|
|
133
|
+
file_path: str,
|
|
134
|
+
blob_sha: str,
|
|
135
|
+
) -> Iterator[Chunk]:
|
|
136
|
+
"""One section per heading, scoped by the adornment hierarchy."""
|
|
137
|
+
# Build heading level map from adornment character order.
|
|
138
|
+
adornment_levels: dict[str, int] = {}
|
|
139
|
+
for child in children:
|
|
140
|
+
if child.type == "section":
|
|
141
|
+
char = _rst_adornment_char(child)
|
|
142
|
+
if char and char not in adornment_levels:
|
|
143
|
+
adornment_levels[char] = len(adornment_levels) + 1
|
|
144
|
+
|
|
145
|
+
# Walk siblings: associate content with preceding section.
|
|
146
|
+
chunks: list[Chunk] = []
|
|
147
|
+
scope_stack: list[tuple[str, int]] = []
|
|
148
|
+
current_title = ""
|
|
149
|
+
current_start = 0
|
|
150
|
+
current_line_start = 1
|
|
151
|
+
in_section = False
|
|
152
|
+
|
|
153
|
+
for child in children:
|
|
154
|
+
if child.type == "section":
|
|
155
|
+
# Close previous section if open.
|
|
156
|
+
if in_section:
|
|
157
|
+
text = (
|
|
158
|
+
content_bytes[current_start : child.start_byte]
|
|
159
|
+
.decode("utf-8", errors="replace")
|
|
160
|
+
.strip()
|
|
161
|
+
)
|
|
162
|
+
if text:
|
|
163
|
+
# Exclude the section being closed (the stack top) so
|
|
164
|
+
# scope is the enclosing path only — never self. The
|
|
165
|
+
# final-section close below does the same via [:-1].
|
|
166
|
+
chunks.append(
|
|
167
|
+
Chunk.model_validate(
|
|
168
|
+
{
|
|
169
|
+
"blob_sha": blob_sha,
|
|
170
|
+
"file_path": file_path,
|
|
171
|
+
"kind": ChunkKind.DOC_SECTION,
|
|
172
|
+
"name": current_title,
|
|
173
|
+
"scope": [t for t, _ in scope_stack[:-1]],
|
|
174
|
+
"content": text,
|
|
175
|
+
"line_start": current_line_start,
|
|
176
|
+
"line_end": child.start_point[0],
|
|
177
|
+
}
|
|
178
|
+
)
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
# Start new section.
|
|
182
|
+
title = _rst_title_text(child)
|
|
183
|
+
char = _rst_adornment_char(child)
|
|
184
|
+
level = adornment_levels.get(char, 0)
|
|
185
|
+
|
|
186
|
+
# Pop scope stack to parent level.
|
|
187
|
+
while scope_stack and scope_stack[-1][1] >= level:
|
|
188
|
+
scope_stack.pop()
|
|
189
|
+
|
|
190
|
+
current_title = title
|
|
191
|
+
current_start = child.start_byte
|
|
192
|
+
current_line_start = child.start_point[0] + 1
|
|
193
|
+
in_section = True
|
|
194
|
+
|
|
195
|
+
scope_stack.append((title, level))
|
|
196
|
+
|
|
197
|
+
# Close final section and yield all accumulated chunks.
|
|
198
|
+
if in_section:
|
|
199
|
+
text = content_bytes[current_start:].decode("utf-8", errors="replace").strip()
|
|
200
|
+
if text:
|
|
201
|
+
scope_stack_for_scope = scope_stack[:-1]
|
|
202
|
+
chunks.append(
|
|
203
|
+
Chunk.model_validate(
|
|
204
|
+
{
|
|
205
|
+
"blob_sha": blob_sha,
|
|
206
|
+
"file_path": file_path,
|
|
207
|
+
"kind": ChunkKind.DOC_SECTION,
|
|
208
|
+
"name": current_title,
|
|
209
|
+
"scope": [t for t, _ in scope_stack_for_scope],
|
|
210
|
+
"content": text,
|
|
211
|
+
"line_start": current_line_start,
|
|
212
|
+
"line_end": last_line(root),
|
|
213
|
+
}
|
|
214
|
+
)
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
yield from chunks
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
# ── Reference extraction ─────────────────────────────────────────────
|
|
221
|
+
|
|
222
|
+
_REF_QUERY = load_query(__package__, "references")
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _strip_rst_target(role: str, raw: str) -> str:
|
|
226
|
+
"""Clean an RST role target: strip backticks and ~ prefix.
|
|
227
|
+
|
|
228
|
+
For symbol roles (:func:, :class:, :meth:), `~` means
|
|
229
|
+
"show only the last component" — we extract the last
|
|
230
|
+
component as the symbol name.
|
|
231
|
+
"""
|
|
232
|
+
target = raw.strip("`")
|
|
233
|
+
if target.startswith("~"):
|
|
234
|
+
target = target[1:]
|
|
235
|
+
# ~module.Class.method → method (last component)
|
|
236
|
+
if "." in target:
|
|
237
|
+
target = target.rsplit(".", 1)[1]
|
|
238
|
+
return target
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _extract_references(
|
|
242
|
+
root: Node,
|
|
243
|
+
grammar: Language,
|
|
244
|
+
content_bytes: bytes,
|
|
245
|
+
file_path: str,
|
|
246
|
+
blob_sha: str,
|
|
247
|
+
) -> Iterator[Chunk]:
|
|
248
|
+
"""Extract RST cross-references as IMPORT chunks.
|
|
249
|
+
|
|
250
|
+
Uses tree-sitter queries on the already-parsed tree for:
|
|
251
|
+
- Domain roles (:func:, :class:, :doc:, etc.)
|
|
252
|
+
- Toctree directives
|
|
253
|
+
- Hyperlink references (`text <url>`_)
|
|
254
|
+
"""
|
|
255
|
+
query = Query(grammar, _REF_QUERY)
|
|
256
|
+
for _, captures in QueryCursor(query).matches(root):
|
|
257
|
+
# Domain roles: :func:`name`, :doc:`path`, etc.
|
|
258
|
+
if "ref" in captures:
|
|
259
|
+
role_nodes = captures.get("role", [])
|
|
260
|
+
ref_nodes = captures["ref"]
|
|
261
|
+
if not role_nodes or not ref_nodes:
|
|
262
|
+
continue
|
|
263
|
+
role_text = role_nodes[0].text.decode() if role_nodes[0].text else ""
|
|
264
|
+
ref_node = ref_nodes[0]
|
|
265
|
+
|
|
266
|
+
# Get inner interpreted_text child (the target).
|
|
267
|
+
target_text = ""
|
|
268
|
+
for child in ref_node.children:
|
|
269
|
+
if child.type == "interpreted_text" and child is not role_nodes[0]:
|
|
270
|
+
target_text = child.text.decode().strip("`") if child.text else ""
|
|
271
|
+
break
|
|
272
|
+
if not target_text:
|
|
273
|
+
continue
|
|
274
|
+
|
|
275
|
+
target = _strip_rst_target(role_text, target_text)
|
|
276
|
+
meta = ImportMeta()
|
|
277
|
+
if role_text in _SYMBOL_ROLES:
|
|
278
|
+
meta.names = target
|
|
279
|
+
elif role_text in _PATH_ROLES:
|
|
280
|
+
meta.module = target
|
|
281
|
+
else:
|
|
282
|
+
continue
|
|
283
|
+
|
|
284
|
+
yield Chunk(
|
|
285
|
+
blob_sha=blob_sha,
|
|
286
|
+
file_path=file_path,
|
|
287
|
+
kind=ChunkKind.IMPORT,
|
|
288
|
+
name=f"{role_text}`{target_text}`",
|
|
289
|
+
scope="",
|
|
290
|
+
content=ref_node.text.decode() if ref_node.text else "",
|
|
291
|
+
metadata=meta,
|
|
292
|
+
line_start=ref_node.start_point[0] + 1,
|
|
293
|
+
line_end=last_line(ref_node),
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
# Toctree directives.
|
|
297
|
+
elif "directive" in captures:
|
|
298
|
+
type_nodes = captures.get("dir_type", [])
|
|
299
|
+
content_nodes = captures.get("content", [])
|
|
300
|
+
if not type_nodes or not content_nodes:
|
|
301
|
+
continue
|
|
302
|
+
dtype = type_nodes[0].text.decode() if type_nodes[0].text else ""
|
|
303
|
+
if dtype != "toctree":
|
|
304
|
+
continue
|
|
305
|
+
raw_content = content_nodes[0].text.decode() if content_nodes[0].text else ""
|
|
306
|
+
for line in raw_content.splitlines():
|
|
307
|
+
entry = line.strip()
|
|
308
|
+
if not entry:
|
|
309
|
+
continue
|
|
310
|
+
yield Chunk(
|
|
311
|
+
blob_sha=blob_sha,
|
|
312
|
+
file_path=file_path,
|
|
313
|
+
kind=ChunkKind.IMPORT,
|
|
314
|
+
name=f"toctree:{entry}",
|
|
315
|
+
scope="",
|
|
316
|
+
content=entry,
|
|
317
|
+
metadata=ImportMeta(module=entry),
|
|
318
|
+
line_start=content_nodes[0].start_point[0] + 1,
|
|
319
|
+
line_end=last_line(content_nodes[0]),
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
# Hyperlink references: `text <url>`_
|
|
323
|
+
elif "hyperlink" in captures:
|
|
324
|
+
for ref_node in captures["hyperlink"]:
|
|
325
|
+
if ref_node.text is None:
|
|
326
|
+
continue
|
|
327
|
+
text = ref_node.text.decode()
|
|
328
|
+
# Extract URL from between < and > in node text.
|
|
329
|
+
lt = text.rfind("<")
|
|
330
|
+
gt = text.rfind(">")
|
|
331
|
+
if lt == -1 or gt == -1 or gt <= lt:
|
|
332
|
+
continue
|
|
333
|
+
url = text[lt + 1 : gt]
|
|
334
|
+
if any(url.startswith(s) for s in _EXTERNAL_SCHEMES):
|
|
335
|
+
continue
|
|
336
|
+
yield Chunk(
|
|
337
|
+
blob_sha=blob_sha,
|
|
338
|
+
file_path=file_path,
|
|
339
|
+
kind=ChunkKind.IMPORT,
|
|
340
|
+
name=text,
|
|
341
|
+
scope="",
|
|
342
|
+
content=text,
|
|
343
|
+
metadata=ImportMeta(module=url),
|
|
344
|
+
line_start=ref_node.start_point[0] + 1,
|
|
345
|
+
line_end=last_line(ref_node),
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
# ── Plugin ───────────────────────────────────────────────────────────
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
rst = LanguageRegistration(
|
|
353
|
+
id="rst",
|
|
354
|
+
extensions=frozenset({".rst"}),
|
|
355
|
+
grammar_module="tree_sitter_rst",
|
|
356
|
+
extraction_serial=4,
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
rst.chunker(chunk_rst)
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
[
|
|
2
|
+
"rst.rst:::class:`Greeter` -> helpers.py::Greeter [documents]",
|
|
3
|
+
"rst.rst:::doc:`config` -> config.rst::Config [documents]",
|
|
4
|
+
"rst.rst:::doc:`topics/intro` -> topics/intro.rst::Intro [documents]",
|
|
5
|
+
"rst.rst:::func:`format_greeting` -> helpers.py::format_greeting [documents]",
|
|
6
|
+
"rst.rst::`the guide <guide.rst>`_ -> guide.rst::Guide [documents]",
|
|
7
|
+
"topics/intro.rst:::doc:`detail` -> topics/detail.rst::Detail [documents]"
|
|
8
|
+
]
|