Skip to content

Commit fda2cd2

Browse files
committed
xref wip
1 parent 35b7e5d commit fda2cd2

File tree

5 files changed

+122
-9
lines changed

5 files changed

+122
-9
lines changed

calango/pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ authors = [
77
{ name = "Luciano Ramalho", email = "luciano@ramalho.org" }
88
]
99
requires-python = ">=3.14"
10-
dependencies = []
10+
dependencies = [
11+
"beautifulsoup4>=4.13.5",
12+
]
1113

1214
[project.scripts]
1315
calango = "calango:main"

calango/src/calango/xrefs.py

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import xml.etree.ElementTree as ET
12
from enum import Enum, auto
23
from dataclasses import dataclass
4+
from bs4 import BeautifulSoup
35

46

57
class Kind(Enum):
@@ -13,19 +15,38 @@ class Kind(Enum):
1315
@dataclass
1416
class Target:
1517
ident: str
16-
vol: int = 0
17-
chap: int = 0
18-
numbers: tuple[int, ...] | None = None
18+
numbers: tuple[int, ...] = ()
1919
kind: Kind = Kind.PART
2020

2121
def __post_init__(self):
2222
self.kind = self.get_kind()
23+
if self.kind is not Kind.CHAPTER:
24+
return
25+
self.numbers = (self.chapter, )
26+
27+
@property
28+
def volume(self) -> int:
29+
return (self.chapter - 1) // 8 + 1
30+
31+
@property
32+
def chapter(self) -> int:
33+
return CHAPTER_NUMBER[self.ident]
2334

2435
def get_kind(self) -> Kind:
2536
if self.ident.startswith('ch_'):
2637
return Kind.CHAPTER
38+
elif self.ident.endswith('_sec'):
39+
return Kind.SECTION
2740
raise ValueError(f'Kind of {self.ident!r} is unknown')
2841

42+
def get_section_title(self, root: BeautifulSoup) -> str:
43+
element = root.find(id=self.ident)
44+
if element:
45+
return element.get_text(strip=True)
46+
raise LookupError(f'element {self.ident} not found')
47+
48+
49+
2950

3051
def validate(xref):
3152
return any([
@@ -34,4 +55,34 @@ def validate(xref):
3455
xref.endswith('_sec'),
3556
xref.endswith('_part'),
3657
xref.endswith('_tbl'),
37-
])
58+
])
59+
60+
CHAPTER_ID = {
61+
1 : 'ch_data_model',
62+
2 : 'ch_sequences',
63+
3 : 'ch_dicts_sets',
64+
4 : 'ch_str_bytes',
65+
5 : 'ch_dataclass',
66+
6 : 'ch_refs_mut_mem',
67+
7 : 'ch_func_objects',
68+
8 : 'ch_type_hints_def',
69+
9 : 'ch_closure_decorator',
70+
10 : 'ch_design_patterns',
71+
11 : 'ch_pythonic_obj',
72+
12 : 'ch_seq_methods',
73+
13 : 'ch_ifaces_prot_abc',
74+
14 : 'ch_inheritance',
75+
15 : 'ch_more_types',
76+
16 : 'ch_op_overload',
77+
17 : 'ch_generators',
78+
18 : 'ch_with_match',
79+
19 : 'ch_concurrency_models',
80+
20 : 'ch_executors',
81+
21 : 'ch_async',
82+
22 : 'ch_dynamic_attrs',
83+
23 : 'ch_descriptors',
84+
24 : 'ch_class_metaprog',
85+
}
86+
87+
CHAPTER_NUMBER = {i:n for (n, i) in CHAPTER_ID.items()}
88+

calango/tests/test_xrefs.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
import pytest
22
from calango.xrefs import Target, Kind
3+
from bs4 import BeautifulSoup
34

4-
def test_recursion_depth():
5+
@pytest.mark.parametrize('ident, expected', [
6+
('ch_data_model', Kind.CHAPTER),
7+
('data_model_emulating_sec', Kind.SECTION)
8+
])
9+
def test_kind(ident, expected):
10+
target = Target(ident)
11+
assert target.kind == expected
12+
13+
@pytest.mark.parametrize('ident, expected', [
14+
('ch_data_model', (1, 1)),
15+
('ch_seq_methods', (2, 12)),
16+
('ch_descriptors', (3, 23)),
17+
('data_model_emulating_sec', (1, 1))
18+
])
19+
def test_volume_and_chapter(ident, expected):
20+
target = Target(ident)
21+
assert target.volume, target.chapter == expected
22+
23+
def test_unknown_kind():
524
with pytest.raises(ValueError) as excinfo:
6-
_ = Target('something')
25+
Target('something')
26+
27+
assert "Kind of 'something' is unknown" in str(excinfo.value)
728

8-
assert "Kind of 'something' is unknown" in str(excinfo.value)
29+
def test_get_section_title():
30+
html = '''<h3 id="typeddict_sec"><a class="link" href="#typeddict_sec">15.3. TypedDict</a></h3>'''
31+
root = BeautifulSoup(html, 'html.parser')
32+
target = Target('typeddict_sec')
33+
assert target.get_section_title(root) == '15.3. TypedDict'

calango/uv.lock

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

print/xrefs/list_targets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def main(adoc_names):
1616
for line in lines:
1717
if line.startswith('[['):
1818
ch_id = line.strip()[2:-2]
19-
print(vol_of_chapter(ch), ch, ch_id, sep='\t')
19+
print(f' {ch} : {ch_id!r},')
2020
break
2121

2222
if __name__ == '__main__':

0 commit comments

Comments
 (0)