Example E5032
Visible to All Users

Syntax highlighting HTML markup using DevExpress CodeParser library and Syntax Highlight tokens

This example demonstrates how to highlight HTML markup content using the ISyntaxHighlightService.

In this example, the following modifications of the mentioned approach were made:

  • the ITokenCategoryHelper instance was created using the ParserLanguageID.Html parameter
  • the TokenCategory.Html..... and TokenCategory.Css...... categories were analyzed in the HighlightCategorizedToken method.

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

WindowsFormsApplication1/Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using DevExpress.XtraRichEdit; using DevExpress.XtraRichEdit.Services; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { richEditControl1.ReplaceService<ISyntaxHighlightService>(new HTMLSyntaxHighlightService(richEditControl1)); using(IRichEditDocumentServer server = richEditControl1.CreateDocumentServer()) { server.Text = "some HTML text"; richEditControl1.Text = server.HtmlText; } richEditControl1.Document.DefaultCharacterProperties.FontName = "Consolas"; richEditControl1.Document.DefaultCharacterProperties.FontSize = 9; } } }
WindowsFormsApplication1/SyntaxHighlighting.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.XtraRichEdit.Services; using DevExpress.XtraRichEdit.API.Native; using DevExpress.CodeParser; using System.Drawing; using DevExpress.XtraRichEdit; namespace WindowsFormsApplication1 { public class HTMLSyntaxHighlightService : ISyntaxHighlightService { readonly RichEditControl syntaxEditor; Dictionary<TokenCategory, SyntaxHighlightProperties> TokensMapping = new Dictionary<TokenCategory, SyntaxHighlightProperties>(); SyntaxHighlightProperties textProperties; void AddTokensMapping(TokenCategory category, Color foreColor) { SyntaxHighlightProperties tokenProperties = new SyntaxHighlightProperties(); tokenProperties.ForeColor = foreColor; TokensMapping.Add(category, tokenProperties); } SyntaxHighlightProperties GetTokensMapping(TokenCategory category) { if(TokensMapping.ContainsKey(category)) return TokensMapping[category]; else return textProperties; } public HTMLSyntaxHighlightService(RichEditControl syntaxEditor) { this.syntaxEditor = syntaxEditor; } void HighlightSyntax(TokenCollection tokens) { if(TokensMapping.Count == 0) { AddTokensMapping(TokenCategory.HtmlAttributeName, Color.Red); AddTokensMapping(TokenCategory.HtmlAttributeValue, Color.Blue); AddTokensMapping(TokenCategory.HtmlComment, Color.Green); AddTokensMapping(TokenCategory.HtmlElementName, Color.Brown); AddTokensMapping(TokenCategory.HtmlEntity, Color.Green); AddTokensMapping(TokenCategory.HtmlOperator, Color.Blue); AddTokensMapping(TokenCategory.HtmlServerSideScript, Color.Gray); AddTokensMapping(TokenCategory.HtmlString, Color.Black); AddTokensMapping(TokenCategory.HtmlTagDelimiter, Color.Blue); AddTokensMapping(TokenCategory.CssComment, Color.Green); AddTokensMapping(TokenCategory.CssKeyword, Color.Red); AddTokensMapping(TokenCategory.CssPropertyName, Color.Red); AddTokensMapping(TokenCategory.CssPropertyValue, Color.Blue); AddTokensMapping(TokenCategory.CssSelector, Color.Brown); AddTokensMapping(TokenCategory.CssStringValue, Color.Blue); textProperties = new SyntaxHighlightProperties(); textProperties.ForeColor = Color.Black; } if(tokens == null || tokens.Count == 0) return; Document document = syntaxEditor.Document; List<SyntaxHighlightToken> syntaxTokens = new List<SyntaxHighlightToken>(tokens.Count); foreach(Token token in tokens) { HighlightCategorizedToken((CategorizedToken)token, syntaxTokens); } document.ApplySyntaxHighlight(syntaxTokens); } void HighlightCategorizedToken(CategorizedToken token, List<SyntaxHighlightToken> syntaxTokens) { Color backColor = syntaxEditor.ActiveView.BackColor; TokenCategory category = token.Category; syntaxTokens.Add(SetTokenColor(token, GetTokensMapping(category), backColor)); } SyntaxHighlightToken SetTokenColor(Token token, SyntaxHighlightProperties foreColor, Color backColor) { if(syntaxEditor.Document.Paragraphs.Count < token.Range.Start.Line) return null; int paragraphStart = DocumentHelper.GetParagraphStart(syntaxEditor.Document.Paragraphs[token.Range.Start.Line - 1]); int tokenStart = paragraphStart + token.Range.Start.Offset - 1; if(token.Range.End.Line != token.Range.Start.Line) paragraphStart = DocumentHelper.GetParagraphStart(syntaxEditor.Document.Paragraphs[token.Range.End.Line - 1]); int tokenEnd = paragraphStart + token.Range.End.Offset - 1; return new SyntaxHighlightToken(tokenStart, tokenEnd - tokenStart, foreColor); } #region #ISyntaxHighlightServiceMembers public void Execute() { string newText = syntaxEditor.Text; // Use DevExpress.CodeParser to parse text into tokens. ITokenCategoryHelper tokenHelper = TokenCategoryHelperFactory.CreateHelper(ParserLanguageID.Html); TokenCollection highlightTokens; highlightTokens = tokenHelper.GetTokens(newText); HighlightSyntax(highlightTokens); } public void ForceExecute() { Execute(); } #endregion #ISyntaxHighlightServiceMembers } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.