def open_unl(self,unl): parts = unl.split("#",1) c = g.openWithFileName(parts[0]) if len(parts) > 1: segs = parts[1].split("-->") g.recursiveUNLSearch(segs, c)
def add_bookmark(self): """Return the file like 'f' that leo_interface.send_head makes """ parsed_url = urlparse.urlparse(self.request_handler.path) query = urlparse.parse_qs(parsed_url.query) # print(parsed_url.query) # print(query) name = query.get('name', ['NO TITLE'])[0] url = query['url'][0] one_tab_links = [] if 'www.one-tab.com' in url.lower(): one_tab_links = query.get('ln', [''])[0] one_tab_links = json.loads(one_tab_links) c = None # outline for bookmarks previous = None # previous bookmark for adding selections parent = None # parent node for new bookmarks using_root = False path = self.bookmark_unl if path: parsed = urlparse.urlparse(path) leo_path = os.path.expanduser(parsed.path) c = g.openWithFileName(leo_path, old_c=None) if c: g.es_print("Opened '%s' for bookmarks" % path) if parsed.fragment: g.recursiveUNLSearch(parsed.fragment.split("-->"), c) parent = c.currentPosition() if parent.hasChildren(): previous = parent.getFirstChild() else: g.es_print("Failed to open '%s' for bookmarks" % self.bookmark_unl) if c is None: using_root = True c = g.app.commanders()[0] parent = c.rootPosition() previous = c.rootPosition() f = StringIO() if previous and url == previous.b.split('\n', 1)[0]: # another marking of the same page, just add selection self.add_bookmark_selection(previous, query.get('selection', [''])[0]) c.selectPosition(previous) # required for body text redraw c.redraw() f.write(""" <body onload="setTimeout('window.close();', 350);" style='font-family:mono'> <p>Selection added</p></body>""") return f if '_form' in query: # got extra details, save to new node f.write(""" <body onload="setTimeout('window.close();', 350);" style='font-family:mono'> <p>Bookmark saved</p></body>""") if using_root: nd = parent.insertAfter() nd.moveToRoot(c.rootPosition()) else: nd = parent.insertAsNthChild(0) if g.pluginIsLoaded('leo.plugins.bookmarks'): nd.h = name else: nd.h = '@url ' + name selection = query.get('selection', [''])[0] if selection: selection = '\n\n"""\n' + selection + '\n"""' tags = query.get('tags', [''])[0] if one_tab_links: if tags: tags += ', OneTabList' else: tags = 'OneTabList' self.get_one_tab(one_tab_links, nd) nd.b = "%s\n\nTags: %s\n\n%s\n\nCollected: %s%s\n\n%s" % ( url, tags, query.get('_name', [''])[0], time.strftime("%c"), selection, query.get('description', [''])[0], ) c.setChanged(True) c.selectPosition(nd) # required for body text redraw c.redraw() return f # send form to collect extra details f.write(""" <html> <head> <style> body {font-family:mono; font-size: 80%%;} th {text-align:right} </style> <style>%s</style> <title>Leo Add Bookmark</title> </head> <body onload='document.getElementById("tags").focus();'> <form method='GET' action='/_/add/bkmk/'> <input type='hidden' name='_form' value='1'/> <input type='hidden' name='_name' value=%s/> <input type='hidden' name='selection' value=%s/> <input type='hidden' name='ln' value=%s/> <table> <tr><th>Tags:</th><td><input id='tags' name='tags' size='60'/>(comma sep.)</td></tr> <tr><th>Title:</th><td><input name='name' value=%s size='60'/></td></tr> <tr><th>URL:</th><td><input name='url' value=%s size='60'/></td></tr> <tr><th>Notes:</th><td><textarea name='description' cols='60' rows='6'></textarea></td></tr> </table> <input type='submit' value='Save'/><br/> </form> </body> </html>""" % ( getData( 'user_bookmark_stylesheet'), # EKR: Add config.css to style. quoteattr(name), quoteattr(query.get('selection', [''])[0]), quoteattr(json.dumps(one_tab_links)), quoteattr(name), quoteattr(url))) return f
def add_bookmark(self): """Return the file like 'f' that leo_interface.send_head makes """ parsed_url = urlparse.urlparse(self.request_handler.path) query = urlparse.parse_qs(parsed_url.query) # print(parsed_url.query) # print(query) name = query.get('name', ['NO TITLE'])[0] url = query['url'][0] one_tab_links = [] if 'www.one-tab.com' in url.lower(): one_tab_links = query.get('ln', [''])[0] one_tab_links = json.loads(one_tab_links) c = None # outline for bookmarks previous = None # previous bookmark for adding selections parent = None # parent node for new bookmarks using_root = False path = self.bookmark_unl # g.trace(path) if path: parsed = urlparse.urlparse(path) leo_path = os.path.expanduser(parsed.path) c = g.openWithFileName(leo_path,old_c=None) if c: g.es_print("Opened '%s' for bookmarks"% path) if parsed.fragment: g.recursiveUNLSearch(parsed.fragment.split("-->"),c) parent = c.currentPosition() if parent.hasChildren(): previous = parent.getFirstChild() else: g.es_print("Failed to open '%s' for bookmarks"%self.bookmark_unl) if c is None: using_root = True c = g.app.commanders()[0] parent = c.rootPosition() previous = c.rootPosition() f = StringIO() if previous and url == previous.b.split('\n',1)[0]: # another marking of the same page, just add selection self.add_bookmark_selection( previous, query.get('selection', [''])[0]) c.selectPosition(previous) # required for body text redraw c.redraw() f.write(""" <body onload="setTimeout('window.close();', 350);" style='font-family:mono'> <p>Selection added</p></body>""") return f if '_form' in query: # got extra details, save to new node f.write(""" <body onload="setTimeout('window.close();', 350);" style='font-family:mono'> <p>Bookmark saved</p></body>""") if using_root: nd = parent.insertAfter() nd.moveToRoot(c.rootPosition()) else: nd = parent.insertAsNthChild(0) if g.pluginIsLoaded('leo.plugins.bookmarks'): nd.h = name else: nd.h = '@url '+name selection = query.get('selection', [''])[0] if selection: selection = '\n\n"""\n'+selection+'\n"""' tags = query.get('tags', [''])[0] if one_tab_links: if tags: tags += ', OneTabList' else: tags = 'OneTabList' self.get_one_tab(one_tab_links, nd) nd.b = "%s\n\nTags: %s\n\n%s\n\nCollected: %s%s\n\n%s" % ( url, tags, query.get('_name', [''])[0], time.strftime("%c"), selection, query.get('description', [''])[0], ) c.setChanged(True) c.selectPosition(nd) # required for body text redraw c.redraw() return f # send form to collect extra details f.write(""" <html><head><style> body {font-family:mono; font-size: 80%%;} th {text-align:right} </style><title>Leo Add Bookmark</title> </head><body onload='document.getElementById("tags").focus();'> <form method='GET' action='/_/add/bkmk/'> <input type='hidden' name='_form' value='1'/> <input type='hidden' name='_name' value=%s/> <input type='hidden' name='selection' value=%s/> <input type='hidden' name='ln' value=%s/> <table> <tr><th>Tags:</th><td><input id='tags' name='tags' size='60'/>(comma sep.)</td></tr> <tr><th>Title:</th><td><input name='name' value=%s size='60'/></td></tr> <tr><th>URL:</th><td><input name='url' value=%s size='60'/></td></tr> <tr><th>Notes:</th><td><textarea name='description' cols='60' rows='6'></textarea></td></tr> </table> <input type='submit' value='Save'/><br/> </form> </body></html>""" % (quoteattr(name), quoteattr(query.get('selection', [''])[0]), quoteattr(json.dumps(one_tab_links)), quoteattr(name), quoteattr(url))) return f