Added a CSS class based formatter.
Instead of inline HTML being generated, span's are given classes based on the scope they contain. Clsses can be defined in the StyleSheet and are set to default values in the DefaultStyleSheet.
This commit is contained in:
Родитель
432f20251b
Коммит
f81c3cf195
|
@ -36,7 +36,7 @@ namespace ColorCode
|
|||
|
||||
codeColorizer.Colorize(sourceCode, new StubLanguage(), stubFormatter, stubStyleSheet, stubTextWriter);
|
||||
|
||||
Assert.Equal(stubTextWriter, stubFormatter.writeHeader__textWriter);
|
||||
Assert.Equal(stubTextWriter, stubFormatter.WriteHeader__textWriter);
|
||||
Assert.Equal(stubStyleSheet, stubFormatter.WriteHeader__styleSheet);
|
||||
}
|
||||
|
||||
|
|
|
@ -79,6 +79,7 @@
|
|||
<Compile Include="Compilation\CompiledLanguageFacts.cs" />
|
||||
<Compile Include="Compilation\LanguageCompilerFacts.cs" />
|
||||
<Compile Include="Compilation\LanguageRuleFacts.cs" />
|
||||
<Compile Include="Formatting\HtmlClassFormatterFacts.cs" />
|
||||
<Compile Include="Formatting\HtmlFormatterFacts.cs" />
|
||||
<Compile Include="Parsing\LanguageParserFacts.cs" />
|
||||
<Compile Include="Parsing\ScopeFacts.cs" />
|
||||
|
|
|
@ -0,0 +1,229 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using ColorCode.Common;
|
||||
using ColorCode.Parsing;
|
||||
using ColorCode.Stubs;
|
||||
using Xunit;
|
||||
|
||||
namespace ColorCode.Formatting
|
||||
{
|
||||
public class HtmlClassFormatter_Class_Facts
|
||||
{
|
||||
public class WriterHeader_Method_Facts
|
||||
{
|
||||
[Fact]
|
||||
public void It_will_write_the_language_name_into_the_header()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
|
||||
StubStyleSheet stubStyleSheet = new StubStyleSheet { Name__getValue = "fnord", Styles__getValue = new StyleDictionary { } };
|
||||
StubLanguage language = new StubLanguage { CssClassName_getValue = "fnord" };
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
|
||||
formatter.WriteHeader(stubStyleSheet, language, stubTextWriter);
|
||||
|
||||
Assert.Equal("<div class=\"fnord\"><pre>", stubTextWriter.Write__buffer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_will_write_the_header_with_no_class_name_if_language_does_not_specify_one()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
StubStyleSheet stubStyleSheet = new StubStyleSheet { Name__getValue = "fnord", Styles__getValue = new StyleDictionary() };
|
||||
StubLanguage stubLanguage = new StubLanguage { CssClassName_getValue = "" };
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
|
||||
formatter.WriteHeader(stubStyleSheet, stubLanguage, stubTextWriter);
|
||||
|
||||
Assert.Equal("<div><pre>", stubTextWriter.Write__buffer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_will_throw_when_style_sheet_is_null()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
|
||||
Exception ex = Record.Exception(() => formatter.WriteHeader(null, new StubLanguage(), new StubTextWriter()));
|
||||
|
||||
Assert.IsType<ArgumentNullException>(ex);
|
||||
Assert.Equal("styleSheet", ((ArgumentNullException)ex).ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_will_throw_when_language_is_null()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
|
||||
Exception ex = Record.Exception(() => formatter.WriteHeader(new StubStyleSheet(), null, new StubTextWriter()));
|
||||
|
||||
Assert.IsType<ArgumentNullException>(ex);
|
||||
Assert.Equal("language", ((ArgumentNullException)ex).ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_will_throw_when_text_writer_is_null()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
|
||||
Exception ex = Record.Exception(() => formatter.WriteHeader(new StubStyleSheet(), new StubLanguage(), null));
|
||||
|
||||
Assert.IsType<ArgumentNullException>(ex);
|
||||
Assert.Equal("textWriter", ((ArgumentNullException)ex).ParamName);
|
||||
}
|
||||
}
|
||||
|
||||
public class Write_Method_Facts
|
||||
{
|
||||
[Fact]
|
||||
public void It_will_write_parsed_source_code_with_no_scopes()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
StubStyleSheet stubStyleSheet = new StubStyleSheet
|
||||
{
|
||||
Name__getValue = "fnord",
|
||||
Styles__getValue = new StyleDictionary { new Style(ScopeName.PlainText) { Background = Color.White, Foreground = Color.Black } }
|
||||
};
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
|
||||
formatter.Write("the source code", new List<Scope>(), stubStyleSheet, stubTextWriter);
|
||||
|
||||
Assert.Equal("the source code", stubTextWriter.Write__buffer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_will_write_parsed_source_code_with_a_single_scope()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
StubStyleSheet stubStyleSheet = new StubStyleSheet
|
||||
{
|
||||
Name__getValue = "fnord",
|
||||
Styles__getValue = new StyleDictionary { new Style(ScopeName.Keyword) { CssClassName = "keyword" } }
|
||||
};
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
List<Scope> scopes = new List<Scope> { new Scope(ScopeName.Keyword, 0, 5) };
|
||||
|
||||
formatter.Write("false", scopes, stubStyleSheet, stubTextWriter);
|
||||
|
||||
Assert.Equal("<span class=\"keyword\">false</span>", stubTextWriter.Write__buffer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_will_write_parsed_source_code_with_multiple_scopes()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
StubStyleSheet stubStyleSheet = new StubStyleSheet
|
||||
{
|
||||
Name__getValue = "fnord",
|
||||
Styles__getValue = new StyleDictionary
|
||||
{
|
||||
new Style(ScopeName.Keyword) { CssClassName = "keyword" },
|
||||
new Style(ScopeName.String) { CssClassName = "string" }
|
||||
}
|
||||
};
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
List<Scope> scopes = new List<Scope>
|
||||
{
|
||||
new Scope(ScopeName.Keyword, 0, 4),
|
||||
new Scope(ScopeName.Keyword, 13, 5)
|
||||
};
|
||||
|
||||
formatter.Write("bool fnord = false;", scopes, stubStyleSheet, stubTextWriter);
|
||||
|
||||
Assert.Equal("<span class=\"keyword\">bool</span> fnord = <span class=\"keyword\">false</span>;", stubTextWriter.Write__buffer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_will_write_encoded_HTML()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
StubStyleSheet stubStyleSheet = new StubStyleSheet
|
||||
{
|
||||
Name__getValue = "fnord",
|
||||
Styles__getValue = new StyleDictionary { new Style(ScopeName.String) { CssClassName = "string" } }
|
||||
};
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
List<Scope> scopes = new List<Scope> { new Scope(ScopeName.String, 0, 10) };
|
||||
|
||||
formatter.Write("\"a string\"", scopes, stubStyleSheet, stubTextWriter);
|
||||
|
||||
Assert.Equal("<span class=\"string\">"a string"</span>", stubTextWriter.Write__buffer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_will_write_parsed_source_code_with_nested_scopes()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
StubStyleSheet stubStyleSheet = new StubStyleSheet
|
||||
{
|
||||
Name__getValue = "fnord",
|
||||
Styles__getValue = new StyleDictionary
|
||||
{
|
||||
new Style(ScopeName.String) { CssClassName = "string" },
|
||||
new Style(ScopeName.HtmlEntity) { CssClassName = "htmlEntity" }
|
||||
}
|
||||
};
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
List<Scope> scopes = new List<Scope>
|
||||
{
|
||||
new Scope(ScopeName.String, 0, 20)
|
||||
};
|
||||
scopes[0].AddChild(new Scope(ScopeName.HtmlEntity, 0, 6));
|
||||
scopes[0].AddChild(new Scope(ScopeName.HtmlEntity, 14, 6));
|
||||
|
||||
formatter.Write(""a string"", scopes, stubStyleSheet, stubTextWriter);
|
||||
|
||||
Assert.Equal("<span class=\"string\"><span class=\"htmlEntity\">&quot;</span>a string<span class=\"htmlEntity\">&quot;</span></span>", stubTextWriter.Write__buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public class WriterFooter_Method_Facts
|
||||
{
|
||||
[Fact]
|
||||
public void It_will_write_the_footer()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
StubStyleSheet stubStyleSheet = new StubStyleSheet { Name__getValue = "fnord", Styles__getValue = new StyleDictionary { } };
|
||||
StubLanguage stubLanguage = new StubLanguage { CssClassName_getValue = "fnord" };
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
|
||||
formatter.WriteFooter(stubStyleSheet, stubLanguage, stubTextWriter);
|
||||
|
||||
Assert.Equal("</pre></div>", stubTextWriter.Write__buffer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_will_throw_when_style_sheet_is_null()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
|
||||
Exception ex = Record.Exception(() => formatter.WriteFooter(null, new StubLanguage(), new StubTextWriter()));
|
||||
|
||||
Assert.IsType<ArgumentNullException>(ex);
|
||||
Assert.Equal("styleSheet", ((ArgumentNullException)ex).ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_will_throw_when_language_is_null()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
|
||||
Exception ex = Record.Exception(() => formatter.WriteFooter(new StubStyleSheet(), null, new StubTextWriter()));
|
||||
|
||||
Assert.IsType<ArgumentNullException>(ex);
|
||||
Assert.Equal("language", ((ArgumentNullException)ex).ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_will_throw_when_text_writer_is_null()
|
||||
{
|
||||
HtmlClassFormatter formatter = new HtmlClassFormatter();
|
||||
|
||||
Exception ex = Record.Exception(() => formatter.WriteFooter(new StubStyleSheet(), new StubLanguage(), null));
|
||||
|
||||
Assert.IsType<ArgumentNullException>(ex);
|
||||
Assert.Equal("textWriter", ((ArgumentNullException)ex).ParamName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,7 +21,7 @@ namespace ColorCode.Formatting
|
|||
StubStyleSheet stubStyleSheet = new StubStyleSheet { Name__getValue = "fnord", Styles__getValue = new StyleDictionary { new Style(ScopeName.PlainText) { Background = Color.White, Foreground = Color.Black } } };
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
|
||||
formatter.WriteHeader(stubStyleSheet, stubTextWriter);
|
||||
formatter.WriteHeader(stubStyleSheet, new StubLanguage(), stubTextWriter);
|
||||
|
||||
Assert.Equal("<div style=\"color:Black;background-color:White;\"><pre>", stubTextWriter.Write__buffer);
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ namespace ColorCode.Formatting
|
|||
StubStyleSheet stubStyleSheet = new StubStyleSheet { Name__getValue = "fnord", Styles__getValue = new StyleDictionary() };
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
|
||||
formatter.WriteHeader(stubStyleSheet, stubTextWriter);
|
||||
formatter.WriteHeader(stubStyleSheet, new StubLanguage(), stubTextWriter);
|
||||
|
||||
Assert.Equal("<div><pre>", stubTextWriter.Write__buffer);
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ namespace ColorCode.Formatting
|
|||
StubStyleSheet stubStyleSheet = new StubStyleSheet { Name__getValue = "fnord", Styles__getValue = new StyleDictionary { new Style(ScopeName.PlainText) { Foreground = Color.Black } } };
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
|
||||
formatter.WriteHeader(stubStyleSheet, stubTextWriter);
|
||||
formatter.WriteHeader(stubStyleSheet, new StubLanguage(), stubTextWriter);
|
||||
|
||||
Assert.Equal("<div style=\"color:Black;\"><pre>", stubTextWriter.Write__buffer);
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ namespace ColorCode.Formatting
|
|||
StubStyleSheet stubStyleSheet = new StubStyleSheet { Name__getValue = "fnord", Styles__getValue = new StyleDictionary { new Style(ScopeName.PlainText) { Background = Color.White } } };
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
|
||||
formatter.WriteHeader(stubStyleSheet, stubTextWriter);
|
||||
formatter.WriteHeader(stubStyleSheet, new StubLanguage(), stubTextWriter);
|
||||
|
||||
Assert.Equal("<div style=\"background-color:White;\"><pre>", stubTextWriter.Write__buffer);
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ namespace ColorCode.Formatting
|
|||
{
|
||||
HtmlFormatter formatter = new HtmlFormatter();
|
||||
|
||||
Exception ex = Record.Exception(() => formatter.WriteHeader(null, new StubTextWriter()));
|
||||
Exception ex = Record.Exception(() => formatter.WriteHeader(null, new StubLanguage(), new StubTextWriter()));
|
||||
|
||||
Assert.IsType<ArgumentNullException>(ex);
|
||||
Assert.Equal("styleSheet", ((ArgumentNullException)ex).ParamName);
|
||||
|
@ -78,7 +78,7 @@ namespace ColorCode.Formatting
|
|||
{
|
||||
HtmlFormatter formatter = new HtmlFormatter();
|
||||
|
||||
Exception ex = Record.Exception(() => formatter.WriteHeader(new StubStyleSheet(), null));
|
||||
Exception ex = Record.Exception(() => formatter.WriteHeader(new StubStyleSheet(), new StubLanguage(), null));
|
||||
|
||||
Assert.IsType<ArgumentNullException>(ex);
|
||||
Assert.Equal("textWriter", ((ArgumentNullException)ex).ParamName);
|
||||
|
@ -224,7 +224,7 @@ namespace ColorCode.Formatting
|
|||
StubStyleSheet stubStyleSheet = new StubStyleSheet { Name__getValue = "fnord", Styles__getValue = new StyleDictionary { new Style(ScopeName.PlainText) { Background = Color.White, Foreground = Color.Black } } };
|
||||
StubTextWriter stubTextWriter = new StubTextWriter();
|
||||
|
||||
formatter.WriteFooter(stubStyleSheet, stubTextWriter);
|
||||
formatter.WriteFooter(stubStyleSheet, new StubLanguage(), stubTextWriter);
|
||||
|
||||
Assert.Equal("</pre></div>", stubTextWriter.Write__buffer);
|
||||
}
|
||||
|
@ -234,7 +234,7 @@ namespace ColorCode.Formatting
|
|||
{
|
||||
HtmlFormatter formatter = new HtmlFormatter();
|
||||
|
||||
Exception ex = Record.Exception(() => formatter.WriteFooter(null, new StubTextWriter()));
|
||||
Exception ex = Record.Exception(() => formatter.WriteFooter(null, new StubLanguage(), new StubTextWriter()));
|
||||
|
||||
Assert.IsType<ArgumentNullException>(ex);
|
||||
Assert.Equal("styleSheet", ((ArgumentNullException)ex).ParamName);
|
||||
|
@ -245,7 +245,7 @@ namespace ColorCode.Formatting
|
|||
{
|
||||
HtmlFormatter formatter = new HtmlFormatter();
|
||||
|
||||
Exception ex = Record.Exception(() => formatter.WriteFooter(new StubStyleSheet(), null));
|
||||
Exception ex = Record.Exception(() => formatter.WriteFooter(new StubStyleSheet(), new StubLanguage(), null));
|
||||
|
||||
Assert.IsType<ArgumentNullException>(ex);
|
||||
Assert.Equal("textWriter", ((ArgumentNullException)ex).ParamName);
|
||||
|
|
|
@ -7,9 +7,11 @@ namespace ColorCode.Stubs
|
|||
internal class StubFormatter : IFormatter
|
||||
{
|
||||
public IStyleSheet WriteFooter__styleSheet;
|
||||
public ILanguage WriteFooter__langauge;
|
||||
public TextWriter WriteFooter__writer;
|
||||
public IStyleSheet WriteHeader__styleSheet;
|
||||
public TextWriter writeHeader__textWriter;
|
||||
public ILanguage WriteHeader__language;
|
||||
public TextWriter WriteHeader__textWriter;
|
||||
public Stack<string> Write__parsedSourceCode = new Stack<string>();
|
||||
|
||||
public void Write(string parsedSourceCode,
|
||||
|
@ -21,16 +23,20 @@ namespace ColorCode.Stubs
|
|||
}
|
||||
|
||||
public void WriteFooter(IStyleSheet styleSheet,
|
||||
ILanguage language,
|
||||
TextWriter textWriter)
|
||||
{
|
||||
WriteFooter__writer = textWriter;
|
||||
WriteFooter__langauge = language;
|
||||
WriteFooter__styleSheet = styleSheet;
|
||||
}
|
||||
|
||||
public void WriteHeader(IStyleSheet styleSheet,
|
||||
ILanguage language,
|
||||
TextWriter textWriter)
|
||||
{
|
||||
writeHeader__textWriter = textWriter;
|
||||
WriteHeader__textWriter = textWriter;
|
||||
WriteHeader__language = language;
|
||||
WriteHeader__styleSheet = styleSheet;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ namespace ColorCode.Stubs
|
|||
public bool Rules__getInvoked;
|
||||
public bool FirstLinePattern__getInvoked;
|
||||
public string FirstLinePattern__getValue;
|
||||
public string CssClassName_getValue;
|
||||
|
||||
public string Id
|
||||
{
|
||||
|
@ -44,5 +45,11 @@ namespace ColorCode.Stubs
|
|||
return rules__getValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return CssClassName_getValue; }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -85,11 +85,11 @@ namespace ColorCode
|
|||
Guard.ArgNotNull(styleSheet, "styleSheet");
|
||||
Guard.ArgNotNull(textWriter, "textWriter");
|
||||
|
||||
formatter.WriteHeader(styleSheet, textWriter);
|
||||
formatter.WriteHeader(styleSheet, language, textWriter);
|
||||
|
||||
languageParser.Parse(sourceCode, language, (parsedSourceCode, captures) => formatter.Write(parsedSourceCode, captures, styleSheet, textWriter));
|
||||
|
||||
formatter.WriteFooter(styleSheet, textWriter);
|
||||
formatter.WriteFooter(styleSheet, language, textWriter);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -100,6 +100,7 @@
|
|||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Formatters.cs" />
|
||||
<Compile Include="Formatting\HtmlClassFormatter.cs" />
|
||||
<Compile Include="Formatting\HtmlFormatter.cs" />
|
||||
<Compile Include="Parsing\Scope.cs" />
|
||||
<Compile Include="IFormatter.cs" />
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "ASAX"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "asax"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -16,6 +16,11 @@ namespace ColorCode.Compilation.Languages
|
|||
{
|
||||
get { return "ASHX"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "ashx"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
|
|
|
@ -16,6 +16,11 @@ namespace ColorCode.Compilation.Languages
|
|||
{
|
||||
get { return "ASPX"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "aspx"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "ASPX (C#)"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "aspx-cs"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "ASPX (VB.NET)"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "aspx-vb"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "C#"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "csharp"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "C++"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "cplusplus"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "CSS"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "css"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "HTML"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "html"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "Java"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "java"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "JavaScript"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "javascript"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "PHP"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "php"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "PowerShell"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "powershell"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get { return null; }
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "SQL"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "sql"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "VB.NET"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "vb-net"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace ColorCode.Compilation.Languages
|
|||
get { return "XML"; }
|
||||
}
|
||||
|
||||
public string CssClassName
|
||||
{
|
||||
get { return "xml"; }
|
||||
}
|
||||
|
||||
public string FirstLinePattern
|
||||
{
|
||||
get
|
||||
|
|
|
@ -0,0 +1,146 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using ColorCode.Common;
|
||||
using ColorCode.Parsing;
|
||||
|
||||
namespace ColorCode.Formatting
|
||||
{
|
||||
public class HtmlClassFormatter : IFormatter
|
||||
{
|
||||
public void Write(string parsedSourceCode,
|
||||
IList<Scope> scopes,
|
||||
IStyleSheet styleSheet,
|
||||
TextWriter textWriter)
|
||||
{
|
||||
var styleInsertions = new List<TextInsertion>();
|
||||
|
||||
foreach (Scope scope in scopes)
|
||||
GetStyleInsertionsForCapturedStyle(scope, styleInsertions);
|
||||
|
||||
styleInsertions.SortStable((x, y) => x.Index.CompareTo(y.Index));
|
||||
|
||||
int offset = 0;
|
||||
|
||||
foreach (TextInsertion styleInsertion in styleInsertions)
|
||||
{
|
||||
textWriter.Write(HttpUtility.HtmlEncode(parsedSourceCode.Substring(offset, styleInsertion.Index - offset)));
|
||||
if (string.IsNullOrEmpty(styleInsertion.Text))
|
||||
BuildSpanForCapturedStyle(styleInsertion.Scope, styleSheet, textWriter);
|
||||
else
|
||||
textWriter.Write(styleInsertion.Text);
|
||||
offset = styleInsertion.Index;
|
||||
}
|
||||
|
||||
textWriter.Write(HttpUtility.HtmlEncode(parsedSourceCode.Substring(offset)));
|
||||
}
|
||||
|
||||
public void WriteFooter(IStyleSheet styleSheet,
|
||||
ILanguage language,
|
||||
TextWriter textWriter)
|
||||
{
|
||||
Guard.ArgNotNull(styleSheet, "styleSheet");
|
||||
Guard.ArgNotNull(language, "language");
|
||||
Guard.ArgNotNull(textWriter, "textWriter");
|
||||
|
||||
textWriter.WriteLine();
|
||||
WriteHeaderPreEnd(textWriter);
|
||||
WriteHeaderDivEnd(textWriter);
|
||||
}
|
||||
|
||||
public void WriteHeader(IStyleSheet styleSheet,
|
||||
ILanguage language,
|
||||
TextWriter textWriter)
|
||||
{
|
||||
Guard.ArgNotNull(styleSheet, "styleSheet");
|
||||
Guard.ArgNotNull(language, "language");
|
||||
Guard.ArgNotNull(textWriter, "textWriter");
|
||||
|
||||
WriteHeaderDivStart(styleSheet, language, textWriter);
|
||||
WriteHeaderPreStart(textWriter);
|
||||
textWriter.WriteLine();
|
||||
}
|
||||
|
||||
private static void GetStyleInsertionsForCapturedStyle(Scope scope, ICollection<TextInsertion> styleInsertions)
|
||||
{
|
||||
styleInsertions.Add(new TextInsertion
|
||||
{
|
||||
Index = scope.Index,
|
||||
Scope = scope
|
||||
});
|
||||
|
||||
|
||||
foreach (Scope childScope in scope.Children)
|
||||
GetStyleInsertionsForCapturedStyle(childScope, styleInsertions);
|
||||
|
||||
styleInsertions.Add(new TextInsertion
|
||||
{
|
||||
Index = scope.Index + scope.Length,
|
||||
Text = "</span>"
|
||||
});
|
||||
}
|
||||
|
||||
private static void BuildSpanForCapturedStyle(Scope scope,
|
||||
IStyleSheet styleSheet,
|
||||
TextWriter writer)
|
||||
{
|
||||
string cssClassName = "";
|
||||
|
||||
if (styleSheet.Styles.Contains(scope.Name))
|
||||
{
|
||||
Style style = styleSheet.Styles[scope.Name];
|
||||
|
||||
cssClassName = style.CssClassName;
|
||||
}
|
||||
|
||||
WriteElementStart("span", cssClassName, writer);
|
||||
}
|
||||
|
||||
private static void WriteHeaderDivEnd(TextWriter writer)
|
||||
{
|
||||
WriteElementEnd("div", writer);
|
||||
}
|
||||
|
||||
private static void WriteElementEnd(string elementName,
|
||||
TextWriter writer)
|
||||
{
|
||||
writer.Write("</{0}>", elementName);
|
||||
}
|
||||
|
||||
private static void WriteHeaderPreEnd(TextWriter writer)
|
||||
{
|
||||
WriteElementEnd("pre", writer);
|
||||
}
|
||||
|
||||
private static void WriteHeaderPreStart(TextWriter writer)
|
||||
{
|
||||
WriteElementStart("pre", writer);
|
||||
}
|
||||
|
||||
private static void WriteHeaderDivStart(IStyleSheet styleSheet,
|
||||
ILanguage language,
|
||||
TextWriter writer)
|
||||
{
|
||||
WriteElementStart("div", language.CssClassName, writer);
|
||||
}
|
||||
|
||||
private static void WriteElementStart(string elementName,
|
||||
TextWriter writer)
|
||||
{
|
||||
WriteElementStart(elementName, "", writer);
|
||||
}
|
||||
|
||||
private static void WriteElementStart(string elementName,
|
||||
string cssClassName,
|
||||
TextWriter writer)
|
||||
{
|
||||
writer.Write("<{0}", elementName);
|
||||
if (!String.IsNullOrEmpty(cssClassName)) {
|
||||
writer.Write(" class=\"{0}\"", cssClassName);
|
||||
}
|
||||
writer.Write(">");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -40,6 +40,7 @@ namespace ColorCode.Formatting
|
|||
}
|
||||
|
||||
public void WriteFooter(IStyleSheet styleSheet,
|
||||
ILanguage language,
|
||||
TextWriter textWriter)
|
||||
{
|
||||
Guard.ArgNotNull(styleSheet, "styleSheet");
|
||||
|
@ -51,6 +52,7 @@ namespace ColorCode.Formatting
|
|||
}
|
||||
|
||||
public void WriteHeader(IStyleSheet styleSheet,
|
||||
ILanguage language,
|
||||
TextWriter textWriter)
|
||||
{
|
||||
Guard.ArgNotNull(styleSheet, "styleSheet");
|
||||
|
|
|
@ -27,16 +27,20 @@ namespace ColorCode
|
|||
/// Generates and writes the footer to the output.
|
||||
/// </summary>
|
||||
/// <param name="styleSheet">The style sheet according to which the footer will be generated.</param>
|
||||
/// <param name="language">The language that is having its footer written.</param>
|
||||
/// <param name="textWriter">The text writer to which footer will be written.</param>
|
||||
void WriteFooter(IStyleSheet styleSheet,
|
||||
ILanguage language,
|
||||
TextWriter textWriter);
|
||||
|
||||
/// <summary>
|
||||
/// Generates and writes the header to the output.
|
||||
/// </summary>
|
||||
/// <param name="styleSheet">The style sheet according to which the header will be generated.</param>
|
||||
/// <param name="language">The language that is having its header written.</param>
|
||||
/// <param name="textWriter">The text writer to which header will be written.</param>
|
||||
void WriteHeader(IStyleSheet styleSheet,
|
||||
ILanguage language,
|
||||
TextWriter textWriter);
|
||||
}
|
||||
}
|
|
@ -29,5 +29,10 @@ namespace ColorCode
|
|||
/// Gets the collection of language rules in the language.
|
||||
/// </summary>
|
||||
IList<LanguageRule> Rules { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Get the CSS class name to use for a language
|
||||
/// </summary>
|
||||
string CssClassName { get; }
|
||||
}
|
||||
}
|
|
@ -36,6 +36,11 @@ namespace ColorCode
|
|||
/// </summary>
|
||||
/// <value>The name of the scope the style defines.</value>
|
||||
public string ScopeName { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the name of the CSS class.
|
||||
/// </summary>
|
||||
/// <value>The CSS class name.</value>
|
||||
public string CssClassName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="System.String"/> that represents this instance.
|
||||
|
|
|
@ -17,131 +17,163 @@ namespace ColorCode.Styling.StyleSheets
|
|||
new Style(ScopeName.PlainText)
|
||||
{
|
||||
Foreground = Color.Black,
|
||||
Background = Color.White
|
||||
Background = Color.White,
|
||||
CssClassName = "plainText"
|
||||
},
|
||||
new Style(ScopeName.HtmlServerSideScript)
|
||||
{
|
||||
Background = Color.Yellow
|
||||
Background = Color.Yellow,
|
||||
CssClassName = "htmlServerSideScript"
|
||||
},
|
||||
new Style(ScopeName.HtmlComment)
|
||||
{
|
||||
Foreground = Color.Green
|
||||
Foreground = Color.Green,
|
||||
CssClassName = "htmlComment"
|
||||
},
|
||||
new Style(ScopeName.HtmlTagDelimiter)
|
||||
{
|
||||
Foreground = Color.Blue
|
||||
Foreground = Color.Blue,
|
||||
CssClassName = "htmlTagDelimiter"
|
||||
},
|
||||
new Style(ScopeName.HtmlElementName)
|
||||
{
|
||||
Foreground = DullRed
|
||||
Foreground = DullRed,
|
||||
CssClassName = "htmlElementName"
|
||||
},
|
||||
new Style(ScopeName.HtmlAttributeName)
|
||||
{
|
||||
Foreground = Color.Red
|
||||
Foreground = Color.Red,
|
||||
CssClassName = "htmlAttributeName"
|
||||
},
|
||||
new Style(ScopeName.HtmlAttributeValue)
|
||||
{
|
||||
Foreground = Color.Blue
|
||||
Foreground = Color.Blue,
|
||||
CssClassName = "htmlAttributeValue"
|
||||
},
|
||||
new Style(ScopeName.HtmlOperator)
|
||||
{
|
||||
Foreground = Color.Blue
|
||||
Foreground = Color.Blue,
|
||||
CssClassName = "htmlOperator"
|
||||
},
|
||||
new Style(ScopeName.Comment)
|
||||
{
|
||||
Foreground = Color.Green
|
||||
Foreground = Color.Green,
|
||||
CssClassName = "comment"
|
||||
},
|
||||
new Style(ScopeName.XmlDocTag)
|
||||
{
|
||||
Foreground = Color.Gray
|
||||
Foreground = Color.Gray,
|
||||
CssClassName = "xmlDocTag"
|
||||
},
|
||||
new Style(ScopeName.XmlDocComment)
|
||||
{
|
||||
Foreground = Color.Green
|
||||
Foreground = Color.Green,
|
||||
CssClassName = "xmlDocComment"
|
||||
},
|
||||
new Style(ScopeName.String)
|
||||
{
|
||||
Foreground = DullRed
|
||||
Foreground = DullRed,
|
||||
CssClassName = "string"
|
||||
},
|
||||
new Style(ScopeName.StringCSharpVerbatim)
|
||||
{
|
||||
Foreground = DullRed
|
||||
Foreground = DullRed,
|
||||
CssClassName = "stringCSharpVerbatim"
|
||||
},
|
||||
new Style(ScopeName.Keyword)
|
||||
{
|
||||
Foreground = Color.Blue
|
||||
Foreground = Color.Blue,
|
||||
CssClassName = "keyword"
|
||||
},
|
||||
new Style(ScopeName.PreprocessorKeyword)
|
||||
{
|
||||
Foreground = Color.Blue
|
||||
Foreground = Color.Blue,
|
||||
CssClassName = "preprocessorKeyword"
|
||||
},
|
||||
new Style(ScopeName.HtmlEntity)
|
||||
{
|
||||
Foreground = Color.Red
|
||||
Foreground = Color.Red,
|
||||
CssClassName = "htmlEntity"
|
||||
},
|
||||
new Style(ScopeName.XmlAttribute)
|
||||
{
|
||||
Foreground = Color.Red
|
||||
Foreground = Color.Red,
|
||||
CssClassName = "xmlAttribute"
|
||||
},
|
||||
new Style(ScopeName.XmlAttributeQuotes)
|
||||
{
|
||||
Foreground = Color.Black
|
||||
Foreground = Color.Black,
|
||||
CssClassName = "xmlAttributeQuotes"
|
||||
},
|
||||
new Style(ScopeName.XmlAttributeValue)
|
||||
{
|
||||
Foreground = Color.Blue
|
||||
Foreground = Color.Blue,
|
||||
CssClassName = "xmlAttributeValue"
|
||||
},
|
||||
new Style(ScopeName.XmlCDataSection)
|
||||
{
|
||||
Foreground = Color.Gray
|
||||
Foreground = Color.Gray,
|
||||
CssClassName = "xmlCDataSection"
|
||||
},
|
||||
new Style(ScopeName.XmlComment)
|
||||
{
|
||||
Foreground = Color.Green
|
||||
Foreground = Color.Green,
|
||||
CssClassName = "xmlComment"
|
||||
},
|
||||
new Style(ScopeName.XmlDelimiter)
|
||||
{
|
||||
Foreground = Color.Blue
|
||||
Foreground = Color.Blue,
|
||||
CssClassName = "xmlDelimiter"
|
||||
},
|
||||
new Style(ScopeName.XmlName)
|
||||
{
|
||||
Foreground = DullRed
|
||||
Foreground = DullRed,
|
||||
CssClassName = "xmlName"
|
||||
},
|
||||
new Style(ScopeName.ClassName)
|
||||
{
|
||||
Foreground = Color.MediumTurquoise
|
||||
Foreground = Color.MediumTurquoise,
|
||||
CssClassName = "className"
|
||||
},
|
||||
new Style(ScopeName.CssSelector)
|
||||
{
|
||||
Foreground = DullRed
|
||||
Foreground = DullRed,
|
||||
CssClassName = "cssSelector"
|
||||
},
|
||||
new Style(ScopeName.CssPropertyName)
|
||||
{
|
||||
Foreground = Color.Red
|
||||
Foreground = Color.Red,
|
||||
CssClassName = "cssPropertyName"
|
||||
},
|
||||
new Style(ScopeName.CssPropertyValue)
|
||||
{
|
||||
Foreground = Color.Blue
|
||||
Foreground = Color.Blue,
|
||||
CssClassName = "cssPropertyValue"
|
||||
},
|
||||
new Style(ScopeName.SqlSystemFunction)
|
||||
{
|
||||
Foreground = Color.Magenta
|
||||
Foreground = Color.Magenta,
|
||||
CssClassName = "sqlSystemFunction"
|
||||
},
|
||||
new Style(ScopeName.PowerShellAttribute)
|
||||
{
|
||||
Foreground = Color.PowderBlue
|
||||
Foreground = Color.PowderBlue,
|
||||
CssClassName = "powershellAttribute"
|
||||
},
|
||||
new Style(ScopeName.PowerShellOperator)
|
||||
{
|
||||
Foreground = Color.Gray
|
||||
Foreground = Color.Gray,
|
||||
CssClassName = "powershellOperator"
|
||||
},
|
||||
new Style(ScopeName.PowerShellType)
|
||||
{
|
||||
Foreground = Color.Teal
|
||||
Foreground = Color.Teal,
|
||||
CssClassName = "powershellType"
|
||||
},
|
||||
new Style(ScopeName.PowerShellVariable)
|
||||
{
|
||||
Foreground = Color.OrangeRed
|
||||
Foreground = Color.OrangeRed,
|
||||
CssClassName = "powershellVariable"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче