"); + output.Write(">"); if (ShowLineNumbers) { - b.Append("'); - b.Append(lineNumber.ToString().PadLeft(longestNumberLength)); - b.Append(": "); - b.Append(""); - } - - - if (lineSegment.Words.Count == 0) { - b.Append(" "); - } else { - PrintWords(lineSegment, b); + output.Write("'); + output.Write(lineNumber.ToString().PadLeft(longestNumberLength)); + output.Write(": "); + output.Write(""); } - b.AppendLine(""); - lineNumber++; + PrintWords(output, line); + output.WriteLine(""); } - b.AppendLine("
"); - foreach (LineSegment lineSegment in document.LineSegmentCollection) { - PrintWords(lineSegment, b); - b.AppendLine(); + output.Write(""); } - return b.ToString(); - } - - void PrintWords(LineSegment lineSegment, StringBuilder b) - { - string currentSpan = null; - foreach (TextWord word in lineSegment.Words) { - if (word.Type == TextWordType.Space) { - b.Append(' '); - } else if (word.Type == TextWordType.Tab) { - b.Append('\t'); - } else { - string newSpan = GetStyle(word); - if (currentSpan != newSpan) { - if (currentSpan != null) b.Append(""); - if (newSpan != null) { - b.Append("'); - } - currentSpan = newSpan; - } - b.Append(HtmlEncode(word.Word)); - } + if (CreateStylesheet && stylesheet.Length > 0) { + string result = "" + output.ToString(); + stylesheet = new StringBuilder(); + return result; + } else { + return output.ToString(); } - if (currentSpan != null) b.Append(""); } - static string HtmlEncode(string word) + void PrintWords(TextWriter writer, HighlightedLine line) { - return word.Replace("&", "&").Replace("<", "<").Replace(">", ">"); + writer.Write(line.ToHtml(new MyHtmlOptions(this))); } - void WriteStyle(string style, StringBuilder b) + class MyHtmlOptions : HtmlOptions { - if (CreateStylesheet) { - b.Append(" class=\""); - b.Append(GetClass(style)); - b.Append('"'); - } else { - b.Append(" style='"); - b.Append(style); - b.Append("'"); - } - } - - string GetStyle(TextWord word) - { - if (word.SyntaxColor == null || word.SyntaxColor.ToString() == currentDefaultTextColor.ToString()) - return null; + readonly HtmlWriter htmlWriter; - string style = "color: " + ColorToString(word.Color) + ";"; - if (word.Bold) { - style += " font-weight: bold;"; - } - if (word.Italic) { - style += " font-style: italic;"; + internal MyHtmlOptions(HtmlWriter htmlWriter) + { + this.htmlWriter = htmlWriter; } - if (word.SyntaxColor.HasBackground) { - style += " background-color: " + ColorToString(word.SyntaxColor.BackgroundColor) + ";"; + + public override void WriteStyleAttributeForColor(TextWriter writer, HighlightingColor color) + { + htmlWriter.WriteStyle(writer, color.ToCss()); } - return style; } - static string ColorToString(System.Drawing.Color color) + void WriteStyle(TextWriter writer, string style) { - return "#" + color.R.ToString("x2") + color.G.ToString("x2") + color.B.ToString("x2"); + if (CreateStylesheet) { + writer.Write(" class=\""); + writer.Write(GetClass(style)); + writer.Write('"'); + } else { + writer.Write(" style='"); + writer.Write(style); + writer.Write("'"); + } } } } diff --git a/samples/HtmlSyntaxColorizer/Main.cs b/samples/HtmlSyntaxColorizer/Main.cs index 8cae6c5b1c..91e69a7bff 100644 --- a/samples/HtmlSyntaxColorizer/Main.cs +++ b/samples/HtmlSyntaxColorizer/Main.cs @@ -1,5 +1,5 @@ // SharpDevelop samples -// Copyright (c) 2007, AlphaSierraPapa +// Copyright (c) 2010, AlphaSierraPapa // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, are @@ -26,9 +26,11 @@ // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Collections.Generic; + +using ICSharpCode.AvalonEdit.Highlighting; namespace ICSharpCode.HtmlSyntaxColorizer { @@ -36,11 +38,13 @@ namespace ICSharpCode.HtmlSyntaxColorizer { public static void Main(string[] args) { + IHighlightingDefinition highlightDefinition = HighlightingManager.Instance.GetDefinition("C#"); + HtmlWriter w = new HtmlWriter(); w.ShowLineNumbers = true; w.AlternateLineBackground = true; string code = File.ReadAllText("../../Main.cs"); - string html = w.GenerateHtml(code, "C#"); + string html = w.GenerateHtml(code, highlightDefinition); File.WriteAllText("output.html", "" + html + ""); Process.Start("output.html"); // view in browser } diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightedLine.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightedLine.cs index 962e5298eb..5de428006b 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightedLine.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightedLine.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; +using System.IO; using System.Text; using ICSharpCode.AvalonEdit.Document; @@ -79,7 +80,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting return 1; } else { if (IsEnd) - return -Nesting.CompareTo(other.Nesting); + return other.Nesting.CompareTo(Nesting); else return Nesting.CompareTo(other.Nesting); } @@ -121,24 +122,24 @@ namespace ICSharpCode.AvalonEdit.Highlighting elements.Sort(); TextDocument document = this.Document; - StringBuilder b = new StringBuilder(); + StringWriter w = new StringWriter(); int textOffset = startOffset; foreach (HtmlElement e in elements) { int newOffset = Math.Min(e.Offset, endOffset); if (newOffset > startOffset) { - HtmlClipboard.EscapeHtml(b, document.GetText(textOffset, newOffset - textOffset), options); + HtmlClipboard.EscapeHtml(w, document.GetText(textOffset, newOffset - textOffset), options); } textOffset = Math.Max(textOffset, newOffset); if (e.IsEnd) { - b.Append(""); + w.Write(""); } else { - b.Append(""); + w.Write("'); } } - HtmlClipboard.EscapeHtml(b, document.GetText(textOffset, endOffset - textOffset), options); - return b.ToString(); + HtmlClipboard.EscapeHtml(w, document.GetText(textOffset, endOffset - textOffset), options); + return w.ToString(); } ///"); + for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++) { + HighlightedLine line = highlighter.HighlightLine(lineNumber); + PrintWords(output, line); + output.WriteLine(); } - b.AppendLine(""); + output.WriteLine("