def run(self): args = self.get_arguments() command = args[0] notebook, p = self.build_notebook() if self.opts.get('direction', 'forw').lower().startswith('back'): dir = LINK_DIR_BACKWARD else: dir = LINK_DIR_FORWARD if command == 'sort': list = sort_by_number_of_links(notebook, dir) elif command == 'compare': if args[3] is not None: page1 = Path(Path.makeValidPageName(args[2])) page2 = Path(Path.makeValidPageName(args[3])) list = find_common_links(notebook, page1, page2, dir) elif args[2] is not None: page1 = Path(Path.makeValidPageName(args[2])) list = compare_by_links(notebook, dir, page1) else: list = compare_by_links(notebook, dir) else: raise UsageError, _('Unknown command: %s') % command for columns in list: print '\t'.join(map(unicode, columns))
def get_notebook_argument(self): '''Get the notebook and page arguments for this command @returns: a 2-tuple of an L{NotebookInfo} object and an optional L{Path} or C{(None, None)} if the notebook argument is optional and not given @raises NotebookLookupError: if the notebook is mandatory and not given, or if it is given but could not be resolved ''' assert self.arguments[0] in ('NOTEBOOK', '[NOTEBOOK]') args = self.get_arguments() notebook = args[0] if notebook is None: if self.arguments[0] == 'NOTEBOOK': # not optional raise NotebookLookupError(_('Please specify a notebook')) # T: Error when looking up a notebook else: return None, None notebookinfo = resolve_notebook(notebook, pwd=self.pwd) if not notebookinfo: raise NotebookLookupError( _('Could not find notebook: %s') % notebook) # T: error message if len(self.arguments) > 1 \ and self.arguments[1] in ('PAGE', '[PAGE]') \ and args[1] is not None: pagename = Path.makeValidPageName(args[1]) return notebookinfo, Path(pagename) else: return notebookinfo, None
def do_response_ok(self): name = self.form['name'] parent = self.form['parent'] head = self.form['head'] update = self.form['update'] newbasename = Path.makeValidPageName(name) newpath = parent + newbasename if parent else Path(newbasename) if newpath == self.path: return False self.hide() # hide this dialog before showing the progressbar op = NotebookOperation( self.notebook, _('Updating Links'), # T: label for progress dialog self.notebook.move_page_iter( self.path, newpath, update_links=update, update_heading=head ) ) dialog = ProgressDialog(self, op) dialog.run() return True
def _link_tree(links, notebook, path): # Convert a list of links (of any type) into a parsetree #~ print('LINKS: ', links) #~ print('NOTEBOOK and PATH:', notebook, path) builder = ParseTreeBuilder() builder.start(FORMATTEDTEXT) for i in range(len(links)): if i > 0: builder.text(' ') link = links[i] type = link_type(link) isimage = False if type == 'interwiki': prefix = notebook.interwiki + '?' if link.startswith(prefix): link = link[len(prefix):] type = link_type(link) elif type == 'file': try: file = File(link) isimage = file.isimage() except: pass logger.debug('Pasting link: %s (type: %s, isimage: %s)', link, type, isimage) if isimage: src = notebook.relative_filepath(file, path) or file.uri builder.append(IMAGE, {'src': src}) elif link.startswith('@'): # FIXME - is this ever used ?? builder.append(TAG, {'name': links[i][1:]}, links[i]) else: name = None if type == 'page': anchor = None if '#' in link: link, anchor = link.split('#', 1) target = Path(Path.makeValidPageName( link)) # Assume links are always absolute href = notebook.pages.create_link(path, target) href.anchor = anchor link = href.to_wiki_link() if notebook.config['Notebook']['short_links']: name = href.parts()[-1] if anchor: name += '#' + anchor elif type == 'file': file = File(link) # Assume links are always URIs link = notebook.relative_filepath(file, path) or file.uri builder.append(LINK, {'href': link}, name or link) builder.end(FORMATTEDTEXT) tree = builder.get_parsetree() tree.resolve_images(notebook, path) tree.decode_urls() return tree
def _parse_page_list(input): paths = [] if not input or not input.strip(): return paths for name in input.split(','): try: paths.append(Path(Path.makeValidPageName(name.strip()))) except ValueError: logger.warn('Could not parse page name: "%s"', name) return paths
def on_text_changed(self, buffer): if not self._title_set_manually: # Automatically generate a (valid) page name self._updating_title = True start, end = buffer.get_bounds() title = start.get_text(end).strip()[:50] # Cut off at 50 characters to prevent using a whole paragraph title = title.replace(':', '') if '\n' in title: title, _ = title.split('\n', 1) try: title = Path.makeValidPageName(title.replace(':', '')) self.form['basename'] = title except ValueError: pass self._updating_title = False
def _link_tree(links, notebook, path): # Convert a list of links (of any type) into a parsetree #~ print 'LINKS: ', links #~ print 'NOTEBOOK and PATH:', notebook, path builder = ParseTreeBuilder() builder.start(FORMATTEDTEXT) for i in range(len(links)): if i > 0: builder.text(' ') link = links[i] type = link_type(link) isimage = False if type == 'file': try: file = File(link) isimage = file.isimage() except: pass logger.debug('Pasting link: %s (type: %s, isimage: %s)', link, type, isimage) if isimage: src = notebook.relative_filepath(file, path) or file.uri builder.append(IMAGE, {'src': src}) elif link.startswith('@'): # FIXME - is this ever used ?? builder.append(TAG, {'name': links[i][1:]}, links[i]) else: if type == 'page': target = Path(Path.makeValidPageName( link)) # Assume links are always absolute href = notebook.pages.create_link(path, target) link = href.to_wiki_link() elif type == 'file': file = File(link) # Assume links are always URIs link = notebook.relative_filepath(file, path) or file.uri builder.append(LINK, {'href': link}, link) builder.end(FORMATTEDTEXT) tree = builder.get_parsetree() tree.resolve_images(notebook, path) tree.decode_urls() return tree
def __init__(self, layout, template, format, index_page=None, document_root_url=None): '''Constructor @param layout: a L{ExportLayout} to map pages to files @param template: a L{Template} object @param format: the format for the file content @param index_page: a page to output the index or C{None} @param document_root_url: optional URL for the document root ''' FilesExporterBase.__init__(self, layout, template, format, document_root_url) if index_page: if isinstance(index_page, basestring): self.index_page = Path(Path.makeValidPageName(index_page)) else: self.index_page = index_page else: self.index_page = None