Skip to content

Commit

Permalink
move handling of escaped variables and directives to the parser
Browse files Browse the repository at this point in the history
Once text reaches the compiler, it is too late to parse escaped
variables and directives.  The compiler does not know where the text
came from, and can not determine when it should skip processing of
escape sequences for raw blocks.

Signed-off-by: R. Tyler Ballance <tyler@monkeypox.org>
  • Loading branch information
karmix authored and R. Tyler Ballance committed Dec 15, 2009
1 parent 5cd0220 commit 0cc9f78
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
17 changes: 1 addition & 16 deletions cheetah/Compiler.py
Expand Up @@ -428,27 +428,12 @@ def _appendToPrevStrConst(self, strConst):
else:
self._pendingStrConstChunks = [strConst]

def _unescapeCheetahVars(self, theString):
"""Unescape any escaped Cheetah \$vars in the string.
"""

token = self.setting('cheetahVarStartToken')
return theString.replace('\\' + token, token)

def _unescapeDirectives(self, theString):
"""Unescape any escaped Cheetah \$vars in the string.
"""

token = self.setting('directiveStartToken')
return theString.replace('\\' + token, token)

def commitStrConst(self):
"""Add the code for outputting the pending strConst without chopping off
any whitespace from it.
"""
if self._pendingStrConstChunks:
strConst = self._unescapeCheetahVars(''.join(self._pendingStrConstChunks))
strConst = self._unescapeDirectives(strConst)
strConst = ''.join(self._pendingStrConstChunks)
self._pendingStrConstChunks = []
if not strConst:
return
Expand Down
15 changes: 15 additions & 0 deletions cheetah/Parser.py
Expand Up @@ -510,6 +510,19 @@ def _makePspREs(self):
endTokenEsc = escapeRegexChars(endToken)
self.PSPEndTokenRE = cachedRegex(escCharLookBehind + endTokenEsc)

def _unescapeCheetahVars(self, theString):
"""Unescape any escaped Cheetah \$vars in the string.
"""

token = self.setting('cheetahVarStartToken')
return theString.replace('\\' + token, token)

def _unescapeDirectives(self, theString):
"""Unescape any escaped Cheetah directives in the string.
"""

token = self.setting('directiveStartToken')
return theString.replace('\\' + token, token)

def isLineClearToStartToken(self, pos=None):
return self.isLineClearToPos(pos)
Expand Down Expand Up @@ -1497,6 +1510,8 @@ def eatPlainText(self):
else:
self.advance()
strConst = self.readTo(self.pos(), start=startPos)
strConst = self._unescapeCheetahVars(strConst)
strConst = self._unescapeDirectives(strConst)
self._compiler.addStrConst(strConst)
return match

Expand Down

0 comments on commit 0cc9f78

Please sign in to comment.