Пример #1
0
        void WriteText(HtmlWriter htmlWriter, string text)
        {
            var      callback   = HtmlTagCallback ?? DefaultHtmlTagCallback;
            var      content    = text.ToCharArray();
            int      endIndex   = content.Length;
            int      startIndex = 0;
            UrlMatch match;
            int      count;

            do
            {
                count = endIndex - startIndex;

                if (scanner.Scan(content, startIndex, count, out match))
                {
                    count = match.EndIndex - match.StartIndex;

                    if (match.StartIndex > startIndex)
                    {
                        // write everything up to the match
                        htmlWriter.WriteText(content, startIndex, match.StartIndex - startIndex);
                    }

                    var href = match.Prefix + new string (content, match.StartIndex, count);
                    var ctx  = new TextToHtmlTagContext(HtmlTagId.A, new HtmlAttribute(HtmlAttributeId.Href, href));
                    callback(ctx, htmlWriter);

                    if (!ctx.SuppressInnerContent)
                    {
                        htmlWriter.WriteText(content, match.StartIndex, count);
                    }

                    if (!ctx.DeleteEndTag)
                    {
                        ctx.SetIsEndTag(true);

                        if (ctx.InvokeCallbackForEndTag)
                        {
                            callback(ctx, htmlWriter);
                        }
                        else
                        {
                            ctx.WriteTag(htmlWriter);
                        }
                    }

                    startIndex = match.EndIndex;
                }
                else
                {
                    htmlWriter.WriteText(content, startIndex, count);
                    break;
                }
            } while (startIndex < endIndex);
        }
Пример #2
0
        /// <summary>
        /// Convert the contents of <paramref name="reader"/> from the <see cref="InputFormat"/> to the
        /// <see cref="OutputFormat"/> and uses the <paramref name="writer"/> to write the resulting text.
        /// </summary>
        /// <remarks>
        /// Converts the contents of <paramref name="reader"/> from the <see cref="InputFormat"/> to the
        /// <see cref="OutputFormat"/> and uses the <paramref name="writer"/> to write the resulting text.
        /// </remarks>
        /// <param name="reader">The text reader.</param>
        /// <param name="writer">The text writer.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="reader"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="writer"/> is <c>null</c>.</para>
        /// </exception>
        public override void Convert(TextReader reader, TextWriter writer)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (!OutputHtmlFragment)
            {
                writer.Write("<html><body>");
            }

            if (!string.IsNullOrEmpty(Header))
            {
                if (HeaderFormat == HeaderFooterFormat.Text)
                {
                    var converter = new TextToHtml {
                        OutputHtmlFragment = true
                    };

                    using (var sr = new StringReader(Header))
                        converter.Convert(sr, writer);
                }
                else
                {
                    writer.Write(Header);
                }
            }

            using (var htmlWriter = new HtmlWriter(writer)) {
                var callback = HtmlTagCallback ?? DefaultHtmlTagCallback;
                var stack = new List <TextToHtmlTagContext> ();
                int currentQuoteDepth = 0, quoteDepth;
                TextToHtmlTagContext ctx;
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    line = Unquote(line, out quoteDepth);

                    while (currentQuoteDepth < quoteDepth)
                    {
                        ctx = new TextToHtmlTagContext(HtmlTagId.BlockQuote);
                        callback(ctx, htmlWriter);
                        currentQuoteDepth++;
                        stack.Add(ctx);
                    }

                    while (quoteDepth < currentQuoteDepth)
                    {
                        ctx = stack[stack.Count - 1];
                        stack.RemoveAt(stack.Count - 1);

                        if (!SuppressContent(stack) && !ctx.DeleteEndTag)
                        {
                            ctx.SetIsEndTag(true);

                            if (ctx.InvokeCallbackForEndTag)
                            {
                                callback(ctx, htmlWriter);
                            }
                            else
                            {
                                ctx.WriteTag(htmlWriter);
                            }
                        }

                        if (ctx.TagId == HtmlTagId.BlockQuote)
                        {
                            currentQuoteDepth--;
                        }
                    }

                    if (!SuppressContent(stack))
                    {
                        WriteText(htmlWriter, line);

                        ctx = new TextToHtmlTagContext(HtmlTagId.Br);
                        callback(ctx, htmlWriter);
                    }
                }

                for (int i = stack.Count; i > 0; i--)
                {
                    ctx = stack[i - 1];

                    ctx.SetIsEndTag(true);

                    if (ctx.InvokeCallbackForEndTag)
                    {
                        callback(ctx, htmlWriter);
                    }
                    else
                    {
                        ctx.WriteTag(htmlWriter);
                    }
                }

                htmlWriter.Flush();
            }

            if (!string.IsNullOrEmpty(Footer))
            {
                if (FooterFormat == HeaderFooterFormat.Text)
                {
                    var converter = new TextToHtml {
                        OutputHtmlFragment = true
                    };

                    using (var sr = new StringReader(Footer))
                        converter.Convert(sr, writer);
                }
                else
                {
                    writer.Write(Footer);
                }
            }

            if (!OutputHtmlFragment)
            {
                writer.Write("</body></html>");
            }
        }
Пример #3
0
		/// <summary>
		/// Convert the contents of <paramref name="reader"/> from the <see cref="InputFormat"/> to the
		/// <see cref="OutputFormat"/> and uses the <paramref name="writer"/> to write the resulting text.
		/// </summary>
		/// <remarks>
		/// Converts the contents of <paramref name="reader"/> from the <see cref="InputFormat"/> to the
		/// <see cref="OutputFormat"/> and uses the <paramref name="writer"/> to write the resulting text.
		/// </remarks>
		/// <param name="reader">The text reader.</param>
		/// <param name="writer">The text writer.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <para><paramref name="reader"/> is <c>null</c>.</para>
		/// <para>-or-</para>
		/// <para><paramref name="writer"/> is <c>null</c>.</para>
		/// </exception>
		public override void Convert (TextReader reader, TextWriter writer)
		{
			if (reader == null)
				throw new ArgumentNullException (nameof (reader));

			if (writer == null)
				throw new ArgumentNullException (nameof (writer));

			if (!string.IsNullOrEmpty (Header)) {
				if (HeaderFormat == HeaderFooterFormat.Text) {
					var converter = new TextToHtml ();

					using (var sr = new StringReader (Header))
						converter.Convert (sr, writer);
				} else {
					writer.Write (Header);
				}
			}

			using (var htmlWriter = new HtmlWriter (writer)) {
				var callback = HtmlTagCallback ?? DefaultHtmlTagCallback;
				var stack = new List<TextToHtmlTagContext> ();
				int currentQuoteDepth = 0, quoteDepth;
				TextToHtmlTagContext ctx;
				string line;

				while ((line = reader.ReadLine ()) != null) {
					line = Unquote (line, out quoteDepth);

					while (currentQuoteDepth < quoteDepth) {
						ctx = new TextToHtmlTagContext (HtmlTagId.BlockQuote);
						callback (ctx, htmlWriter);
						currentQuoteDepth++;
						stack.Add (ctx);
					}

					while (quoteDepth < currentQuoteDepth) {
						ctx = stack[stack.Count - 1];
						stack.RemoveAt (stack.Count - 1);

						if (!SuppressContent (stack) && !ctx.DeleteEndTag) {
							ctx.SetIsEndTag (true);

							if (ctx.InvokeCallbackForEndTag)
								callback (ctx, htmlWriter);
							else
								ctx.WriteTag (htmlWriter);
						}

						if (ctx.TagId == HtmlTagId.BlockQuote)
							currentQuoteDepth--;
					}

					if (!SuppressContent (stack)) {
						WriteText (htmlWriter, line);

						ctx = new TextToHtmlTagContext (HtmlTagId.Br);
						callback (ctx, htmlWriter);
					}
				}

				for (int i = stack.Count; i > 0; i--) {
					ctx = stack[i - 1];

					ctx.SetIsEndTag (true);

					if (ctx.InvokeCallbackForEndTag)
						callback (ctx, htmlWriter);
					else
						ctx.WriteTag (htmlWriter);
				}

				htmlWriter.Flush ();
			}

			if (!string.IsNullOrEmpty (Footer)) {
				if (FooterFormat == HeaderFooterFormat.Text) {
					var converter = new TextToHtml ();

					using (var sr = new StringReader (Footer))
						converter.Convert (sr, writer);
				} else {
					writer.Write (Footer);
				}
			}
		}
Пример #4
0
		void WriteText (HtmlWriter htmlWriter, string text)
		{
			var callback = HtmlTagCallback ?? DefaultHtmlTagCallback;
			var content = text.ToCharArray ();
			int endIndex = content.Length;
			int startIndex = 0;
			UrlMatch match;
			int count;

			do {
				count = endIndex - startIndex;

				if (scanner.Scan (content, startIndex, count, out match)) {
					count = match.EndIndex - match.StartIndex;

					if (match.StartIndex > startIndex) {
						// write everything up to the match
						htmlWriter.WriteText (content, startIndex, match.StartIndex - startIndex);
					}

					var href = match.Prefix + new string (content, match.StartIndex, count);
					var ctx = new TextToHtmlTagContext (HtmlTagId.A, new HtmlAttribute (HtmlAttributeId.Href, href));
					callback (ctx, htmlWriter);

					if (!ctx.SuppressInnerContent)
						htmlWriter.WriteText (content, match.StartIndex, count);

					if (!ctx.DeleteEndTag) {
						ctx.SetIsEndTag (true);

						if (ctx.InvokeCallbackForEndTag)
							callback (ctx, htmlWriter);
						else
							ctx.WriteTag (htmlWriter);
					}

					startIndex = match.EndIndex;
				} else {
					htmlWriter.WriteText (content, startIndex, count);
					break;
				}
			} while (startIndex < endIndex);
		}