src/reportlab/lib/yaml.py
branchpy33
changeset 3794 398ea04239b5
parent 3721 0c93dd8ff567
child 4252 fe660f227cac
equal deleted inserted replaced
3793:cc3f9cc828f7 3794:398ea04239b5
    37 dot endPre
    37 dot endPre
    38 - ends a preformatted object.
    38 - ends a preformatted object.
    39 """
    39 """
    40 __version__=''' $Id$ '''
    40 __version__=''' $Id$ '''
    41 
    41 
    42 
       
    43 import sys
    42 import sys
    44 import string
       
    45 
    43 
    46 #modes:
    44 #modes:
    47 PLAIN = 1
    45 PLAIN = 1
    48 PREFORMATTED = 2
    46 PREFORMATTED = 2
    49 
    47 
    76         self.endPara()
    74         self.endPara()
    77         return self._results
    75         return self._results
    78 
    76 
    79     def parseText(self, textBlock):
    77     def parseText(self, textBlock):
    80         "Parses the a possible multi-line text block"
    78         "Parses the a possible multi-line text block"
    81         lines = string.split(textBlock, '\n')
    79         lines = textBlock.split('\n')
    82         for line in lines:
    80         for line in lines:
    83             self.readLine(line)
    81             self.readLine(line)
    84         self.endPara()
    82         self.endPara()
    85         return self._results
    83         return self._results
    86 
    84 
    87     def readLine(self, line):
    85     def readLine(self, line):
    88         #this is the inner loop
    86         #this is the inner loop
    89         self._lineNo = self._lineNo + 1
    87         self._lineNo = self._lineNo + 1
    90         stripped = string.lstrip(line)
    88         stripped = line.lstrip()
    91         if len(stripped) == 0:
    89         if len(stripped) == 0:
    92             if self._mode == PLAIN:
    90             if self._mode == PLAIN:
    93                 self.endPara()
    91                 self.endPara()
    94             else:  #preformatted, append it
    92             else:  #preformatted, append it
    95                 self._buf.append(line)
    93                 self._buf.append(line)
    96         elif line[0]=='.':
    94         elif line[0]=='.':
    97             # we have a command of some kind
    95             # we have a command of some kind
    98             self.endPara()
    96             self.endPara()
    99             words = string.split(stripped[1:])
    97             words = stripped[1:].split()
   100             cmd, args = words[0], words[1:]
    98             cmd, args = words[0], words[1:]
   101 
    99 
   102             #is it a parser method?
   100             #is it a parser method?
   103             if hasattr(self.__class__, cmd):
   101             if hasattr(self.__class__, cmd):
   104                 #this was very bad; any type error in the method was hidden
   102                 #this was very bad; any type error in the method was hidden
   110                     raise
   108                     raise
   111             else:
   109             else:
   112                 # assume it is a paragraph style -
   110                 # assume it is a paragraph style -
   113                 # becomes the formatter's problem
   111                 # becomes the formatter's problem
   114                 self.endPara()  #end the last one
   112                 self.endPara()  #end the last one
   115                 words = string.split(stripped, ' ', 1)
   113                 words = stripped.split(' ', 1)
   116                 assert len(words)==2, "Style %s but no data at line %d" % (words[0], self._lineNo)
   114                 assert len(words)==2, "Style %s but no data at line %d" % (words[0], self._lineNo)
   117                 (styletag, data) = words
   115                 (styletag, data) = words
   118                 self._style = styletag[1:]
   116                 self._style = styletag[1:]
   119                 self._buf.append(data)
   117                 self._buf.append(data)
   120         else:
   118         else:
   122             self._buf.append(line)
   120             self._buf.append(line)
   123 
   121 
   124     def endPara(self):
   122     def endPara(self):
   125         #ends the current paragraph, or preformatted block
   123         #ends the current paragraph, or preformatted block
   126 
   124 
   127         text = string.join(self._buf, ' ')
   125         text = ' '.join(self._buf)
   128         if text:
   126         if text:
   129             if self._mode == PREFORMATTED:
   127             if self._mode == PREFORMATTED:
   130                 #item 3 is list of lines
   128                 #item 3 is list of lines
   131                 self._results.append(('PREFORMATTED', self._style,
   129                 self._results.append(('PREFORMATTED', self._style,
   132                                  string.join(self._buf,'\n')))
   130                                  '\n'.join(self._buf)))
   133             else:
   131             else:
   134                 self._results.append(('PARAGRAPH', self._style, text))
   132                 self._results.append(('PARAGRAPH', self._style, text))
   135         self._buf = []
   133         self._buf = []
   136         self._style = 'Normal'
   134         self._style = 'Normal'
   137 
   135