public void HtmlTagCallback (HtmlTagContext ctx, HtmlWriter htmlWriter) { if (ctx.TagId != HtmlTagId.Image || ctx.IsEndTag) { ctx.WriteTag (htmlWriter, true); return; } // write the IMG tag, but don't write out the attributes. ctx.WriteTag (htmlWriter, false); // manually write the attributes so that we can replace the SRC attributes foreach (var attribute in ctx.Attributes) { if (attribute.Id == HtmlAttributeId.Src) { int index; Uri uri; // parse the <img src=...> attribute value into a Uri if (Uri.IsWellFormedUriString (attribute.Value, UriKind.Absolute)) uri = new Uri (attribute.Value, UriKind.Absolute); else uri = new Uri (attribute.Value, UriKind.Relative); // locate the index of the attachment within the multipart/related (if it exists) if ((index = related.IndexOf (uri)) != -1) { var attachment = related[index] as MimePart; if (attachment == null) { // the body part is not a basic leaf part (IOW it's a multipart or message-part) htmlWriter.WriteAttribute (attribute); continue; } var data = GetDataUri (attachment); htmlWriter.WriteAttributeName (attribute.Name); htmlWriter.WriteAttributeValue (data); } else { htmlWriter.WriteAttribute (attribute); } } } }
static void DefaultHtmlTagCallback (HtmlTagContext tagContext, HtmlWriter htmlWriter) { tagContext.WriteTag (htmlWriter, true); }
void HtmlTagCallback(HtmlTagContext ctx, HtmlWriter htmlWriter) { if (ctx.TagId == HtmlTagId.Image && !ctx.IsEndTag && _stack.Count > 0) { ctx.WriteTag(htmlWriter, false); // replace the src attribute with a file:// URL foreach (var attribute in ctx.Attributes) { if (attribute.Id == HtmlAttributeId.Src) { MimePart image; if (!TryGetImage(attribute.Value, out image)) { htmlWriter.WriteAttribute(attribute); continue; } var url = SaveImage(image, attribute.Value); htmlWriter.WriteAttributeName(attribute.Name); htmlWriter.WriteAttributeValue(url); } else htmlWriter.WriteAttribute(attribute); } } else if (ctx.TagId == HtmlTagId.Body && !ctx.IsEndTag) { ctx.WriteTag(htmlWriter, false); // add and/or replace oncontextmenu="return false;" foreach (var attribute in ctx.Attributes) { if (attribute.Name.ToLowerInvariant() == "oncontextmenu") continue; htmlWriter.WriteAttribute(attribute); } htmlWriter.WriteAttribute("oncontextmenu", "return false;"); } else { // pass the tag through to the output ctx.WriteTag(htmlWriter, true); } }
void ReplaceUrlsWithFileNames (HtmlTagContext ctx, HtmlWriter htmlWriter) { if (ctx.TagId == HtmlTagId.Image) { htmlWriter.WriteEmptyElementTag (ctx.TagName); ctx.DeleteEndTag = true; for (int i = 0; i < ctx.Attributes.Count; i++) { var attr = ctx.Attributes[i]; if (attr.Id == HtmlAttributeId.Src) { var fileName = Path.GetFileName (attr.Value); htmlWriter.WriteAttributeName (attr.Name); htmlWriter.WriteAttributeValue (fileName); } else { htmlWriter.WriteAttribute (attr); } } } else { ctx.WriteTag (htmlWriter, true); } }
void HtmlTagCallback (HtmlTagContext ctx, HtmlWriter htmlWriter) { if (ctx.TagId == HtmlTagId.Body && !ctx.IsEmptyElementTag) { if (ctx.IsEndTag) { // end our opening <blockquote> htmlWriter.WriteEndTag (HtmlTagId.BlockQuote); // pass the </body> tag through to the output ctx.WriteTag (htmlWriter, true); } else { // pass the <body> tag through to the output ctx.WriteTag (htmlWriter, true); // prepend the HTML reply with "On {DATE}, {SENDER} wrote:" htmlWriter.WriteStartTag (HtmlTagId.P); htmlWriter.WriteText (GetOnDateSenderWrote (message)); htmlWriter.WriteEndTag (HtmlTagId.P); // Wrap the original content in a <blockquote> htmlWriter.WriteStartTag (HtmlTagId.BlockQuote); htmlWriter.WriteAttribute (HtmlAttributeId.Style, "border-left: 1px #ccc solid; margin: 0 0 0 .8ex; padding-left: 1ex;"); ctx.InvokeCallbackForEndTag = true; } } else { // pass the tag through to the output ctx.WriteTag (htmlWriter, true); } }
static void DefaultHtmlTagCallback(HtmlTagContext tagContext, HtmlWriter htmlWriter) { tagContext.WriteTag(htmlWriter, true); }
/// <summary> /// Get a text preview of a stream of text. /// </summary> /// <remarks> /// Gets a text preview of a stream of text. /// </remarks> /// <param name="reader">The original text stream.</param> /// <returns>A string representing a shortened preview of the original text.</returns> /// <exception cref="System.ArgumentNullException"> /// <paramref name="reader"/> is <c>null</c>. /// </exception> public override string GetPreviewText(TextReader reader) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } var tokenizer = new HtmlTokenizer(reader) { IgnoreTruncatedTags = true }; var preview = new char[MaximumPreviewLength]; var stack = new List <HtmlTagContext> (); var prefix = string.Empty; int previewLength = 0; HtmlTagContext ctx; HtmlAttribute attr; bool body = false; bool full = false; bool lwsp = true; HtmlToken token; while (!full && tokenizer.ReadNextToken(out token)) { switch (token.Kind) { case HtmlTokenKind.Tag: var tag = (HtmlTagToken)token; if (!tag.IsEndTag) { if (body) { switch (tag.Id) { case HtmlTagId.Image: if ((attr = tag.Attributes.FirstOrDefault(x => x.Id == HtmlAttributeId.Alt)) != null) { full = Append(preview, ref previewLength, prefix + attr.Value, ref lwsp); prefix = string.Empty; } break; case HtmlTagId.LI: if ((ctx = GetListItemContext(stack)) != null) { if (ctx.TagId == HtmlTagId.OL) { full = Append(preview, ref previewLength, $" {++ctx.ListIndex}. ", ref lwsp); prefix = string.Empty; } else { //full = Append (preview, ref previewLength, " \u2022 ", ref lwsp); prefix = " "; } } break; case HtmlTagId.Br: case HtmlTagId.P: prefix = " "; break; } if (!tag.IsEmptyElement) { ctx = new HtmlTagContext(tag.Id) { SuppressInnerContent = ShouldSuppressInnerContent(tag.Id) }; stack.Add(ctx); } } else if (tag.Id == HtmlTagId.Body && !tag.IsEmptyElement) { body = true; } } else if (tag.Id == HtmlTagId.Body) { stack.Clear(); body = false; } else { Pop(stack, tag.Id); } break; case HtmlTokenKind.Data: if (body && !SuppressContent(stack)) { var data = (HtmlDataToken)token; full = Append(preview, ref previewLength, prefix + data.Data, ref lwsp); prefix = string.Empty; } break; } } if (lwsp && previewLength > 0) { previewLength--; } return(new string (preview, 0, previewLength)); }