private string CleanPreviewText(string body, int lines, bool skipQuoting) { StringBuilder resultBuilder = StringBuilderPool.Alloc(); try { int pos = 0; int foundLines = 0; int quotedLines = 0; while (foundLines < lines) { int nextPos = body.IndexOf('\n', pos); if (nextPos == -1) { if (foundLines < lines - 1 && quotedLines > 0) { return(CleanPreviewText(body, lines, false)); } resultBuilder.Append(body.Substring(pos)); break; } string nextLine = body.Substring(pos, nextPos - pos); pos = nextPos + 1; while (pos < body.Length && body [pos] == '\n') { pos++; } if (skipQuoting && MailBodyParser.GetQuoteLevel(nextLine) > 0) { quotedLines++; continue; } if (foundLines > 0) { resultBuilder.Append("\r\n"); } foundLines++; resultBuilder.Append(nextLine); if (pos == body.Length) { break; } } if (foundLines == 0 && quotedLines > 0) { return(CleanPreviewText(body, lines, false)); } return(resultBuilder.ToString()); } finally { StringBuilderPool.Dispose(resultBuilder); } }
public string Quote(MailBodyParser parser, IResource origMail, QuoteSettings quoteSettings) { StringBuilder quoteBuilder = StringBuilderPool.Alloc(); try { string initials = ""; if (origMail != null) { IResourceList senders = origMail.GetLinksOfType("Contact", "From"); if (senders.Count > 0) { IResource sender = senders [0]; string name = sender.GetPropText("FirstName"); if (name.Length == 0) { name = sender.DisplayName; } if (quoteSettings.PrefixInitials) { initials = GetInitials(sender); } if (quoteSettings.GreetingInReplies) { quoteBuilder.Append(quoteSettings.GreetingString + " " + name + ",\r\n\r\n"); } } } if (quoteSettings.UseSignature && quoteSettings.SignatureInReplies == SignaturePosition.BeforeQuote) { quoteBuilder.Append("\r\n"); quoteBuilder.Append(quoteSettings.Signature); quoteBuilder.Append("\r\n\r\n"); } for (int i = 0; i < parser.ParagraphCount; i++) { MailBodyParser.Paragraph para = parser.GetParagraph(i); QuoteParagraph(quoteBuilder, initials, para, quoteSettings); } if (quoteSettings.UseSignature && quoteSettings.SignatureInReplies == SignaturePosition.AfterQuote) { quoteBuilder.Append(quoteSettings.Signature); } return(quoteBuilder.ToString()); } finally { StringBuilderPool.Dispose(quoteBuilder); } }
public MailBodyParser(string body, int minWrapWidth, MailBodyParser origText) { _minWrapWidth = minWrapWidth; _origText = origText; if (body != null) { long startTicks = DateTime.Now.Ticks; ParseMailBody(body); long endTicks = DateTime.Now.Ticks; Debug.WriteLine("MailBodyParser parsing took " + (endTicks - startTicks) / 10000 + " ms"); } }
public static string FormatBody(MailBodyParser parser, string subject, bool noWrap, MarkerInjector injector, string fontFace, int fontSize) { // A link converter that is capable of sweeping out the markers when // creating the links out of plain text (by using the MarkedHtmlLinkConverter). // If there are no markers injected, HtmlLinkConverter is used. HtmlLinkConverter linkconverter = (injector != null) ? new MarkedHtmlLinkConverter(injector) : new HtmlLinkConverter(); string style = _cDefaultStyleTmpl.Replace("$FontFace$", fontFace).Replace("$FontSize$", fontSize.ToString()); _builder.Length = 0; _quoteTableStarted = _quoteRowStarted = false; FormatBody(parser, subject, linkconverter, style, noWrap); return(_builder.ToString()); }
private static string GetFormattedText(bool isNoFormat, string body, string replyToBody, string subject, MarkerInjector injector, string fontFace, int fontSize) { string text; int minWrapWidth = Core.SettingStore.ReadInt("Formatting", "MinimumWrapWidth", _iDefaultMessageWidth); if (isNoFormat) { text = "<pre>" + HttpUtility.HtmlEncode(body) + "</pre>"; } else { MailBodyParser replyParser = new MailBodyParser(replyToBody, minWrapWidth); MailBodyParser parser = new MailBodyParser(body, minWrapWidth, replyParser); text = MailBodyFormatter.FormatBody(parser, subject, false, injector, fontFace, fontSize); } return(text); }
private static void FormatBody(MailBodyParser parser, string subject, HtmlLinkConverter converter, string style, bool noWrap) { _builder.Append("<html><head>").Append(style). Append(Core.MessageFormatter.DualMediaSubjectStyle).Append("</head><body>"); if (!String.IsNullOrEmpty(subject)) { _builder.Append(MessageFormatter.FormattedHeader(subject)); } int oldQuoteLevel = 0; string oldQuotePrefix = ""; for (int i = 0; i < parser.ParagraphCount; i++) { MailBodyParser.Paragraph para = parser.GetParagraph(i); if (para.QuoteLevel > 0) { if (!_quoteTableStarted) { _builder.Append(_cTableStartTag); _quoteTableStarted = true; _quoteRowStarted = false; } if (_quoteRowStarted) { if (oldQuoteLevel != para.QuoteLevel || oldQuotePrefix != para.QuotePrefix) { _builder.Append("</td></tr>"); _quoteRowStarted = false; } } if (!_quoteRowStarted) { _builder.Append("<tr class=\""); _builder.Append((para.QuoteLevel % 2 != 0) ? "oddquote" : "evenquote"); _builder.Append("\"><td>"); _builder.Append(HTMLEncode(para.QuotePrefix)); for (int q = 0; q < para.QuoteLevel; q++) { _builder.Append(">"); } _builder.Append("</td><td>"); _quoteRowStarted = true; } else { _builder.Append("<br>"); } if (para.Type == ParagraphType.Fixed) { _builder.Append(converter.ConvertLinks(ReplaceSpaces(HTMLEncode(para.Text), noWrap))); } else { // show an extra separator line above and below every quoted // plain-text paragraph if (i > 0) { MailBodyParser.Paragraph prevPara = parser.GetParagraph(i - 1); if (prevPara.Type != ParagraphType.Plain && prevPara.QuoteLevel == para.QuoteLevel && prevPara.QuotePrefix == para.QuotePrefix) { _builder.Append("<br>"); } } _builder.Append(converter.ConvertLinks(HTMLEncode(para.Text))); _builder.Append("<br>"); } } else { CloseOpenElements(); switch (para.Type) { case ParagraphType.Plain: _builder.Append("<p>"); if (para.OutlookQuote) { _builder.Append("<span class=\"outlookquote\">"); _builder.Append(converter.ConvertLinks(HTMLEncode(para.Text))); _builder.Append("</span>"); } else { _builder.Append(converter.ConvertLinks(HTMLEncode(para.Text))); } _builder.Append("</p>\r\n"); break; case ParagraphType.Fixed: case ParagraphType.Service: if (para.OutlookQuote) { _builder.Append("<span class=\"outlookquote\">"); _builder.Append(converter.ConvertLinks(ReplaceSpaces(HTMLEncode(para.Text), noWrap))); _builder.Append("</span>"); } else { _builder.Append(converter.ConvertLinks(ReplaceSpaces(HTMLEncode(para.Text), noWrap))); } _builder.Append("<br>\r\n"); break; case ParagraphType.Sig: _builder.Append("<span class=\"sig\">"); _builder.Append(converter.ConvertLinks(para.Text)); _builder.Append("</span></br>"); break; } } oldQuoteLevel = para.QuoteLevel; oldQuotePrefix = para.QuotePrefix; } CloseOpenElements(); _builder.Append("</body></html> "); }
public string QuoteMessage(IResource resource, string body, QuoteSettings settings) { MailBodyParser parser = new MailBodyParser(body, 50); return(_quoter.Quote(parser, resource, settings)); }
public static string FormatBody(MailBodyParser parser, bool noWrap, MarkerInjector injector, string fontFace, int fontSize) { return(FormatBody(parser, null, noWrap, injector, fontFace, fontSize)); }