Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finish up support for the #transform directive, and the Markdown filter

Now you can set:
	#from Cheetah.Filters import Markdown
	#transform Markdown

	This is my header!
	==================

And your output will be:
	<h1>This is my header!</h1>

Signed-off-by: R. Tyler Ballance <tyler@slide.com>
  • Loading branch information...
commit c37b9972171567585aaedcd9556bc6bd2fa7fbcd 1 parent fc6addb
R. Tyler Ballance authored
View
11 src/Compiler.py
@@ -973,9 +973,16 @@ def setErrorCatcher(self, errorCatcherName):
def nextFilterRegionID(self):
return self.nextCacheID()
+
+ def setTransform(self, transformer, isKlass):
+ self.addChunk('trans = TransformerTransaction()')
+ self.addChunk('trans._response = trans.response()')
+ self.addChunk('trans._response._filter = %s' % transformer)
+ self.addChunk('write = trans._response.write')
def setFilter(self, theFilter, isKlass):
- class FilterDetails: pass
+ class FilterDetails:
+ pass
filterDetails = FilterDetails()
filterDetails.ID = ID = self.nextFilterRegionID()
filterDetails.theFilter = theFilter
@@ -1638,7 +1645,7 @@ def _setupCompilerState(self):
"from Cheetah.Version import MinCompatibleVersion as RequiredCheetahVersion",
"from Cheetah.Version import MinCompatibleVersionTuple as RequiredCheetahVersionTuple",
"from Cheetah.Template import Template",
- "from Cheetah.DummyTransaction import DummyTransaction",
+ "from Cheetah.DummyTransaction import *",
"from Cheetah.NameMapper import NotFound, valueForName, valueFromSearchList, valueFromFrameOrSearchList",
"from Cheetah.CacheRegion import CacheRegion",
"import Cheetah.Filters as Filters",
View
40 src/DummyTransaction.py
@@ -15,6 +15,8 @@
__author__ = "Tavis Rudd <tavis@damnsimple.com>"
__revision__ = "$Revision: 1.13 $"[11:-2]
+import types
+
def flush():
pass
@@ -56,3 +58,41 @@ def __init__(self, DummyResponse=DummyResponse):
def response(resp=DummyResponse()):
return resp
self.response = response
+
+class TransformerResponse(object):
+ def __init__(self, *args, **kwargs):
+ self._output = []
+ self._filter = None
+
+ def write(self, value):
+ self._output.append(value)
+
+ def flush(self):
+ pass
+
+ def writeln(self, line):
+ self.write(line)
+ self.write('\n')
+
+ def writelines(self, *lines):
+ [self.writeln(line) for line in lines]
+
+ def getvalue(self, **kwargs):
+ output = kwargs.get('outputChunks') or self._output
+ rc = ''.join(output)
+ if self._filter:
+ _filter = self._filter
+ if isinstance(_filter, types.TypeType):
+ _filter = _filter()
+ return _filter.filter(rc)
+ return rc
+
+
+class TransformerTransaction(object):
+ def __init__(self, *args, **kwargs):
+ self._response = None
+ def response(self):
+ if self._response:
+ return self._response
+ return TransformerResponse()
+
View
2  src/Filters.py
@@ -97,7 +97,7 @@ class Markdown(EncodeUnicode):
'''
def filter(self, value, **kwargs):
encoded = super(Markdown, self).filter(value, **kwargs)
- return markdown.markdown(rc)
+ return markdown.markdown(encoded)
class MaxLen(Filter):
View
25 src/Parser.py
@@ -172,6 +172,7 @@ def makeTripleQuoteRe(start, end):
'filter': 'eatFilter',
'echo': None,
'silent': None,
+ 'transform' : 'eatTransform',
'call': 'eatCall',
'arg': 'eatCallArg',
@@ -2473,6 +2474,30 @@ def eatFilter(self):
self.pushToOpenDirectivesStack("filter")
self._eatRestOfDirectiveTag(isLineClearToStartToken, endOfFirstLinePos)
self._compiler.setFilter(theFilter, isKlass)
+
+ def eatTransform(self):
+ isLineClearToStartToken = self.isLineClearToStartToken()
+ endOfFirstLinePos = self.findEOL()
+
+ self.getDirectiveStartToken()
+ self.advance(len('transform'))
+ self.getWhiteSpace()
+ startPos = self.pos()
+ if self.matchCheetahVarStart():
+ isKlass = True
+ transformer = self.getExpression(pyTokensToBreakAt=[':'])
+ else:
+ isKlass = False
+ transformer = self.getIdentifier()
+ self.getWhiteSpace()
+ transformer = self._applyExpressionFilters(transformer, 'transform', startPos=startPos)
+
+ if self.peek()==':':
+ self.advance()
+ self.getWhiteSpace()
+ self._eatRestOfDirectiveTag(isLineClearToStartToken, endOfFirstLinePos)
+ self._compiler.setTransform(transformer, isKlass)
+
def eatErrorCatcher(self):
isLineClearToStartToken = self.isLineClearToStartToken()
View
9 src/Tests/Filters.py
@@ -11,15 +11,18 @@ class BasicMarkdownFilterTest(unittest.TestCase):
'''
def test_BasicHeader(self):
template = '''
-#import Cheetah.Filters
-#transform Cheetah.Filters.Markdown
+#from Cheetah.Filters import Markdown
+#transform Markdown
$foo
Header
======
'''
+ expected = '''<p>bar</p>
+<h1>Header</h1>'''
template = Cheetah.Template.Template(template, searchList=[{'foo' : 'bar'}])
- print template
+ template = str(template)
+ assert template == expected
if __name__ == '__main__':
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.