Skip to content

Commit 04d193b

Browse files
authored
Merge pull request #42 from sphinx-notes/feat/path-indexer
feat: Impl PathIndexer
2 parents c5355b4 + 6262eac commit 04d193b

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ on:
55
schedule:
66
- cron: '0 7 * * 6'
77
jobs:
8-
test-ubuntu:
8+
test:
99
runs-on: ubuntu-latest
1010
steps:
1111
- uses: actions/checkout@v2
12+
- uses: actions/setup-python@v5
13+
with:
14+
python-version-file: 'pyproject.toml'
1215
- name: Install the dependencies
13-
run: python3 -m pip install sphinx -r ./requirements.txt
16+
run: python3 -m pip install .[dev]
1417
- name: Build
1518
run: make test

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ classifiers = [
2727
"Topic :: Documentation :: Sphinx",
2828
]
2929

30-
requires-python = ">=3.8"
30+
requires-python = ">=3.12"
3131
dependencies = [
3232
"Sphinx >= 4",
3333
"Jinja2",

src/sphinxnotes/any/schema.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,8 @@ def classify(self, objref: Value) -> list[Category]:
160160

161161
_T = TypeVar('_T')
162162

163-
@abstractmethod
164163
def sort(self, data: Iterable[_T], key: Callable[[_T], Category]) -> list[_T]:
165-
raise NotImplementedError
164+
return sorted(data, key=lambda x: key(x)._sort_key)
166165

167166
@abstractmethod
168167
def anchor(self, refval: str) -> str:
@@ -178,11 +177,6 @@ def classify(self, objref: Value) -> list[Category]:
178177
entries.append(Category(main=v))
179178
return entries
180179

181-
def sort(
182-
self, data: Iterable[Indexer._T], key: Callable[[Indexer._T], Category]
183-
) -> list[Indexer._T]:
184-
return sorted(data, key=lambda x: key(x)._sort_key)
185-
186180
def anchor(self, refval: str) -> str:
187181
return refval
188182

@@ -303,6 +297,27 @@ def anchor(self, refval: str) -> str:
303297
return ''
304298

305299

300+
class PathIndexer(Indexer):
301+
name = 'path'
302+
303+
def __init__(self, sep: str, maxsplit: Literal[1, 2]):
304+
self.sep = sep
305+
self.maxsplit = maxsplit
306+
307+
def classify(self, objref: Value) -> list[Category]:
308+
entries = []
309+
for v in objref.as_list():
310+
comps = v.split(self.sep, maxsplit=self.maxsplit)
311+
category = Category(main=comps[0], extra=v)
312+
if self.maxsplit == 2:
313+
category.sub = v[1] if len(comps) > 1 else None
314+
entries.append(category)
315+
return entries
316+
317+
def anchor(self, refval: str) -> str:
318+
return refval.split(self.sep, maxsplit=self.maxsplit)[0]
319+
320+
306321
@dataclasses.dataclass(frozen=True)
307322
class Object(object):
308323
objtype: str

tests/test_schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ def test_equal(self):
1515
def new_schema(self) -> Schema:
1616
return Schema(
1717
'cat',
18-
name=Field(referenceable=True, form=Field.Form.LINES),
18+
name=Field(ref=True, form=Field.Forms.LINES),
1919
attrs={
20-
'id': Field(unique=True, referenceable=True, required=True),
20+
'id': Field(uniq=True, ref=True, required=True),
2121
'owner': Field(),
2222
'height': Field(),
2323
'width': Field(),

0 commit comments

Comments
 (0)