Commits

Daniel Kluev committed ba4eed6

Allow to use direct printing to stdout for testing

Comments (0)

Files changed (2)

 template_name = os.path.split(template_path)[-1]
 template_text = open(template_path, 'r').read()
 
-compiler = MakoToJSCompiler()        
+compiler = MakoToJSCompiler(stdout=True)
 code = compiler.compile(template_text, template_name)
-print code
 

mako2js/__init__.py

 __docformat__ = 'restructuredtext'
 __version__ = 'head'
 
+import sys
+
 from mako import util, ast, parsetree, filters
 from mako.codegen import _CompileContext
 from mako.lexer import Lexer
 from .jsprinter import JSPrinter
 
 class MakoToJSCompiler(object):
-    def __init__(self):
-        pass
+    def __init__(self, stdout=False):
+        self.stdout = stdout
     
     def compile(self, text, relpath=None, abspath=None):
         lexer = Lexer(text, abspath, disable_unicode=False, input_encoding="utf-8")
         node = lexer.parse()
-        buf = util.FastEncodingBuffer()
+        if self.stdout:
+            buf = sys.stdout
+        else:
+            buf = util.FastEncodingBuffer()
         printer = JSPrinter(buf)
         _GenerateRenderMethodJS(printer,
               _CompileContext(relpath, abspath, [], [], None, "utf-8", None, False, False), node)
-        
-        return buf.getvalue()
+              
+        if not self.stdout:
+            return buf.getvalue()