public void TextNodeHtmlTemplate() { var parserNull = new BBCodeParser(ErrorMode.Strict, null, new[] { new BBTag("b", "<b>", "</b>", 1), }); var parserEmpty = new BBCodeParser(ErrorMode.Strict, "", new[] { new BBTag("b", "<b>", "</b>", 1), }); var parserDiv = new BBCodeParser(ErrorMode.Strict, "<div>${content}</div>", new[] { new BBTag("b", "<b>", "</b>", 1), }); Assert.Equal(@"", parserNull.ToHtml(@"")); Assert.Equal(@"abc", parserNull.ToHtml(@"abc")); Assert.Equal(@"abc<b>def</b>", parserNull.ToHtml(@"abc[b]def[/b]")); Assert.Equal(@"", parserEmpty.ToHtml(@"")); Assert.Equal(@"", parserEmpty.ToHtml(@"abc")); Assert.Equal(@"<b></b>", parserEmpty.ToHtml(@"abc[b]def[/b]")); Assert.Equal(@"", parserDiv.ToHtml(@"")); Assert.Equal(@"<div>abc</div>", parserDiv.ToHtml(@"abc")); Assert.Equal(@"<div>abc</div><b><div>def</div></b>", parserDiv.ToHtml(@"abc[b]def[/b]")); }
static void XmlGenerateReturnTypes(BBCodeParser bbRules, StringBuilder SB, CmdletObject cmdlet) { List <String> returnTypes = new List <String>(cmdlet.GeneralHelp.ReturnType.Split(new[] { ';' })); List <String> returnUrls = new List <String>(cmdlet.GeneralHelp.ReturnUrl.Split(new[] { ';' })); List <String> returnDescription = new List <String>(cmdlet.GeneralHelp.ReturnTypeDescription.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)); SB.Append(" <command:returnValues>" + n); for (Int32 index = 0; index < returnTypes.Count; index++) { SB.Append(" <command:returnValue>"+ n); SB.Append(" <dev:type>"+ n); SB.Append(" <maml:name>"+ bbRules.ToHtml(returnTypes[index], true) + "</maml:name>" + n); try { SB.Append(" <maml:uri>"+ bbRules.ToHtml(returnUrls[index], true) + "</maml:uri>" + n); } catch { SB.Append(" <maml:uri />"+ n); } SB.Append(" <maml:description/>"+ n); SB.Append(" </dev:type>"+ n); SB.Append(" <maml:description>"+ n); try { SB.Append(generateParagraphs(returnDescription[index], bbRules, 4)); } catch { SB.Append("<maml:para />" + n); } SB.Append(" </maml:description>"+ n); SB.Append(" </command:returnValue>"+ n); } SB.Append(" </command:returnValues>" + n); }
static void HtmlGenerateInputTypes(BBCodeParser rules, StringBuilder SB, CmdletObject cmdlet) { SB.Append("<h2><strong>Inputs</strong></h2>" + _nl); List <String> inputTypes = new List <String>(cmdlet.GeneralHelp.InputType.Split(';')); List <String> inputUrls = new List <String>(cmdlet.GeneralHelp.InputUrl.Split(';')); List <String> inputDescription = new List <String>(cmdlet.GeneralHelp.InputTypeDescription.Split(';')); for (Int32 index = 0; index < inputTypes.Count; index++) { if (index < inputUrls.Count) { if (String.IsNullOrEmpty(inputUrls[index])) { SB.Append("<p style=\"margin-left: 40px;\">" + rules.ToHtml(inputTypes[index]) + "</p>" + _nl); } else { SB.Append("<p style=\"margin-left: 40px;\"><a href=\"" + inputUrls[index] + "\">" + rules.ToHtml(inputTypes[index]) + "</a></p>" + _nl); } } else { SB.Append("<p style=\"margin-left: 40px;\">" + rules.ToHtml(inputTypes[index]) + "</p>" + _nl); } if (index < inputDescription.Count) { SB.Append("<p style=\"margin-left: 80px;\">" + rules.ToHtml(inputDescription[index]) + "</p>" + _nl); } } }
static void htmlGenerateReturnTypes(BBCodeParser rules, StringBuilder SB, CmdletObject cmdlet) { SB.Append("<h2>Outputs</h2>" + _nl); List <String> returnTypes = new List <String>(cmdlet.GeneralHelp.ReturnType.Split(';')); List <String> returnUrls = new List <String>(cmdlet.GeneralHelp.ReturnUrl.Split(';')); List <String> returnDescription = new List <String>(cmdlet.GeneralHelp.ReturnTypeDescription.Split(';')); for (Int32 index = 0; index < returnTypes.Count; index++) { if (index < returnUrls.Count) { SB.AppendLine(String.IsNullOrEmpty(returnUrls[index]) ? addIndentedParagraphText(rules.ToHtml(returnTypes[index])) : addIndentedParagraphText($"<a href=\"{returnUrls[index]}\">{rules.ToHtml(returnTypes[index])}</a>")); } else { SB.AppendLine(addIndentedParagraphText(rules.ToHtml(returnTypes[index]))); } if (index < returnDescription.Count) { SB.AppendLine($"<p style=\"margin-left: 80px;\">{rules.ToHtml(returnDescription[index])}</p>"); } } }
public void AttributeValueTransformer() { var parser = new BBCodeParser(ErrorMode.Strict, null, new[] { new BBTag("font", "<span style=\"${color}${font}\">", "</span>", true, true, new BBAttribute("color", "color", attributeRenderingContext => string.IsNullOrEmpty(attributeRenderingContext.AttributeValue) ? "" : "color:" + attributeRenderingContext.AttributeValue + ";"), new BBAttribute("font", "font", attributeRenderingContext => string.IsNullOrEmpty(attributeRenderingContext.AttributeValue) ? "" : "font-family:" + attributeRenderingContext.AttributeValue + ";")), }); Assert.Equal("<span style=\"color:red;font-family:Arial;\">abc</span>", parser.ToHtml("[font color=red font=Arial]abc[/font]")); Assert.Equal("<span style=\"color:red;\">abc</span>", parser.ToHtml("[font color=red]abc[/font]")); }
static void htmlGenerateExamples(BBCodeParser rules, StringBuilder SB, CmdletObject cmdlet) { SB.AppendLine("<h2>Examples</h2>"); for (Int32 index = 0; index < cmdlet.Examples.Count; index++) { Example example = cmdlet.Examples[index]; String name = String.IsNullOrEmpty(example.Name) ? $"Example {index + 1}" : example.Name; SB.AppendLine($"<h3>{SecurityElement.Escape(name)}</h3>"); if (!String.IsNullOrEmpty(example.Cmd)) { String cmd = !example.Cmd.StartsWith("PS C:\\>") ? $"PS C:\\> {example.Cmd}" : example.Cmd; SB.AppendLine($"<pre style=\"margin-left: 40px;\">{SecurityElement.Escape(cmd)}</pre>"); } if (!String.IsNullOrEmpty(example.Output)) { SB.AppendLine($"<pre style=\"margin-left: 40px;\">{SecurityElement.Escape(example.Output)}</pre>"); } if (!String.IsNullOrEmpty(example.Description)) { String str = rules.ToHtml(example.Description); SB.AppendLine(addIndentedParagraphText(str)); } } }
/// <summary> /// Translates a string from bbCode to http /// </summary> /// <param name="inString"></param> /// <returns></returns> internal string Transform(string inString, bool keepAllChars = false) { //Set the tags that we are interested in translating var parser = new BBCodeParser(new[] { // a newline is automatically parsed when \n is encountered. Hence [p] is not necessary. new BBTag("b", "<b>", "</b>"), new BBTag("i", "<i>", "</i>"), new BBTag("u", "<u>", "</u>"), new BBTag("url", "<a href=\"${href}\">", "</a>", new BBAttribute("href", ""), new BBAttribute("href", "href")), }); try { inString = parser.ToHtml(inString); if (inString.Contains("&#") && keepAllChars) { inString = BBcodeClean(inString); } } catch { throw new FormatException(); } return(inString); }
/// <summary> /// Find formatting tags in the text and transform them into static tags. /// </summary> /// <param name="text">The text to be transformed.</param> /// <param name="replyRepository">An instance of IReplyRepository.</param> /// <param name="isModerator">True if the current user is a moderator.</param> /// <returns>A formatted string.</returns> public static string SimplifyComplexTags(string text, IReplyRepository replyRepository, bool isModerator) { var parser = new BBCodeParser(new[] { new BBTag("quote", "", "", true, true, content => { string reformattedQuote = string.Format(@"[quote]{0}[/quote]", content); if (content.IsLong()) { Reply reply = replyRepository.GetReply(content.ToLong()); if (reply != null) { if (!reply.IsModsOnly() || isModerator) { var author = reply.Topic.Board.Anonymous ? "Anon" : reply.Username; reformattedQuote = string.Format("[quote]Posted by: [transmit=USER]{0}[/transmit] on {1}\n\n{2}[/quote]", author, reply.PostedDate, reply.Body); } } } return(reformattedQuote); }), }); return(parser.ToHtml(text, false)); }
static void HtmlGenerateExamples(BBCodeParser rules, StringBuilder SB, CmdletObject cmdlet) { SB.Append("<h2><strong>Examples</strong></h2>" + _nl); foreach (Example example in cmdlet.Examples) { String name = String.IsNullOrEmpty(example.Name) ? "unknown" : example.Name; SB.Append("<h3>" + SecurityElement.Escape(name) + "</h3>" + _nl); if (!String.IsNullOrEmpty(example.Cmd)) { String cmd; if (!example.Cmd.StartsWith("PS C:\\>")) { cmd = "PS C:\\> " + example.Cmd; } else { cmd = example.Cmd; } SB.Append("<pre style=\"margin-left: 40px;\">" + SecurityElement.Escape(cmd) + "</pre>" + _nl); } if (!String.IsNullOrEmpty(example.Output)) { SB.Append("<pre style=\"margin-left: 40px;\">" + SecurityElement.Escape(example.Output) + "</pre>" + _nl); } if (!String.IsNullOrEmpty(example.Description)) { String str = rules.ToHtml(example.Description); SB.Append("<p style=\"margin-left: 40px;\">" + str + "</p>" + _nl); } } }
public void TestParseDocuments(string inputFile, string outputFile) { var input = GetResource(inputFile); var parser = new BBCodeParser(); Assert.Equal(GetResource(outputFile), parser.ToHtml(input)); }
public static IHtmlString RenderBBCode(this System.Web.Mvc.HtmlHelper html, string bbcode) { if (String.IsNullOrEmpty(bbcode)) { return(html.Raw("")); } bbcode = bbcode.Replace("\n", "[br]"); return(html.Raw(DefaultBBCodeParser.ToHtml(bbcode))); }
public string Process(string categoryAlias, TPrimaryKey threadId, string text) { var bbParsed = _bbCodeParser.ToHtml(text); var uriParsed = UriToHtmlLinks(bbParsed); var crossLinksParsed = CrossLinksToHtmlLinks(categoryAlias, threadId, uriParsed); var result = crossLinksParsed; return(result); }
static void HtmlGenerateNotes(BBCodeParser rules, StringBuilder SB, CmdletObject cmdlet) { SB.Append("<h2><strong>Notes</strong></h2>" + _nl); if (!String.IsNullOrEmpty(cmdlet.GeneralHelp.Notes)) { String str = rules.ToHtml(GenerateHtmlLink(cmdlet.GeneralHelp.Notes, null)); SB.Append("<p style=\"margin-left: 40px;\">" + str + "</p>" + _nl); } }
static void htmlGenerateDescription(BBCodeParser rules, StringBuilder SB, IReadOnlyList <CmdletObject> cmdlets, CmdletObject cmdlet) { SB.AppendLine("<h2>Description</h2>"); if (!String.IsNullOrEmpty(cmdlet.GeneralHelp.Description)) { String str = rules.ToHtml(generateHtmlLink(cmdlet.GeneralHelp.Description, cmdlets)); SB.AppendLine(addIndentedParagraphText(str)); } }
static void htmlGenerateNotes(BBCodeParser rules, StringBuilder SB, CmdletObject cmdlet) { SB.AppendLine("<h2>Notes</h2>"); if (!String.IsNullOrEmpty(cmdlet.GeneralHelp.Notes)) { String str = rules.ToHtml(generateHtmlLink(cmdlet.GeneralHelp.Notes, null)); SB.AppendLine(addIndentedParagraphText(str)); } }
public void NewlineTrailingOpeningTagIsIgnored() { var parser = new BBCodeParser(ErrorMode.ErrorFree, null, new[] { new BBTag("code", "<pre>", "</pre>") }); var input = "[code]\nHere is some code[/code]"; var expected = "<pre>Here is some code</pre>"; // No newline after the opening PRE Assert.AreEqual(expected, parser.ToHtml(input)); }
static void HtmlGenerateDescription(BBCodeParser rules, StringBuilder SB, IReadOnlyList <CmdletObject> cmdlets, CmdletObject cmdlet) { SB.Append("<h2><strong>Description</strong></h2>" + _nl); if (!String.IsNullOrEmpty(cmdlet.GeneralHelp.Description)) { String str = rules.ToHtml(GenerateHtmlLink(cmdlet.GeneralHelp.Description, cmdlets)); SB.Append("<p style=\"margin-left: 40px;\">" + str + "</p>" + _nl); } }
public void TagContentTransformer() { var parser = new BBCodeParser(new[] { new BBTag("b", "<b>", "</b>", true, true, content => content.Trim()), }); Assert.AreEqual("<b>abc</b>", parser.ToHtml("[b] abc [/b]")); }
/// <summary> /// HTML encodes a string then parses BB codes. /// </summary> /// <param name="text">The string to HTML encode anad BB code parse</param> /// <returns></returns> public string ParseBBCodeText(string text) { var tags = _bbCodeRepository.Get().Select(GetTag).ToList(); var parser = new BBCodeParser(ErrorMode.ErrorFree, null, tags); var result = parser.ToHtml(text); result = result.Replace(Environment.NewLine, "<br />"); return(result); }
public static string BBCodeToHtml(string rawString, IList <BBTag> bbTags, bool htmlEncode = false) { if (string.IsNullOrEmpty(rawString) || (bbTags == null)) { return(rawString); } BBCodeParser parser = new BBCodeParser(bbTags); return(parser.ToHtml(rawString, htmlEncode)); }
public string Process(string categoryAlias, TPrimaryKey threadId, string text) { var normalizedLineBreaks = NormalizeLineBreaks(text); var limitedLineBreaksCount = LimitLineBreaksCount(normalizedLineBreaks); var convertedToHtml = _bbCodeParser.ToHtml(limitedLineBreaksCount); var linksProcessed = ReplaceUrisWithHtmlLinks(convertedToHtml); var crossLinksProcessed = ReplaceCrossLinksWithHtmlLinks(categoryAlias, threadId, linksProcessed); return(crossLinksProcessed); }
public static string BBCodeToHtml(string rawString, System.Collections.Generic.IList <BBTag> bbTags) { if (string.IsNullOrEmpty(rawString) || bbTags == null) { return(rawString); } BBCodeParser bBCodeParser = new BBCodeParser(bbTags); return(bBCodeParser.ToHtml(rawString)); }
public void StopProcessingDirective_StopsParserProcessingTagLikeText_UntilClosingTag() { var parser = new BBCodeParser(ErrorMode.ErrorFree, null, new[] { new BBTag("code", "<pre>", "</pre>") { StopProcessing = true } }); var input = "[code][i]This should [u]be a[/u] text literal[/i][/code]"; var expected = "<pre>[i]This should [u]be a[/u] text literal[/i]</pre>"; Assert.Equal(expected, parser.ToHtml(input)); }
public void NestedLists_AreCorrect(string bbcode, string html) { var parser = new BBCodeParser(new List <BBTag> { new BBTag("*", "<li>", "</li>", true, BBTagClosingStyle.AutoCloseElement, null, true, 20), new BBTag("list", "<${attr}>", "</${attr}>", true, BBTagClosingStyle.RequiresClosingTag, null, 9, "", true, new BBAttribute("attr", "", a => string.IsNullOrWhiteSpace(a.AttributeValue) ? "ul" : $"ol type=\"{a.AttributeValue}\"")) }); Assert.Equal(html, HttpUtility.HtmlDecode(parser.ToHtml(bbcode))); }
public void SuppressFirstNewlineAfter_StopsFirstNewlineAfterClosingTag() { var parser = new BBCodeParser(ErrorMode.ErrorFree, null, new[] { new BBTag("code", "<pre>", "</pre>") { SuppressFirstNewlineAfter = true } }); var input = "[code]Here is some code[/code]\nMore text!"; var expected = "<pre>Here is some code</pre>More text!"; // No newline after the closing PRE Assert.AreEqual(expected, parser.ToHtml(input)); }
public void BBCodeToHtml_SameAsRegular(string bbcode, string html) { var parser = new BBCodeParser(new List <BBTag> { new BBTag("*", "<li>", "</li>", true, BBTagClosingStyle.AutoCloseElement, null, true, 20), new BBTag("list", "<${attr}>", "</${attr}>", true, BBTagClosingStyle.RequiresClosingTag, null, 9, "", true, new BBAttribute("attr", "", a => string.IsNullOrWhiteSpace(a.AttributeValue) ? "ul" : $"ol type=\"{a.AttributeValue}\"")) }); var(bbCode, uid, _) = parser.TransformForBackwardsCompatibility(bbcode); Assert.Equal(html, HttpUtility.HtmlDecode(parser.ToHtml(bbcode, uid))); }
public void GreedyAttributeProcessing_ConsumesAllTokensForAttributeValue() { var parser = new BBCodeParser(ErrorMode.ErrorFree, null, new[] { new BBTag("quote", "<div><span>Posted by ${name}</span>", "</div>", new BBAttribute("name", "")) { GreedyAttributeProcessing = true } }); var input = "[quote=Test User With Spaces]Here is my comment[/quote]"; var expected = "<div><span>Posted by Test User With Spaces</span>Here is my comment</div>"; Assert.AreEqual(expected, parser.ToHtml(input)); }
public void Newline_ListItem_IsCorrect(string input, string expected) { var bbcodes = new List <BBTag> { new BBTag("*", "<li>", "</li>", true, BBTagClosingStyle.AutoCloseElement, x => x, true, 20), new BBTag("list", "<${attr}>", "</${attr}>", true, true, 9, "", true, new BBAttribute("attr", "", a => string.IsNullOrWhiteSpace(a.AttributeValue) ? "ul" : $"ol type=\"{a.AttributeValue}\"")) }; var parser = new BBCodeParser(bbcodes); Assert.Equal(expected, parser.ToHtml(input)); }
/// <summary> /// Find formatting tags in the text and transform them into the appropriate HTML. /// </summary> /// <param name="text">The text to be transformed.</param> /// <returns>A formatted string.</returns> public static string ConvertTagsToHtml(string text) { var parser = new BBCodeParser(new[] { new BBTag("transmit", "<span class='transmit' transmit='${transmit}'>", "</span>", new BBAttribute("transmit", "")), new BBTag("code", "<pre><code>", "</code></pre>"), new BBTag("color", "<span style='color: ${color}; border-color: ${color};'>", "</span>", new BBAttribute("color", "")), new BBTag("img", "", "", true, true, content => { string imageUrl = ConfirmHttp(content); try { var client = new WebClient(); var stream = client.OpenRead(imageUrl); var bitmap = new Bitmap(stream); stream.Flush(); stream.Close(); var width = Convert.ToDecimal(bitmap.Size.Width); var height = Convert.ToDecimal(bitmap.Size.Height); if (width > 500m) { var ratio = width / 500m; height = height / ratio; width = 500m; } return(string.Format("<div style='height: {0}px; width: {1}px;'><a target='_blank' href='{2}'><img style='height: {0}px; width: {1}px;' src='{2}' /></a></div>", height, width, imageUrl)); } catch { return(string.Format("<div><a target='_blank' href='{0}'><img src='{0}' /></a></div>", imageUrl)); } }), new BBTag("url", "<a target='_blank' href='${href}'>", "</a>", new BBAttribute("href", "", context => { if (!string.IsNullOrWhiteSpace(context.AttributeValue)) { return(context.AttributeValue); } var tagContent = context.GetAttributeValueByID(BBTag.ContentPlaceholderName); return(tagContent); })), new BBTag("quote", "<div class='quote'>", "</div>"), new BBTag("b", "<b>", "</b>"), new BBTag("i", "<span style=\"font-style:italic;\">", "</span>"), new BBTag("u", "<span style=\"text-decoration:underline;\">", "</span>"), new BBTag("s", "<strike>", "</strike>"), new BBTag("br", "<br />", "", true, false) }); return(parser.ToHtml(text)); }
private void ShowNews() { var news = _news[_index]; var content = GetContent(news); if (content.IsEmpty()) { return; } HtmlPanel.Text = _parser.ToHtml(content); }