home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2112 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.9 KB  |  98 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from odf.element import Node
  5. import odf.opendocument as odf
  6. from odf.text import S, LineBreak, Tab
  7.  
  8. class WhitespaceText(object):
  9.     
  10.     def __init__(self):
  11.         self.textBuffer = []
  12.         self.spaceCount = 0
  13.  
  14.     
  15.     def addTextToElement(self, odfElement, s):
  16.         i = 0
  17.         ch = ' '
  18.         while i < len(s):
  19.             ch = s[i]
  20.             if ch == '\t':
  21.                 self._emitTextBuffer(odfElement)
  22.                 odfElement.addElement(Tab())
  23.                 i += 1
  24.                 continue
  25.             if ch == '\n':
  26.                 self._emitTextBuffer(odfElement)
  27.                 odfElement.addElement(LineBreak())
  28.                 i += 1
  29.                 continue
  30.             if ch == ' ':
  31.                 self.textBuffer.append(' ')
  32.                 i += 1
  33.                 self.spaceCount = 0
  34.                 while i < len(s) and s[i] == ' ':
  35.                     self.spaceCount += 1
  36.                     i += 1
  37.                     continue
  38.                     self
  39.                 if self.spaceCount > 0:
  40.                     self._emitTextBuffer(odfElement)
  41.                     self._emitSpaces(odfElement)
  42.                 
  43.             self.spaceCount > 0
  44.             self.textBuffer.append(ch)
  45.             i += 1
  46.         self._emitTextBuffer(odfElement)
  47.  
  48.     
  49.     def _emitTextBuffer(self, odfElement):
  50.         if len(self.textBuffer) > 0:
  51.             odfElement.addText(''.join(self.textBuffer))
  52.         
  53.         self.textBuffer = []
  54.  
  55.     
  56.     def _emitSpaces(self, odfElement):
  57.         if self.spaceCount > 0:
  58.             spaceElement = S(c = self.spaceCount)
  59.             odfElement.addElement(spaceElement)
  60.         
  61.         self.spaceCount = 0
  62.  
  63.  
  64.  
  65. def addTextToElement(odfElement, s):
  66.     wst = WhitespaceText()
  67.     wst.addTextToElement(odfElement, s)
  68.  
  69.  
  70. def extractText(odfElement):
  71.     result = []
  72.     if len(odfElement.childNodes) != 0:
  73.         for child in odfElement.childNodes:
  74.             if child.nodeType == Node.TEXT_NODE:
  75.                 result.append(child.data)
  76.                 continue
  77.             if child.nodeType == Node.ELEMENT_NODE:
  78.                 subElement = child
  79.                 tagName = subElement.qname
  80.                 if tagName == (u'urn:oasis:names:tc:opendocument:xmlns:text:1.0', u'line-break'):
  81.                     result.append('\n')
  82.                 elif tagName == (u'urn:oasis:names:tc:opendocument:xmlns:text:1.0', u'tab'):
  83.                     result.append('\t')
  84.                 elif tagName == (u'urn:oasis:names:tc:opendocument:xmlns:text:1.0', u's'):
  85.                     c = subElement.getAttribute('c')
  86.                     if c:
  87.                         spaceCount = int(c)
  88.                     else:
  89.                         spaceCount = 1
  90.                     result.append(' ' * spaceCount)
  91.                 else:
  92.                     result.append(extractText(subElement))
  93.             tagName == (u'urn:oasis:names:tc:opendocument:xmlns:text:1.0', u'line-break')
  94.         
  95.     
  96.     return ''.join(result)
  97.  
  98.