import re


def to_yw7(text):
    """Convert html tags to yWriter 6/7 raw markup. 
    Return a yw6/7 markup string.
    """

    # Clean up polluted HTML code.

    text = re.sub('</*font.*?>', '', text)
    text = re.sub('</*span.*?>', '', text)
    text = re.sub('</*FONT.*?>', '', text)
    text = re.sub('</*SPAN.*?>', '', text)

    # Put everything in one line.

    text = text.replace('\n', ' ')
    text = text.replace('\r', ' ')
    text = text.replace('\t', ' ')

    while '  ' in text:
        text = text.replace('  ', ' ').rstrip().lstrip()

    # Replace HTML tags by yWriter markup.

    text = text.replace('<i>', '[i]')
    text = text.replace('<I>', '[i]')
    text = text.replace('</i>', '[/i]')
    text = text.replace('</I>', '[/i]')
    text = text.replace('</em>', '[/i]')
    text = text.replace('</EM>', '[/i]')
    text = text.replace('<b>', '[b]')
    text = text.replace('<B>', '[b]')
    text = text.replace('</b>', '[/b]')
    text = text.replace('</B>', '[/b]')
    text = text.replace('</strong>', '[/b]')
    text = text.replace('</STRONG>', '[/b]')
    text = re.sub('<em.*?>', '[i]', text)
    text = re.sub('<EM.*?>', '[i]', text)
    text = re.sub('<strong.*?>', '[b]', text)
    text = re.sub('<STRONG.*?>', '[b]', text)

    # Remove orphaned tags.

    text = text.replace('[/b][b]', '')
    text = text.replace('[/i][i]', '')
    text = text.replace('[/b][b]', '')

    return text