private static FormattedText ProcessToken( ref FormattedText prevItem, StringTokenizer tokenizer, bool hasOpenTag, TinyHTMLParsers.TinyHTMLParsersData parserData, ref bool shouldProduceNewLine) { string source = tokenizer.NextToken(); StringTokenizer stringTokenizer = new StringTokenizer(source, ">"); bool flag = source.IndexOf(">") > -1; string htmlTag = stringTokenizer.NextToken(); string text = stringTokenizer.NextToken(); FormattedText currentFormattedText = new FormattedText(prevItem); if (!hasOpenTag || !flag) { currentFormattedText.text = htmlTag; currentFormattedText.HtmlTag = string.Empty; } else { currentFormattedText.text = !TinyHTMLParsers.ApplyHTMLSettingsFromTag(ref currentFormattedText, prevItem, htmlTag, parserData, ref shouldProduceNewLine, ref text) ? htmlTag + text : text; currentFormattedText.HtmlTag = htmlTag; } if (!string.IsNullOrEmpty(currentFormattedText.text)) { currentFormattedText.text = currentFormattedText.text.Replace("<", "<").Replace(">", ">").Replace("[CDATA[\\]]", "\\"); } currentFormattedText.StartNewLine = shouldProduceNewLine; return(currentFormattedText); }
public static FormattedTextBlock Parse( string text, Color baseForeColor, string baseFont, float fontSize, FontStyle fontStyle, ContentAlignment aligment) { if (string.IsNullOrEmpty(text)) { return(new FormattedTextBlock()); } new Stack <FormattedText.HTMLLikeListType>().Push(FormattedText.HTMLLikeListType.None); text = text.Replace("\\", "[CDATA[\\]]"); text = text.Replace("\\<", "<"); text = text.Replace("\\>", ">"); text = text.Replace("\r\n", "<br>"); text = text.Replace("\n", "<br>"); FormattedTextBlock formattedTextBlock = new FormattedTextBlock(); FormattedText prevItem = new FormattedText(); prevItem.FontColor = baseForeColor; prevItem.FontName = baseFont; prevItem.FontSize = fontSize; prevItem.ContentAlignment = aligment; prevItem.FontStyle = fontStyle; StringTokenizer tokenizer = new StringTokenizer(text, "<"); int num = tokenizer.Count(); bool hasOpenTag = text.IndexOf("<") > -1; TextLine textLine = new TextLine(); formattedTextBlock.Lines.Add(textLine); bool shouldProduceNewLine = false; TinyHTMLParsers.TinyHTMLParsersData parserData = new TinyHTMLParsers.TinyHTMLParsersData(); for (int index = 0; index < num; ++index) { FormattedText prototypeFormattedText = TinyHTMLParsers.ProcessToken(ref prevItem, tokenizer, hasOpenTag, parserData, ref shouldProduceNewLine); if (!string.IsNullOrEmpty(prototypeFormattedText.HtmlTag) && prototypeFormattedText.HtmlTag.Length >= 1 && shouldProduceNewLine) { textLine = new TextLine(); formattedTextBlock.Lines.Add(textLine); if (prototypeFormattedText.HtmlTag.Length >= 2) { prototypeFormattedText.HtmlTag = prototypeFormattedText.HtmlTag.TrimEnd(' ').Trim('/'); } shouldProduceNewLine = !string.IsNullOrEmpty(prototypeFormattedText.text) && prototypeFormattedText.text.Trim().Length == 0; } textLine.List.Add(prototypeFormattedText); prevItem = new FormattedText(prototypeFormattedText); } return(formattedTextBlock); }
private static void ProcessSingleListEntry( bool isCloseTag, ref int numberCount, FormattedText currentFormattedText, TinyHTMLParsers.TinyHTMLParsersData parserData) { FormattedText.HTMLLikeListType htmlLikeListType = parserData.lastListType.Peek(); currentFormattedText.ShouldDisplayBullet = !isCloseTag; if (htmlLikeListType != FormattedText.HTMLLikeListType.OrderedList) { return; } if (!isCloseTag) { ++numberCount; } currentFormattedText.Number = numberCount; }
private static void ProcessSpan( ref FormattedText currentFormattedText, string lowerHtmlTag, string htmlTag, bool isClosingTag, TinyHTMLParsers.TinyHTMLParsersData parserData) { if (isClosingTag) { if (parserData.lastFormattedText.Count <= 0) { return; } currentFormattedText = parserData.lastFormattedText.Pop(); currentFormattedText.ShouldDisplayBullet = false; } else { parserData.lastFormattedText.Push(new FormattedText(currentFormattedText)); string attribute = TinyHTMLParsers.ParseAttribute(htmlTag, lowerHtmlTag, "style"); string tag1 = TinyHTMLParsers.ProcessStyle(attribute, "color"); if (!string.IsNullOrEmpty(tag1)) { currentFormattedText.FontColor = TinyHTMLParsers.ParseColor(tag1, new Color?(currentFormattedText.FontColor)); } string tag2 = TinyHTMLParsers.ProcessStyle(attribute, "background-color"); if (!string.IsNullOrEmpty(tag2)) { currentFormattedText.BgColor = new Color?(TinyHTMLParsers.ParseColor(tag2, currentFormattedText.BgColor)); } string htmlTag1 = TinyHTMLParsers.ProcessStyle(attribute, "font-family"); if (!string.IsNullOrEmpty(htmlTag1)) { currentFormattedText.FontName = TinyHTMLParsers.ParseFont(htmlTag1); } string tag3 = TinyHTMLParsers.ProcessStyle(attribute, "font-size"); if (string.IsNullOrEmpty(tag3)) { return; } currentFormattedText.FontSize = TinyHTMLParsers.ParseNumber(tag3, currentFormattedText.FontSize); } }
public static bool ApplyHTMLSettingsFromTag( ref FormattedText currentFormattedText, FormattedText prevText, string htmlTag, TinyHTMLParsers.TinyHTMLParsersData parserData, ref bool shouldProduceNewLine, ref string text) { if (string.IsNullOrEmpty(htmlTag)) { return(true); } htmlTag = htmlTag.Trim('<', '>'); bool flag1 = htmlTag.StartsWith("/"); currentFormattedText.IsClosingTag = flag1; if (flag1) { htmlTag = htmlTag.TrimStart('/'); } string lower = htmlTag.ToLower(); bool flag2 = true; if (lower == "i" || lower == "em") { currentFormattedText.FontStyle = TinyHTMLParsers.ProcessFontStyle(currentFormattedText.FontStyle, FontStyle.Italic, flag1); } else if (lower == "b" || lower == "strong") { currentFormattedText.FontStyle = TinyHTMLParsers.ProcessFontStyle(currentFormattedText.FontStyle, FontStyle.Bold, flag1); } else if (lower == "u") { currentFormattedText.FontStyle = TinyHTMLParsers.ProcessFontStyle(currentFormattedText.FontStyle, FontStyle.Underline, flag1); } else if (lower.StartsWith("color") && htmlTag.Length > 6) { currentFormattedText.FontColor = TinyHTMLParsers.ParseColor(htmlTag.Substring(6), new Color?(currentFormattedText.FontColor)); } else if (lower.StartsWith("size=") || lower.StartsWith("size =")) { currentFormattedText.FontSize = TinyHTMLParsers.ParseSize(htmlTag, prevText.FontSize); } else if ((lower.StartsWith("font=") || lower.StartsWith("font =")) && htmlTag.Length > 5) { currentFormattedText.FontName = TinyHTMLParsers.ParseFont(htmlTag.Substring(5)); } else if (lower == "strike") { currentFormattedText.FontStyle = TinyHTMLParsers.ProcessFontStyle(currentFormattedText.FontStyle, FontStyle.Strikeout, flag1); } else if (lower.StartsWith("bgcolor")) { currentFormattedText.BgColor = TinyHTMLParsers.ProcessBgColor(lower, currentFormattedText.BgColor, flag1); } else if (lower == "ul") { TinyHTMLParsers.ProcessListEntry(flag1, FormattedText.HTMLLikeListType.List, parserData.lastListType, parserData.lastListNumberCount, currentFormattedText); } else if (lower == "ol") { TinyHTMLParsers.ProcessListEntry(flag1, FormattedText.HTMLLikeListType.OrderedList, parserData.lastListType, parserData.lastListNumberCount, currentFormattedText); } else if (lower == "li") { int numberCount = 0; shouldProduceNewLine = !shouldProduceNewLine && !flag1; if (parserData.lastListNumberCount.Count > 0) { numberCount = parserData.lastListNumberCount.Pop(); } if (parserData.lastListType.Count > 0) { TinyHTMLParsers.ProcessSingleListEntry(flag1, ref numberCount, currentFormattedText, parserData); } parserData.lastListNumberCount.Push(numberCount); } else if (!(lower == "html")) { if (lower == "br" || lower == "br /" || lower == "br/") { shouldProduceNewLine = !flag1; } else if (lower == "p") { if (flag1) { shouldProduceNewLine = false; } else if (!prevText.IsClosingTag && !prevText.StartNewLine) { shouldProduceNewLine = !shouldProduceNewLine; } } else if (lower.StartsWith("img ")) { TinyHTMLParsers.SetImage(TinyHTMLParsers.ParseImageAttribute(htmlTag, lower, "src"), TinyHTMLParsers.ParseAttribute(htmlTag, lower, "width"), TinyHTMLParsers.ParseAttribute(htmlTag, lower, "height"), currentFormattedText); } else if (lower.StartsWith("a")) { TinyHTMLParsers.ProcessLink(currentFormattedText, htmlTag, lower, flag1, ref text); } else if (lower.StartsWith("span")) { TinyHTMLParsers.ProcessSpan(ref currentFormattedText, lower, htmlTag, flag1, parserData); } else { flag2 = false; } } return(flag2); }