Пример #1
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.</param>
        /// <param name="segment">The part of the document to create HTML for. You can pass null 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, DocumentHighlighter highlighter, ISegment segment, HtmlOptions options)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            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);
                }
                else
                {
                    highlightedLine = new HighlightedLine(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());
        }
Пример #2
0
        public Run[] GetInlines(int lineIndex)
        {
            if (_encoding == null)
            {
                return(new Run[0]); //TODO: Refactor whole viewfilelister, syntax, preview
            }

            var docLine = _textDocument.GetLineByNumber(lineIndex);
            var text    = _textDocument.GetText(docLine.Offset, docLine.Length);

            if (_highlighter != null)
            {
                var hLine = _highlighter.HighlightLine(lineIndex);
                return(hLine.ToRichText().CreateRuns());
            }

            var run = new Run(text)
            {
                Foreground = _currentForeground
            };

            return(new[] { run });
        }
Пример #3
0
        // See: http://stackoverflow.com/questions/21558386/avalonedit-getting-syntax-highlighted-text
        public void ApplyHighlighter(string code)
        {
            Inlines.Clear();
            if (string.IsNullOrEmpty(code))
            {
                return;
            }

            var item = this;

            var document    = new TextDocument(code);
            var highlighter = new DocumentHighlighter(document, SyntaxHighlighting);
            var lineCount   = document.LineCount;

            for (var lineNumber = 1; lineNumber <= document.LineCount; lineNumber++)
            {
                var line = highlighter.HighlightLine(lineNumber);

                var lineText = document.GetText(line.DocumentLine);

                var offset = line.DocumentLine.Offset;

                var sectionCount = line.Sections.Count;
                for (var sectionNumber = 0; sectionNumber < sectionCount; sectionNumber++)
                {
                    var section = line.Sections[sectionNumber];

                    //Deal with previous text
                    if (section.Offset > offset)
                    {
                        item.Inlines.Add(new Run(document.GetText(offset, section.Offset - offset)));
                    }

                    var runItem = new Run(document.GetText(section));

                    if (runItem.Foreground != null)
                    {
                        runItem.Foreground = section.Color.Foreground.GetBrush(null);
                    }

                    if (section.Color.FontWeight != null)
                    {
                        runItem.FontWeight = section.Color.FontWeight.Value;
                    }

                    item.Inlines.Add(runItem);

                    offset = section.Offset + section.Length;
                }

                //Deal with stuff at end of line
                var lineEnd = line.DocumentLine.Offset + lineText.Length;
                if (lineEnd > offset)
                {
                    item.Inlines.Add(new Run(document.GetText(offset, lineEnd - offset)));
                }

                //If not last line add a new line
                if (lineNumber < lineCount)
                {
                    item.Inlines.Add(new Run("\n"));
                }
            }
        }