import re


def cell_factory(selection, cellType, prefix='', attrs=None):
    """Return a series of xml v2-Cells as a list of strings

    Args:
        selection (list): Lines of text to be processed
        cellType  (string): The name of the v2-Cell. e.g. `row/col`
    Kwargs:
        prefix (string): Text to be prefixed to the
        v2-Cell's label e.g. `r1/c1`
    Return:
        list. List of strings making up the formatted v2-Cells
    """
    cellTemplate = ('  <%(cellType)s label="%(label)s">'
                    '%(cell)s'
                    '</%(cellType)s>')

    attrs = {} if attrs is None else attrs
    selection = [line.strip() for line in selection if line.strip()]
    label_rgx = re.compile("^\[?([a-zA-Z0-9_]{1,6})\]?\.\s{1,}")

    cells = []
    for i, cell in enumerate(selection):
        label = label_rgx.search(cell)
        if label:
            label = label.groups()[0]
            cell = label_rgx.sub('', cell)
            if label[0].isdigit():
                label = prefix + label
        else:
            label = prefix + str(i + 1)
        cells.append(cellTemplate %
                     dict(cellType=cellType, label=label, cell=cell))

    attrs_str = " ".join('%s="%s"' % (k, v) for k, v in attrs.items())

    if attrs_str:
        for i, cell in enumerate(cells):
            cells[i] = cell.replace('">', '" ' + attrs_str + ">", 1)

    return cells