"Fossies" - the Fresh Open Source Software Archive

Member "zim-0.70/zim/export/layouts.py" (14 Mar 2019, 5540 Bytes) of package /linux/privat/zim-0.70.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "layouts.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.69.1_vs_0.70.

    1 
    2 # Copyright 2008-2014 Jaap Karssenberg <jaap.karssenberg@gmail.com>
    3 
    4 
    5 '''The ExportLayout object determines the mapping of pages to files
    6 when exporting. The subclasses give alternative file layouts.
    7 '''
    8 
    9 from zim.fs import PathLookupError
   10 from zim.fs import File as OldFile
   11 from zim.fs import Dir as OldDir
   12 from zim.newfs import LocalFile, LocalFolder
   13 
   14 from zim.notebook import encode_filename
   15 
   16 
   17 class ExportLayout(object):
   18     '''The ExportLayout object determines the mapping of pages to files
   19     when exporting. This is the base class that defines the public API.
   20     '''
   21 
   22     relative_root = None #: Used by linker to make paths relative
   23 
   24     def page_file(self, page):
   25         '''Returns the file for a page
   26         @param page: a L{Page} or L{Path} object
   27         @returns: a L{File} object
   28         @raises PathLookupError: if page can not be mapped
   29         @implementation: must be implemented by subclasses
   30         '''
   31         raise NotImplementedError
   32 
   33     def attachments_dir(self, page):
   34         '''Returns the attachments folder for a page
   35         @param page: a L{Page} or L{Path} object
   36         @returns: a L{Dir} object
   37         @raises PathLookupError: if folder can not be mapped
   38         @implementation: must be implemented by subclasses
   39         '''
   40         raise NotImplementedError
   41 
   42     def resources_dir(self):
   43         '''Returns the folder for template resources
   44         @returns: a L{Dir} object
   45         '''
   46         raise NotImplementedError
   47 
   48 
   49 class DirLayoutBase(ExportLayout):
   50 
   51     # Assumes "namespace" and "dir" attributes
   52 
   53     def attachments_dir(self, page):
   54         if self.namespace:
   55             if page == self.namespace:
   56                 return self.dir
   57             elif page.ischild(self.namespace):
   58                 path = page.relname(self.namespace)
   59             else:
   60                 raise PathLookupError(
   61                     '%s not a child of %s' % (page, self.namespace)
   62                 )
   63             name = page.relname(self.namespace)
   64         else:
   65             name = page.name
   66         return self.dir.folder(encode_filename(name))
   67 
   68     def resources_dir(self):
   69         return self.dir.folder('_resources')
   70 
   71 
   72 class MultiFileLayout(DirLayoutBase):
   73     '''Layout that maps pages to files in a folder similar to how a
   74     notebook is stored.
   75 
   76     Layout::
   77 
   78       dir/
   79        `--> _resources/
   80        `--> page.html
   81        `--> page/
   82              `--> attachment.png
   83 
   84     The root for relative links is "dir/"
   85     '''
   86 
   87     def __init__(self, dir, ext, namespace=None):
   88         '''Constructor
   89         @param dir: a L{Dir} object
   90         @param ext: the file extension to be used, e.g. 'html'
   91         @param namespace: optional namespace prefix to strip from
   92         page names
   93         '''
   94         if isinstance(dir, OldDir):
   95             dir = LocalFolder(dir.path)
   96         self.dir = dir
   97         self.ext = ext
   98         self.namespace = namespace
   99         self.relative_root = self.dir
  100 
  101     def page_file(self, page):
  102         if page.isroot:
  103             raise PathLookupError('Can not export: %s', page)
  104         elif self.namespace:
  105             if page.ischild(self.namespace):
  106                 name = page.relname(self.namespace)
  107             else:
  108                 # This layout can not store page == namespace !
  109                 raise PathLookupError(
  110                     '%s not a child of %s' % (page, self.namespace)
  111                 )
  112         else:
  113             name = page.name
  114         return self.dir.file(encode_filename(name) + '.' + self.ext)
  115 
  116 
  117 #~ class MultiFolderLayout(DirLayoutBase):
  118 
  119     # dir/
  120     #  `--> _resources/
  121     #  `--> page/
  122     #        `--> index.html  # page contents
  123     #        `--> attachment.png
  124 
  125     # Root for relative links is "dir/"
  126 
  127 
  128 class FileLayout(DirLayoutBase):
  129     '''Layout that maps pages to files in a folder with one specific
  130     page as the top level file. Use to export sub-tree of a notebook.
  131 
  132     Layout::
  133 
  134       page.html
  135       page_files/
  136        `--> attachment.png
  137        `--> subpage.html
  138        `--> subpage/attachment.pdf
  139        `--> _resources/
  140 
  141     The root for relative links is "page_files/"
  142     '''
  143 
  144     def __init__(self, file, page, ext):
  145         '''Constructor
  146         @param file: a L{File} object
  147         @param page: a L{Path} object for the top level page
  148         @param ext: the file extension to be used for sub-pages, e.g. 'html'
  149         '''
  150         if isinstance(file, OldFile):
  151             file = LocalFile(file.path)
  152         self.file = file
  153         self.namespace = page
  154         self.ext = ext
  155 
  156         basename = file.basename
  157         if '.' in basename:
  158             basename, x = basename.rsplit('.', 1)
  159         self.dir = file.parent().folder(basename + '_files')
  160         self.relative_root = self.dir
  161 
  162     def page_file(self, page):
  163         if page == self.namespace:
  164             return self.file
  165         elif page.ischild(self.namespace):
  166             name = page.relname(self.namespace)
  167         else:
  168             raise PathLookupError(
  169                 '%s not a child of %s' % (page, self.namespace)
  170             )
  171         return self.dir.file(encode_filename(name) + '.' + self.ext)
  172 
  173 
  174 
  175 class SingleFileLayout(DirLayoutBase):
  176     '''Like FileLayout, except all pages are stored in a single file
  177     while attachments still follow folder structure per page.
  178     Can be used to export a page with sub-pages, but also for a
  179     complete notebook.
  180 
  181     Layout::
  182 
  183       page.html
  184       page_files/
  185        `--> attachment.png
  186        `--> subpage/attachment.pdf
  187        `--> _resources/
  188 
  189     The root for relative links is "page_files/"
  190     '''
  191 
  192     def __init__(self, file, page=None):
  193         '''Constructor
  194         @param file: a L{File} object
  195         @param page: an optional L{Path} object for the top level page
  196         '''
  197         if isinstance(file, OldFile):
  198             file = LocalFile(file.path)
  199         self.file = file
  200 
  201         basename = file.basename
  202         if '.' in basename:
  203             basename, x = basename.rsplit('.', 1)
  204         self.dir = file.parent().folder(basename + '_files')
  205         self.relative_root = self.dir
  206 
  207         self.namespace = page
  208 
  209     def page_file(self, page):
  210         if page.isroot:
  211             raise PathLookupError('Can not export: %s', page)
  212         elif self.namespace \
  213         and not page == self.namespace \
  214         and not page.ischild(self.namespace):
  215             raise PathLookupError(
  216                 '%s not a child of %s' % (page, self.namespace)
  217             )
  218         else:
  219             return self.file