コード例 #1
0
    def _dump(self):
        '''Returns the page source'''
        tree = self.get_parsetree()
        if tree is None:
            raise AssertionError, 'BUG: Can not store a page without content'

        #~ print 'STORE', tree.tostring()
        if tree.hascontent:
            new = False
            if self.properties is None:
                self.properties = HeadersDict()
                new = True
            self.properties['Content-Type'] = 'text/x-zim-wiki'
            self.properties['Wiki-Format'] = WIKI_FORMAT_VERSION
            if new:
                now = datetime.now()
                self.properties['Creation-Date'] = now.isoformat()

            # Note: No "Modification-Date" here because it causes conflicts
            # when merging branches with version control, use mtime from filesystem
            # If we see this header, remove it because it will not be updated.
            try:
                del self.properties['Modification-Date']
            except:
                pass

            lines = self.properties.dump()
            lines.append('\n')
            lines.extend(self.format.Dumper().dump(tree))
            return lines
        else:
            return []
コード例 #2
0
 def _fetch_parsetree(self, lines=None):
     '''Fetch a parsetree from source or returns None'''
     #~ print '!! fetch tree', self
     ## Enable these lines to test error handling in the UI
     #~ import random
     #~ if random.random() > 0.5:
     #~ raise Exception, 'This is a test error'
     ###
     try:
         lines = lines or self.source.readlines()
         self.properties = HeadersDict()
         self.properties.read(lines)
         # TODO: detect other formats by the header as well
         if 'Wiki-Format' in self.properties:
             version = self.properties['Wiki-Format']
         else:
             version = 'Unknown'
         parser = self.format.Parser(version)
         return parser.parse(lines)
     except FileNotFoundError:
         return None