-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathorglinks.py
628 lines (564 loc) · 22.3 KB
/
orglinks.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import sublime
import sublime_plugin
import re
import os
from OrgExtended.orgparse.sublimenode import *
import OrgExtended.orgutil.util as util
from OrgExtended.orgutil.util import *
import logging
import OrgExtended.orgdb as db
import OrgExtended.asettings as sets
import OrgExtended.orgextension as ext
from collections import defaultdict
import struct
import imghdr
import urllib.request
import yaml
import OrgExtended.orgneovi as nvi
import OrgExtended.pymitter as evt
from OrgExtended.orgplist import *
import OrgExtended.orguniqueview as uview
try:
import importlib
except ImportError:
pass
log = logging.getLogger(__name__)
# This is entirely copied from the wonderful OrgMode plugin
# found on package control. The orgmode plugin has a great base
# resolver system. I have copied it and will be extending it
# somewhat. OrgExtended is not compatible with orgmode
# so I have had to consume it here rather than just recommend
# you use it.
def find_all_links(view):
links = view.find_by_selector("orgmode.link")
return links
def extract_link(view):
pt = view.sel()[0].begin()
links = find_all_links(view)
for link in links:
if(link.contains(pt)):
return link
return None
DEFAULT_LINK_RESOLVERS = [
'internal',
'http',
'https',
'prompt',
'jira',
'email',
'file',
]
available_resolvers = ext.find_extension_modules('orgresolver', DEFAULT_LINK_RESOLVERS)
linkre = re.compile(r"\[\[([^\[\]]+)\]\s*(\[[^\[\]]*\])?\]")
# Returns the url from the full link
def extract_link_url(str):
m = linkre.search(str)
return m.group(1) if m is not None else str
def extract_link_url_from_region(view, region):
return extract_link_url(view.substr(region))
def is_region_link(view, region):
return 'orgmode.link' in view.scope_name(region.end())
def get_link_region_at(view):
if(is_region_link(view, view.sel()[0])):
return extract_link(view)
return None
def find_image_file(view, url):
# ABS
if(os.path.isabs(url)):
return url
# Relative
if(view != None):
curDir = os.path.dirname(view.file_name())
filename = os.path.join(curDir, url)
if(os.path.isfile(filename)):
return filename
# In search path
searchHere = sets.Get("imageSearchPath",[])
for direc in searchHere:
filename = os.path.join(direc, url)
if(os.path.isfile(filename)):
return filename
searchHere = sets.Get("orgDirs",[])
for direc in searchHere:
filename = os.path.join(direc, "images", url)
if(os.path.isfile(filename)):
return filename
RE_TARGET = re.compile(r'<<(?P<target>[^>]+)>>')
RE_NAMED = re.compile(r'[#][+]NAME[:]\s*(?P<target>.+)')
def CreateLink(view):
fn = view.file_name()
# Org Files have a LOT more potential for making links!
if(util.isPotentialOrgFile(fn)):
r = view.curRow()
line = view.getLine(r)
linet = RE_TARGET.match(line)
namet = RE_NAMED.match(line)
link = None
# have target on this line?
if(linet):
link = "[[file:{0}::{1}][{1}]]".format(view.file_name(),linet.group('target'))
# have named object on this line?
if(link == None and namet):
link = "[[file:{0}::{1}][{1}]]".format(view.file_name(),namet.group('target'))
n = db.Get().AtInView(view)
if(link == None and n and not n.is_root()):
p = n.get_property("ID")
cp = n.get_property("CUSTOM_ID")
if(p):
link = "[[file:{0}::#{1}][{2}]]".format(view.file_name(),p,n.heading)
# Have custom id?
elif(cp):
link = "[[file:{0}::#{1}][{2}]]".format(view.file_name(),cp,n.heading)
# Am near a heading?
else:
link = "[[file:{0}::*{1}][{1}]]".format(view.file_name(),n.heading)
# okay then just use row,col
if(link == None):
r,c = view.curRowCol()
link = "[[file:{0}::{1},{2}][{3}]]".format(view.file_name(),r,c,os.path.basename(view.file_name()))
return link
else:
# Other file types only have line and column
r,c = view.curRowCol()
link = "[[{0}::{1}::{2}][{3}]]".format(fn,r,c,os.path.basename(fn))
return link
class OrgOpenLinkCommand(sublime_plugin.TextCommand):
def resolve(self, content):
for resolver in self.resolvers:
result = resolver.resolve(content)
if result is not None:
return resolver, result
return None, None
def is_valid_scope(self, region):
return is_region_link(self.view, region)
def extract_content(self, region):
return extract_link_url_from_region(self.view, region)
def run(self, edit):
# reload our resolvers if they are not loaded.
#if(not hasattr(self, "resolvers")):
wanted_resolvers = sets.Get("linkResolvers", DEFAULT_LINK_RESOLVERS)
self.resolvers = [available_resolvers[name].Resolver(self.view) for name in wanted_resolvers]
# This is goofy. File loads get resolve called that calls extract.
# extact may launch the file into sublime. IF we return a path
# sublime will call start on the file.
view = self.view
for sel in view.sel():
if not self.is_valid_scope(sel):
continue
region = extract_link(view) #view.extract_scope(sel.end())
content = self.extract_content(region)
resolver, newcontent = self.resolve(content)
if newcontent is None:
log.error(" Could not resolve link:\n%s" % content)
sublime.error_message('Could not resolve link:\n%s' % content)
continue
resolver.execute(newcontent)
class OrgCreateLinkCommand(sublime_plugin.TextCommand):
def run(self, edit):
link = CreateLink(self.view)
sublime.set_clipboard(link)
nvi.TestAndSetClip(self.view, link)
# global magic
VIEWS_WITH_IMAGES = set()
# Stolen from:
# https://github.com/renerocksai/sublime_zk/blob/master/sublime_zk.py
# The excellent work in that system showed a way of using
# Phantoms to show images inline. This gives us a part of one of Org Modes
# Most powerful features which is babel modes ability to make diagrams in
# documents.
class ImageHandler:
Phantoms = defaultdict(set)
Cache = {}
@staticmethod
def save_cache():
user_settings_path = os.path.join(sublime.packages_path(), "User","OrgExtended_image_cache.yaml")
f = open(user_settings_path,"w")
data = yaml.dump(ImageHandler.Cache, f)
f.close()
@staticmethod
def load_cache():
user_settings_path = os.path.join(sublime.packages_path(), "User","OrgExtended_image_cache.yaml")
if(os.path.isfile(user_settings_path)):
stream = open(user_settings_path, 'r')
ImageHandler.Cache = yaml.load(stream, Loader=yaml.SafeLoader)
stream.close()
@staticmethod
def show_image(region, view, max_width=1024):
width = -1
height = -1
node = db.Get().AtRegion(view,region)
if(node):
attr = node.get_comment("ORG_ATTR",None)
if(attr):
params = PList.createPList(attr)
try:
width = int(params.Get('width',-1))
height = int(params.Get('height',-1))
except:
log.error("Could not extract width and height from plist / ORG_ATTR comment")
# If we already have this image then exit out
if view.id() in ImageHandler.Phantoms and str(region) in ImageHandler.Phantoms[view.id()]:
return
url = extract_link_url_from_region(view, region)
# We can only handle links to images this way.
if not util.is_image(url):
return
level = db.Get().GetIndentForRegion(view, region)
indent = " " * (level * 2)
if url.startswith('http') or url.startswith('https'):
img = url
local_filename = None
if(url in ImageHandler.Cache and os.path.isfile(ImageHandler.Cache[url])):
local_filename = ImageHandler.Cache[url]
log.debug("Loaded from cache: " + url)
else:
log.debug("Downloaded: " + url)
local_filename, headers = urllib.request.urlretrieve(url)
ImageHandler.Cache[url] = local_filename
ImageHandler.save_cache()
size = ImageHandler.get_image_size(local_filename)
ttype = None
if None != size:
w, h, ttype = size
if ttype and ttype == 'svg':
view.erase_phantoms(str(region))
html_img = util.get_as_string(img)
print(html_img)
view.add_phantom(str(region), region, html_img, sublime.LAYOUT_BLOCK)
ImageHandler.Phantoms[view.id()].add(str(region))
return
FMT = u'''
{}<img src="data:image/{}" class="centerImage" {}>
'''
img = ttype + ";base64," + util.get_as_base64(img)
elif url.startswith("file:"):
url = url.replace("file:","")
log.debug("FILE: " + url)
FMT = '''
{}<img src="file://{}" class="centerImage" {}>
'''
img = find_image_file(view, url)
if(img):
size = ImageHandler.get_image_size(img)
if(width > 0):
size = [width, size[1], size[2]]
if(height > 0):
size = [size[0], height, size[2]]
else:
size = (100,100,"png")
else:
log.debug("local file: " + url)
FMT = '''
{}<img src="file://{}" class="centerImage" {}>
'''
img = find_image_file(view, url)
log.debug("local file2: " + url)
size = ImageHandler.get_image_size(img)
if(width > 0):
size = [width, size[1], size[2]]
if(height > 0):
size = [size[0], height, size[2]]
if not size:
return
w, h, t = size
line_region = view.line(region)
imgattr = ImageHandler.check_imgattr(view, line_region, region)
if not imgattr:
if w > max_width:
m = max_width / w
h *= m
w = max_width
imgattr = 'width="{}" height="{}"'.format(w, h)
view.erase_phantoms(str(region))
html_img = FMT.format(indent, img, imgattr)
view.add_phantom(str(region), region, html_img, sublime.LAYOUT_BLOCK)
ImageHandler.Phantoms[view.id()].add(str(region))
@staticmethod
def hide_image(region, view):
try:
view.erase_phantoms(str(region))
ImageHandler.Phantoms[view.id()].remove(str(region))
except:
pass
@staticmethod
def show_image_at(view, max_width=1024):
reg = get_link_region_at(view)
if(reg):
ImageHandler.show_image(reg, view)
@staticmethod
def hide_image_at(view, max_width=1024):
reg = get_link_region_at(view)
if(reg):
ImageHandler.hide_image(reg, view)
@staticmethod
def show_images(view, max_width=1024):
global VIEWS_WITH_IMAGES
skip = 0
while True:
imageRegions = view.find_by_selector('orgmode.link')[skip:]
skip += 1
if not imageRegions:
break
region = imageRegions[0]
ImageHandler.show_image(region, view, max_width)
VIEWS_WITH_IMAGES.add(view.id())
@staticmethod
def check_imgattr(view, line_region, link_region=None):
# find attrs for this link
full_line = view.substr(line_region)
link_till_eol = full_line[link_region.a - line_region.a:]
# find attr if present
m = re.match(r'.*\)\{(.*)\}', link_till_eol)
if m:
return m.groups()[0]
@staticmethod
def hide_images(view, edit):
for rel_p in ImageHandler.Phantoms[view.id()]:
view.erase_phantoms(rel_p)
del ImageHandler.Phantoms[view.id()]
skip = 0
while True:
img_regs = view.find_by_selector('orgmode.link.href')[skip:]
skip += 1
if not img_regs:
break
region = img_regs[0]
rel_p = view.substr(region)
if(util.is_image(rel_p)):
pass
#line_region = view.line(region)
#line_str = view.substr(line_region)
#view.replace(edit, line_region, line_str.strip())
VIEWS_WITH_IMAGES.discard(view.id())
@staticmethod
def get_image_size(img):
"""
Determine the image type of img and return its size.
"""
#print("IMG: " + img)
try:
with open(img, 'rb') as f:
head = f.read(24)
ttype = None
if b'<svg' in head:
ttype = 'svg'
width, height = (100,100)
return width, height, ttype
if len(head) != 24:
return
if imghdr.what(img) == 'png':
ttype = "png"
check = struct.unpack('>i', head[4:8])[0]
if check != 0x0d0a1a0a:
return
width, height = struct.unpack('>ii', head[16:24])
elif imghdr.what(img) == 'gif':
ttype = "gif"
width, height = struct.unpack('<HH', head[6:10])
elif imghdr.what(img) == 'jpeg':
ttype = "jpeg"
try:
f.seek(0) # Read 0xff next
size = 2
ftype = 0
while not 0xc0 <= ftype <= 0xcf:
f.seek(size, 1)
byte = f.read(1)
while ord(byte) == 0xff:
byte = f.read(1)
ftype = ord(byte)
size = struct.unpack('>H', f.read(2))[0] - 2
# SOFn block
f.seek(1, 1) # skip precision byte.
height, width = struct.unpack('>HH', f.read(4))
except Exception:
return
else:
return
return width, height, ttype
except:
return 100,100,'png'
class OrgCycleImagesCommand(sublime_plugin.TextCommand):
def OnDone(self):
self.view.sel().clear()
self.view.sel().add(self.cursor)
evt.EmitIf(self.onDone)
def OnShown(self):
self.OnDone()
def OnHidden(self):
self.view.run_command("org_show_images",{"onDone": evt.Make(self.OnShown)})
def run(self, edit, onDone=None):
self.onDone = onDone
self.cursor = self.view.sel()[0]
self.view.run_command("org_hide_images",{"onDone": evt.Make(self.OnHidden)})
class OrgShowImagesCommand(sublime_plugin.TextCommand):
def run(self, edit,onDone=None):
ImageHandler.show_images(self.view)
evt.EmitIf(onDone)
class OrgHideImagesCommand(sublime_plugin.TextCommand):
def run(self, edit,onDone=None):
ImageHandler.hide_images(self.view, edit)
evt.EmitIf(onDone)
class OrgShowImageCommand(sublime_plugin.TextCommand):
def run(self, edit):
ImageHandler.show_image_at(self.view)
class OrgHideImageCommand(sublime_plugin.TextCommand):
def run(self, edit):
ImageHandler.hide_image_at(self.view)
# ON LOAD HANDLER: if startup is set we show or hide the images.
#+STARTUP: inlineimages
#+STARTUP: noinlineimages
def get_show_images_default():
return sets.Get("startup",["noinlineimages"])
def get_image_startup(node):
startupDefault = get_show_images_default()
return node.startup(startupDefault)
def onShutdown():
ImageHandler.save_cache()
def onLoad(view):
ImageHandler.load_cache()
file = db.Get().FindInfo(view)
if(file):
startup = get_image_startup(file.org[0])
if(Startup.inlineimages in startup):
ImageHandler.show_images(view)
class OrgLinkToFileCommand(sublime_plugin.TextCommand):
def on_done(self, index, modifiers=None):
if(index >= 0):
f = self.files[index]
link = self.view.MakeRelativeToMe(f[0])
desc = os.path.basename(link)
if(len(f) > 1):
desc = f[1]
includeRoamTag = sets.Get("insertRoamTagToFileLink", True)
if includeRoamTag is False:
log.error(self.rawTitles[f[0]])
desc = self.rawTitles[f[0]]
indent = ""
node = db.Get().AtInView(self.view)
if(node):
indent = node.indent()
data = r"{indent}[[file:{link}][{desc}]]".format(indent=indent, link=link, desc=desc)
self.view.run_command("org_internal_insert", {"location": self.view.sel()[0].begin(), "text": data})
def run(self, edit):
self.files = []
self.rawTitles = {}
for i in range(0, len(db.Get().Files)):
filename = db.Get().Files[i].filename
title = " ".join(db.Get().Files[i].org.get_comment("TITLE", "")).strip()
self.rawTitles[filename] = title
useRoamTags = sets.Get("linkFindUseRoamTags", True)
if(useRoamTags):
tags = " ".join(db.Get().Files[i].org.get_comment("ROAM_TAGS", "")).strip()
if(tags != ""):
title = "(" + tags + ") " + title
if(title != ""):
self.files.append([filename, title])
else:
self.files.append([filename])
self.view.window().show_quick_panel(self.files, self.on_done, -1, -1)
class OrgJumpToBacklinksCommand(sublime_plugin.TextCommand):
def on_done(self, index, modifiers=None):
if(index >= 0):
f = self.links[index]
fname = f.fromFile.filename + ":" + str(f.row)
self.view.window().open_file(fname, sublime.ENCODED_POSITION)
def run(self, edit):
self.files = []
bl = db.Get().GetBacklinks(self.view)
if(bl):
self.links = bl
for l in bl:
title = l.fromFile.org.get_comment("TITLE",[""])[0]
desc = l.desc if l.desc else ""
if(title.strip() != ""):
desc = title + ": " + desc
self.files.append([desc, l.link])
self.view.window().show_quick_panel(self.files, self.on_done, -1, -1)
return
log.debug("NO BACKLINKS")
def BuildBacklinksDisplay(view):
bl = db.Get().GetBacklinks(view)
if(bl):
out = str(len(bl)) + " Backlinks\n"
files = {}
for l in bl:
title = l.fromFile.org.get_comment("TITLE",[""])[0]
if(title.strip() == ""):
title = l.desc
if(not title):
title = l.link
if(not title in files):
files[title] = []
files[title].append(l)
keys = list(files.keys())
keys.sort()
for k in keys:
out += "\n"
flist = files[k]
flist = sorted(flist,key=lambda x: x.row)
f = flist[0]
fn = f.fromFile.filename
out += "* {}\n".format(k)
for f in flist:
desc = f.desc
if(not desc):
desc = f.link
out += " - [[file:{}::{}][{}]]\n".format(fn,f.row,desc)
if(f.linktext != f.text and (len(f.linktext)+5) < len(f.text)):
out += " " + f.text.strip() + "\n"
return out
return "0 Backlinks"
def UpdateBacklinksForDisplay(view):
if(uview.UniqueView.IsShowing("Backlinks")):
out = BuildBacklinksDisplay(view)
uv = uview.UniqueView.Get("Backlinks",curview=view)
uv.view.run_command("org_remove_all_folds")
uv.view.set_read_only(False)
uv.view.run_command("org_internal_replace", {"start": 0, "end": uv.view.size(), "text": out + "\n"})
uv.view.set_read_only(True)
uv.view.run_command("org_fold_all_links")
uv.view.sel().clear()
class OrgShowBacklinksCommand(sublime_plugin.TextCommand):
def run(self, edit):
out = BuildBacklinksDisplay(self.view)
#bl = db.Get().GetBacklinks(self.view)
self.uv = uview.UniqueView.Get("Backlinks",curview=self.view)
self.uv.view.run_command("org_remove_all_folds")
self.uv.view.set_read_only(False)
self.uv.view.run_command("org_internal_replace", {"start": 0, "end": self.uv.view.size(), "text": out + "\n"})
self.uv.view.set_read_only(True)
self.uv.view.run_command("org_fold_all_links")
self.uv.view.sel().clear()
class OrgSearchLinksCommand(sublime_plugin.TextCommand):
def on_done(self, index, modifiers=None):
if(self.transientview != None):
self.transientview.close()
self.transientview = None
if(index >= 0):
f = self.lobjs[index]
fname = f.inFile.filename + ":" + str(f.row+1)
self.view.window().open_file(fname, sublime.ENCODED_POSITION)
def on_highlighted(self, index, modifiers=None):
if(self.transientview != None):
self.transientview.close()
self.transientview = None
if(index >= 0):
f = self.lobjs[index]
fname = f.inFile.filename + ":" + str(f.row+1)
self.transientview = self.view.window().open_file(fname, sublime.ENCODED_POSITION|sublime.TRANSIENT)
def run(self, edit):
self.links = []
self.lobjs = []
self.transientview = None
files = db.Get().Files
if(files):
for f in files:
lnks = f.org.env.links
for l in lnks:
desc = l.desc + " : " if l.desc else ""
self.links.append([desc + l.link,f.filename])
l.inFile = f
self.lobjs.append(l)
self.view.window().show_quick_panel(self.links, self.on_done, -1, -1,self.on_highlighted)
return