private static void ProcessSingleListEntry(bool isCloseTag, ref int numberCount, FormattedText currentFormattedText, TinyHTMLParsersData parserData) { FormattedText.HTMLLikeListType listType = parserData.lastListType.Peek(); currentFormattedText.ShouldDisplayBullet = !isCloseTag; if (listType != FormattedText.HTMLLikeListType.OrderedList) { return; } if (!isCloseTag) { ++numberCount; } currentFormattedText.Number = numberCount; }
/// <summary> /// Parse single HTML tag and apply settings /// </summary> /// <param name="currentFormattedText"></param> /// <param name="prevText"></param> /// <param name="htmlTag"></param> /// <param name="parserData"></param> /// <param name="shouldProduceNewLine"></param> /// <param name="text"></param> public static bool ApplyHTMLSettingsFromTag(ref FormattedText currentFormattedText, FormattedText prevText, string htmlTag, TinyHTMLParsersData parserData, ref bool shouldProduceNewLine, ref string text) { if (string.IsNullOrEmpty(htmlTag)) { return(true); } htmlTag = htmlTag.Trim('<', '>'); bool isClosingTag = htmlTag.StartsWith("/"); currentFormattedText.IsClosingTag = isClosingTag; if (isClosingTag) { htmlTag = htmlTag.TrimStart('/'); } string lowerHtmlTag = htmlTag.ToLower(); bool isKnowCommand = true; if (lowerHtmlTag == "i" || lowerHtmlTag == "em") { currentFormattedText.FontStyle = ProcessFontStyle(currentFormattedText.FontStyle, FontStyle.Italic, isClosingTag); } else if (lowerHtmlTag == "b" || lowerHtmlTag == "strong") { currentFormattedText.FontStyle = ProcessFontStyle(currentFormattedText.FontStyle, FontStyle.Bold, isClosingTag); } else if (lowerHtmlTag == "u") { currentFormattedText.FontStyle = ProcessFontStyle(currentFormattedText.FontStyle, FontStyle.Underline, isClosingTag); } else if (lowerHtmlTag.StartsWith("color") && htmlTag.Length > 6) { currentFormattedText.FontColor = ParseColor(htmlTag.Substring(6), currentFormattedText.FontColor); } else if (lowerHtmlTag.StartsWith("size=") || lowerHtmlTag.StartsWith("size =")) { currentFormattedText.FontSize = ParseSize(htmlTag, prevText.FontSize); } else if ((lowerHtmlTag.StartsWith("font=") || lowerHtmlTag.StartsWith("font =")) && htmlTag.Length > 5) { currentFormattedText.FontName = ParseFont(htmlTag.Substring(5)); } else if (lowerHtmlTag == "strike") { currentFormattedText.FontStyle = ProcessFontStyle(currentFormattedText.FontStyle, FontStyle.Strikeout, isClosingTag); } else if (lowerHtmlTag.StartsWith("bgcolor")) { currentFormattedText.BgColor = ProcessBgColor(lowerHtmlTag, currentFormattedText.BgColor, isClosingTag); } //lists else if (lowerHtmlTag == "ul") { ProcessListEntry(isClosingTag, FormattedText.HTMLLikeListType.List, parserData.lastListType, parserData.lastListNumberCount, currentFormattedText); } else if (lowerHtmlTag == "ol") { ProcessListEntry(isClosingTag, FormattedText.HTMLLikeListType.OrderedList, parserData.lastListType, parserData.lastListNumberCount, currentFormattedText); } else if (lowerHtmlTag == "li") { int lastNumber = 0; shouldProduceNewLine = !shouldProduceNewLine && !isClosingTag; if (parserData.lastListNumberCount.Count > 0) { lastNumber = parserData.lastListNumberCount.Pop(); } if (parserData.lastListType.Count > 0) { ProcessSingleListEntry(isClosingTag, ref lastNumber, currentFormattedText, parserData); } parserData.lastListNumberCount.Push(lastNumber); } //end lists else if (lowerHtmlTag == "html") { } else if (lowerHtmlTag == "br" || lowerHtmlTag == "br /" || lowerHtmlTag == "br/") { shouldProduceNewLine = !isClosingTag; } else if (lowerHtmlTag == "p") { if (isClosingTag) { shouldProduceNewLine = false; } else { if (!prevText.IsClosingTag && !prevText.StartNewLine) { shouldProduceNewLine = !shouldProduceNewLine; } } } else if (lowerHtmlTag.StartsWith("img ")) { string imageName = ParseAttribute(htmlTag, lowerHtmlTag, "src"); string width = ParseAttribute(htmlTag, lowerHtmlTag, "width"); string height = ParseAttribute(htmlTag, lowerHtmlTag, "height"); SetImage(imageName, width, height, currentFormattedText); } else if (lowerHtmlTag.StartsWith("a")) { ProcessLink(currentFormattedText, htmlTag, lowerHtmlTag, isClosingTag, ref text); } else if (lowerHtmlTag.StartsWith("span")) { ProcessSpan(ref currentFormattedText, lowerHtmlTag, htmlTag, isClosingTag, parserData); } else { isKnowCommand = false; } //if (parserData.shouldClearProduceBullets) //{ // currentFormattedText.ShouldDisplayBullet = false; // parserData.shouldClearProduceBullets = false; //} return(isKnowCommand); }