Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple text to HTML conversion

Tags:

html

c#

parsing

I have a very simple asp:textbox with the multiline attribute enabled. I then accept just text, with no markup, from the textbox. Is there a common method by which line breaks and returns can be converted to <p> and <br/> tags?

I'm not looking for anything earth shattering, but at the same time I don't just want to do something like:

html.Insert(0, "<p>");
html.Replace(Enviroment.NewLine + Enviroment.NewLine, "</p><p>");
html.Replace(Enviroment.NewLine, "<br/>");
html.Append("</p>");

The above code doesn't work right, as in generating correct html, if there are more than 2 line breaks in a row. Having html like <br/></p><p> is not good; the <br/> can be removed.

like image 386
Mark Avatar asked Oct 21 '10 20:10

Mark


1 Answers

I know this is old, but I couldn't find anything better after some searching, so here is what I'm using:

public static string TextToHtml(string text)
{
    text = HttpUtility.HtmlEncode(text);
    text = text.Replace("\r\n", "\r");
    text = text.Replace("\n", "\r");
    text = text.Replace("\r", "<br>\r\n");
    text = text.Replace("  ", " &nbsp;");
    return text;
}

If you can't use HttpUtility for some reason, then you'll have to do the HTML encoding some other way, and there are lots of minor details to worry about (not just <>&).

HtmlEncode only handles the special characters for you, so after that I convert any combo of carriage-return and/or line-feed to a BR tag, and any double-spaces to a single-space plus a NBSP.

Optionally you could use a PRE tag for the last part, like so:

public static string TextToHtml(string text)
{
    text = "<pre>" + HttpUtility.HtmlEncode(text) + "</pre>";
    return text;
}
like image 94
eselk Avatar answered Sep 22 '22 11:09

eselk