def _safe_wrap_in_html_tag(content, tag_name):
    """
    Adds an html tag around content but handles new line characters
    to prevent incorrectly nested tags later
    :param content: The content to wrap in the tag
    :param tag_name: The name of the tag to use e.g. 'em', 'strong'
    :return: the HTML tagged string
    """
    raw_lines = content.split('\n')
    tagged_lines = []
    for line in raw_lines:
        if line.strip():
            tagged_line = '<{tag_name}>{line}</{tag_name}>'.format(tag_name=tag_name, line=line)
            tagged_lines.append(tagged_line)
        else:
            tagged_lines.append(line)

    return '\n'.join(tagged_lines)