Changeset 683

Show
Ignore:
Timestamp:
08/11/05 06:40:31 (3 years ago)
Author:
tzanko
Message:

nbshell:
Added support for loading docbook files. This commit is broken

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nbshell/trunk/nbshell/PlainTextPlugin.py

    r679 r683  
    1212__version__ = Release.version 
    1313 
     14import StringIO 
     15 
    1416import wx 
    1517from wx import stc 
     18 
     19from nbshell.SimpleXMLWriter import XMLWriter 
    1620 
    1721def GetPluginFactory(): 
     
    4246    #    return "encoded" #Probably only the python code plugin should be raw 
    4347         
    44     def CreateDocumentPlugin(self,document, element): 
     48    def CreateDocumentPlugin(self,document): 
    4549        """Creates the document part of the plugin. The returned object is  
    4650        stored in ipgDocument.celllist and is responsible for storing and 
    4751        serialization of data. "data" contains initial data for the plugin. 
    4852        """ 
    49         return PlainTextDocumentPlugin(document, element
     53        return PlainTextDocumentPlugin(document
    5054     
    5155    def CreateViewPlugin(self,docplugin, view): 
     
    6872 
    6973class PlainTextDocumentPlugin(object): 
    70     def __init__(self, document, element): 
     74    def __init__(self, document): 
    7175        """Initialization. If element is <sheet> then the text is 
    7276        element.text. If the element is something else, then the text is 
     
    7579        self.document = document 
    7680        self.sheet = document.sheet 
    77         self.element = element 
    78         if element.tag == 'sheet': 
    79             self.start = True #self.start stores if the text block is the first in the sheet 
    80         else: 
    81             self.start = False 
     81        self.text = '' 
    8282        self.index = None   #Set by AddCell, InsertCell, DeleteCell 
    8383        self.view = None    #This plugin is designed for a single view. For 
    8484                            #multiple views there should be some modifications 
    85         #self.LoadData(data) 
    86      
     85 
    8786    type='plaintext' 
    88  
     87     
     88    def LoadXML(self, iterator, prevlist, elemlist, endtaglist): 
     89        """The LoadXML method gets text representing a part of the xml tree. 
     90        The first element in this part is 'elemlist[-1]', the next are 
     91        retrieved by 'iterator'. 'prevlist' and 'elemlist' are the previous two 
     92        results of calling 'iterator.next()'. The LoadXML method stops when the 
     93        'iterator' throws a 'StopIteration' exception, or it finds an element for 
     94        which there is a plugin to process it. Currently there are such 
     95        plugins for the <ipython-block> and <ipython-figure> elements. In that 
     96        case it returns the last 'elemlist'. The tags on which LoadXML 
     97        should exit are in 'endtaglist'""" 
     98         
     99        text = StringIO.StringIO() 
     100        #TODO: what encoding should I choose? 
     101        writer = XMLWriter(text, encoding='utf-8') 
     102 
     103        l1 = len(prevlist) 
     104        i = l1-1 
     105        l2 = len(elemlist) 
     106        while i>=0 and (i>= l2 or prevlist[i] != elemlist[i]): 
     107            writer.end(prevlist[i].tag) 
     108            writer.data(prevlist[i].tail or '') 
     109            i-=1 
     110 
     111        while elemlist != () and elemlist[-1].tag not in endtaglist: 
     112            prevlist = elemlist 
     113            writer.start(elemlist[-1].tag, elemlist[-1].attrib) 
     114            writer.data(elemlist[-1].text or '') 
     115            try: 
     116                elemlist = iterator.next() 
     117            except StopIteration: 
     118                elemlist = () 
     119            l1 = len(prevlist) 
     120            i = l1-1 
     121            l2 = len(elemlist) 
     122            while i>=0 and (i>= l2 or prevlist[i] != elemlist[i]): 
     123                writer.end(prevlist[i].tag) 
     124                writer.data(prevlist[i].tail or '') 
     125                i-=1 
     126             
     127        writer.flush() 
     128        self.text = text.getvalue() 
     129        if elemlist == (): 
     130            raise StopIteration 
     131        else: #elemlist[-1].tag in endtaglist: 
     132            return elemlist 
     133         
     134         
    89135    def __len__(self): 
    90136        return self.view.window.GetLenght() 
     
    96142    modified = property(fget = IsModified) 
    97143 
    98     def GetText(self): 
    99         text = self.element.text 
    100         if self.start: 
    101             text = self.element.text 
    102         else: 
    103             text = self.element.tail 
    104         return (text is not None) and text or '' 
    105  
    106     def SetText(self, text): 
    107         """Sets the text in the document""" 
    108         if self.start: 
    109             self.element.text = text 
    110         else: 
    111             self.element.tail = text 
    112         return text 
     144    #def GetText(self): 
     145    #    text = self.element.text 
     146    #    if self.start: 
     147    #        text = self.element.text 
     148    #    else: 
     149    #        text = self.element.tail 
     150    #    return (text is not None) and text or '' 
     151 
     152    #def SetText(self, text): 
     153    #    """Sets the text in the document""" 
     154    #    if self.start: 
     155    #        self.element.text = text 
     156    #    else: 
     157    #        self.element.tail = text 
     158    #    return text 
    113159     
    114160    def Clear(self): 
    115161        """Clears all data""" 
    116         self.SetText('') 
     162        self.text = '' 
    117163        if self.view is not None: 
    118164            self.view.Update() 
    119165 
    120     text = property(GetText, SetText, Clear, doc = """The text contained in this instance""") 
     166    #text = property(GetText, SetText, Clear, doc = """The text contained in this instance""") 
    121167     
    122168    def GetFactory(self): 
     
    125171    def Split(self, pos, update = True): 
    126172        """Removes the text after the given position and returns it""" 
    127         text = self.GetText() 
    128         self.SetText(text[:pos]) 
     173        text = self.text 
     174        self.text = text[:pos] 
    129175        if update: 
    130176            self.view.Update() 
     
    232278    def InsertCode(self): 
    233279        #lazy document update. We update the document when it is needed 
    234         self.doc.SetText(self.window.GetText()
     280        self.doc.text = self.window.GetText(
    235281        pos = self.window.GetCurrentPos() 
    236282        self.doc.sheet.InsertCode(self.doc,pos, update = True) 
  • nbshell/trunk/nbshell/Sheet.py

    r679 r683  
    1919 
    2020from nbshell import PythonPlugin 
    21 from nbshell.utils import getindex 
     21from nbshell.utils import getindex, getiterator2 
    2222 
    2323 
     
    8484            #This is the first or the last text cell and cannot be deleted. 
    8585            #Just clear the text. 
    86             cell.SetText('') 
     86            cell.text= '' 
    8787            if update: 
    8888                cell.view.Update() 
     
    151151        for doccell in self.celllist: 
    152152            doccell.view.UpdateDoc() 
     153             
     154     
     155    def __append_plaintext_cell(self, iterator, prevlist, elemlist,\ 
     156                                endtaglist, update = True): 
     157        """Append a plaintext cell at the end of the document. Plaintext cells 
     158        are cells which deal with yet unsupported parts of the notebook 
     159        format. They simply display the xml for these parts. The supported 
     160        tags are in endtaglist""" 
     161        factory = self.factory['plaintext'] 
     162        cell = factory.CreateDocumentPlugin(self.doc) 
     163        try: 
     164            elemlist = cell.LoadXML(iterator, prevlist, elemlist,\ 
     165                                    endtaglist) 
     166        finally: 
     167            if cell.text != '': 
     168                view = factory.CreateViewPlugin(cell, self.view) 
     169                self.__add_cell(cell) 
     170                if update: 
     171                    view.Update() 
     172                    self.view.Update() 
     173        return elemlist 
     174 
    153175 
    154176    def __update_celllist(self, update = False): 
    155177        """Updates the celllist from self.element and updates the view if 
    156178        update == True """ 
    157         if self.element.text is not None: 
    158             self.InsertCell('plaintext',update=False,element = self.element) 
    159         l = len(self.element) 
    160         for i in range(l): 
    161             elem = self.element[i] 
    162             if elem.tag == 'ipython-block': 
    163                 self.InsertCell('python', update=False, ipython_block = elem) 
    164             elif elem.tag == 'ipython-figure': 
    165                 self.InsertCell('figure', update = False, element = elem) 
    166             self.InsertCell('plaintext', update=False, element = elem) 
     179         
     180        iter = getiterator2(self.element) 
     181        elemlist = iter.next() 
     182        tag2type = {'ipython-block':'python', 'ipython-figure':'figure'} 
     183        flag = False 
     184        while True: 
     185            try: 
     186                elem = elemlist[-1] 
     187                if elem.tag in tag2type.keys(): 
     188                    self.InsertCell(tag2type[elem.tag], update = False, \ 
     189                                    ipython_block = elem) 
     190                    l = len(elemlist) 
     191                    prevlist = elemlist[:-1] 
     192                    while len(elemlist)>=l and elemlist[l-1] == elem: 
     193                        flag = True 
     194                        elemlist = iter.next() 
     195                    flag = False 
     196                    elemlist = self.__append_plaintext_cell(iter, prevlist,\ 
     197                                elemlist, update = False,\ 
     198                                endtaglist = tuple(tag2type.keys())) 
     199                else: 
     200                    #plaintext cells will deal with all the elements we have not 
     201                    #dealt with yet 
     202                    prevlist = () 
     203                    flag = False 
     204                    elemlist = self.__append_plaintext_cell(iter, prevlist,\ 
     205                                elemlist, update = False,\ 
     206                                endtaglist = ('ipython-block', 'ipython-figure')) 
     207            except StopIteration: 
     208                #Add any remaining tags 
     209                if flag: 
     210                    try: 
     211                        self.__append_plaintext_cell(iter, 
     212                            prevlist,(), update = False,\ 
     213                            endtaglist = ('ipython-block', 'ipython-figure')) 
     214                    except StopIteration: 
     215                        pass 
     216                break 
    167217        if update: 
    168218            self.view.Update() 
     219 
    169220         
    170221    def __clear_celllist(self, update = False): 
     
    306357        #We only need to set self.last 
    307358        self.element = self.notebook.default_sheet() 
    308         self.element.text = \ 
    309 """ This is a temporary message, until I write proper help. Please use Return 
    310 to insert new lines and Shift-Return to execute inputs
    311 """ 
    312          
     359        textelem = etree.Element('para') 
     360        textelem.text =\ 
     361""" This is a temporary message, until I write proper help
     362Please use Return to insert new lines and Shift-Return to execute inputs. """ 
     363        self.element[0:0] = [textelem] 
    313364        # Now remove the old sheet and replace it with the new one 
    314365        oldsheet = self.notebook.root.find('sheet') 
     
    320371        #etree.dump(self.notebook.root) #dbg 
    321372        #Here we do not call InsertElement, so we must update the dictionaries 
     373        etree.dump(self.notebook.root) 
    322374        self.Update(update, celllist = True, dicts = True) 
    323375         
  • nbshell/trunk/nbshell/ipnDocument.py

    r679 r683  
    2020from nbshell import IPythonLog 
    2121from nbshell import Sheet 
     22from nbshell import utils 
    2223 
    2324class ipnDocument(object): 
  • nbshell/trunk/nbshell/utils.py

    r667 r683  
    4141            return i 
    4242    return None 
     43 
     44def getiterator2(root): 
     45    """Returns an iterator which for each subelement yields a tuple of all 
     46    elements in the path from the root to the given subelement""" 
     47     
     48    root_tup = (root,) 
     49    yield root_tup 
     50    #Recursive generators, yummy :) 
     51    for subelement in root: 
     52        iter = getiterator2(subelement) 
     53        for result in iter: 
     54            yield root_tup + result