2012-05-06 9 views
0

私は電子メールクライアントを書いています。kmailテンプレートから%CURSORのようなものを実装したいと思います。生成された電子メールメッセージが表示された後にカーソルを置く位置をマークしてください。どのようなテンプレートエンジンがこのタイプのことをすることができますか?ブロック位置を記録できるテンプレートエンジン

+0

これはPythonとどのように関連していますか? – Amber

+0

@Amber私はPythonのテンプレートエンジンが必要です。私はkmailに直接関係していません。私はメールクライアントを書いていますが、kmailへのプラグインではありません。 'kmail'はあくまで例です。 – ZyX

+0

テンプレートエンジンのカーソル位置を処理したくないと思います。代わりに、私はちょうどあなたが後で検出し、レンダリングされたテンプレートからカーソルを置くために削除するトークンを持っていると言うでしょう。 – Amber

答えて

0
from mako.runtime import Context 
class CursorContext(Context): 
    __slots__=set(('lines', 'position')) 

    def __init__(self, *args, **kwargs): 
     super(CursorContext, self.__class__).__init__(self, self, *args, **kwargs) 
     self.lines=[''] 
     self.position=None 

    def write(self, v): 
     ls=s(v).split('\n') 
     self.lines[-1]+=ls.pop(0) 
     self.lines+=ls 

    def _record_position(self): 
     if self.position is not None: 
      raise ValueError('Position already defined') 
     self.position=(len(self.lines), len(self.lines[-1]) if self.lines else 0) 

    def _get_position(self): 
     if self.position is None: 
      self._record_position() 
     return self.position 

<...> 

context=CursorContext(**env) # env is a dictionary with template environment variables 
template.render_context(context) # template is mako.template.Template object 
# Now template is in context.lines and 
# cursor position can be obtained by context._get_position() 
関連する問題