Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.1@2521 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
8 changed files with 358 additions and 3 deletions
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
#region Using directives
|
||||
|
||||
using System.Reflection; |
||||
using System.Runtime.CompilerServices; |
||||
using System.Runtime.InteropServices; |
||||
|
||||
#endregion
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("HtmlSyntaxColorizer")] |
||||
[assembly: AssemblyDescription("")] |
||||
[assembly: AssemblyConfiguration("")] |
||||
[assembly: AssemblyCompany("")] |
||||
[assembly: AssemblyProduct("HtmlSyntaxColorizer")] |
||||
[assembly: AssemblyCopyright("")] |
||||
[assembly: AssemblyTrademark("")] |
||||
[assembly: AssemblyCulture("")] |
||||
|
||||
// This sets the default COM visibility of types in the assembly to invisible.
|
||||
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
|
||||
[assembly: ComVisible(false)] |
||||
|
||||
// The assembly version has following format :
|
||||
//
|
||||
// Major.Minor.Build.Revision
|
||||
//
|
||||
// You can specify all the values or you can use the default the Revision and
|
||||
// Build Numbers by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("1.0.*")] |
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
||||
<PropertyGroup> |
||||
<ProjectGuid>{6D17428C-A444-4C26-8FE3-976160F41D97}</ProjectGuid> |
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
||||
<OutputType>Exe</OutputType> |
||||
<RootNamespace>HtmlSyntaxColorizer</RootNamespace> |
||||
<AssemblyName>HtmlSyntaxColorizer</AssemblyName> |
||||
</PropertyGroup> |
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> |
||||
<OutputPath>bin\Debug\</OutputPath> |
||||
<DebugSymbols>True</DebugSymbols> |
||||
<DebugType>Full</DebugType> |
||||
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow> |
||||
<DefineConstants>DEBUG;TRACE</DefineConstants> |
||||
</PropertyGroup> |
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> |
||||
<OutputPath>bin\Release\</OutputPath> |
||||
<DebugSymbols>False</DebugSymbols> |
||||
<DebugType>None</DebugType> |
||||
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow> |
||||
<DefineConstants>TRACE</DefineConstants> |
||||
</PropertyGroup> |
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" /> |
||||
<ItemGroup> |
||||
<Reference Include="ICSharpCode.TextEditor"> |
||||
<HintPath>..\..\bin\ICSharpCode.TextEditor.dll</HintPath> |
||||
<SpecificVersion>False</SpecificVersion> |
||||
</Reference> |
||||
<Reference Include="System" /> |
||||
<Reference Include="System.Data" /> |
||||
<Reference Include="System.Drawing" /> |
||||
<Reference Include="System.Xml" /> |
||||
</ItemGroup> |
||||
<ItemGroup> |
||||
<Compile Include="AssemblyInfo.cs" /> |
||||
<Compile Include="HtmlWriter.cs" /> |
||||
<Compile Include="Main.cs" /> |
||||
</ItemGroup> |
||||
</Project> |
@ -0,0 +1,18 @@
@@ -0,0 +1,18 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 9.00 |
||||
# Visual Studio 2005 |
||||
# SharpDevelop 2.1.0.2513 |
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HtmlSyntaxColorizer", "HtmlSyntaxColorizer.csproj", "{6D17428C-A444-4C26-8FE3-976160F41D97}" |
||||
EndProject |
||||
Global |
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
||||
Debug|Any CPU = Debug|Any CPU |
||||
Release|Any CPU = Release|Any CPU |
||||
EndGlobalSection |
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
||||
{6D17428C-A444-4C26-8FE3-976160F41D97}.Debug|Any CPU.Build.0 = Debug|Any CPU |
||||
{6D17428C-A444-4C26-8FE3-976160F41D97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
||||
{6D17428C-A444-4C26-8FE3-976160F41D97}.Release|Any CPU.Build.0 = Release|Any CPU |
||||
{6D17428C-A444-4C26-8FE3-976160F41D97}.Release|Any CPU.ActiveCfg = Release|Any CPU |
||||
EndGlobalSection |
||||
EndGlobal |
@ -0,0 +1,230 @@
@@ -0,0 +1,230 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <author name="Daniel Grunwald"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using System.Text; |
||||
using System.Xml; |
||||
using ICSharpCode.TextEditor; |
||||
using ICSharpCode.TextEditor.Document; |
||||
|
||||
namespace ICSharpCode.HtmlSyntaxColorizer |
||||
{ |
||||
public class HtmlWriter |
||||
{ |
||||
public bool AlternateLineBackground; |
||||
public bool ShowLineNumbers; |
||||
|
||||
public string MainStyle = "font-size: small; font-family: Consolas, \"Courier New\", Courier, Monospace;"; |
||||
public string LineStyle = "margin: 0em;"; |
||||
public string AlternateLineStyle = "margin: 0em; width: 100%; background-color: #f0f0f0;"; |
||||
|
||||
#region Stylesheet cache
|
||||
/// <summary>
|
||||
/// Specifies whether a CSS stylesheet should be created to reduce the size of the created code.
|
||||
/// The default value is true.
|
||||
/// </summary>
|
||||
public bool CreateStylesheet = true; |
||||
|
||||
/// <summary>
|
||||
/// Specifies the prefix to use in front of generate style class names.
|
||||
/// </summary>
|
||||
public string StyleClassPrefix = "code"; |
||||
|
||||
Dictionary<string, string> stylesheetCache = new Dictionary<string, string>(); |
||||
|
||||
/// <summary>
|
||||
/// Resets the CSS stylesheet cache. Stylesheet classes will be cached on GenerateHtml calls.
|
||||
/// If you want to reuse the HtmlWriter for multiple
|
||||
/// </summary>
|
||||
public void ResetStylesheetCache() |
||||
{ |
||||
stylesheetCache.Clear(); |
||||
} |
||||
|
||||
string GetClass(string style) |
||||
{ |
||||
return stylesheetCache[style]; |
||||
} |
||||
|
||||
void CacheClass(string style, StringBuilder b) |
||||
{ |
||||
if (style == null) return; |
||||
if (!stylesheetCache.ContainsKey(style)) { |
||||
string styleName = StyleClassPrefix + stylesheetCache.Count; |
||||
stylesheetCache[style] = styleName; |
||||
b.Append('.'); |
||||
b.Append(styleName); |
||||
b.Append(" { "); |
||||
b.Append(style); |
||||
b.AppendLine(" }"); |
||||
} |
||||
} |
||||
#endregion
|
||||
|
||||
public string GenerateHtml(string code, string highlighterName) |
||||
{ |
||||
IDocument doc = new DocumentFactory().CreateDocument(); |
||||
doc.HighlightingStrategy = HighlightingManager.Manager.FindHighlighter(highlighterName); |
||||
doc.TextContent = code; |
||||
return GenerateHtml(doc); |
||||
} |
||||
|
||||
HighlightColor currentDefaultTextColor; |
||||
|
||||
public string GenerateHtml(IDocument document) |
||||
{ |
||||
string myMainStyle = MainStyle; |
||||
currentDefaultTextColor = document.HighlightingStrategy.GetColorFor("Default"); |
||||
myMainStyle += " color: " + ColorToString(currentDefaultTextColor.Color) + ";" |
||||
+ " background-color: " + ColorToString(currentDefaultTextColor.BackgroundColor); |
||||
|
||||
string LineNumberStyle; |
||||
HighlightColor lineNumbersColor = document.HighlightingStrategy.GetColorFor("LineNumbers"); |
||||
if (lineNumbersColor != null) { |
||||
LineNumberStyle = " color: " + ColorToString(lineNumbersColor.Color) + ";" |
||||
+ " background-color: " + ColorToString(lineNumbersColor.BackgroundColor); |
||||
} else { |
||||
LineNumberStyle = " color: #606060"; |
||||
} |
||||
|
||||
StringBuilder b = new StringBuilder(); |
||||
if (CreateStylesheet) { |
||||
b.AppendLine("<style type=\"text/css\">"); |
||||
if (ShowLineNumbers || AlternateLineBackground) { |
||||
CacheClass(myMainStyle, b); |
||||
CacheClass(LineStyle, b); |
||||
} else { |
||||
CacheClass(myMainStyle + LineStyle, b); |
||||
} |
||||
if (AlternateLineBackground) CacheClass(AlternateLineStyle, b); |
||||
if (ShowLineNumbers) CacheClass(LineNumberStyle, b); |
||||
foreach (LineSegment ls in document.LineSegmentCollection) { |
||||
foreach (TextWord word in ls.Words) { |
||||
CacheClass(GetStyle(word), b); |
||||
} |
||||
} |
||||
b.AppendLine("</style>"); |
||||
} |
||||
if (ShowLineNumbers || AlternateLineBackground) { |
||||
b.Append("<div"); |
||||
WriteStyle(myMainStyle, b); |
||||
b.AppendLine(">"); |
||||
|
||||
int longestNumberLength = 1 + (int)Math.Log10(document.TotalNumberOfLines); |
||||
|
||||
int lineNumber = 1; |
||||
foreach (LineSegment lineSegment in document.LineSegmentCollection) { |
||||
b.Append("<pre"); |
||||
if (AlternateLineBackground && (lineNumber % 2) == 0) { |
||||
WriteStyle(AlternateLineStyle, b); |
||||
} else { |
||||
WriteStyle(LineStyle, b); |
||||
} |
||||
b.Append(">"); |
||||
|
||||
if (ShowLineNumbers) { |
||||
b.Append("<span"); |
||||
WriteStyle(LineNumberStyle, b); |
||||
b.Append('>'); |
||||
b.Append(lineNumber.ToString().PadLeft(longestNumberLength)); |
||||
b.Append(": "); |
||||
b.Append("</span>"); |
||||
} |
||||
|
||||
|
||||
if (lineSegment.Words.Count == 0) { |
||||
b.Append(" "); |
||||
} else { |
||||
PrintWords(lineSegment, b); |
||||
} |
||||
b.AppendLine("</pre>"); |
||||
|
||||
lineNumber++; |
||||
} |
||||
b.AppendLine("</div>"); |
||||
} else { |
||||
b.Append("<pre"); |
||||
WriteStyle(myMainStyle + LineStyle, b); |
||||
b.AppendLine(">"); |
||||
foreach (LineSegment lineSegment in document.LineSegmentCollection) { |
||||
PrintWords(lineSegment, b); |
||||
b.AppendLine(); |
||||
} |
||||
b.AppendLine("</pre>"); |
||||
} |
||||
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("</span>"); |
||||
if (newSpan != null) { |
||||
b.Append("<span"); |
||||
WriteStyle(newSpan, b); |
||||
b.Append('>'); |
||||
} |
||||
currentSpan = newSpan; |
||||
} |
||||
b.Append(HtmlEncode(word.Word)); |
||||
} |
||||
} |
||||
if (currentSpan != null) b.Append("</span>"); |
||||
} |
||||
|
||||
static string HtmlEncode(string word) |
||||
{ |
||||
return word.Replace("&", "&").Replace("<", "<").Replace(">", ">"); |
||||
} |
||||
|
||||
void WriteStyle(string style, StringBuilder b) |
||||
{ |
||||
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; |
||||
|
||||
string style = "color: " + ColorToString(word.Color) + ";"; |
||||
if (word.Bold) { |
||||
style += " font-weight: bold;"; |
||||
} |
||||
if (word.Italic) { |
||||
style += " font-style: italic;"; |
||||
} |
||||
if (word.SyntaxColor.HasBackground) { |
||||
style += " background-color: " + ColorToString(word.SyntaxColor.BackgroundColor) + ";"; |
||||
} |
||||
return style; |
||||
} |
||||
|
||||
string ColorToString(System.Drawing.Color color) |
||||
{ |
||||
return "#" + color.R.ToString("x2") + color.G.ToString("x2") + color.B.ToString("x2"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <author name="Daniel Grunwald"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
using System; |
||||
using System.Diagnostics; |
||||
using System.IO; |
||||
using System.Collections.Generic; |
||||
|
||||
namespace ICSharpCode.HtmlSyntaxColorizer |
||||
{ |
||||
class MainClass |
||||
{ |
||||
public static void Main(string[] args) |
||||
{ |
||||
HtmlWriter w = new HtmlWriter(); |
||||
w.ShowLineNumbers = true; |
||||
w.AlternateLineBackground = true; |
||||
string code = File.ReadAllText("../../Main.cs"); |
||||
string html = w.GenerateHtml(code, "C#"); |
||||
File.WriteAllText("output.html", "<html><body>" + html + "</body></html>"); |
||||
Process.Start("output.html"); // view in browser
|
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue