Skip to content

Commit

Permalink
try to implement global substitutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Feb 16, 2024
1 parent 1025213 commit 1ebeaa4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/numpy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,6 @@ docspage = 'https://numpy.org/doc/1.26/'
[global.expected_errors]
VisitCitationReferenceNotImplementedError = ["numpy.fft"]

[global.substitution_definitions]
version = '1.2.3'

4 changes: 2 additions & 2 deletions papyri/crosslink.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ def _ingest_examples(
f"TBD (examples, {path}), supposed to be QA",
known_refs,
set(),
{},
aliases,
substitution_defs={},
aliases=aliases,
version=version,
config={},
)
Expand Down
24 changes: 22 additions & 2 deletions papyri/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
# delayed import
if True:
from .myst_ast import MText
from .myst_ast import ReplaceNode
from .myst_ast import MParagraph


class ErrorCollector:
Expand Down Expand Up @@ -433,6 +435,7 @@ class Config:
fail_unseen_error: bool = False
execute_doctests: bool = True
directives: Dict[str, str] = dataclasses.field(default_factory=lambda: {})
substitution_definitions: Dict[str, str] = dataclasses.field(default_factory=dict)

def replace(self, **kwargs):
return dataclasses.replace(self, **kwargs)
Expand Down Expand Up @@ -1456,6 +1459,18 @@ def collect_narrative_docs(self):
trees = {}
title_map = {}
blbs = {}
global_substitutions = {}
for k, v in self.config.substitution_definitions.items():
res = ts.parse(v.encode(), qa="global_substitution")
# HERE are some assumptions as we are parsing a "full document" with tree sitter
# this is going to give a single Section with a single paragraph.
sec: Section
[sec] = res
assert isinstance(sec, Section)
[par] = sec.children
assert isinstance(par, MParagraph)
par: MParagraph
global_substitutions["|" + k + "|"] = [ReplaceNode(k, v, par.children)]
with self.progress() as p2:
task = p2.add_task("Parsing narrative", total=len(files))

Expand All @@ -1477,11 +1492,16 @@ def collect_narrative_docs(self):
blob = DocBlob.new()
key = ":".join(parts)[:-4]
try:
from copy import copy

G = copy(global_substitutions)
G.update({})
ref_set: frozenset[RefInfo] = frozenset({})
dv = DVR(
key,
set(),
ref_set,
local_refs=set(),
substitution_defs={},
substitution_defs=G,
aliases={},
version=self._meta["version"],
config=self.config.directives,
Expand Down
2 changes: 2 additions & 0 deletions papyri/take2.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ class SubstitutionRef(UnserializableNode):
value: str

def __init__(self, value):
assert value.startswith("|")
assert value.endswith("|")
self.value = value

def to_json(self):
Expand Down
5 changes: 4 additions & 1 deletion papyri/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from collections import Counter, defaultdict
from functools import lru_cache
from typing import Any, Dict, FrozenSet, List, Set, Tuple, Callable
from typing import Any, Dict, FrozenSet, List, Set, Tuple, Callable, Union

from .take2 import (
Directive,
Expand Down Expand Up @@ -533,12 +533,15 @@ class DirectiveVisiter(TreeReplacer):
"""

substitution_defs: Dict[str, Union[ReplaceNode, MImage]]

def __init__(
self,
qa: str,
known_refs: FrozenSet[RefInfo],
local_refs,
substitution_defs,
*,
aliases,
version,
config=None,
Expand Down

0 comments on commit 1ebeaa4

Please sign in to comment.