def prunecontainers(blocks, keep):
    """Prune unwanted containers.

    The blocks must have a 'type' field, i.e., they should have been
    run through findliteralblocks first.
    """
    pruned = []
    i = 0
    while i + 1 < len(blocks):
        if blocks[i][b'type'] == b'paragraph' and blocks[i][b'lines'][0
            ].startswith(b'.. container::'):
            indent = blocks[i][b'indent']
            adjustment = blocks[i + 1][b'indent'] - indent
            containertype = blocks[i][b'lines'][0][15:]
            prune = True
            for c in keep:
                if c in containertype.split(b'.'):
                    prune = False
            if prune:
                pruned.append(containertype)
            del blocks[i]
            j = i
            i -= 1
            while j < len(blocks) and blocks[j][b'indent'] > indent:
                if prune:
                    del blocks[j]
                else:
                    blocks[j][b'indent'] -= adjustment
                    j += 1
        i += 1
    return blocks, pruned
