Exemplo n.º 1
0
		/// <summary>
		/// Creates a new <see cref="FixedHighlighter"/> for a copy of a portion
		/// of the input document (including the original highlighting).
		/// </summary>
		public static FixedHighlighter CreateView(IHighlighter highlighter, int offset, int endOffset)
		{
			var oldDocument = highlighter.Document;
			// ReadOnlyDocument would be better; but displaying the view in AvalonEdit
			// requires a TextDocument
			var newDocument = new TextDocument(oldDocument.CreateSnapshot(offset, endOffset - offset));
			
			var oldStartLine = oldDocument.GetLineByOffset(offset);
			var oldEndLine = oldDocument.GetLineByOffset(endOffset);
			int oldStartLineNumber = oldStartLine.LineNumber;
			HighlightedLine[] newLines = new HighlightedLine[oldEndLine.LineNumber - oldStartLineNumber + 1];
			highlighter.BeginHighlighting();
			try {
				for (int i = 0; i < newLines.Length; i++) {
					HighlightedLine oldHighlightedLine = highlighter.HighlightLine(oldStartLineNumber + i);
					IDocumentLine newLine = newDocument.GetLineByNumber(1 + i);
					HighlightedLine newHighlightedLine = new HighlightedLine(newDocument, newLine);
					MoveSections(oldHighlightedLine.Sections, -offset, newLine.Offset, newLine.EndOffset, newHighlightedLine.Sections);
					newHighlightedLine.ValidateInvariants();
					newLines[i] = newHighlightedLine;
				}
			} finally {
				highlighter.EndHighlighting();
			}
			return new FixedHighlighter(newDocument, newLines);
		}
Exemplo n.º 2
0
        /// <summary>
        /// Creates a HTML fragment from a part of a document.
        /// </summary>
        /// <param name="document">The document to create HTML from.</param>
        /// <param name="highlighter">The highlighter used to highlight the document. <c>null</c> is valid and will create HTML without any highlighting.</param>
        /// <param name="segment">The part of the document to create HTML for. You can pass <c>null</c> to create HTML for the whole document.</param>
        /// <param name="options">The options for the HTML creation.</param>
        /// <returns>HTML code for the document part.</returns>
        public static string CreateHtmlFragment(TextDocument document, IHighlighter highlighter, ISegment segment, HtmlOptions options)
        {
            if (document == null)
                throw new ArgumentNullException("document");
            if (options == null)
                throw new ArgumentNullException("options");
            if (highlighter != null && highlighter.Document != document)
                throw new ArgumentException("Highlighter does not belong to the specified document.");
            if (segment == null)
                segment = new SimpleSegment(0, document.TextLength);

            StringBuilder html = new StringBuilder();
            int segmentEndOffset = segment.EndOffset;
            DocumentLine line = document.GetLineByOffset(segment.Offset);
            while (line != null && line.Offset < segmentEndOffset) {
                HighlightedLine highlightedLine;
                if (highlighter != null)
                    highlightedLine = highlighter.HighlightLine(line.LineNumber);
                else
                    highlightedLine = new HighlightedLine(document, line);
                SimpleSegment s = segment.GetOverlap(line);
                if (html.Length > 0)
                    html.AppendLine("<br>");
                html.Append(highlightedLine.ToHtml(s.Offset, s.EndOffset, options));
                line = line.NextLine;
            }
            return html.ToString();
        }
Exemplo n.º 3
0
		/// <summary>
		/// Converts a readonly TextDocument to a Block and applies the provided highlighter.
		/// </summary>
		public static Block ConvertTextDocumentToBlock(IDocument document, IHighlighter highlighter)
		{
			if (document == null)
				throw new ArgumentNullException("document");
//			Table table = new Table();
//			table.Columns.Add(new TableColumn { Width = GridLength.Auto });
//			table.Columns.Add(new TableColumn { Width = new GridLength(1, GridUnitType.Star) });
//			TableRowGroup trg = new TableRowGroup();
//			table.RowGroups.Add(trg);
			Paragraph p = new Paragraph();
			p.TextAlignment = TextAlignment.Left;
			for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++) {
				if (lineNumber > 1)
					p.Inlines.Add(new LineBreak());
				var line = document.GetLineByNumber(lineNumber);
//				TableRow row = new TableRow();
//				trg.Rows.Add(row);
//				row.Cells.Add(new TableCell(new Paragraph(new Run(lineNumber.ToString()))) { TextAlignment = TextAlignment.Right });
//				Paragraph p = new Paragraph();
//				row.Cells.Add(new TableCell(p));
				if (highlighter != null) {
					HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber);
					p.Inlines.AddRange(highlightedLine.ToRichText().CreateRuns());
				} else {
					p.Inlines.Add(document.GetText(line));
				}
			}
			return p;
		}
Exemplo n.º 4
0
		public static Block ConvertTextDocumentToBlock(TextDocument document, IHighlighter highlighter)
		{
			if (document == null)
				throw new ArgumentNullException("document");
//			Table table = new Table();
//			table.Columns.Add(new TableColumn { Width = GridLength.Auto });
//			table.Columns.Add(new TableColumn { Width = new GridLength(1, GridUnitType.Star) });
//			TableRowGroup trg = new TableRowGroup();
//			table.RowGroups.Add(trg);
			Paragraph p = new Paragraph();
			foreach (DocumentLine line in document.Lines) {
				int lineNumber = line.LineNumber;
//				TableRow row = new TableRow();
//				trg.Rows.Add(row);
//				row.Cells.Add(new TableCell(new Paragraph(new Run(lineNumber.ToString()))) { TextAlignment = TextAlignment.Right });
				HighlightedInlineBuilder inlineBuilder = new HighlightedInlineBuilder(document.GetText(line));
				if (highlighter != null) {
					HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber);
					int lineStartOffset = line.Offset;
					foreach (HighlightedSection section in highlightedLine.Sections)
						inlineBuilder.SetHighlighting(section.Offset - lineStartOffset, section.Length, section.Color);
				}
//				Paragraph p = new Paragraph();
//				row.Cells.Add(new TableCell(p));
				p.Inlines.AddRange(inlineBuilder.CreateRuns());
				p.Inlines.Add(new LineBreak());
			}
			return p;
		}
 /// <inheritdoc/>
 protected override void ColorizeLine(DocumentLine line)
 {
     if (highlighter != null)
     {
         lineNumberBeingColorized = line.LineNumber;
         HighlightedLine hl = highlighter.HighlightLine(lineNumberBeingColorized);
         lineNumberBeingColorized = 0;
         foreach (HighlightedSection section in hl.Sections)
         {
             if (IsEmptyColor(section.Color))
             {
                 continue;
             }
             ChangeLinePart(section.Offset, section.Offset + section.Length,
                            visualLineElement => ApplyColorToElement(visualLineElement, section.Color));
         }
     }
     this.lastColorizedLine = line;
 }
Exemplo n.º 6
0
		/// <summary>
		/// Converts an IDocument to a Block and applies the provided highlighter.
		/// </summary>
		public static Block ConvertTextDocumentToBlock(IDocument document, IHighlighter highlighter)
		{
			if (document == null)
				throw new ArgumentNullException("document");
			Paragraph p = new Paragraph();
			p.TextAlignment = TextAlignment.Left;
			for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++) {
				if (lineNumber > 1)
					p.Inlines.Add(new LineBreak());
				var line = document.GetLineByNumber(lineNumber);
				if (highlighter != null) {
					HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber);
					p.Inlines.AddRange(highlightedLine.ToRichText().CreateRuns());
				} else {
					p.Inlines.Add(document.GetText(line));
				}
			}
			return p;
		}
        public static bool IsInHighlightSection(this TextArea textArea, string highlightName, int caretOffset = -1)
        {
            IHighlighter highlighter = textArea.GetService(typeof(IHighlighter)) as IHighlighter;

            if (highlighter == null)
            {
                return(false);
            }
            if (caretOffset == -1)
            {
                caretOffset = textArea.Caret.Offset;
            }

            HighlightedLine result = highlighter.HighlightLine(textArea.Document.GetLineByOffset(caretOffset).LineNumber);

            return(result.Sections.Any(s => s.Offset <= caretOffset &&
                                       s.Offset + s.Length >= caretOffset &&
                                       s.Color.Name == highlightName));
        }
Exemplo n.º 8
0
        /// <inheritdoc/>
        protected override void Colorize(VisualLine line)
        {
            IHighlighter highlighter = line.TextLayer.Services.GetService(typeof(IHighlighter)) as IHighlighter;

            if (highlighter != null)
            {
                lineNumberBeingColorized = line.DocumentLine.LineNumber;
                HighlightedLine hl = highlighter.HighlightLine(lineNumberBeingColorized);
                lineNumberBeingColorized = 0;
                foreach (HighlightedSection section in hl.Sections)
                {
                    if (IsEmptyColor(section.Color))
                    {
                        continue;
                    }

                    ChangeVisualElements(section.Offset, section.Offset + section.Length,
                                         visualLineElement => ApplyColorToElement(visualLineElement, section.Color));
                }
            }
        }
Exemplo n.º 9
0
        /// <inheritdoc/>
        protected override void ColorizeLine(DocumentLine line)
        {
            IHighlighter highlighter = CurrentContext.TextView.Services.GetService(typeof(IHighlighter)) as IHighlighter;
            string       lineString  = document.GetText(line);

            if (highlighter != null)
            {
                if (lineString.Length < 3 || lineString.Substring(0, 3) == ">>>" || lineString.Substring(0, 3) == "...")
                {
                    HighlightedLine hl = highlighter.HighlightLine(line.LineNumber);
                    foreach (HighlightedSection section in hl.Sections)
                    {
                        ChangeLinePart(section.Offset, section.Offset + section.Length,
                                       visualLineElement => ApplyColorToElement(visualLineElement, section.Color));
                    }
                }
                else   // Could add foreground colour functionality here.
                {
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Sets the TextDataFormat.Html on the data object to the specified html fragment.
        /// This helper methods takes care of creating the necessary CF_HTML header.
        /// </summary>
        //public static void SetHtml(DataObject dataObject, string htmlFragment)
        //{
        //	if (dataObject == null)
        //		throw new ArgumentNullException("dataObject");
        //	if (htmlFragment == null)
        //		throw new ArgumentNullException("htmlFragment");

        //	string htmlStart = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">" + Environment.NewLine
        //		+ "<HTML>" + Environment.NewLine
        //		+ "<BODY>" + Environment.NewLine
        //		+ "<!--StartFragment-->" + Environment.NewLine;
        //	string htmlEnd = "<!--EndFragment-->" + Environment.NewLine + "</BODY>" + Environment.NewLine + "</HTML>" + Environment.NewLine;
        //	string dummyHeader = BuildHeader(0, 0, 0, 0);
        //	// the offsets are stored as UTF-8 bytes (see CF_HTML documentation)
        //	int startHTML = dummyHeader.Length;
        //	int startFragment = startHTML + htmlStart.Length;
        //	int endFragment = startFragment + Encoding.UTF8.GetByteCount(htmlFragment);
        //	int endHTML = endFragment + htmlEnd.Length;
        //	string cf_html = BuildHeader(startHTML, endHTML, startFragment, endFragment) + htmlStart + htmlFragment + htmlEnd;
        //	Debug.WriteLine(cf_html);
        //	dataObject.SetText(cf_html, TextDataFormat.Html);
        //}

        /// <summary>
        /// Creates a HTML fragment from a part of a document.
        /// </summary>
        /// <param name="document">The document to create HTML from.</param>
        /// <param name="highlighter">The highlighter used to highlight the document. <c>null</c> is valid and will create HTML without any highlighting.</param>
        /// <param name="segment">The part of the document to create HTML for. You can pass <c>null</c> to create HTML for the whole document.</param>
        /// <param name="options">The options for the HTML creation.</param>
        /// <returns>HTML code for the document part.</returns>
        public static string CreateHtmlFragment(IDocument document, IHighlighter highlighter, ISegment segment, HtmlOptions options)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (highlighter != null && highlighter.Document != document)
            {
                throw new ArgumentException("Highlighter does not belong to the specified document.");
            }
            if (segment == null)
            {
                segment = new SimpleSegment(0, document.TextLength);
            }

            var html             = new StringBuilder();
            var segmentEndOffset = segment.EndOffset;
            var line             = document.GetLineByOffset(segment.Offset);

            while (line != null && line.Offset < segmentEndOffset)
            {
                // ReSharper disable once UnusedVariable
                var highlightedLine = highlighter != null?highlighter.HighlightLine(line.LineNumber) : new HighlightedLine(document, line);

                if (html.Length > 0)
                {
                    html.AppendLine("<br>");
                }
                // TODO: html
                var s = SimpleSegment.GetOverlap(segment, line);
                html.Append(highlightedLine.ToHtml(s.Offset, s.EndOffset, options));
                line = line.NextLine;
            }
            return(html.ToString());
        }
        public HighlightedInlineBuilder BuildInlines(int lineNumber)
        {
            HighlightedInlineBuilder builder = new HighlightedInlineBuilder(document.GetLine(lineNumber).Text);

            if (highlighter != null)
            {
                HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber);
                int             startOffset     = highlightedLine.DocumentLine.Offset;
                // copy only the foreground and background colors
                foreach (HighlightedSection section in highlightedLine.Sections)
                {
                    if (section.Color.Foreground != null)
                    {
                        builder.SetForeground(section.Offset - startOffset, section.Length, section.Color.Foreground.GetBrush(null));
                    }
                    if (section.Color.Background != null)
                    {
                        builder.SetBackground(section.Offset - startOffset, section.Length, section.Color.Background.GetBrush(null));
                    }
                }
            }
            return(builder);
        }
Exemplo n.º 12
0
        public static Block ConvertTextDocumentToBlock(TextDocument document, IHighlighter highlighter)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }
//			Table table = new Table();
//			table.Columns.Add(new TableColumn { Width = GridLength.Auto });
//			table.Columns.Add(new TableColumn { Width = new GridLength(1, GridUnitType.Star) });
//			TableRowGroup trg = new TableRowGroup();
//			table.RowGroups.Add(trg);
            Paragraph p = new Paragraph();

            foreach (DocumentLine line in document.Lines)
            {
                int lineNumber = line.LineNumber;
//				TableRow row = new TableRow();
//				trg.Rows.Add(row);
//				row.Cells.Add(new TableCell(new Paragraph(new Run(lineNumber.ToString()))) { TextAlignment = TextAlignment.Right });
                HighlightedInlineBuilder inlineBuilder = new HighlightedInlineBuilder(document.GetText(line));
                if (highlighter != null)
                {
                    HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber);
                    int             lineStartOffset = line.Offset;
                    foreach (HighlightedSection section in highlightedLine.Sections)
                    {
                        inlineBuilder.SetHighlighting(section.Offset - lineStartOffset, section.Length, section.Color);
                    }
                }
//				Paragraph p = new Paragraph();
//				row.Cells.Add(new TableCell(p));
                p.Inlines.AddRange(inlineBuilder.CreateRuns());
                p.Inlines.Add(new LineBreak());
            }
            return(p);
        }
Exemplo n.º 13
0
        public string GenerateHtml(TextDocument document, IHighlighter highlighter)
        {
            string myMainStyle     = MainStyle;
            string LineNumberStyle = "color: #606060;";

            DocumentLine docline  = null;
            string       textLine = null;

            if (highlighter == null)
            {
                docline = document.GetLineByNumber(1);
            }

            StringWriter output = new StringWriter();

            if (ShowLineNumbers || AlternateLineBackground)
            {
                output.Write("<div");
                WriteStyle(output, myMainStyle);
                output.WriteLine(">");

                int longestNumberLength = 1 + (int)Math.Log10(document.LineCount);

                for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++)
                {
                    HighlightedLine line = null;

                    if (highlighter != null)
                    {
                        line = highlighter.HighlightLine(lineNumber);
                    }
                    else
                    {
                        textLine = document.GetText(docline);
                        docline  = docline.NextLine;
                    }

                    output.Write("<pre");
                    if (AlternateLineBackground && (lineNumber % 2) == 0)
                    {
                        WriteStyle(output, AlternateLineStyle);
                    }
                    else
                    {
                        WriteStyle(output, LineStyle);
                    }
                    output.Write(">");

                    if (ShowLineNumbers)
                    {
                        output.Write("<span");
                        WriteStyle(output, LineNumberStyle);
                        output.Write('>');
                        output.Write(lineNumber.ToString().PadLeft(longestNumberLength));
                        output.Write(":  ");
                        output.Write("</span>");
                    }

                    PrintWords(output, line, textLine);
                    output.WriteLine("</pre>");
                }
                output.WriteLine("</div>");
            }
            else
            {
                output.Write("<pre");
                WriteStyle(output, myMainStyle + LineStyle);
                output.WriteLine(">");
                for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++)
                {
                    HighlightedLine line = highlighter.HighlightLine(lineNumber);
                    PrintWords(output, line, textLine);
                    output.WriteLine();
                }
                output.WriteLine("</pre>");
            }
            if (CreateStylesheet && stylesheet.Length > 0)
            {
                string result = "<style type=\"text/css\">" + stylesheet.ToString() + "</style>" + output.ToString();
                stylesheet = new StringBuilder();
                return(result);
            }
            else
            {
                return(output.ToString());
            }
        }
Exemplo n.º 14
0
		public string GenerateHtml(TextDocument document, IHighlighter highlighter)
		{
			string myMainStyle = MainStyle;
			string LineNumberStyle = "color: #606060;";
			
			StringWriter output = new StringWriter();
			if (ShowLineNumbers || AlternateLineBackground) {
				output.Write("<div");
				WriteStyle(output, myMainStyle);
				output.WriteLine(">");
				
				int longestNumberLength = 1 + (int)Math.Log10(document.LineCount);
				
				for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++) {
					HighlightedLine line = highlighter.HighlightLine(lineNumber);
					output.Write("<pre");
					if (AlternateLineBackground && (lineNumber % 2) == 0) {
						WriteStyle(output, AlternateLineStyle);
					} else {
						WriteStyle(output, LineStyle);
					}
					output.Write(">");
					
					if (ShowLineNumbers) {
						output.Write("<span");
						WriteStyle(output, LineNumberStyle);
						output.Write('>');
						output.Write(lineNumber.ToString().PadLeft(longestNumberLength));
						output.Write(":  ");
						output.Write("</span>");
					}
					
					PrintWords(output, line);
					output.WriteLine("</pre>");
				}
				output.WriteLine("</div>");
			} else {
				output.Write("<pre");
				WriteStyle(output, myMainStyle + LineStyle);
				output.WriteLine(">");
				for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++) {
					HighlightedLine line = highlighter.HighlightLine(lineNumber);
					PrintWords(output, line);
					output.WriteLine();
				}
				output.WriteLine("</pre>");
			}
			if (CreateStylesheet && stylesheet.Length > 0) {
				string result = "<style type=\"text/css\">" + stylesheet.ToString() + "</style>" + output.ToString();
				stylesheet = new StringBuilder();
				return result;
			} else {
				return output.ToString();
			}
		}
Exemplo n.º 15
0
		public static HighlightedInlineBuilder CreateInlineBuilder(Location startPosition, Location endPosition, TextDocument document, IHighlighter highlighter)
		{
			if (startPosition.Line >= 1 && startPosition.Line <= document.LineCount) {
				var matchedLine = document.GetLineByNumber(startPosition.Line);
				HighlightedInlineBuilder inlineBuilder = new HighlightedInlineBuilder(document.GetText(matchedLine));
				if (highlighter != null) {
					HighlightedLine highlightedLine = highlighter.HighlightLine(startPosition.Line);
					int startOffset = highlightedLine.DocumentLine.Offset;
					// copy only the foreground color
					foreach (HighlightedSection section in highlightedLine.Sections) {
						if (section.Color.Foreground != null) {
							inlineBuilder.SetForeground(section.Offset - startOffset, section.Length, section.Color.Foreground.GetBrush(null));
						}
					}
				}
				
				// now highlight the match in bold
				if (startPosition.Column >= 1) {
					if (endPosition.Line == startPosition.Line && endPosition.Column > startPosition.Column) {
						// subtract one from the column to get the offset inside the line's text
						int startOffset = startPosition.Column - 1;
						int endOffset = Math.Min(inlineBuilder.Text.Length, endPosition.Column - 1);
						inlineBuilder.SetFontWeight(startOffset, endOffset - startOffset, FontWeights.Bold);
					}
				}
				return inlineBuilder;
			}
			return null;
		}
Exemplo n.º 16
0
		public static RichText CreateInlineBuilder(TextLocation startPosition, TextLocation endPosition, IDocument document, IHighlighter highlighter)
		{
			if (startPosition.Line >= 1 && startPosition.Line <= document.LineCount) {
				var highlightedLine = highlighter.HighlightLine(startPosition.Line);
				var documentLine = highlightedLine.DocumentLine;
				var inlineBuilder = highlightedLine.ToRichTextModel();
				// reset bold/italics
				inlineBuilder.SetFontWeight(0, documentLine.Length, FontWeights.Normal);
				inlineBuilder.SetFontStyle(0, documentLine.Length, FontStyles.Normal);
				
				// now highlight the match in bold
				if (startPosition.Column >= 1) {
					if (endPosition.Line == startPosition.Line && endPosition.Column > startPosition.Column) {
						// subtract one from the column to get the offset inside the line's text
						int startOffset = startPosition.Column - 1;
						int endOffset = Math.Min(documentLine.Length, endPosition.Column - 1);
						inlineBuilder.SetFontWeight(startOffset, endOffset - startOffset, FontWeights.Bold);
					}
				}
				return new RichText(document.GetText(documentLine), inlineBuilder);
			}
			return null;
		}
Exemplo n.º 17
0
		/// <summary>
		/// Converts an IDocument to a RichText and applies the provided highlighter.
		/// </summary>
		public static RichText ConvertTextDocumentToRichText(IDocument document, IHighlighter highlighter)
		{
			if (document == null)
				throw new ArgumentNullException("document");
			var texts = new List<RichText>();
			for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++) {
				var line = document.GetLineByNumber(lineNumber);
				if (lineNumber > 1)
					texts.Add(line.PreviousLine.DelimiterLength == 2 ? "\r\n" : "\n");
				if (highlighter != null) {
					HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber);
					texts.Add(highlightedLine.ToRichText());
				} else {
					texts.Add(document.GetText(line));
				}
			}
			return RichText.Concat(texts.ToArray());
		}
Exemplo n.º 18
0
 private Block ConvertTextDocumentToBlock(TextDocument document, IHighlighter highlighter)
 {
     if (document == null)
     {
         throw new System.ArgumentNullException("document");
     }
     Paragraph paragraph = new Paragraph();
     foreach (DocumentLine current in document.Lines)
     {
         int lineNumber = current.LineNumber;
         HighlightedInlineBuilder highlightedInlineBuilder = new HighlightedInlineBuilder(document.GetText(current));
         if (highlighter != null)
         {
             HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber);
             int offset = current.Offset;
             foreach (HighlightedSection current2 in highlightedLine.Sections)
             {
                 highlightedInlineBuilder.SetHighlighting(current2.Offset - offset, current2.Length, current2.Color);
             }
         }
         paragraph.Inlines.AddRange(highlightedInlineBuilder.CreateRuns());
         paragraph.Inlines.Add(new LineBreak());
     }
     return paragraph;
 }
Exemplo n.º 19
0
        /// <summary>
        /// Converts a TextDocument to Block.
        /// </summary>
        static Block ConvertTextDocumentToBlock(TextDocument document, IHighlighter highlighter)
        {
            // ref.:  http://community.sharpdevelop.net/forums/t/12012.aspx
              if (document == null)
            throw new ArgumentNullException("document");

              Paragraph p = new Paragraph();

              foreach (DocumentLine line in document.Lines)
              {
            int lineNumber = line.LineNumber;

            HighlightedInlineBuilder inlineBuilder = new HighlightedInlineBuilder(document.GetText(line));

            if (highlighter != null)
            {
              HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber);

              int lineStartOffset = line.Offset;

              foreach (HighlightedSection section in highlightedLine.Sections)
            inlineBuilder.SetHighlighting(section.Offset - lineStartOffset, section.Length, section.Color);
            }

            p.Inlines.AddRange(inlineBuilder.CreateRuns());
            p.Inlines.Add(new LineBreak());
              }

              return p;
        }