Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
authored April 05, 2009
11  src/Compiler.py
@@ -973,9 +973,16 @@ def setErrorCatcher(self, errorCatcherName):
973 973
 
974 974
     def nextFilterRegionID(self):
975 975
         return self.nextCacheID()
  976
+
  977
+    def setTransform(self, transformer, isKlass):
  978
+        self.addChunk('trans = TransformerTransaction()')
  979
+        self.addChunk('trans._response = trans.response()')
  980
+        self.addChunk('trans._response._filter = %s' % transformer)
  981
+        self.addChunk('write = trans._response.write')
976 982
         
977 983
     def setFilter(self, theFilter, isKlass):
978  
-        class FilterDetails: pass
  984
+        class FilterDetails: 
  985
+            pass
979 986
         filterDetails = FilterDetails()
980 987
         filterDetails.ID = ID = self.nextFilterRegionID()
981 988
         filterDetails.theFilter = theFilter
@@ -1638,7 +1645,7 @@ def _setupCompilerState(self):
1638 1645
             "from Cheetah.Version import MinCompatibleVersion as RequiredCheetahVersion",            
1639 1646
             "from Cheetah.Version import MinCompatibleVersionTuple as RequiredCheetahVersionTuple",
1640 1647
             "from Cheetah.Template import Template",
1641  
-            "from Cheetah.DummyTransaction import DummyTransaction",
  1648
+            "from Cheetah.DummyTransaction import *",
1642 1649
             "from Cheetah.NameMapper import NotFound, valueForName, valueFromSearchList, valueFromFrameOrSearchList",
1643 1650
             "from Cheetah.CacheRegion import CacheRegion",
1644 1651
             "import Cheetah.Filters as Filters",
40  src/DummyTransaction.py
@@ -15,6 +15,8 @@
15 15
 __author__ = "Tavis Rudd <tavis@damnsimple.com>"
16 16
 __revision__ = "$Revision: 1.13 $"[11:-2]
17 17
 
  18
+import types
  19
+
18 20
 def flush():
19 21
     pass
20 22
 
@@ -56,3 +58,41 @@ def __init__(self, DummyResponse=DummyResponse):
56 58
         def response(resp=DummyResponse()):
57 59
             return resp
58 60
         self.response = response
  61
+
  62
+class TransformerResponse(object):
  63
+    def __init__(self, *args, **kwargs):
  64
+        self._output = []
  65
+        self._filter = None
  66
+
  67
+    def write(self, value):
  68
+        self._output.append(value)
  69
+
  70
+    def flush(self):
  71
+        pass
  72
+
  73
+    def writeln(self, line):
  74
+        self.write(line)
  75
+        self.write('\n')
  76
+
  77
+    def writelines(self, *lines):
  78
+        [self.writeln(line) for line in lines]
  79
+
  80
+    def getvalue(self, **kwargs):
  81
+        output = kwargs.get('outputChunks') or self._output
  82
+        rc = ''.join(output)
  83
+        if self._filter:
  84
+            _filter = self._filter
  85
+            if isinstance(_filter, types.TypeType):
  86
+                _filter = _filter()
  87
+            return _filter.filter(rc)
  88
+        return rc
  89
+
  90
+
  91
+class TransformerTransaction(object):
  92
+    def __init__(self, *args, **kwargs):
  93
+        self._response = None
  94
+    def response(self):
  95
+        if self._response:
  96
+            return self._response
  97
+        return TransformerResponse()
  98
+
2  src/Filters.py
@@ -97,7 +97,7 @@ class Markdown(EncodeUnicode):
97 97
     '''
98 98
     def filter(self,  value, **kwargs):
99 99
         encoded = super(Markdown, self).filter(value, **kwargs)
100  
-        return markdown.markdown(rc)
  100
+        return markdown.markdown(encoded)
101 101
 
102 102
 
103 103
 class MaxLen(Filter):
25  src/Parser.py
@@ -172,6 +172,7 @@ def makeTripleQuoteRe(start, end):
172 172
     'filter': 'eatFilter',
173 173
     'echo': None,
174 174
     'silent': None,
  175
+    'transform' : 'eatTransform',
175 176
     
176 177
     'call': 'eatCall',
177 178
     'arg': 'eatCallArg',
@@ -2473,6 +2474,30 @@ def eatFilter(self):
2473 2474
             self.pushToOpenDirectivesStack("filter")
2474 2475
             self._eatRestOfDirectiveTag(isLineClearToStartToken, endOfFirstLinePos)
2475 2476
             self._compiler.setFilter(theFilter, isKlass)        
  2477
+
  2478
+    def eatTransform(self):
  2479
+        isLineClearToStartToken = self.isLineClearToStartToken()
  2480
+        endOfFirstLinePos = self.findEOL()
  2481
+
  2482
+        self.getDirectiveStartToken()
  2483
+        self.advance(len('transform'))
  2484
+        self.getWhiteSpace()
  2485
+        startPos = self.pos()
  2486
+        if self.matchCheetahVarStart():
  2487
+            isKlass = True
  2488
+            transformer = self.getExpression(pyTokensToBreakAt=[':'])
  2489
+        else:
  2490
+            isKlass = False
  2491
+            transformer = self.getIdentifier()
  2492
+            self.getWhiteSpace()
  2493
+        transformer = self._applyExpressionFilters(transformer, 'transform', startPos=startPos)
  2494
+
  2495
+        if self.peek()==':':
  2496
+            self.advance()
  2497
+        self.getWhiteSpace()
  2498
+        self._eatRestOfDirectiveTag(isLineClearToStartToken, endOfFirstLinePos)
  2499
+        self._compiler.setTransform(transformer, isKlass)
  2500
+
2476 2501
         
2477 2502
     def eatErrorCatcher(self):
2478 2503
         isLineClearToStartToken = self.isLineClearToStartToken()
9  src/Tests/Filters.py
@@ -11,15 +11,18 @@ class BasicMarkdownFilterTest(unittest.TestCase):
11 11
     '''
12 12
     def test_BasicHeader(self):
13 13
         template = '''  
14  
-#import Cheetah.Filters
15  
-#transform Cheetah.Filters.Markdown
  14
+#from Cheetah.Filters import Markdown
  15
+#transform Markdown
16 16
 $foo
17 17
 
18 18
 Header
19 19
 ======
20 20
         '''
  21
+        expected = '''<p>bar</p>
  22
+<h1>Header</h1>'''
21 23
         template = Cheetah.Template.Template(template, searchList=[{'foo' : 'bar'}])
22  
-        print template
  24
+        template = str(template)
  25
+        assert template == expected
23 26
 
24 27
 if __name__ == '__main__':
25 28
     unittest.main()

0 notes on commit c37b997

Please sign in to comment.
Something went wrong with that request. Please try again.