def Orca_includesub(oElem: Union[Element, List[Element]], pLoader: Callable, uOrgDefinitionContext: str, oDef: cDefinition, uFileName: str = "Unknown Filename") -> Element: """ sub function for the include loader """ global bBlockInclude aElemens: List[Element] = [] oReplacementVars: cDefinitionVars = cDefinitionVars() oSaveReplacementVars: cDefinitionVars = cDefinitionVars() for e in oElem: aElemens.append(e) if oDef: oReplacementVars = oDef.oDefinitionVars i: int = 0 for e in aElemens: if oDef: e.set('definitionalias', oDef.uAlias) if e.tag == 'startskip': if CheckCondition(oPar=e): bBlockInclude = True if e.tag == 'stopskip': bBlockInclude = False if e.tag == ElementInclude.XINCLUDE_INCLUDE: if CheckCondition(oPar=e) and not bBlockInclude: # process xinclude directive uHref: str = e.get("href") uHref = ReplaceVars(uHref) uParse: str = e.get("parse", None) if uParse is None: uParse = "xml" if uParse == "xml": uNewDefinitionContext = u'' if "DEFINITIONPATH[" in uHref: uNewDefinitionContext, uHref = GetSetDefinitionName( uText=uHref) oTmpReplacementVars = e.get("definitionvars") if oTmpReplacementVars is None: oTmpReplacementVars = oReplacementVars aIncludeReplacementVars = e.get("includedefinitionvars") if aIncludeReplacementVars is None: aIncludeReplacementVars = cDefinitionVars() else: aIncludeReplacementVars = ToDic( aIncludeReplacementVars) aIncludeReplacementVars = aIncludeReplacementVars.copy() if oTmpReplacementVars is not None: oTmpReplacementVars = oTmpReplacementVars.copy() oSaveReplacementVars = oReplacementVars oReplacementVars = oTmpReplacementVars oReplacementVars.update(aIncludeReplacementVars) if oDef: oDef.oDefinitionVars = oTmpReplacementVars aNodes: List[Element] = pLoader(uHref, uParse, None, oReplacementVars) oNodes: Element # included element not found if len(aNodes) == 0: del oElem[i] i -= 1 else: uHref2 = uHref if uNewDefinitionContext == u'': oNodes = Orca_includesub(aNodes, pLoader, uOrgDefinitionContext, oDef, uHref2) else: oNodes = Orca_includesub(aNodes, pLoader, uNewDefinitionContext, oDef, uHref2) if oTmpReplacementVars is not None: oReplacementVars = oSaveReplacementVars if oDef: oDef.oDefinitionVars = oSaveReplacementVars if uNewDefinitionContext != u'': SetDefinitionContext( uDefinitionName=uOrgDefinitionContext) # we got a valid list of nodes bFirst: bool = True for oNode in oNodes: oNode = copy(oNode) if e.tail: oNode.tail = (oNode.tail or "") + e.tail if bFirst: oElem[i] = oNode bFirst = False else: oElem.insert(i, oNode) i += 1 else: Orca_includesub(e, pLoader, uOrgDefinitionContext, oDef, uFileName) # pass i += 1 return oElem
def Orca_includesub(elem, loader, uOrgDefinitionContext, oDef, uFileName="Unknown Filename"): """ sub function for the include loader """ global bBlockInclude elemens = [] oReplacementVars = cDefinitionVars() for e in elem: elemens.append(e) if oDef: oReplacementVars = oDef.oDefinitionVars i = 0 for e in elemens: e.set('definitioncontext', uOrgDefinitionContext) if oDef: e.set('definitionalias', oDef.uAlias) e.set('linefilename', uFileName) if e.tag == 'startskip': if CheckCondition(e): bBlockInclude = True if e.tag == 'stopskip': bBlockInclude = False if e.tag == ElementInclude.XINCLUDE_INCLUDE: if CheckCondition(e) and not bBlockInclude: # process xinclude directive href = e.get("href") parse = e.get("parse", "xml") if parse == "xml": uNewDefinitionContext = u'' if "DEFINITIONPATH[" in href: uNewDefinitionContext, href = GetSetDefinitionName( href) oTmpReplacementVars = e.get("definitionvars") if oTmpReplacementVars is None: oTmpReplacementVars = oReplacementVars aIncludeReplacementVars = e.get("includedefinitionvars") if aIncludeReplacementVars is None: aIncludeReplacementVars = cDefinitionVars() else: aIncludeReplacementVars = ToDic( aIncludeReplacementVars) # aIncludeReplacementVars = aIncludeReplacementVars.copy() if oTmpReplacementVars is not None: oTmpReplacementVars = oTmpReplacementVars.copy() #oTmpReplacementVars=ToDic(oTmpReplacementVars) aSaveReplacementVars = oReplacementVars oReplacementVars = oTmpReplacementVars oReplacementVars.update(aIncludeReplacementVars) if oDef: oDef.oDefinitionVars = oTmpReplacementVars oFnHRefRedirect = Globals.oTheScreen.oSkin.dSkinRedirects.get( cFileName(u'').ImportFullPath(ReplaceVars(href))) if oFnHRefRedirect is not None: oFnHRef = oFnHRefRedirect.string oFnHRef = cFileName(u'').ImportFullPath(ReplaceVars(href)) nodes = loader(oFnHRef.string, parse, None, oReplacementVars) if nodes is not None: if uNewDefinitionContext == u'': nodes = Orca_includesub(nodes, loader, uOrgDefinitionContext, oDef, oFnHRef.string) else: nodes = Orca_includesub(nodes, loader, uNewDefinitionContext, oDef, oFnHRef.string) if oTmpReplacementVars is not None: oReplacementVars = aSaveReplacementVars if oDef: oDef.oDefinitionVars = aSaveReplacementVars if uNewDefinitionContext != u'': SetDefinitionContext(uOrgDefinitionContext) if isinstance(nodes, list): bFirst = True for node in nodes: node = copy(node) if e.tail: node.tail = (node.tail or "") + e.tail if bFirst: elem[i] = node bFirst = False else: elem.insert(i, node) i = i + 1 elif nodes is None: del elem[i] i = i - 1 else: nodes = copy(nodes) if e.tail: nodes.tail = (nodes.tail or "") + e.tail elem[i] = nodes else: Orca_includesub(e, loader, uOrgDefinitionContext, oDef, uFileName) i = i + 1 return elem