Next / Previous / Contents / Shipman's homepage

20.6. InputFile.__sliceLine(): Cut an input line into output lines

listext2
# - - -   I n p u t F i l e . _ _ s l i c e L i n e

    def __sliceLine(self, line, goodBreak):
        '''Chop an input line into output-sized pieces.

          [ line is a string ->
              self.__blockList  +:=  (a new FirstBlock instance with
                  goodBreak=(goodBreak) made from line up to size
                  self.bodyLayout.charsPerCol) + (zero or more
                  OverflowBlock instances representing the remainder
                  of line sliced into pieces of size
                  self.bodyLayout.charsPerOver) ]
        '''

First we check to see if the line overflows.

listext2
        #-- 1
        firstCut = self.bodyLayout.charsPerCol
        overCut = self.bodyLayout.charsPerOver

        #-- 2
        # [ if len(line) <= firstCut ->
        #       firstOut  :=  line
        #       overflow  :=  ''
        #   else ->
        #       firstOut  :=  initial (firstCut) characters of line
        #       overflow  :=  balance of line ]
        if len(line) <= firstCut:
            firstOut = line; overflow = ''
        else:
            firstOut = line[:firstCut]
            overflow = line[firstCut:]

Now we are ready to turn the pieces of the input line into OutBlock instances: one FirstBlock for the first or only line, with OverflowBlock instances as needed. The goodBreak flag, True for lines that start with the break string, is applied only to the first block.

listext2
        #-- 2
        # [ self.__blockList  +:=  a new FirstBlock made from
        #       firstOut, with goodBreak=(goodBreak) ]
        self.__blockList.append(FirstBlock(firstOut,
            goodBreak, self.bodyLayout))

Any remaining text is sliced into pieces of size overCut.

listext2
        #-- 3
        # [ self.__blockList  +:=  OverflowBlock instances containing
        #       the text from overflow cut into pieces no longer than
        #       overCut ]
        while len(overflow) > 0:
            #-- 3 body
            # [ self.__blockList  +:=  an OverflowBlock containing
            #       the first (overCut) characters of overflow, or
            #       all of overflow, whichever is shorter
            #   overflow  :=  overflow with the first (overCut)
            #       characters removed, or all of it, whichever
            #       is shorter ]
            self.__blockList.append(OverflowBlock(overflow[:overCut],
                self.bodyLayout))
            overflow = overflow[overCut:]