/// <summary> /// Colors the XAML. /// </summary> /// <param name="line">The line.</param> /// <returns></returns> public static Paragraph FormatCode(string line) { line = Regex.Replace(line, "[ ]*=[ ]*", "=", RegexOptions.Multiline); XmlTokenizer tokenizer = new XmlTokenizer(); XmlTokenizerMode mode = XmlTokenizerMode.OutsideElement; List <XmlToken> tokens = tokenizer.Tokenize(line, ref mode); List <string> tokenTexts = new List <string>(tokens.Count); List <Color> colors = new List <Color>(tokens.Count); int position = 0; foreach (XmlToken token in tokens) { string tokenText = line.Substring(position, token.Length); tokenTexts.Add(tokenText); Color color = XmlTokenizer.ColorForToken(token, tokenText); colors.Add(color); position += token.Length; } Paragraph p = new Paragraph(); for (int i = 0; i < tokenTexts.Count; i++) { Run r = new Run(tokenTexts[i]) { FontFamily = new FontFamily("Segoe UI"), FontSize = 14 }; r.Foreground = new SolidColorBrush(colors[i]); p.Inlines.Add(r); } return(p); }
/// <summary> /// Tokenizes the specified input. /// </summary> /// <param name="input">The input.</param> /// <returns></returns> public static List <XmlToken> Tokenize(string input) { XmlTokenizerMode mode = XmlTokenizerMode.OutsideElement; XmlTokenizer tokenizer = new XmlTokenizer(); return(tokenizer.Tokenize(input, ref mode)); }