From bfa2870ed3a6260c35b2a662b1f0d1f804630463 Mon Sep 17 00:00:00 2001 From: bashirs Date: Thu, 14 Feb 2013 10:09:49 -0800 Subject: [PATCH] Allowed for alias names for language ids --- .../Common/LanguageRepositoryFacts.cs | 30 +++++++++++++++++++ ColorCode.Facts/Stubs/StubLanguage.cs | 7 ++++- ColorCode/Common/LanguageRepository.cs | 10 +++++++ ColorCode/Compilation/Languages/Asax.cs | 5 ++++ ColorCode/Compilation/Languages/Ashx.cs | 5 ++++ ColorCode/Compilation/Languages/Aspx.cs | 5 ++++ ColorCode/Compilation/Languages/AspxCs.cs | 14 +++++++++ ColorCode/Compilation/Languages/AspxVb.cs | 14 +++++++++ ColorCode/Compilation/Languages/CSharp.cs | 13 ++++++++ ColorCode/Compilation/Languages/Cpp.cs | 13 ++++++++ ColorCode/Compilation/Languages/Css.cs | 5 ++++ ColorCode/Compilation/Languages/Html.cs | 5 ++++ ColorCode/Compilation/Languages/Java.cs | 5 ++++ ColorCode/Compilation/Languages/JavaScript.cs | 12 ++++++++ ColorCode/Compilation/Languages/Php.cs | 14 +++++++++ ColorCode/Compilation/Languages/PowerShell.cs | 13 ++++++++ ColorCode/Compilation/Languages/Sql.cs | 5 ++++ ColorCode/Compilation/Languages/VbDotNet.cs | 16 ++++++++++ ColorCode/Compilation/Languages/Xml.cs | 5 ++++ ColorCode/ILanguage.cs | 5 ++++ 20 files changed, 200 insertions(+), 1 deletion(-) diff --git a/ColorCode.Facts/Common/LanguageRepositoryFacts.cs b/ColorCode.Facts/Common/LanguageRepositoryFacts.cs index 14bb533..1e13a47 100644 --- a/ColorCode.Facts/Common/LanguageRepositoryFacts.cs +++ b/ColorCode.Facts/Common/LanguageRepositoryFacts.cs @@ -44,6 +44,36 @@ namespace ColorCode.Common Assert.Equal(expected, actual); } + [Fact] + public void Will_find_a_loaded_language_with_a_matching_alias() + { + var expected = new StubLanguage(); + expected.id__getValue = "fnord"; + expected.Alias_getValue = "alias"; + var loadedLanguages = new Dictionary(); + loadedLanguages.Add(expected.Id, expected); + var languageRepository = new LanguageRepository(loadedLanguages); + + ILanguage actual = languageRepository.FindById(expected.Alias_getValue); + + Assert.Equal(expected, actual); + } + + [Fact] + public void Will_return_null_if_no_loaded_languages_match_the_language_identifier_nor_the_alias() + { + var expected = new StubLanguage(); + expected.id__getValue = "fnord"; + expected.Alias_getValue = "alias"; + var loadedLanguages = new Dictionary(); + loadedLanguages.Add(expected.Id, expected); + var languageRepository = new LanguageRepository(loadedLanguages); + + ILanguage actual = languageRepository.FindById("fake"); + + Assert.Null(actual); + } + [Fact] public void Will_return_null_if_no_loaded_languages_match_the_language_identifier() { diff --git a/ColorCode.Facts/Stubs/StubLanguage.cs b/ColorCode.Facts/Stubs/StubLanguage.cs index 10f718b..ee7c9e1 100644 --- a/ColorCode.Facts/Stubs/StubLanguage.cs +++ b/ColorCode.Facts/Stubs/StubLanguage.cs @@ -13,6 +13,7 @@ namespace ColorCode.Stubs public bool FirstLinePattern__getInvoked; public string FirstLinePattern__getValue; public string CssClassName_getValue; + public string Alias_getValue; public string Id { @@ -45,7 +46,11 @@ namespace ColorCode.Stubs return rules__getValue; } } - + + public bool HasAlias(string lang) + { + return (lang == Alias_getValue); + } public string CssClassName { diff --git a/ColorCode/Common/LanguageRepository.cs b/ColorCode/Common/LanguageRepository.cs index 837523a..9a5326f 100644 --- a/ColorCode/Common/LanguageRepository.cs +++ b/ColorCode/Common/LanguageRepository.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; namespace ColorCode.Common @@ -32,8 +33,17 @@ namespace ColorCode.Common try { + // If we have a matching name for the language then use it + // otherwise check if any languages have that string as an + // alias. For example: "js" is an alias for Javascript. if (loadedLanguages.ContainsKey(languageId)) + { language = loadedLanguages[languageId]; + } + else + { + language = loadedLanguages.FirstOrDefault(x => x.Value.HasAlias(languageId)).Value; + } } finally { diff --git a/ColorCode/Compilation/Languages/Asax.cs b/ColorCode/Compilation/Languages/Asax.cs index 0d41ab2..79962f4 100644 --- a/ColorCode/Compilation/Languages/Asax.cs +++ b/ColorCode/Compilation/Languages/Asax.cs @@ -101,6 +101,11 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + return false; + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/Ashx.cs b/ColorCode/Compilation/Languages/Ashx.cs index 62d9c40..2275d17 100644 --- a/ColorCode/Compilation/Languages/Ashx.cs +++ b/ColorCode/Compilation/Languages/Ashx.cs @@ -72,6 +72,11 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + return false; + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/Aspx.cs b/ColorCode/Compilation/Languages/Aspx.cs index 9f0d191..e29153e 100644 --- a/ColorCode/Compilation/Languages/Aspx.cs +++ b/ColorCode/Compilation/Languages/Aspx.cs @@ -119,6 +119,11 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + return false; + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/AspxCs.cs b/ColorCode/Compilation/Languages/AspxCs.cs index 30069ed..7e5b32c 100644 --- a/ColorCode/Compilation/Languages/AspxCs.cs +++ b/ColorCode/Compilation/Languages/AspxCs.cs @@ -121,6 +121,20 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + switch (lang.ToLower()) + { + case "aspx-cs": + case "aspx (cs)": + case "aspx(cs)": + return true; + + default: + return false; + } + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/AspxVb.cs b/ColorCode/Compilation/Languages/AspxVb.cs index 9410e10..66ed0e6 100644 --- a/ColorCode/Compilation/Languages/AspxVb.cs +++ b/ColorCode/Compilation/Languages/AspxVb.cs @@ -133,6 +133,20 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + switch (lang.ToLower()) + { + case "aspx-vb": + case "aspx (vb.net)": + case "aspx(vb.net)": + return true; + + default: + return false; + } + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/CSharp.cs b/ColorCode/Compilation/Languages/CSharp.cs index 1aeb7d5..c85687d 100644 --- a/ColorCode/Compilation/Languages/CSharp.cs +++ b/ColorCode/Compilation/Languages/CSharp.cs @@ -97,6 +97,19 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + switch (lang.ToLower()) + { + case "cs": + case "c#": + return true; + + default: + return false; + } + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/Cpp.cs b/ColorCode/Compilation/Languages/Cpp.cs index 30985da..48eefa9 100644 --- a/ColorCode/Compilation/Languages/Cpp.cs +++ b/ColorCode/Compilation/Languages/Cpp.cs @@ -64,6 +64,19 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + switch (lang.ToLower()) + { + case "c++": + case "c": + return true; + + default: + return false; + } + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/Css.cs b/ColorCode/Compilation/Languages/Css.cs index 1f9e976..5d774b0 100644 --- a/ColorCode/Compilation/Languages/Css.cs +++ b/ColorCode/Compilation/Languages/Css.cs @@ -50,6 +50,11 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + return false; + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/Html.cs b/ColorCode/Compilation/Languages/Html.cs index 31e267e..ec6c8dc 100644 --- a/ColorCode/Compilation/Languages/Html.cs +++ b/ColorCode/Compilation/Languages/Html.cs @@ -123,6 +123,11 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + return false; + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/Java.cs b/ColorCode/Compilation/Languages/Java.cs index 6f55109..e7c4e7e 100644 --- a/ColorCode/Compilation/Languages/Java.cs +++ b/ColorCode/Compilation/Languages/Java.cs @@ -70,6 +70,11 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + return false; + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/JavaScript.cs b/ColorCode/Compilation/Languages/JavaScript.cs index 9cb47d0..84a9e6e 100644 --- a/ColorCode/Compilation/Languages/JavaScript.cs +++ b/ColorCode/Compilation/Languages/JavaScript.cs @@ -70,6 +70,18 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + switch (lang.ToLower()) + { + case "js": + return true; + + default: + return false; + } + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/Php.cs b/ColorCode/Compilation/Languages/Php.cs index 7e1831a..53dd096 100644 --- a/ColorCode/Compilation/Languages/Php.cs +++ b/ColorCode/Compilation/Languages/Php.cs @@ -77,6 +77,20 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + switch (lang.ToLower()) + { + case "php3": + case "php4": + case "php5": + return true; + + default: + return false; + } + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/PowerShell.cs b/ColorCode/Compilation/Languages/PowerShell.cs index a58c331..24b110f 100644 --- a/ColorCode/Compilation/Languages/PowerShell.cs +++ b/ColorCode/Compilation/Languages/PowerShell.cs @@ -127,5 +127,18 @@ namespace ColorCode.Compilation.Languages }; } } + + public bool HasAlias(string lang) + { + switch (lang.ToLower()) + { + case "posh": + case "ps1": + return true; + + default: + return false; + } + } } } \ No newline at end of file diff --git a/ColorCode/Compilation/Languages/Sql.cs b/ColorCode/Compilation/Languages/Sql.cs index 87fdd68..f2b04fa 100644 --- a/ColorCode/Compilation/Languages/Sql.cs +++ b/ColorCode/Compilation/Languages/Sql.cs @@ -90,6 +90,11 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + return false; + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/VbDotNet.cs b/ColorCode/Compilation/Languages/VbDotNet.cs index a36031f..99bd9f1 100644 --- a/ColorCode/Compilation/Languages/VbDotNet.cs +++ b/ColorCode/Compilation/Languages/VbDotNet.cs @@ -70,6 +70,22 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + switch (lang.ToLower()) + { + case "vb.net": + case "vbnet": + case "vb": + case "visualbasic": + case "visual basic": + return true; + + default: + return false; + } + } + public override string ToString() { return Name; diff --git a/ColorCode/Compilation/Languages/Xml.cs b/ColorCode/Compilation/Languages/Xml.cs index ebff360..c2d5e56 100644 --- a/ColorCode/Compilation/Languages/Xml.cs +++ b/ColorCode/Compilation/Languages/Xml.cs @@ -129,6 +129,11 @@ namespace ColorCode.Compilation.Languages } } + public bool HasAlias(string lang) + { + return false; + } + public override string ToString() { return Name; diff --git a/ColorCode/ILanguage.cs b/ColorCode/ILanguage.cs index fee2384..41851e3 100644 --- a/ColorCode/ILanguage.cs +++ b/ColorCode/ILanguage.cs @@ -34,5 +34,10 @@ namespace ColorCode /// Get the CSS class name to use for a language /// string CssClassName { get; } + + /// + /// Returns true if the specified string is an alias for the language + /// + bool HasAlias(string lang); } } \ No newline at end of file