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)): 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())
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))