Skip to content

Commit b947270

Browse files
committed
wip: refactor: Split render to multiple modules
1 parent af96561 commit b947270

13 files changed

Lines changed: 854 additions & 702 deletions

File tree

src/sphinxnotes/data/__init__.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,15 @@
2626
from .template import Phase, Template
2727
from .render import (
2828
Host,
29-
pending_node,
30-
rendered_node,
31-
BaseDataDefiner,
29+
pending_data,
30+
rendered_data,
3231
BaseDataDefineRole,
3332
BaseDataDefineDirective,
3433
StrictDataDefineDirective,
3534
ExtraContextRegistry,
36-
EXTRACTX_REGISTRY,
35+
ExtraContextGenerator,
3736
)
3837
from .config import Config
39-
from . import extractx
4038

4139
if TYPE_CHECKING:
4240
from sphinx.application import Sphinx
@@ -56,13 +54,11 @@
5654
'Phase',
5755
'Template',
5856
'Host',
59-
'pending_node',
60-
'rendered_node',
61-
'rendered_node',
62-
'BaseDataDefiner',
57+
'pending_data',
58+
'rendered_data',
59+
'rendered_data',
6360
'BaseDataDefineRole',
6461
'BaseDataDefineDirective',
65-
'BaseDataDefineDirective',
6662
'StrictDataDefineDirective',
6763
]
6864

@@ -77,8 +73,8 @@ def data(self) -> DataRegistry:
7773
return DATA_REGISTRY
7874

7975
@property
80-
def extractx(cls) -> ExtraContextRegistry:
81-
return EXTRACTX_REGISTRY
76+
def extra_context(cls) -> ExtraContextRegistry:
77+
return ExtraContextGenerator.registry
8278

8379

8480
REGISTRY = Registry()

src/sphinxnotes/data/adhoc.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from sphinx.util.docutils import SphinxDirective
2323

2424
from .data import Field, Schema
25-
from .template import Template, Phase
25+
from .render.template import Template
2626
from .render import BaseDataDefineDirective
2727
from .utils.freestyle import FreeStyleDirective, FreeStyleOptionSpec
2828
from . import preset
@@ -35,9 +35,14 @@
3535
SCHEMA_KEY = 'sphinxnotes-data:schema'
3636

3737

38+
def phase_option_spec(arg):
39+
choice = directives.choice(arg, [x.value for x in Phase])
40+
return Phase[choice.title()]
41+
42+
3843
class TemplateDefineDirective(SphinxDirective):
3944
option_spec = {
40-
'on': Phase.option_spec,
45+
'on': phase_option_spec,
4146
'debug': directives.flag,
4247
}
4348
has_content = True

src/sphinxnotes/data/data.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def _bool_conv(v: str | None) -> bool:
9797
if v in ('false', 'no', '0', 'off', 'n', None):
9898
return False
9999
# Same to :meth:`directives.flag`.
100-
raise ValueError(f'no argument is allowed; "{v}" supplied')
100+
raise ValueError(f'No argument is allowed; "{v}" supplied')
101101

102102

103103
def _str_conv(v: str) -> str:
@@ -172,7 +172,7 @@ def add_form(
172172
self, name: str, ctype: type, sep: str, aliases: list[str] = []
173173
) -> None:
174174
if ctype not in self.ctypes:
175-
raise ValueError(f'unsupported type: "{ctype}". available: {self.ctypes}')
175+
raise ValueError(f'Unsupported type: "{ctype}". Available: {self.ctypes}')
176176

177177
form = Form(ctype, sep)
178178

@@ -297,7 +297,7 @@ def parse(self, rawval: str | None) -> Value:
297297
if rawval is None:
298298
if self.required:
299299
# Same to :meth:`directives.unchanged_required`.
300-
raise ValueError('argument required but none supplied')
300+
raise ValueError('Argument required but none supplied')
301301
if self.ctype:
302302
# Return a empty container when a None value is optional.
303303
return self.ctype()
@@ -329,7 +329,7 @@ def parse(self, rawval: str | None) -> Value:
329329

330330
return self.ctype(elems)
331331
except ValueError as e:
332-
raise ValueError(f"failed to parse '{rawval}' as {self.etype}: {e}") from e
332+
raise ValueError(f"Failed to parse '{rawval}' as {self.etype}: {e}") from e
333333

334334
def __getattr__(self, name: str) -> Value:
335335
if name in self.flags:
@@ -382,13 +382,13 @@ def _apply_modifier(self, mod: str):
382382

383383
if etype not in REGISTRY.etypes:
384384
raise ValueError(
385-
f'unsupported type: "{etype}". '
386-
f'available: {list(REGISTRY.etypes.keys())}'
385+
f'Unsupported type: "{etype}". '
386+
f'Available: {list(REGISTRY.etypes.keys())}'
387387
)
388388
if form not in REGISTRY.forms:
389389
raise ValueError(
390-
f'unsupported form: "{form}". '
391-
f'available: {list(REGISTRY.forms.keys())}'
390+
f'Unsupported form: "{form}". '
391+
f'Available: {list(REGISTRY.forms.keys())}'
392392
)
393393

394394
self.field.etype = REGISTRY.etypes[etype]
@@ -407,8 +407,8 @@ def _apply_modifier(self, mod: str):
407407

408408
if optname not in REGISTRY.byopts:
409409
raise ValueError(
410-
f'unsupported by-option: "{optname}" by. '
411-
f'available: {list(REGISTRY.byopts.keys())}'
410+
f'Unsupported by-option: "{optname}" by. '
411+
f'Available: {list(REGISTRY.byopts.keys())}'
412412
)
413413

414414
flags = self.field.flags
@@ -438,13 +438,13 @@ def _apply_modifier(self, mod: str):
438438
self.field.flags[opt.name] = not opt.default
439439
return
440440

441-
raise ValueError(f"unknown modifier: '{mod}'")
441+
raise ValueError(f"Unknown modifier: '{mod}'")
442442

443443
@staticmethod
444444
def by_option_store_value_error(opt: ByOption) -> ValueError:
445445
raise ValueError(
446-
f'unsupported by-option store: "{opt.store}". '
447-
f'available: {ByOptionStore}' # FIXME:
446+
f'Unsupported by-option store: "{opt.store}". '
447+
f'Available: {ByOptionStore}' # FIXME:
448448
)
449449

450450

@@ -472,14 +472,14 @@ def _parse_single(
472472
) -> Value:
473473
if field[1] is None and rawval is not None:
474474
raise ValueError(
475-
f'parsing {field[0]}: no argument is allowed; "{rawval}" supplied'
475+
f'Parsing {field[0]}: no argument is allowed; "{rawval}" supplied'
476476
)
477477

478478
try:
479479
assert field[1] is not None
480480
return field[1].parse(rawval)
481481
except Exception as e:
482-
raise ValueError(f'parsing {field[0]}: {e}')
482+
raise ValueError(f'Parsing {field[0]}: {e}')
483483

484484
def parse(self, data: RawData) -> ParsedData:
485485
if data.name:
@@ -497,7 +497,7 @@ def parse(self, data: RawData) -> ParsedData:
497497
if rawval := rawattrs.pop(key, None):
498498
attrs[key] = self._parse_single(('attrs.' + key, field), rawval)
499499
for key, rawval in rawattrs.items():
500-
raise ValueError(f'unknown attr: "{key}"')
500+
raise ValueError(f'Unknown attr: "{key}"')
501501

502502
if data.content:
503503
content = self._parse_single(('content', self.content), data.content)

src/sphinxnotes/data/extractx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
if TYPE_CHECKING:
1919
from typing import Any
2020

21+
2122
@dataclass
2223
class _Host:
2324
v: Host

0 commit comments

Comments
 (0)