zim-wiki team mailing list archive
-
zim-wiki team
-
Mailing list archive
-
Message #01052
Re: A plugin to reuse text snippets
On Mon, Jan 3, 2011 at 6:01 PM, Pascollin <pascollin@xxxxxxxxx> wrote:
> I would like a plugin to reuse text snippets from some other pages.
>
> Example : I create a page "my text templates" and set this as snippets
> root in the plugin preferences.
>
> I tried to create such a plugin, but I didn't find how to add zim
> formatted text to the current insert point (textview/textbuffer).
>
> Could you give me some advice ?
To insert formatted text you need to parse the text first in order to
get a "parse tree", then you can use the "insert_parsetree_at_cursor"
method. You can get a parsetree for a page with "get_parsetree" or use
the parser for the specific format (default wiki format most likely).
To parse a piece of text and insert it in the current page the plugin
would need to do the following ("self" being the plugin object and
"text" the snippet):
>>>>>
import zim.formats
format = zim.formats.get_format('wiki')
parser = format.Parser()
parsetree = parser.parse(text)
buffer = self.ui.mainwindow.pageview.view.get_buffer()
buffer.insert_parsetree_at_cursor(parsetree)
<<<<<
Hope this helps,
Jaap
Follow ups
References