Ejemplo n.º 1
0
 public void tokenize_plain_text(string text) {
     var crayonstr = new CrayonString(text);
     var tokens = crayonstr.Tokens;
     
     tokens.Count.ShouldEqual(1);
     var expected = new CrayonString.CrayonToken(text);
     tokens[0].ShouldEqual(expected); 
 }
Ejemplo n.º 2
0
        public CrayonString Colorize(string str)
        {
            var colorized = new CrayonString(str);
            foreach (var pattern in patterns)
            {
                colorized = Colorize(colorized, pattern.Regex);
            }

            return colorized;
        }
Ejemplo n.º 3
0
 public void color_extenions_work()
 {
     var str1 = Crayon.Red("I'm red").Blue("I'm blue");
     var str2 = new CrayonString(":red:I'm red:blue:I'm blue");
     
     str2.ShouldEqual(str1);
     
     str1 = Crayon.Red("I'm red").Blue("I'm blue").Default(" what?");
     str2 = new CrayonString(":red:I'm red:blue:I'm blue:default: what?");
     
     str2.ShouldEqual(str1);            
 }
Ejemplo n.º 4
0
 private CrayonString ColorizeTokens(CrayonString str, Regex regex)
 {
     var tokens = str.Tokens;
     var colorized = new List<CrayonString.CrayonToken>();
     foreach (var token in tokens)
     {
         var ct = ColorizeToken(token, regex);
         colorized.AddRange(ct);
     }
     if (colorized.Last().Color != CrayonColor.Default)
     {
         colorized.Add(new CrayonString.CrayonToken(""));
     }
     return new CrayonString(colorized);
 }
Ejemplo n.º 5
0
        public void plain_string_is_properly_parsed(string str)
        {
            CrayonString crayon = new CrayonString(str);
            var tokens = crayon.Tokenize();

            var sb = new StringBuilder();
            foreach (var token in tokens)
            {
                sb.Append(token.Text);
            }

            var removedExcapes = str.Replace($"{CrayonString.escapeStart}{CrayonString.escapeStart}", $"{CrayonString.escapeStart}");
            
            sb.ToString().ShouldEqual(removedExcapes);
        }
Ejemplo n.º 6
0
        public void colors_are_properly_parsed(string str) {
                  CrayonString crayon = new CrayonString(str);
            var tokens = crayon.Tokenize();

            var sb = new StringBuilder();
            foreach (var token in tokens)
            {
                sb.Append(CrayonString.escapeStart)
                    .Append(token.Color.OriginalName)
                    .Append(CrayonString.escapeEnd);
                sb.Append(token.Text);
            }

            //var removedExcapes = str.Replace($"{CrayonString.escapeStart}{CrayonString.escapeStart}", $"{CrayonString.escapeStart}");
            
            sb.ToString().ShouldEqual(str);       
        }
Ejemplo n.º 7
0
 private List<CrayonString.CrayonToken> ColorizeToken(CrayonString.CrayonToken token, Regex regex) {
     
     if (token.Color != CrayonColor.Default) {
         // do not process colorized tokens
         return new List<CrayonString.CrayonToken>() { token };
     }
     
     var result = new StringBuilder();            
     var text = token.Text;
     var matches = regex.Matches(text);
     var idx = 0;
     
     if (matches.Count > 0)
     {
         var names = regex.GetGroupNames();
         foreach (Match m in matches)
         {
             var replaced = m.Captures[0].Value;
             foreach (var name in names)
             {
                 var i = 0;
                 /// ignore groups without names
                 if (int.TryParse(name, out i)) continue;
                 var group = m.Groups[name];
                 var color = GetColor(name);
                 var coloredString = color.Format(group.Value);
                 replaced = regex.ReplaceGroup(replaced, name, coloredString);
             }
             var before = text.Substring(idx, m.Index - idx);
             result.Append(before);
             result.Append(replaced);
             idx = m.Index + m.Length;
         }
     }
     result.Append(text.Substring(idx, text.Length - idx));
     return new CrayonString(result.ToString()).Tokenize();
 }
Ejemplo n.º 8
0
 public CrayonString Colorize(CrayonString str, Regex regex)
 {
     return ColorizeTokens(str, regex);
 }
Ejemplo n.º 9
0
 private CrayonString ColorizeString(CrayonString str, Regex regex)
 {
     var result = new StringBuilder();
     var matches = regex.Matches(str.Text);
     var idx = 0;
     if (matches.Count > 0)
     {
         var names = regex.GetGroupNames();
         foreach (Match m in matches)
         {
             var replaced = m.Captures[0].Value;
             foreach (var name in names)
             {
                 var i = 0;
                 /// ignore groups without names
                 if (int.TryParse(name, out i)) continue;
                 var group = m.Groups[name];
                 var color = GetColor(name);
                 var coloredString = color.Format(group.Value);
                 replaced = regex.ReplaceGroup(replaced, name, coloredString);
             }
             var before = str.Text.Substring(idx, m.Index - idx);
             result.Append(before);
             result.Append(replaced);
             idx = m.Index + m.Length;
         }
     }
     result.Append(str.Text.Substring(idx, str.Text.Length - idx));
     return new CrayonString(result.ToString());
 }