Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Highlighting/HtmlClipboard.cs @ 16752

Last change on this file since 16752 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 5.2 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Diagnostics;
21using System.Globalization;
22using System.Text;
23using System.Windows;
24using ICSharpCode.AvalonEdit.Document;
25using ICSharpCode.NRefactory.Editor;
26
27namespace ICSharpCode.AvalonEdit.Highlighting
28{
29  /// <summary>
30  /// Allows copying HTML text to the clipboard.
31  /// </summary>
32  public static class HtmlClipboard
33  {
34    /// <summary>
35    /// Builds a header for the CF_HTML clipboard format.
36    /// </summary>
37    static string BuildHeader(int startHTML, int endHTML, int startFragment, int endFragment)
38    {
39      StringBuilder b = new StringBuilder();
40      b.AppendLine("Version:0.9");
41      b.AppendLine("StartHTML:" + startHTML.ToString("d8", CultureInfo.InvariantCulture));
42      b.AppendLine("EndHTML:" + endHTML.ToString("d8", CultureInfo.InvariantCulture));
43      b.AppendLine("StartFragment:" + startFragment.ToString("d8", CultureInfo.InvariantCulture));
44      b.AppendLine("EndFragment:" + endFragment.ToString("d8", CultureInfo.InvariantCulture));
45      return b.ToString();
46    }
47   
48    /// <summary>
49    /// Sets the TextDataFormat.Html on the data object to the specified html fragment.
50    /// This helper methods takes care of creating the necessary CF_HTML header.
51    /// </summary>
52    public static void SetHtml(DataObject dataObject, string htmlFragment)
53    {
54      if (dataObject == null)
55        throw new ArgumentNullException("dataObject");
56      if (htmlFragment == null)
57        throw new ArgumentNullException("htmlFragment");
58     
59      string htmlStart = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">" + Environment.NewLine
60        + "<HTML>" + Environment.NewLine
61        + "<BODY>" + Environment.NewLine
62        + "<!--StartFragment-->" + Environment.NewLine;
63      string htmlEnd = "<!--EndFragment-->" + Environment.NewLine + "</BODY>" + Environment.NewLine + "</HTML>" + Environment.NewLine;
64      string dummyHeader = BuildHeader(0, 0, 0, 0);
65      // the offsets are stored as UTF-8 bytes (see CF_HTML documentation)
66      int startHTML = dummyHeader.Length;
67      int startFragment = startHTML + htmlStart.Length;
68      int endFragment = startFragment + Encoding.UTF8.GetByteCount(htmlFragment);
69      int endHTML = endFragment + htmlEnd.Length;
70      string cf_html = BuildHeader(startHTML, endHTML, startFragment, endFragment) + htmlStart + htmlFragment + htmlEnd;
71      Debug.WriteLine(cf_html);
72      dataObject.SetText(cf_html, TextDataFormat.Html);
73    }
74   
75    /// <summary>
76    /// Creates a HTML fragment from a part of a document.
77    /// </summary>
78    /// <param name="document">The document to create HTML from.</param>
79    /// <param name="highlighter">The highlighter used to highlight the document. <c>null</c> is valid and will create HTML without any highlighting.</param>
80    /// <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>
81    /// <param name="options">The options for the HTML creation.</param>
82    /// <returns>HTML code for the document part.</returns>
83    public static string CreateHtmlFragment(IDocument document, IHighlighter highlighter, ISegment segment, HtmlOptions options)
84    {
85      if (document == null)
86        throw new ArgumentNullException("document");
87      if (options == null)
88        throw new ArgumentNullException("options");
89      if (highlighter != null && highlighter.Document != document)
90        throw new ArgumentException("Highlighter does not belong to the specified document.");
91      if (segment == null)
92        segment = new SimpleSegment(0, document.TextLength);
93     
94      StringBuilder html = new StringBuilder();
95      int segmentEndOffset = segment.EndOffset;
96      IDocumentLine line = document.GetLineByOffset(segment.Offset);
97      while (line != null && line.Offset < segmentEndOffset) {
98        HighlightedLine highlightedLine;
99        if (highlighter != null)
100          highlightedLine = highlighter.HighlightLine(line.LineNumber);
101        else
102          highlightedLine = new HighlightedLine(document, line);
103        SimpleSegment s = SimpleSegment.GetOverlap(segment, line);
104        if (html.Length > 0)
105          html.AppendLine("<br>");
106        html.Append(highlightedLine.ToHtml(s.Offset, s.EndOffset, options));
107        line = line.NextLine;
108      }
109      return html.ToString();
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.