public static string GetFormattedCode(TokenLanguage language, string code, bool isMvc, string[] highlightedTagNames) { DevExpress.CodeParser.TokenCollection tokens = GetTokens(language, code, isMvc); if (tokens != null) { return(GetFormattedCode(language, code, tokens, highlightedTagNames)); } return(string.Empty); }
public static string GetFormattedCode(TokenLanguage language, string code, bool isMvc, bool isRazor) { TokenCollection tokens = GetTokens(language, code, isMvc, isRazor); if (tokens != null) { return(GetFormattedCode(code, tokens)); } return(string.Empty); }
static string GetFormattedCode(TokenLanguage language, string code, DevExpress.CodeParser.TokenCollection tokens, string[] highlightedTagNames) { if ((int)language == (int)TokenLanguage.Html && highlightedTagNames.Length != 0 && HasHighlightedTagsOnPage(tokens, highlightedTagNames) || HasHighlightedCodeBlockMarker(code)) { return(GetFormattedAspxCode(code, tokens, highlightedTagNames)); } else { return(GetFormattedOtherCode(code, tokens, highlightedTagNames)); } }
static bool HasHighlightedTagsOnPage(DevExpress.CodeParser.TokenCollection tokens, string[] highlightedTagNames) { foreach (CategorizedToken token in tokens) { if (ContainsTokenInTagNames(token.Value, highlightedTagNames)) { return(true); } } return(false); }
static bool IsComplexTag(DevExpress.CodeParser.TokenCollection tokens, int index) { for (int i = index; i < tokens.Count; i++) { if (tokens[i].Value == "/>") { return(false); } if (tokens[i].Value == ">") { return(true); } } return(true); }
static string GetFormattedCode(string code, TokenCollection tokens) { var currentLine = new CodeLine(); var lines = new List <CodeLine>(); int pos = 0; foreach (CategorizedToken token in tokens) { AppendCode(lines, ref currentLine, code.Substring(pos, token.StartPosition - pos), null); AppendCode(lines, ref currentLine, token.Value, CssClasses[token.Category].GetClassName(token.Language)); pos = token.EndPosition; } AppendCode(lines, ref currentLine, code.Substring(pos), null); lines.Add(currentLine); return(MergeCodeLines(lines)); }
static string GetFormattedOtherCode(string code, DevExpress.CodeParser.TokenCollection tokens, string[] highlightedTagNames) { CodeLine currentLine = new CodeLine(); List <CodeLine> lines = new List <CodeLine>(); int position = 0; foreach (CategorizedToken token in tokens) { AppendCode(lines, ref currentLine, code.Substring(position, token.StartPosition - position), null); AppendCode(lines, ref currentLine, token.Value, CssClasses[token.Category].GetClassName(token.Language)); position = token.EndPosition; } AppendCode(lines, ref currentLine, code.Substring(position), null); lines.Add(currentLine); return(MergeCodeLines(lines)); }
static string GetFormattedAspxCode(string code, DevExpress.CodeParser.TokenCollection tokens, string[] highlightedTagNames) { int position = 0; int highlightedTagsCount = 0; bool needCloseHighlightedBlock = false; bool isStartedHighlightedComment = false; bool thisTagIsComplex = true; CodeLine currentLine = new CodeLine(); List <CodeLine> lines = new List <CodeLine>(); StartNotHighlightedBlock(lines); for (int i = 0; i < tokens.Count; i++) { CategorizedToken token = tokens[i] as CategorizedToken; if (token.Value == StartHighlightedCodeBlockMarker) { isStartedHighlightedComment = true; position = token.EndPosition; continue; } if (ContainsTokenInTagNames(token.Value, highlightedTagNames)) { if (tokens[i - 1].Value == "<" && tokens[i + 1].Value != ">") { highlightedTagsCount++; thisTagIsComplex = IsComplexTag(tokens, i); } if (highlightedTagsCount > 0) { if (highlightedTagsCount == 1 && tokens[i + 1].Value != ">" && lines.Count > 0) { StartHighlightedBlockAndCloseNotHighlighted(lines, false); } AppendCodes(lines, ref currentLine, code, position, token); if (tokens[i - 1].Value == "</" && tokens[i + 1].Value == ">") { highlightedTagsCount--; needCloseHighlightedBlock = highlightedTagsCount == 0; } } } else { if (token.Value != StartHighlightedCodeBlockMarker && token.Value != EndHighlightedCodeBlockMarker) { AppendCodes(lines, ref currentLine, code, position, token); } if (lines.Count > 0 && isStartedHighlightedComment) { StartHighlightedBlockAndCloseNotHighlighted(lines, true); isStartedHighlightedComment = false; } if (!thisTagIsComplex && token.Value == "/>" || needCloseHighlightedBlock || token.Value == EndHighlightedCodeBlockMarker) { EndHighlightedBlockAndStartNotHighlighted(ref currentLine); } if (!thisTagIsComplex && token.Value == "/>") { if (highlightedTagsCount > 0) { highlightedTagsCount--; } } if (needCloseHighlightedBlock) { needCloseHighlightedBlock = false; } } position = token.EndPosition; } AppendCode(lines, ref currentLine, code.Substring(position), null); EndNotHighlightedBlock(ref currentLine); lines.Add(currentLine); return(MergeCodeLines(lines)); }