import re


def printLabel(xStart, yStart, labels, printLabels, lineNumber,
               printNodeNumbers, commands):
    """ Handles printing the lables.

    xStart -- The starting x coordinate
    yStart -- The starting y coordinate
    labels -- the labels of the node
    printLabels -- true prints the labels, false does not.
    lineNumber -- the current line number.
    printNodeNumbers -- true prints the node numbers, false does not.
    commands -- If not None, print the commands.

    """

    if not printLabels and not printNodeNumbers and (commands is None):
        return ""

    labelString = ""

    # gather together the information to print.
    info = []
    if printNodeNumbers:
        labelString += str(lineNumber) + ': '

    if printLabels:
        info.extend(labels)

    if commands is not None:
        info.append(commands[lineNumber]['cmd'])

    delimiterString = ', '
    for labelEntry in info:
        labelString += labelEntry + delimiterString

    # Before adding the labelString replace invalide html strings.
    labelString = re.sub('&', '&amp;', labelString)
    labelString = re.sub('<', '&lt;', labelString)
    labelString = re.sub('>', '&gt;', labelString)

    labelString = labelString[:-len(delimiterString)]

    # Define the tag
    preTag = '<text x="{}" y="{}" fill="black">'.format(xStart, yStart)
    postTag ='</text>\n'

    return preTag + labelString + postTag