HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpMinor

Convert plain text to HTML C#

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
converttexthtmlplain

Problem

I have the ToHtml() Extension Method

```
public static class StringMethodExtensions
{
private static string _paraBreak = "\r\n\r\n";
private static string _link = "{1}";
private static string _linkNoFollow = "{1}";

///
/// Returns a copy of this string converted to HTML markup.
///
public static string ToHtml(this string s)
{
return ToHtml(s, false);
}

///
/// Returns a copy of this string converted to HTML markup.
///
/// If true, links are given "nofollow"
/// attribute
public static string ToHtml(this string s, bool nofollow)
{
StringBuilder sb = new StringBuilder();

int pos = 0;
while (pos 0)
EncodeParagraph(para, sb, nofollow);

// Skip over paragraph break
pos += _paraBreak.Length;
}
// Return result
return sb.ToString();
}

///
/// Encodes a single paragraph to HTML.
///
/// Text to encode
/// StringBuilder to write results
/// If true, links are given "nofollow"
/// attribute
private static void EncodeParagraph(string s, StringBuilder sb, bool nofollow)
{
// Start new paragraph
sb.AppendLine("");

// HTML encode text
s = HttpUtility.HtmlEncode(s);

// Convert single newlines to
s = s.Replace(Environment.NewLine, "\r\n");

// Encode any hyperlinks
EncodeLinks(s, sb, nofollow);

// Close paragraph
sb.AppendLine("\r\n");
}

///
/// Encodes [[URL]] and [[Text][URL]] links to HTML.
///
/// Text to encode
/// StringBuilder to write results
/// If true, links are given "nofollow"
/// attribute
private static void EncodeLinks(string s, StringBuilder sb, bool nofollow)
{
// Parse and encode any hyperlinks
int pos = 0;
while (pos = 0)
{
link = label.Substring(i + 2);

Solution

public static string ToHtml(this string s, bool nofollow)
{
    s = HttpUtility.HtmlEncode(s);
    string[] paragraphs = s.Split(new string[]{"\r\n\r\n"}, StringSplitOptions.None);
    StringBuilder sb = new StringBuilder();
    foreach (string par in paragraphs) {
        sb.AppendLine("");
        string p = par.Replace(Environment.NewLine, "\r\n");
        if (nofollow) {
            p = Regex.Replace(p, @"\[\[(.+)\]\[(.+)\]\]", "$1");
            p = Regex.Replace(p, @"\[\[(.+)\]\]", "$1");
        } else {
            p = Regex.Replace(p, @"\[\[(.+)\]\[(.+)\]\]", "$1");
            p = Regex.Replace(p, @"\[\[(.+)\]\]", "$1");
            sb.AppendLine(p);
        }
        sb.AppendLine("");
    }
    return sb.ToString();
}


In general, if you find yourself with a variable that you pass around through a bunch of static methods (like nofollow), it may be time to make those methods not static.

Code Snippets

public static string ToHtml(this string s, bool nofollow)
{
    s = HttpUtility.HtmlEncode(s);
    string[] paragraphs = s.Split(new string[]{"\r\n\r\n"}, StringSplitOptions.None);
    StringBuilder sb = new StringBuilder();
    foreach (string par in paragraphs) {
        sb.AppendLine("<p>");
        string p = par.Replace(Environment.NewLine, "<br />\r\n");
        if (nofollow) {
            p = Regex.Replace(p, @"\[\[(.+)\]\[(.+)\]\]", "<a href=\"$2\" rel=\"nofollow\">$1</a>");
            p = Regex.Replace(p, @"\[\[(.+)\]\]", "<a href=\"$1\" rel=\"nofollow\">$1</a>");
        } else {
            p = Regex.Replace(p, @"\[\[(.+)\]\[(.+)\]\]", "<a href=\"$2\">$1</a>");
            p = Regex.Replace(p, @"\[\[(.+)\]\]", "<a href=\"$1\">$1</a>");
            sb.AppendLine(p);
        }
        sb.AppendLine("</p>");
    }
    return sb.ToString();
}

Context

StackExchange Code Review Q#70751, answer score: 2

Revisions (0)

No revisions yet.