Source

odt2org / orgfile.py

Full commit
# -*- coding: utf-8 -*-
#!/usr/python
'''
'''
import codecs
import os

class ORGfile:
    '''This class is used to generate an ORG file'''
    def __init__(self,
                 filename = None,
                 overwrite = False):
        self.filename = filename
        self.isWarning = False
        self.data = [u'-*- mode: org; coding: utf-8 -*-\n' ]
    
    def readfromlist(self, _list):
        #print _list
        if _list[0][1] == u'Title':
            _start = 1
            self.addHeading( level = 1,
                             text = _list[0][2] )
        else:
            _start = 0
            self.addHeading( level = 1,
                             text = [u'%s' % os.path.split(self.filename)[1]] )
            
        for _i in _list[_start:]:
            if _i[3]: # Is heading (a dictionary would be more readable)
                self.addHeading( level = int(_i[0])+1,
                                 text = _i[2] )
            elif _i[4]: # Is heading (a dictionary would be more readable)
                self.addParagraph( text = _i[2] )
            elif _i[5] == 'table':
                self.addTable( table = _i[2] )
            elif _i[5] == 'enumeration':
                self.addEnumeration( enumeration = _i[2] )
                

    def addHeading(self, 
                   level = 1,
                   text = [] ):
        #print text
        # A heading should only have one line.
        if len( text ) > 1:
            pass # TODO: juntar todas las líneas
            # text.replace('\r\n',' ')    
            #text.replace('\n',' ')

        # Check level is valid
        if level < 1:
            text = [u'* [WARNING] ' + text[0]]
            self.isWarning = True
        else:
            text = [u'*'*(level) + u' %s' % text[0]]

        self.data = self.data + text

    def addParagraph(self, 
                     text = [] ):
        self.data = self.data + text

    def addTable(self, 
                 table = []):
        _list = []
        if len(table) > 0:
            # First line
            _list = _list + [u'|-']
            _list = _list + self.__addRow__( table[0] )
            _list = _list + [u'|-']
        if len(table) > 1:
            for _row in table[1:]:
                _list = _list + self.__addRow__( _row )
            _list = _list + [u'|-']

        self.data = self.data +  _list 

    def __addRow__(self, _row):
        '''Helper function to show rows'''
        _list = []
        _n = 0
        _flag = True
        while _flag:
            _text = u'| '
            for _cell in _row:
                _flag = False
                if len(_cell) > _n:
                    _text = _text + u'%s |' % _cell[_n]
                    _flag = True
                else:
                    _text = _text + u'   |'
            if _flag:
                _list.append( _text )
            _n = _n + 1
        return _list

    def addEnumeration(self, 
                       enumeration = []):
        _lista = []
        for _i in enumeration:
            _lista = _lista + [u' ' * (_i[0]-1) + u'- ' + _i[1][0]]
            if len(_i[1]) > 1:
                for _j in _i[1:]:
                    _lista = _lista + [ _j ]

        self.data = self.data +  _lista

    def clean(self):
        pass

    def export(self):
       #try:
           _tmp = u''
           for _i in self.data:
               _tmp = _tmp + u'%s\n' % _i
           _fp = codecs.open( self.filename, 'w', 'utf-8')
           _fp.write( _tmp )
           _fp.close()
       #except:
       #    print u'ERROR: the file was processed, but it failed when writting it:\n   %s' % self.filename


if __name__ == '__main__':
    pass