zim-wiki team mailing list archive
-
zim-wiki team
-
Mailing list archive
-
Message #01057
A plugin to reuse text snippets (first release)
-
To:
Zim <zim-wiki@xxxxxxxxxxxxxxxxxxx>
-
From:
Pascal <pascollin@xxxxxxxxx>
-
Date:
Mon, 10 Jan 2011 23:29:26 +0100
-
In-reply-to:
<AANLkTi=FHqz8wE=Lro8PT7rdfUGLfAQeTfEdKKwWq_5_@mail.gmail.com>
-
User-agent:
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101208 Thunderbird/3.1.7
Hi,
I don't know if this is the correct place to announce and post a new
plugin... but here is mine.
The "Insert snippet" plugin adds a new menu entry "Insert/Text from
page..." which shows a dialog box where you can enter some page path.
Then, its content is added to the current edited page.
Current options are :
- remove title (from inserted text)
- increase header level (h1 goes h2, etc)
- insert backlink to source (above and/or under the inserted text)
- show full namespace (by default, only the basename will be shown)
- add some prefix before backlink (something like "**source : **")
It has been tested on zim 0.49 (Ubuntu)
I hope this can be useful.
PS :
- I can provide french gettext messages for translation
- you're welcome to fix incorrect english words in the plugin...
Best regards,
Pascal
Le 03/01/2011 18:44, Jaap Karssenberg a écrit :
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
# -*- coding: utf-8 -*-
# Copyright 2010 Pascal Collin <pascollin@xxxxxxxxx>
import gtk
import logging
from zim.plugins import PluginClass
from zim.gui.widgets import Dialog, ErrorDialog, Button, PageEntry
from zim.config import config_file
from zim.notebook import Path
from zim.formats.wiki import Parser
logger = logging.getLogger('zim.plugins.insertsnippet')
ui_xml = '''
<ui>
<menubar name='menubar'>
<menu action='insert_menu'>
<placeholder name='plugin_items'>
<menuitem action='insert_snippet'/>
</placeholder>
</menu>
</menubar>
</ui>
'''
ui_actions = (
# name, stock id, label, accelerator, tooltip, readonly
('insert_snippet', None, _('Text From _Page...'), None, '', False), # T: menu item
)
class SnippetPlugin(PluginClass):
plugin_info = {
'name': _('Insert Snippet'), # T: plugin name
'description': _('''This plugin adds the 'Insert text from page' dialog and allows reusing text from some other page.'''), # T: plugin description
'author': 'Pascal Collin',
'help': 'Plugins:Insert Snippet',
}
plugin_preferences = (
('remove_title', 'bool', _('Remove title'), False),
('increase header', 'bool', _('increase header levels'), False),
('prelink_source', 'bool', _('insert source link before text'), False),
('postlink_source', 'bool', _('append source link after text'), False),
('show_namespace','bool',_('links show namespace'),False),
('link_prefix','string',_('link prefix'), '** source ** :'),
)
def initialize_ui(self, ui):
if self.ui.ui_type == 'gtk':
self.ui.add_actions(ui_actions, self)
self.ui.add_ui(ui_xml, self)
def finalize_ui(self, ui):
if self.ui.ui_type == 'gtk':
self.pageview = self.ui.mainwindow.pageview
def disconnect(self):
if self.ui.ui_type == 'gtk':
self.pageview.view.disconnect(self._signal_id)
PluginClass.disconnect(self)
def insert_snippet(self):
InsertSnippetDialog(self.ui, self.preferences).run()
class InsertSnippetDialog(Dialog):
def __init__(self, ui, preferences):
Dialog.__init__(self, ui, _('Insert Text From Page'))
self.buffer = self.ui.mainwindow.pageview.view.get_buffer()
hbox = gtk.HBox(spacing = 4)
hbox.pack_start(gtk.Label(str = '%s :' % (_('Page'),)), False)
self.entry = PageEntry(ui.notebook, path_context = ui.get_path_context())
self.entry.set_activates_default(True)
hbox.pack_start(self.entry, True)
self.vbox.pack_start(hbox, False)
self.preferences = preferences
def do_response_ok(self):
def insert_link(breakline=0):
self.buffer.insert_at_cursor('\r\n') if breakline == -1 else None
self.buffer.insert_parsetree_at_cursor(Parser().parse(self.preferences['link_prefix'])) if self.preferences['link_prefix'] else None
text = page.basename if not self.preferences['show_namespace'] else '%s:%s' % (page.namespace, page.basename)
self.buffer.insert_link_at_cursor(text, href= path.name)
self.buffer.insert_at_cursor('\r\n') if breakline == 1 else None
path = self.entry.get_path()
page = self.ui.notebook.get_page(path)
if page.exists() and page.hascontent :
tree = page.get_parsetree()
root = tree.getroot()
#removing title if required
children = root.getchildren()
root.remove(children[0]) if self.preferences['remove_title'] and len(children) and children[0].tag == 'h' else None
#applying header offset if required
tree.cleanup_headings(offset = 1) if self.preferences['increase header'] else None
#inserting pre-link if required
insert_link(1) if self.preferences['prelink_source'] else None
#inserting source parse_tree
self.buffer.insert_parsetree_at_cursor(tree)
#inserting post-link if required
insert_link(-1) if self.preferences['postlink_source'] else None
#we must restore the modified page parsetree
self.ui.notebook.flush_page_cache(path)
return True
else :
ErrorDialog(self, _('Page not found')).run()
return False
References