vote buttons
8
08 Mar 2015 by Super Human

Convert url in text to hyperlink with option to add nofollow attribute

Find all Urls in plain text and replace them with HTML hyper links. This should allow adding a nofollow attribute, and an option of link opening in a new window.
Contract
IStringUtilities
string ConvertPlainTextUrlsToHtmlHyperlinks(string input, HyperlinkOptions options = HyperlinkOptions.None);
Models
public enum HyperlinkOptions { None = 0, OpenInNewWindow = 1, NoFollow = 2 }
Test Cases
vote buttons
1
Created By Super Human
public void NoOptions_ShouldParseLinkWithoutAdditionalAttributes(IStringUtilities util) { string test = @"This is a test for problem definition at http://www.volatileread.com/UtilityLibrary/EditDefinition?id=2096"; var result = util.ConvertPlainTextUrlsToHtmlHyperlinks(test, HyperlinkOptions.None); var doc = new XmlDocument(); doc.LoadXml(result.Replace("This is a test for problem definition at ", String.Empty)); Assert.AreEqual("a", doc.FirstChild.Name); Assert.AreEqual("http://www.volatileread.com/UtilityLibrary/EditDefinition?id=2096", doc.FirstChild.Attributes["href"].Value); Assert.AreEqual(null, doc.FirstChild.Attributes["rel"]); Assert.AreEqual(null, doc.FirstChild.Attributes["target"]); Assert.AreEqual("http://www.volatileread.com/UtilityLibrary/EditDefinition?id=2096", doc.FirstChild.InnerText); }
vote buttons
1
Created By Super Human
public void NoFollowOption_ShouldParseLinkWithNoFollowAttribute(IStringUtilities util) { string test = @"This is a test for problem definition at http://www.volatileread.com/UtilityLibrary/EditDefinition?id=2096"; var result = util.ConvertPlainTextUrlsToHtmlHyperlinks(test, HyperlinkOptions.NoFollow); var doc = new XmlDocument(); doc.LoadXml(result.Replace("This is a test for problem definition at ", String.Empty)); Assert.AreEqual("a", doc.FirstChild.Name); Assert.AreEqual("http://www.volatileread.com/UtilityLibrary/EditDefinition?id=2096", doc.FirstChild.Attributes["href"].Value); Assert.AreEqual("nofollow", doc.FirstChild.Attributes["rel"].Value); Assert.AreEqual(null, doc.FirstChild.Attributes["target"]); Assert.AreEqual("http://www.volatileread.com/UtilityLibrary/EditDefinition?id=2096", doc.FirstChild.InnerText); }
vote buttons
1
Created By Super Human
public void NewWindowOption_ShouldParseLinkWithBlankTargetAttribute(IStringUtilities util) { string test = @"This is a test for problem definition at http://www.volatileread.com/UtilityLibrary/EditDefinition?id=2096"; var result = util.ConvertPlainTextUrlsToHtmlHyperlinks(test, HyperlinkOptions.OpenInNewWindow); var doc = new XmlDocument(); doc.LoadXml(result.Replace("This is a test for problem definition at ", String.Empty)); Assert.AreEqual("a", doc.FirstChild.Name); Assert.AreEqual("http://www.volatileread.com/UtilityLibrary/EditDefinition?id=2096", doc.FirstChild.Attributes["href"].Value); Assert.AreEqual(null, doc.FirstChild.Attributes["rel"]); Assert.AreEqual("_blank", doc.FirstChild.Attributes["target"].Value); Assert.AreEqual("http://www.volatileread.com/UtilityLibrary/EditDefinition?id=2096", doc.FirstChild.InnerText); }
vote buttons
1
Created By Super Human
public void NewWindowAndNoFollow_ShouldParseLinkWithTargetAndRelAttributes(IStringUtilities util) { string test = @"This is a test for problem definition at http://www.volatileread.com/UtilityLibrary/EditDefinition?id=2096"; var result = util.ConvertPlainTextUrlsToHtmlHyperlinks(test, HyperlinkOptions.OpenInNewWindow | HyperlinkOptions.NoFollow); var doc = new XmlDocument(); doc.LoadXml(result.Replace("This is a test for problem definition at ", String.Empty)); Assert.AreEqual("a", doc.FirstChild.Name); Assert.AreEqual("http://www.volatileread.com/UtilityLibrary/EditDefinition?id=2096", doc.FirstChild.Attributes["href"].Value); Assert.AreEqual("nofollow", doc.FirstChild.Attributes["rel"].Value); Assert.AreEqual("_blank", doc.FirstChild.Attributes["target"].Value); Assert.AreEqual("http://www.volatileread.com/UtilityLibrary/EditDefinition?id=2096", doc.FirstChild.InnerText); }
vote buttons
1
You must be logged in to add test cases

2 Solutions

vote buttons
5
0.53
avg time in ms
08 Mar 2015 by Super Human
public class Solution : IStringUtilities { public string ConvertPlainTextUrlsToHtmlHyperlinks(string input, HyperlinkOptions options = HyperlinkOptions.None) { Regex r = new Regex("(https?://[^ ]+)"); var target = (options & HyperlinkOptions.OpenInNewWindow) == HyperlinkOptions.OpenInNewWindow ? " target='_blank'" : String.Empty; var noFollow = (options & HyperlinkOptions.NoFollow) == HyperlinkOptions.NoFollow ? " rel='nofollow'" : String.Empty; return r.Replace(input, String.Format("<a href=\"$1\"{0}{1}>$1</a>", target, noFollow)); } }
Test Case Run Summary
NoOptions_ShouldParseLinkWithoutAdditionalAttributes
NoFollowOption_ShouldParseLinkWithNoFollowAttribute
NewWindowOption_ShouldParseLinkWithBlankTargetAttribute
NewWindowAndNoFollow_ShouldParseLinkWithTargetAndRelAttributes
ShouldParseHttpsLink
download solution
Try this Solution
Comments
vote buttons
5
0.70
avg time in ms
08 Mar 2015 by Super Human
public class Solution : IStringUtilities { public string ConvertPlainTextUrlsToHtmlHyperlinks(string input, HyperlinkOptions options = HyperlinkOptions.None) { var regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"; Regex r = new Regex(regex, RegexOptions.IgnoreCase); var target = (options & HyperlinkOptions.OpenInNewWindow) == HyperlinkOptions.OpenInNewWindow ? " target='_blank'" : String.Empty; var noFollow = (options & HyperlinkOptions.NoFollow) == HyperlinkOptions.NoFollow ? " rel='nofollow'" : String.Empty; return r.Replace(input, String.Format("<a href=\"$1\"{0}{1}>$1</a>",target, noFollow)); } }
Test Case Run Summary
NoOptions_ShouldParseLinkWithoutAdditionalAttributes
NoFollowOption_ShouldParseLinkWithNoFollowAttribute
NewWindowOption_ShouldParseLinkWithBlankTargetAttribute
NewWindowAndNoFollow_ShouldParseLinkWithTargetAndRelAttributes
ShouldParseHttpsLink
download solution
Try this Solution
Comments