Process a file of rest and yield dictionaries

def generate_records(self, infile):
        """ Process a file of rest and yield dictionaries """

        state = 0
        record = {}

        for item in self.generate_lines(infile):

            line = item['line']
            heading = item['heading']
            
            # any Markdown heading is just a caption, no image
            if heading:
                record['heading'] = True
                record['caption'] = line[1:].strip()

                state = 'caption'
                continue

            if not line[0].isspace():
                # at a potential image
                if state == 'caption':
                    yield record
                    record = {}
                    state = 0

            if state == 'caption':
                record['caption'] += '\n' + line[:-1]
                continue

            fields = line.split(',')

            # nothing there, carry on
            if not fields: continue

            image = fields[0].strip()

            if not image: continue

            record['image'] = image

            try:
                time = float(fields[1])
            except:
                time = 0

            record['time'] = time

            try:
                caption = fields[2].strip()
            except:
                caption = None

            if caption:
                record['caption'] = caption

            # yield it if we have anything
            if record:
                yield record
                record = {}