mirror of
https://git.srhtnin.garden/sarah/SaraView.git
synced 2026-10-15 02:43:17 +02:00
Update Portals.py
This commit is contained in:
1 parent
0cd98873e0
commit
9bd57505bb
1 file changed
+41
-14
+41
-14
@@ -23,6 +23,9 @@ MarkerPattern = re.compile(
|
|||||||
LoopMarkerPattern = re.compile(
|
LoopMarkerPattern = re.compile(
|
||||||
r"\{(?P<LoopSource>children|siblings|parents)\s+%\s+(?P<LoopVar>[A-Za-z_][A-Za-z0-9_]*)\}"
|
r"\{(?P<LoopSource>children|siblings|parents)\s+%\s+(?P<LoopVar>[A-Za-z_][A-Za-z0-9_]*)\}"
|
||||||
r"|\{(?P<CloseSource>children|siblings|parents)\s+%\}"
|
r"|\{(?P<CloseSource>children|siblings|parents)\s+%\}"
|
||||||
|
r"|\{(?:(?P<LoopCrossPath>[^\s{}']+)\s+)?\*\*\*(?P<LoopCrossName>[^\s*{}]+)"
|
||||||
|
r"\s+%\s+(?P<LoopCrossVar>[A-Za-z_][A-Za-z0-9_]*)\}"
|
||||||
|
r"|\{\*\*\*(?P<CloseCrossName>[^\s*{}]+)\s+%\}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -147,7 +150,24 @@ def _ComputeLoopLists(
|
|||||||
return {"children": Children, "siblings": Siblings, "parents": Parents}
|
return {"children": Children, "siblings": Siblings, "parents": Parents}
|
||||||
|
|
||||||
|
|
||||||
def _ResolveLoops(Content: str, Lists: dict[str, list[str]]) -> str:
|
def _FindLoopClose(Matches: list[re.Match], StartIndex: int, IsFixed: bool, Key: str) -> re.Match | None:
|
||||||
|
GroupName = "CloseSource" if IsFixed else "CloseCrossName"
|
||||||
|
|
||||||
|
for Candidate in Matches[StartIndex:]:
|
||||||
|
if Candidate.group(GroupName) == Key:
|
||||||
|
return Candidate
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _ResolveLoops(
|
||||||
|
Content: str,
|
||||||
|
Lists: dict[str, list[str]],
|
||||||
|
RootName: str,
|
||||||
|
RefererDirectory: str,
|
||||||
|
Username: str,
|
||||||
|
UserGroups: list[str],
|
||||||
|
) -> str:
|
||||||
Matches = list(LoopMarkerPattern.finditer(Content))
|
Matches = list(LoopMarkerPattern.finditer(Content))
|
||||||
Output: list[str] = []
|
Output: list[str] = []
|
||||||
Cursor = 0
|
Cursor = 0
|
||||||
@@ -155,23 +175,28 @@ def _ResolveLoops(Content: str, Lists: dict[str, list[str]]) -> str:
|
|||||||
|
|
||||||
while Index < len(Matches):
|
while Index < len(Matches):
|
||||||
Match = Matches[Index]
|
Match = Matches[Index]
|
||||||
|
IsFixed = bool(Match.group("LoopSource"))
|
||||||
|
IsCross = bool(Match.group("LoopCrossName"))
|
||||||
|
|
||||||
if not Match.group("LoopSource"):
|
if not IsFixed and not IsCross:
|
||||||
Index += 1
|
Index += 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
Source = Match.group("LoopSource")
|
if IsFixed:
|
||||||
LoopVar = Match.group("LoopVar")
|
Key = Match.group("LoopSource")
|
||||||
|
LoopVar = Match.group("LoopVar")
|
||||||
CloseMatch = None
|
Items = Lists.get(Key, [])
|
||||||
for Candidate in Matches[Index + 1 :]:
|
else:
|
||||||
if Candidate.group("CloseSource") == Source:
|
Key = Match.group("LoopCrossName")
|
||||||
CloseMatch = Candidate
|
LoopVar = Match.group("LoopCrossVar")
|
||||||
break
|
PortalContent = _ResolveCrossFileReference(
|
||||||
|
RootName, RefererDirectory, Match.group("LoopCrossPath"), Key, Username, UserGroups
|
||||||
|
)
|
||||||
|
Items = PortalContent.splitlines() if PortalContent is not None else []
|
||||||
|
|
||||||
|
CloseMatch = _FindLoopClose(Matches, Index + 1, IsFixed, Key)
|
||||||
BodyEnd = CloseMatch.start() if CloseMatch is not None else len(Content)
|
BodyEnd = CloseMatch.start() if CloseMatch is not None else len(Content)
|
||||||
BodyTemplate = Content[Match.end() : BodyEnd]
|
BodyTemplate = Content[Match.end() : BodyEnd]
|
||||||
Items = Lists.get(Source, [])
|
|
||||||
VariablePattern = re.compile(rf"%{re.escape(LoopVar)}\b")
|
VariablePattern = re.compile(rf"%{re.escape(LoopVar)}\b")
|
||||||
|
|
||||||
Rendered = "\n".join(VariablePattern.sub(Item, BodyTemplate) for Item in Items)
|
Rendered = "\n".join(VariablePattern.sub(Item, BodyTemplate) for Item in Items)
|
||||||
@@ -319,10 +344,12 @@ def _ResolveLineNumberReference(
|
|||||||
def ResolvePortals(
|
def ResolvePortals(
|
||||||
RootName: str, RelativePath: str, Content: str, Username: str, UserGroups: list[str]
|
RootName: str, RelativePath: str, Content: str, Username: str, UserGroups: list[str]
|
||||||
) -> str:
|
) -> str:
|
||||||
LoopLists = _ComputeLoopLists(RootName, RelativePath, Username, UserGroups)
|
|
||||||
WithLoopsResolved = _ResolveLoops(Content, LoopLists)
|
|
||||||
LocallyResolved = _ResolveLocalPortals(WithLoopsResolved)
|
|
||||||
RefererDirectory = RelativePath.rsplit("/", 1)[0] if "/" in RelativePath else ""
|
RefererDirectory = RelativePath.rsplit("/", 1)[0] if "/" in RelativePath else ""
|
||||||
|
LoopLists = _ComputeLoopLists(RootName, RelativePath, Username, UserGroups)
|
||||||
|
WithLoopsResolved = _ResolveLoops(
|
||||||
|
Content, LoopLists, RootName, RefererDirectory, Username, UserGroups
|
||||||
|
)
|
||||||
|
LocallyResolved = _ResolveLocalPortals(WithLoopsResolved)
|
||||||
|
|
||||||
Matches = list(MarkerPattern.finditer(LocallyResolved))
|
Matches = list(MarkerPattern.finditer(LocallyResolved))
|
||||||
Output: list[str] = []
|
Output: list[str] = []
|
||||||
|
|||||||
Reference in new issue
Block a user