void RenderText(TextPart text) { string html; if (text.IsHtml) { // the text content is already in HTML format html = text.Text; } else if (text.IsFlowed) { var converter = new FlowedToHtml(); string delsp; // the delsp parameter specifies whether or not to delete spaces at the end of flowed lines if (!text.ContentType.Parameters.TryGetValue("delsp", out delsp)) { delsp = "no"; } if (string.Compare(delsp, "yes", StringComparison.OrdinalIgnoreCase) == 0) { converter.DeleteSpace = true; } html = converter.Convert(text.Text); } else { html = new TextToHtml().Convert(text.Text); } webView.LoadData(html, "text/html", "utf-8"); }
public void Execute_TextToHtml_Valid(object value) { var function = new TextToHtml(); var result = function.Evaluate(value); Assert.That(result, Is.EqualTo("Cédric")); }
static void RenderText(TextPart text, WebBrowserEditabil pWebBrowser) { string html; if (text.IsHtml) { html = text.Text; } else if (text.IsFlowed) { var converter = new FlowedToHtml(); string delsp; if (!text.ContentType.Parameters.TryGetValue("delsp", out delsp)) { delsp = "no"; } if (string.Compare(delsp, "yes", StringComparison.OrdinalIgnoreCase) == 0) { converter.DeleteSpace = true; } html = converter.Convert(text.Text); } else { html = new TextToHtml().Convert(text.Text); } pWebBrowser.DocumentText = html; }
string RenderText(TextPart text) { string html; if (text.IsHtml) { return(text.Text); } else if (text.IsFlowed) { var converter = new FlowedToHtml(); string delsp; if (!text.ContentType.Parameters.TryGetValue("delsp", out delsp)) { delsp = "no"; } if (string.Compare(delsp, "yes", StringComparison.OrdinalIgnoreCase) == 0) { converter.DeleteSpace = true; } html = converter.Convert(text.Text); } else { html = new TextToHtml().Convert(text.Text); } return(html); }
protected override void VisitTextPart (TextPart entity) { TextConverter converter; if (body != null) { // since we've already found the body, treat this as an attachment attachments.Add (entity); return; } if (entity.IsHtml) { converter = new HtmlToHtml { HtmlTagCallback = HtmlTagCallback }; } else if (entity.IsFlowed) { var flowed = new FlowedToHtml (); string delsp; if (entity.ContentType.Parameters.TryGetValue ("delsp", out delsp)) flowed.DeleteSpace = delsp.ToLowerInvariant () == "yes"; converter = flowed; } else { converter = new TextToHtml (); } body = converter.Convert (entity.Text); }
private bool TryWriteApprovalRequestBodyWithPreview(Body destiniationBody, DsnHumanReadableWriter approvalRequestWriter, ApprovalInformation info, Charset outputCharset) { if (this.originalEmailMessage == null) { return(false); } TextConverter textConverter; if (this.originalMesssageBodyFormat == Microsoft.Exchange.Data.Transport.Email.BodyFormat.Rtf) { textConverter = new RtfToHtml { Header = approvalRequestWriter.GetHtmlModerationBody(info), HeaderFooterFormat = HeaderFooterFormat.Html, OutputEncoding = outputCharset.GetEncoding() }; } else if (this.originalMesssageBodyFormat == Microsoft.Exchange.Data.Transport.Email.BodyFormat.Text) { textConverter = new TextToHtml { Header = approvalRequestWriter.GetHtmlModerationBody(info), HeaderFooterFormat = HeaderFooterFormat.Html, InputEncoding = this.originalMessageBodyCharset.GetEncoding(), OutputEncoding = outputCharset.GetEncoding() }; } else { if (this.originalMesssageBodyFormat != Microsoft.Exchange.Data.Transport.Email.BodyFormat.Html) { return(false); } textConverter = new HtmlToHtml { Header = approvalRequestWriter.GetHtmlModerationBody(info), HeaderFooterFormat = HeaderFooterFormat.Html, InputEncoding = this.originalMessageBodyCharset.GetEncoding(), OutputEncoding = outputCharset.GetEncoding() }; } BodyWriteConfiguration configuration = new BodyWriteConfiguration(Microsoft.Exchange.Data.Storage.BodyFormat.TextHtml, outputCharset); using (Stream stream = destiniationBody.OpenWriteStream(configuration)) { using (Stream contentReadStream = this.originalEmailMessage.Body.GetContentReadStream()) { try { textConverter.Convert(contentReadStream, stream); } catch (ExchangeDataException arg) { ModerationApprovalRequestWriter.diag.TraceDebug <ExchangeDataException>(0L, "Approval request with inline preview failed {0}", arg); return(false); } } } return(true); }
public void TestSimpleTextWithUrlsToHtml() { const string expected = "Check out <a href=\"http://www.xamarin.com\">http://www.xamarin.com</a> - it's amazing!<br/>"; string text = "Check out http://www.xamarin.com - it's amazing!" + Environment.NewLine; var converter = new TextToHtml(); var result = converter.Convert(text); Assert.AreEqual(expected, result); }
protected override void VisitTextPart(TextPart entity) { TextConverter converter; string header, footer; if (_body != null) { // since we've already found the body, treat this as an attachment _attachments.Add(entity); return; } GetHeaderFooter(out header, out footer); if (entity.IsHtml) { converter = new HtmlToHtml { Header = UIStrings.MarkOfTheWeb + Environment.NewLine, HeaderFormat = HeaderFooterFormat.Html, HtmlTagCallback = HtmlTagCallback }; } else if (entity.IsFlowed) { var flowed = new FlowedToHtml { Header = UIStrings.MarkOfTheWeb + Environment.NewLine + header, HeaderFormat = HeaderFooterFormat.Html, Footer = footer, FooterFormat = HeaderFooterFormat.Html }; string delsp; if (entity.ContentType.Parameters.TryGetValue("delsp", out delsp)) { flowed.DeleteSpace = delsp.ToLowerInvariant() == "yes"; } converter = flowed; } else { converter = new TextToHtml { Header = UIStrings.MarkOfTheWeb + Environment.NewLine + header, HeaderFormat = HeaderFooterFormat.Html, Footer = footer, FooterFormat = HeaderFooterFormat.Html }; } string text = entity.Text; _body = converter.Convert(entity.Text); }
public void TestSimpleTextToHtml() { const string expected = "This is some sample text. This is line #1.<br/>" + "This is line #2.<br/>" + "And this is line #3.<br/>"; string text = "This is some sample text. This is line #1." + Environment.NewLine + "This is line #2." + Environment.NewLine + "And this is line #3." + Environment.NewLine; var converter = new TextToHtml(); var result = converter.Convert(text); Assert.AreEqual(expected, result); }
private void SetTextToHtml(TextPart entity) { var beforeAfter = GetBeforeAfterFormatWrapper(UIStrings.TextToHtmlFormatWrapper); var converter = new TextToHtml { Header = $"{UIStrings.MarkOfTheWeb}{Environment.NewLine}{beforeAfter.Before}", HeaderFormat = HeaderFooterFormat.Html, Footer = beforeAfter.After, FooterFormat = HeaderFooterFormat.Html }; _body = converter.Convert(entity.Text); }
public void TestEmoji() { var expected = "<html><body>😱<br/></body></html>"; var buffer = Encoding.ASCII.GetBytes("=F0=9F=98=B1"); var decoder = new QuotedPrintableDecoder(); var length = decoder.EstimateOutputLength(buffer.Length); var decoded = new byte[length]; var n = decoder.Decode(buffer, 0, buffer.Length, decoded); var emoji = Encoding.UTF8.GetString(decoded, 0, n); var converter = new TextToHtml(); var result = converter.Convert(emoji); Assert.AreEqual(expected, result); }
public void TestOutputHtmlFragment() { const string input = "This is the html body"; const string expected = "<html><body>This is the html body<br/></body></html>"; const string expected2 = "This is the html body<br/>"; var converter = new TextToHtml(); string result; result = converter.Convert(input); Assert.AreEqual(expected, result); converter.OutputHtmlFragment = true; result = converter.Convert(input); Assert.AreEqual(expected2, result); }
protected async override void VisitTextPart(TextPart entity) { TextConverter converter; if (renderedBody) { // since we've already found the body, treat this as an attachment attachments.Add(entity); return; } if (entity.IsHtml) { converter = new HtmlToHtml { HtmlTagCallback = HtmlTagCallback }; } else if (entity.IsFlowed) { var flowed = new FlowedToHtml(); if (entity.ContentType.Parameters.TryGetValue("delsp", out string delsp)) { flowed.DeleteSpace = delsp.ToLowerInvariant() == "yes"; } converter = flowed; } else { converter = new TextToHtml(); } var client = new MultipartRelatedWebViewClient(stack); var html = converter.Convert(entity.Text); // Add print header html = Regex.Replace(html, "<[Bb][Oo][Dd][Yy].*?>", "$0" + "<p id=\"emlReaderPrintHeader\" style=\"background: white; color: black;\"></p>"); await webView.EnsureCoreWebView2Async(); webView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All); webView.CoreWebView2.WebResourceRequested += client.ShouldInterceptRequest; webView.NavigateToString(html); renderedBody = true; }
public void TestHeaderFooter() { const string input = "This is the html body"; const string header = "This is the header"; const string footer = "This is the footer"; var expected = "<html><body>" + header + "<br/>" + input + "<br/>" + footer + "<br/></body></html>"; var converter = new TextToHtml { HeaderFormat = HeaderFooterFormat.Text, Header = header, FooterFormat = HeaderFooterFormat.Text, Footer = footer }; var result = converter.Convert(input); Assert.AreEqual(expected, result); }
public void TestDefaultPropertyValues() { var converter = new TextToHtml(); Assert.IsFalse(converter.DetectEncodingFromByteOrderMark, "DetectEncodingFromByteOrderMark"); Assert.IsNull(converter.Footer, "Footer"); Assert.AreEqual(HeaderFooterFormat.Text, converter.FooterFormat, "FooterFormat"); Assert.IsNull(converter.Header, "Header"); Assert.AreEqual(HeaderFooterFormat.Text, converter.HeaderFormat, "HeaderFormat"); Assert.IsNull(converter.HtmlTagCallback, "HtmlTagCallback"); Assert.AreEqual(Encoding.UTF8, converter.InputEncoding, "InputEncoding"); Assert.AreEqual(TextFormat.Text, converter.InputFormat, "InputFormat"); Assert.AreEqual(4096, converter.InputStreamBufferSize, "InputStreamBufferSize"); Assert.AreEqual(Encoding.UTF8, converter.OutputEncoding, "OutputEncoding"); Assert.AreEqual(TextFormat.Html, converter.OutputFormat, "OutputFormat"); Assert.IsFalse(converter.OutputHtmlFragment, "OutputHtmlFragment"); Assert.AreEqual(4096, converter.OutputStreamBufferSize, "OutputStreamBufferSize"); }
public void TestArgumentExceptions() { var converter = new TextToHtml(); var reader = new StringReader(""); var writer = new StringWriter(); Assert.AreEqual(TextFormat.Plain, converter.InputFormat); Assert.AreEqual(TextFormat.Html, converter.OutputFormat); Assert.IsFalse(converter.DetectEncodingFromByteOrderMark); Assert.IsFalse(converter.OutputHtmlFragment); Assert.IsNull(converter.Footer); Assert.IsNull(converter.Header); Assert.AreEqual(HeaderFooterFormat.Text, converter.FooterFormat); Assert.AreEqual(HeaderFooterFormat.Text, converter.HeaderFormat); Assert.Throws <ArgumentNullException> (() => converter.Convert((TextReader)null, writer)); Assert.Throws <ArgumentNullException> (() => converter.Convert(reader, (TextWriter)null)); }
public void SetCharset(Charset detectedCharset) { if (this.toHtmlConverter == null) { throw new InvalidOperationException("HtmlWriteConverterStream.SetCharset() should only be called once."); } HtmlToHtml htmlToHtml = this.toHtmlConverter as HtmlToHtml; if (htmlToHtml != null) { if (CodePageMap.GetCodePage(htmlToHtml.InputEncoding) == detectedCharset.CodePage && this.skipConversionOnMatchingCharset) { this.writeStream = this.outputStream; } else { htmlToHtml.OutputEncoding = detectedCharset.GetEncoding(); this.writeStream = new ConverterStream(this.outputStream, htmlToHtml, ConverterStreamAccess.Write); } } else { RtfToHtml rtfToHtml = this.toHtmlConverter as RtfToHtml; if (rtfToHtml != null) { rtfToHtml.OutputEncoding = detectedCharset.GetEncoding(); } else { TextToHtml textToHtml = this.toHtmlConverter as TextToHtml; if (textToHtml != null) { textToHtml.OutputEncoding = detectedCharset.GetEncoding(); } } this.writeStream = new ConverterStream(this.outputStream, this.toHtmlConverter, ConverterStreamAccess.Write); } this.toHtmlConverter = null; this.cachedContentStream.Position = 0L; Util.StreamHandler.CopyStreamData(this.cachedContentStream, this.writeStream); this.cachedContentStream.Dispose(); this.cachedContentStream = null; }
protected override void VisitTextPart(TextPart entity) { TextConverter converter; if (renderedBody) { // since we've already found the body, treat this as an attachment attachments.Add(entity); return; } if (entity.IsHtml) { converter = new HtmlToHtml { HtmlTagCallback = HtmlTagCallback }; } else if (entity.IsFlowed) { var flowed = new FlowedToHtml(); string delsp; if (entity.ContentType.Parameters.TryGetValue("delsp", out delsp)) { flowed.DeleteSpace = delsp.ToLowerInvariant() == "yes"; } converter = flowed; } else { converter = new TextToHtml(); } var client = new MultipartRelatedWebViewClient(stack); var html = converter.Convert(entity.Text); webView.SetWebViewClient(client); webView.LoadData(html, "text/html", "utf-8"); renderedBody = true; }
public void TestDecreasingQuoteLevels() { string expected = "<blockquote><blockquote><blockquote><blockquote><blockquote><blockquote>Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned pigeon-egg!<br/>" + "</blockquote>Thou artless swag-bellied milk-livered dismal-dreaming idle-headed scut!<br/>" + "</blockquote>Thou errant folly-fallen spleeny reeling-ripe unmuzzled ratsbane!<br/>" + "</blockquote>Henceforth, the coding style is to be strictly enforced, including the use of only upper case.<br/>" + "</blockquote>I've noticed a lack of adherence to the coding styles, of late.<br/>" + "</blockquote>Any complaints?<br/>" + "</blockquote>"; string text = ">>>>>> Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned pigeon-egg!" + Environment.NewLine + ">>>>> Thou artless swag-bellied milk-livered dismal-dreaming idle-headed scut!" + Environment.NewLine + ">>>> Thou errant folly-fallen spleeny reeling-ripe unmuzzled ratsbane!" + Environment.NewLine + ">>> Henceforth, the coding style is to be strictly enforced, including the use of only upper case." + Environment.NewLine + ">> I've noticed a lack of adherence to the coding styles, of late." + Environment.NewLine + "> Any complaints?" + Environment.NewLine; var converter = new TextToHtml { OutputHtmlFragment = true }; var result = converter.Convert(text); Assert.AreEqual(expected, result); }
protected override void VisitTextPart(TextPart entity) { TextConverter converter; if (renderedBody) { // since we've already found the body, treat this as an attachment attachments.Add(entity); return; } if (entity.IsHtml) { converter = new HtmlToHtml { HtmlTagCallback = HtmlTagCallback }; } else if (entity.IsFlowed) { var flowed = new FlowedToHtml(); if (entity.ContentType.Parameters.TryGetValue("delsp", out string delsp)) { flowed.DeleteSpace = delsp.ToLowerInvariant() == "yes"; } converter = flowed; } else { converter = new TextToHtml(); } var html = converter.Convert(entity.Text); NSUrlCache.SharedCache = new MultipartRelatedUrlCache(stack); webView.LoadHtmlString(html, new NSUrl("index.html")); renderedBody = true; }
private static string ConvertToHtml(TextPart entity) { try { TextConverter converter; if (entity.IsHtml) { converter = new HtmlToHtml(); } else if (entity.IsFlowed) { var flowed = new FlowedToHtml(); string delsp; if (entity.ContentType.Parameters.TryGetValue("delsp", out delsp)) { flowed.DeleteSpace = delsp.ToLowerInvariant() == "yes"; } converter = flowed; } else { converter = new TextToHtml(); } var body = converter.Convert(entity.Text); return(body); } catch (Exception) { // skip } return(entity.Text ?? ""); }
public void TestArgumentExceptions() { var converter = new TextToHtml(); var reader = new StringReader(""); var writer = new StringWriter(); Assert.Throws <ArgumentNullException> (() => converter.InputEncoding = null); Assert.Throws <ArgumentNullException> (() => converter.OutputEncoding = null); Assert.Throws <ArgumentOutOfRangeException> (() => converter.InputStreamBufferSize = -1); Assert.Throws <ArgumentOutOfRangeException> (() => converter.OutputStreamBufferSize = -1); Assert.Throws <ArgumentNullException> (() => converter.Convert(null)); Assert.Throws <ArgumentNullException> (() => converter.Convert((Stream)null, Stream.Null)); Assert.Throws <ArgumentNullException> (() => converter.Convert(Stream.Null, (Stream)null)); Assert.Throws <ArgumentNullException> (() => converter.Convert((TextReader)null, Stream.Null)); Assert.Throws <ArgumentNullException> (() => converter.Convert(Stream.Null, (TextWriter)null)); Assert.Throws <ArgumentNullException> (() => converter.Convert((TextReader)null, writer)); Assert.Throws <ArgumentNullException> (() => converter.Convert(reader, (TextWriter)null)); Assert.Throws <ArgumentNullException> (() => converter.Convert(new StreamReader(Stream.Null), (Stream)null)); Assert.Throws <ArgumentNullException> (() => converter.Convert((Stream)null, new StreamWriter(Stream.Null))); Assert.Throws <ArgumentNullException> (() => converter.Convert(new StreamReader(Stream.Null), (TextWriter)null)); }
protected internal override void VisitTextPart(TextPart entity) { TextConverter converter; if (body != null) { base.VisitTextPart(entity); return; } if (entity.IsHtml) { converter = new HtmlToHtml { HtmlTagCallback = HtmlTagCallback }; } else if (entity.IsFlowed) { var flowed = new FlowedToHtml(); string delsp; if (entity.ContentType.Parameters.TryGetValue("delsp", out delsp)) { flowed.DeleteSpace = delsp.ToLowerInvariant() == "yes"; } converter = flowed; } else { converter = new TextToHtml(); } body = converter.Convert(entity.Text); base.VisitTextPart(entity); }
internal HtmlWriteConverterStream(Stream outputStream, TextToHtml textToHtml) : this(outputStream, textToHtml, false) { }
public void TestHtmlGetAttributeValueToSrcAttributeOk(string text, string expectedResult) { var processor = new TextToHtml(text); Assert.Equal(expectedResult, processor.GetHtml()); }