-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautolink.py
66 lines (55 loc) · 2.58 KB
/
autolink.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from zim.plugins import PluginClass
import logging
from zim.gui.pageview import PageViewExtension
from zim.actions import action, toggle_action
from zim.notebook import Path, Notebook, Page, NotebookExtension
logger = logging.getLogger('zim.plugins')
class AutoLinkPlugin(PluginClass):
plugin_info = {
'name': _('Auto Linker'),
'description': _('''\
Automatically adds links to specified pages while you type. (case-sensitve)
Modify the script to include what you want to link to, contact me if you want help with that.
Note: The names of the pages you want to automatically link to cannot have any form of punctuation, due to the way it "cleans" the word. I will not be trying to fix this.
'''),
'author': 'OreoThePony/ HorseLuvver'
}
def clean(word, prefix="", suffix=""):
punctuation = ['"', "'", '.', ',', ';', ':', '[', ']', '{', '}', '=', '+', '-', '_', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '~', '/', '?', '<', '>']
if any([mark in word for mark in punctuation]):
if word[0] in punctuation:
return clean(word[1:], prefix + word[0], suffix)
else:
return clean(word[:-1], prefix, word[-1] + suffix)
else:
return word, prefix, suffix
class AutoLinkerPageViewExtension(PageViewExtension):
def __init__(self, plugin, pageview):
PageViewExtension.__init__(self, plugin, pageview)
self.connectto(pageview.textview, 'end-of-word', self.on_end_of_word)
def link_collector(self, notebook):
linklist = {}
if notebook == "Notebook": #change this to your notebook name
f = open("") #path to the parent page text file (use slashes, not colons)
for line in f.read().splitlines():
if line[:3] == '[[+' and line[-2:] == ']]': #I'm assuming you just have a list of pages linked like [[+Name]]. If not, correct it.
linklist[line[3:-2]] = "" + line[3:-2] #the path where the pages that will be linked to are (colons this time)
f.close()
return linklist
def on_end_of_word(self, textview, start, end, word, char, editmode):
buffer = textview.get_buffer()
logger.debug("End of word, word: %s", word)
links = self.link_collector(buffer.notebook.name)
clean_word, prefix, suffix = clean(word)
logger.debug("Clean word: %s", clean_word)
if clean_word in links.keys():
with buffer.tmp_cursor(start):
buffer.delete(start, end)
buffer.insert_at_cursor(prefix)
buffer.insert_link_at_cursor(clean_word, links[clean_word])
buffer.insert_at_cursor(suffix)
buffer.set_modified(True)
textview.stop_emission('end_of_word')
logger.debug(clean_word + " linked to " + links[clean_word])