From 22832fa5b7c8bf65f2f59194cd1dfc18bea89393 Mon Sep 17 00:00:00 2001 From: JK Date: Sat, 17 Feb 2018 07:26:13 +0100 Subject: [PATCH] Added template include command --- microWebSrv.py | 2 +- microWebTemplate.py | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/microWebSrv.py b/microWebSrv.py index 21b80cf..b7db897 100755 --- a/microWebSrv.py +++ b/microWebSrv.py @@ -549,7 +549,7 @@ def WriteResponsePyHTMLFile(self, filepath, headers=None) : if 'MicroWebTemplate' in globals() : with open(filepath, 'r') as file : code = file.read() - mWebTmpl = MicroWebTemplate(code, escapeStrFunc=MicroWebSrv.HTMLEscape) + mWebTmpl = MicroWebTemplate(code, escapeStrFunc=MicroWebSrv.HTMLEscape, filepath=filepath) try : return self.WriteResponseOk(headers, "text/html", "UTF-8", mWebTmpl.Execute()) except Exception as ex : diff --git a/microWebTemplate.py b/microWebTemplate.py index 24eab2c..8ace5ca 100755 --- a/microWebTemplate.py +++ b/microWebTemplate.py @@ -22,14 +22,16 @@ class MicroWebTemplate : INSTRUCTION_ELSE = 'else' INSTRUCTION_FOR = 'for' INSTRUCTION_END = 'end' + INSTRUCTION_INCLUDE = 'include' # ============================================================================ # ===( Constructor )========================================================== # ============================================================================ - def __init__(self, code, escapeStrFunc=None) : + def __init__(self, code, escapeStrFunc=None, filepath='') : self._code = code self._escapeStrFunc = escapeStrFunc + self._filepath = filepath self._pos = 0 self._endPos = len(code)-1 self._line = 1 @@ -44,6 +46,7 @@ def __init__(self, code, escapeStrFunc=None) : MicroWebTemplate.INSTRUCTION_ELSE : self._processInstructionELSE, MicroWebTemplate.INSTRUCTION_FOR : self._processInstructionFOR, MicroWebTemplate.INSTRUCTION_END : self._processInstructionEND, + MicroWebTemplate.INSTRUCTION_INCLUDE: self._processInstructionINCLUDE, } # ============================================================================ @@ -85,7 +88,7 @@ def _parseBloc(self, execute) : while self._pos <= self._endPos : c = self._code[self._pos] if c == MicroWebTemplate.TOKEN_OPEN[0] and \ - self._code[ self._pos : self._pos + MicroWebTemplate.TOKEN_OPEN_LEN ] == MicroWebTemplate.TOKEN_OPEN : + self._code[ self._pos : self._pos + MicroWebTemplate.TOKEN_OPEN_LEN ] == MicroWebTemplate.TOKEN_OPEN : self._pos += MicroWebTemplate.TOKEN_OPEN_LEN tokenContent = '' x = self._pos @@ -298,6 +301,21 @@ def _processInstructionEND(self, instructionBody, execute) : % (MicroWebTemplate.INSTRUCTION_END, self._line) ) return MicroWebTemplate.INSTRUCTION_END + # ---------------------------------------------------------------------------- + + def _processInstructionINCLUDE(self, instructionBody, execute) : + if not instructionBody : + raise Exception( '"%s" alone is an incomplete syntax (line %s)' % (MicroWebTemplate.INSTRUCTION_INCLUDE, self._line) ) + filename = instructionBody.replace('"','').replace("'",'').strip() + idx = self._filepath.rindex('/') + if idx >= 0 : + filename = self._filepath[:idx+1] + filename + with open(filename, 'r') as file : + includeCode = file.read() + + self._code = self._code[:self._pos] + includeCode + self._code[self._pos:] + self._endPos += len(includeCode) + # ============================================================================ # ============================================================================ # ============================================================================