def init_uistate(self): # Switch between folder selection or file selection based # on whether we selected full notebook or single page in the # first page self.uistate.setdefault('output_folder', '', Dir) self.uistate.setdefault('index_page', '') self.uistate.setdefault('output_file', '', File) show_file = self.uistate.get('selection') == 'page' if show_file: self.form.widgets['folder'].set_sensitive(False) self.form.widgets['folder'].hide() self.form.widgets['file'].set_sensitive(True) self.form.widgets['file'].show() else: self.form.widgets['folder'].set_sensitive(True) self.form.widgets['folder'].show() self.form.widgets['file'].set_sensitive(False) self.form.widgets['file'].hide() if show_file: basename = self.uistate['selected_page'].basename ext = zim.formats.get_format(self.uistate['format']).info['extension'] if self.uistate['output_file'] \ and isinstance(self.uistate['output_file'], File): dir = self.uistate['output_file'].dir file = dir.file(encode_filename(basename + '.' + ext)) else: file = File('~/' + encode_filename(basename + '.' + ext)) self.uistate['output_file'] = file self.form['file'] = self.uistate['output_file'] self.form['folder'] = self.uistate['output_folder']
def page(self, link): try: page = self.notebook.resolve_path(link, source=self.path) except PageNameError: return '' else: if page == self.path: return '' parent = page.commonparent(self.path) if parent == self.path: path = './' + self.path.basename + '/' downpath = page.relname(parent) path += encode_filename(downpath) + self._extension elif parent == page: uppath = self.path.relname(parent) path = '../' * (uppath.count(':') + 1) path += encode_filename(page.basename) + self._extension else: uppath = self.path.relname(parent) downpath = page.relname(parent) path = '../' * uppath.count(':') or './' path += encode_filename(downpath) + self._extension #~ print '>>>', path return path
def init_uistate(self): # Switch between folder selection or file selection based # on whether we selected full notebook or single page in the # first page self.uistate.setdefault('output_folder', None, Dir) self.uistate.setdefault('index_page', '') self.uistate.setdefault('output_file', None, File) show_file = self.uistate.get('selection') == 'page' if show_file: self.form.widgets['folder'].set_sensitive(False) self.form.widgets['folder'].hide() self.form.widgets['file'].set_sensitive(True) self.form.widgets['file'].show() else: self.form.widgets['folder'].set_sensitive(True) self.form.widgets['folder'].show() self.form.widgets['file'].set_sensitive(False) self.form.widgets['file'].hide() if show_file: basename = self.uistate['selected_page'].basename ext = zim.formats.get_format( self.uistate['format']).info['extension'] if self.uistate['output_file'] \ and isinstance(self.uistate['output_file'], File): dir = self.uistate['output_file'].dir file = dir.file(encode_filename(basename + '.' + ext)) else: file = File('~/' + encode_filename(basename + '.' + ext)) self.uistate['output_file'] = file self.form['file'] = self.uistate['output_file'] self.form['folder'] = self.uistate['output_folder']
def export_page(self, dir, page, pages=None, use_namespace=False, filename=None, dirname=None): '''Export a single page and copy it's attachments. Does not include sub-pages. @param dir: a L{Dir} object for the target folder @param page: the L{page} to export @param pages: dict with special pages that is passed on to the template @param use_namespace: when C{False} the export file will be placed directly in C{dir}, when C{True} the page will be put in a sub-folder structure that reflects the namespace of the page @param filename: alternative filename to use for the export file instead of the page name. If needed the appropriate file extension is added to the name. @param dirname: alternative name to use for the folder with attachments, if C{None} it takes the filename without the extension. @todo: Get rid of C{use_namespace} by creating a seperate function for that. Maybe create a variant that does a single page in an folder directly and a higher level function that does an entire namespace including sub-pages @todo: change filename and dirname in File and Dir arguments ''' logger.info('Exporting %s', page.name) if filename is None: if use_namespace: filename = encode_filename(page.name) else: filename = encode_filename(page.basename) extension = '.' + self.format.info['extension'] if not filename.endswith(extension): filename += extension if dirname is None: dirname = filename[:-len(extension)] file = dir.file(filename) attachments = self.notebook.get_attachments_dir(page) self.linker.set_base(attachments.dir) # parent of attachment dir # FIXME, assuming standard file store layout to get correct relative links self.linker.target_file = file fh = file.open('w') self.export_page_to_fh(fh, page, pages) fh.close() subdir = dir.subdir(dirname) for name in attachments.list(): file = attachments.file(name) if file.exists(): # tests os.isfile file.copyto(subdir)
def link_page(self, link): try: page = self.notebook.resolve_path(link, source=self.path) except PageNameError: return '' else: return url_encode('/' + encode_filename(page.name) + '.html')
def link_page(self, link): try: page = self.notebook.resolve_path(link, source=self.path) except PageNameError: return '' else: if page == self.path: return '' parent = page.commonparent(self.path) if parent == self.path: path = './' + self.path.basename + '/' downpath = page.relname(parent) path += downpath elif parent == page: uppath = self.path.relname(parent) path = '../' * (uppath.count(':') + 1) path += page.basename else: uppath = self.path.relname(parent) downpath = page.relname(parent) path = '../' * uppath.count(':') or './' path += downpath path = encode_filename(path) + self._extension #~ print '>>>', path return url_encode(path.replace(' ', '_'))
def _get_dir(self, path): '''Returns a dir object for a notebook path''' if path == self.namespace: return self.dir else: name = path.relname(self.namespace) dirpath = encode_filename(name) return Dir([self.dir, dirpath])
def _get_dir(self, path): '''Returns a dir object for a notebook path''' if path == self.namespace: return self.dir else: name = path.relname(self.namespace) dirpath = encode_filename(name) return self.dir.subdir(dirpath)
def page_file(self, page): if page == self.namespace: return self.file elif page.ischild(self.namespace): name = page.relname(self.namespace) else: raise PathLookupError('%s not a child of %s' % (page, self.namespace)) return self.dir.file(encode_filename(name) + '.' + self.ext)
def _get_file(self, path): '''Returns a File object for a notebook path''' assert path != self.namespace, 'Can not get a file for the toplevel namespace' name = path.relname(self.namespace) filepath = encode_filename(name) + '.txt' # FIXME hard coded extension file = self.dir.file(filepath) file.checkoverwrite = True file.endofline = self.notebook.endofline return file
def init_uistate(self): # Switch between folder selection or file selection based # on whether we selected full notebook or single page in the # first page self.uistate.setdefault('output', 'multi_file') self.uistate.setdefault('output_folder', None, Dir) self.uistate.setdefault('index_page', '') self.uistate.setdefault('output_file', None, File) if self.uistate.get('format', '').startswith('MHTML'): # XXX make this a format property to be queried self.form.widgets['output:multi_file'].set_sensitive(False) self.form.widgets['output:single_file'].set_sensitive(False) self.form.widgets['output:single_file'].set_active(True) else: self.form.widgets['output:multi_file'].set_sensitive(True) self.form.widgets['output:single_file'].set_sensitive(True) self.form.widgets['output:multi_file'].show() self.form.widgets['output:single_file'].show() self.form['output'] = self.uistate['output'] self.on_output_changed(None) # Set file name basename = self.uistate['selected_page'].basename format = self.uistate['format'] if format.startswith('MHTML'): ext = 'mht' else: ext = zim.formats.get_format(format).info['extension'] if self.uistate['output_file'] \ and isinstance(self.uistate['output_file'], File): dir = self.uistate['output_file'].dir file = dir.file(encode_filename(basename + '.' + ext)) else: file = File('~/' + encode_filename(basename + '.' + ext)) self.uistate['output_file'] = file self.form['file'] = self.uistate['output_file'] self.form['folder'] = self.uistate['output_folder']
def page_file(self, page): if page == self.namespace: return self.file elif page.ischild(self.namespace): name = page.relname(self.namespace) else: raise PathLookupError( '%s not a child of %s' % (page, self.namespace) ) return self.dir.file(encode_filename(name) + '.' + self.ext)
def _link_notebook(self, link): if link.startswith('zim+'): link = link[4:] if '?' in link: link, path = link.split('?') # FIXME: code below is not robust because we don't know the # storage mode of linked notebook... path = url_decode(path) # was already encoded by interwiki_link() path = encode_filename(path).replace(' ', '_') return link + '/' + url_encode(path) + '.txt' else: return link
def link_notebook(self, url): if url.startswith('zim+'): url = url[4:] if '?' in url: uri, path = url.split('?') # FIXME: code below is not robust because we don't know the # storage mode of linked notebook... path = url_decode(path) # was already encoded by interwiki_link() path = encode_filename(path).replace(' ', '_') return uri + '/' + url_encode(path) + '.txt' else: return url
def attachments_dir(self, page): if self.namespace: if page == self.namespace: return self.dir elif page.ischild(self.namespace): path = page.relname(self.namespace) else: raise PathLookupError('%s not a child of %s' % (page, self.namespace)) name = page.relname(self.namespace) else: name = page.name return self.dir.subdir(encode_filename(name))
def page_file(self, page): if page.isroot: raise PathLookupError('Can not export: %s', page) elif self.namespace: if page.ischild(self.namespace): name = page.relname(self.namespace) else: # This layout can not store page == namespace ! raise PathLookupError('%s not a child of %s' % (page, self.namespace)) else: name = page.name return self.dir.file(encode_filename(name) + '.' + self.ext)
def page_file(self, page): if page.isroot: raise PathLookupError('Can not export: %s', page) elif self.namespace: if page.ischild(self.namespace): name = page.relname(self.namespace) else: # This layout can not store page == namespace ! raise PathLookupError( '%s not a child of %s' % (page, self.namespace) ) else: name = page.name return self.dir.file(encode_filename(name) + '.' + self.ext)
def attachments_dir(self, page): if self.namespace: if page == self.namespace: return self.dir elif page.ischild(self.namespace): path = page.relname(self.namespace) else: raise PathLookupError( '%s not a child of %s' % (page, self.namespace) ) name = page.relname(self.namespace) else: name = page.name return self.dir.subdir(encode_filename(name))
def export_iter(self, pages): basename = encode_filename(pages.name) dir = get_tmpdir().subdir('mhtml_export_tmp_dir') dir.remove_children() file = dir.file(basename + '.html') layout = SingleFileLayout(file, pages.prefix) exporter = SingleFileExporter(layout, self.template, 'html', document_root_url=self.document_root_url) for p in exporter.export_iter(pages): yield p encoder = MHTMLEncoder() linker = ExportLinker(pages.notebook, layout, output=file, usebase=True) self.file.write(encoder(layout, linker))
def export_page(self, dir, page): '''Export 'page' to a file below 'dir'. Path below 'dir' will be determined by the namespace of 'page'. Attachments wil also be copied along. ''' logger.info('Exporting %s', page.name) dirname = encode_filename(page.name) filename = dirname + '.' + self.format.info['extension'] file = dir.file(filename) attachments = self.notebook.get_attachments_dir(page) self.linker.set_base(attachments.dir) # FIXME, assuming standard file store layout to get correct relative links fh = file.open('w') self.export_page_to_fh(fh, page) fh.close() subdir = dir.subdir(dirname) for name in attachments.list(): file = attachments.file(name) if file.exists(): # tests os.isfile file.copyto(subdir)
def page_object(self, path): '''Turn a L{Path} object in a relative link or URI''' return url_encode('/' + encode_filename(path.name) + '.html')
def _get_file(self, path): '''Returns a File object for a notebook path''' assert path != self.namespace, 'Can not get a file for the toplevel namespace' name = path.relname(self.namespace) filepath = encode_filename(name)+'.txt' # FIXME hard coded extension return File([self.dir, filepath])