diff --git a/alexi/link.py b/alexi/link.py index e92e44c..85940c8 100644 --- a/alexi/link.py +++ b/alexi/link.py @@ -8,7 +8,7 @@ import re from typing import Optional -from .analyse import PALIERS, Document +from .analyse import PALIERS, Document, MILIEU, MTYPE LOGGER = logging.getLogger("link") @@ -23,6 +23,7 @@ re.IGNORECASE, ) REG_RE = re.compile(r"règlement[^\d]+(?P[\d\.A-Z-]+)", re.IGNORECASE) +MILIEU_RE = re.compile(rf"{MILIEU}\s+(?P{MTYPE})", re.IGNORECASE | re.VERBOSE) PALIER_IDX = {palier: idx for idx, palier in enumerate(PALIERS)} @@ -61,10 +62,22 @@ def __call__( self, text: str, srcpath: str = "", doc: Optional[Document] = None ) -> str: url = self.resolve_external(text) + if url: + return url + url = self.resolve_zonage(text, srcpath) if url: return url return self.resolve_internal(text, srcpath, doc) + def resolve_zonage(self, text: str, srcpath: str) -> Optional[str]: + m = MILIEU_RE.search(text) + if m is None: + return None + milieu = self.metadata["zonage"]["milieu"].get(m.group("mtype")) + if milieu is None: + return None + return os.path.relpath(f"../{milieu['url']}/index.html", srcpath) + def resolve_absolute_internal( self, docpath: str, secpath: str, srcpath: str ) -> Optional[str]: diff --git a/test/test_link.py b/test/test_link.py index 655378e..3a87d20 100644 --- a/test/test_link.py +++ b/test/test_link.py @@ -132,12 +132,14 @@ def test_laws(test_input, expected): "Règlement de zonage", "../index.html#20231213-Codification-administrative-Rgl-1314-2021-Z", ), - # NOTE: sous-section not expected to work yet ( "section 3 du chapitre 5 du Règlement de zonage 1314-2021-Z", "../20231213-Codification-administrative-Rgl-1314-2021-Z/Chapitre/5/Section/3/index.html", ), - # TODO: links to milieux, usages, etc + ( + "type de milieu T5.2", + "../20231213-Codification-administrative-Rgl-1314-2021-Z/Chapitre/7/Section/6/SousSection/_89/index.html", + ), # FIXME: Also test invalid links somehow! ] @@ -145,7 +147,7 @@ def test_laws(test_input, expected): @pytest.mark.parametrize("test_input,expected", BYLAWS) def test_bylaws(test_input, expected): r = Resolver(METADATA) - assert r.resolve_internal(test_input, ".") == expected + assert r(test_input, ".") == expected with open(DATADIR / "lotissement.json", "rt") as infh: