Created
January 28, 2011 02:39
-
-
Save usagi/799732 to your computer and use it in GitHub Desktop.
The test of strange slug from a title with Japanese on Orchard CMS 1.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text.RegularExpressions; | |
using System.Text; | |
using System.Globalization; | |
namespace code_test_dotnet | |
{ | |
/// <summary> | |
/// its dummy for the test | |
/// </summary> | |
public interface IEventHandler { } | |
public interface ISlugEventHandler : IEventHandler | |
{ | |
void FillingSlugFromTitle(FillSlugContext context); | |
void FilledSlugFromTitle(FillSlugContext context); | |
} | |
public class FillSlugContext | |
{ | |
public FillSlugContext(string slug) | |
{ | |
Slug = slug; | |
} | |
public string Slug { get; set; } | |
public bool Adjusted { get; set; } | |
} | |
/// <summary> | |
/// its dummy for the test | |
/// </summary> | |
public class Model | |
{ | |
public string Title { get; set; } | |
public string Slug { get; set; } | |
public override string ToString() | |
{ return "Title: " + Title + "\n" + "Slug : " + Slug + "\n"; } | |
} | |
class Program | |
{ | |
IEnumerable<ISlugEventHandler> _slugEventHandlers = new List<ISlugEventHandler>(); | |
string RemoveDiacritics(string slug) | |
{ | |
string stFormD = slug.Normalize(NormalizationForm.FormD); | |
StringBuilder sb = new StringBuilder(); | |
for (int ich = 0; ich < stFormD.Length; ich++) | |
{ | |
UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]); | |
if (uc != UnicodeCategory.NonSpacingMark) | |
{ | |
sb.Append(stFormD[ich]); | |
} | |
} | |
return (sb.ToString().Normalize(NormalizationForm.FormC)); | |
} | |
/// <summary> | |
/// its "FillSlugFromTitle" fake for the test | |
/// </summary> | |
void test(ref Model model) | |
{ | |
FillSlugContext slugContext = new FillSlugContext(model.Title); | |
foreach (ISlugEventHandler slugEventHandler in _slugEventHandlers) | |
{ | |
slugEventHandler.FillingSlugFromTitle(slugContext); | |
} | |
if (!slugContext.Adjusted) | |
{ | |
var disallowed = new Regex(@"[/:?#\[\]@!$&'()*+,;=\s\""\<\>]+"); | |
slugContext.Slug = disallowed.Replace(slugContext.Slug, "-").Trim('-'); | |
if (slugContext.Slug.Length > 1000) | |
slugContext.Slug = slugContext.Slug.Substring(0, 1000); | |
// dots are not allowed at the begin and the end of routes | |
slugContext.Slug = RemoveDiacritics(slugContext.Slug.Trim('.').ToLower()); | |
} | |
foreach (ISlugEventHandler slugEventHandler in _slugEventHandlers) | |
{ | |
slugEventHandler.FilledSlugFromTitle(slugContext); | |
} | |
model.Slug = slugContext.Slug; | |
} | |
static void Main() | |
{ | |
var p = new Program(); | |
var m = new Model() { Title = "じゅんぱな"}; | |
p.test(ref m); | |
Console.WriteLine(m); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment