/// <summary> /// Syntax highlights the given source code using the rules and styles of the specified programming language /// </summary> /// <param name="textformatter">The formatter to be called back to apply the appropriate formatting to a specified range of the text</param> /// <param name="language">The programming language in which the text is written</param> /// <param name="text">The source code to be syntax highlighted</param> /// <param name="offset">Optional parameter: the position in the text where the highlightning begins</param> public void Colorize(TextFormatter textformatter, string language, string text, int offset = 0) { // Check for invalid or unavaliable language Language lang; try { lang = this.languages[language]; } catch (KeyNotFoundException e) { throw new InvalidLanguageException(language, e); } if (lang.InvalidRegex) { throw new InvalidRegexException(language); } // Syntax higlightning using the regular expressions foreach (Match match in lang.MasterRegex.Matches(text)) { foreach (LanguageElement element in lang.Elements) { if (match.Groups[element.Name].Success) { // callback to the formatter for every language element found textformatter.ColorRangeOfText(match.Index + offset, match.Length, element.Color, element.CharFormat); // Recursive call if it finds a code block written in a different programming language if (element.SubLanguage != null) this.Colorize(textformatter, element.SubLanguage.Name, text.Substring(match.Index, match.Length), match.Index); } } } }
/// <summary> /// Syntax highlights the given source code using the rules and styles of the specified programming language /// </summary> /// <param name="textformatter">The formatter to be called back to apply the appropriate formatting to a specified range of the text</param> /// <param name="language">The programming language in which the text is written</param> /// <param name="text">The source code to be syntax highlighted</param> /// <param name="offset">Optional parameter: the position in the text where the highlightning begins</param> public void Colorize(TextFormatter textformatter, string language, string text, int offset = 0) { // Check for invalid or unavaliable language Language lang; try { lang = this.languages[language]; } catch (KeyNotFoundException e) { throw new InvalidLanguageException(language, e); } if (lang.InvalidRegex) { throw new InvalidRegexException(language); } // Syntax higlightning using the regular expressions foreach (Match match in lang.MasterRegex.Matches(text)) { foreach (LanguageElement element in lang.Elements) { if (match.Groups[element.Name].Success) { // callback to the formatter for every language element found textformatter.ColorRangeOfText(match.Index + offset, match.Length, element.Color, element.CharFormat); // Recursive call if it finds a code block written in a different programming language if (element.SubLanguage != null) { this.Colorize(textformatter, element.SubLanguage.Name, text.Substring(match.Index, match.Length), match.Index); } } } } }