public static string convertPlugin(string text, PluginManager pluginManager) { //match group index. const int mgiAllText = 0; const int mgiEscapes = 1; const int mgiMarks = 2; const int mgiOpenMarks = 3; const int mgiCloseMarks = 4; Stack<PluginMatch> matchStack = new Stack<PluginMatch>(); Match match = null; int nextMatchIndex = 0; while (true) { match = KARAS.RegexPlugin.Match(text, nextMatchIndex); if (match.Success == false) { break; } if (match.Groups[mgiEscapes].Length % 2 == 1) { nextMatchIndex = match.Groups[mgiMarks].Index + 1; continue; } if (match.Groups[mgiOpenMarks].Length != 0) { PluginMatch pluginMatch = new PluginMatch(); pluginMatch.index = match.Groups[mgiMarks].Index; pluginMatch.marks = match.Groups[mgiMarks].Value; matchStack.Push(pluginMatch); nextMatchIndex = match.Groups[mgiAllText].Index + match.Groups[mgiAllText].Length; continue; } if (matchStack.Count == 0) { nextMatchIndex = match.Groups[mgiAllText].Index + match.Groups[mgiAllText].Length; continue; } PluginMatch preMatch = matchStack.Pop(); int markedupTextIndex = preMatch.index + preMatch.marks.Length; string markedupText = text.Substring (markedupTextIndex, match.Groups[mgiAllText].Index - markedupTextIndex); string openMarks = removeWhiteSpace(preMatch.marks); string closeMarks = removeWhiteSpace(match.Groups[mgiCloseMarks].Value); string newText = constructPluginText (text, markedupText, openMarks, closeMarks, pluginManager); int markDiff = openMarks.Length - closeMarks.Length; if (markDiff > 0) { openMarks = openMarks.Remove(markDiff); closeMarks = ""; if (markDiff > 1) { preMatch.marks = openMarks.Remove(markDiff); matchStack.Push(preMatch); } } else { openMarks = ""; closeMarks = closeMarks.Remove(-markDiff); } newText = openMarks + newText + closeMarks; //It is important to trim close marks to exclude whitespace out of syntax. text = removeAndInsertText(text, preMatch.index, match.Groups[mgiAllText].Index + match.Groups[mgiAllText].Value.Trim().Length - preMatch.index, newText); nextMatchIndex = preMatch.index + newText.Length - closeMarks.Length; } return text; }
public static string replaceTextInPluginSyntax(string text, string oldText, string newText) { //match group index. const int mgiAllText = 0; const int mgiEscapes = 1; const int mgiMarks = 2; const int mgiOpenMarks = 3; //const int mgiCloseMarks = 4; Stack<PluginMatch> matchStack = new Stack<PluginMatch>(); Match match = null; int nextMatchIndex = 0; while (true) { match = KARAS.RegexPlugin.Match(text, nextMatchIndex); if (match.Success == false) { break; } if (match.Groups[mgiEscapes].Length % 2 == 1) { nextMatchIndex = match.Groups[mgiMarks].Index + 1; continue; } if (match.Groups[mgiOpenMarks].Length != 0) { PluginMatch pluginMatch = new PluginMatch(); pluginMatch.index = match.Groups[mgiMarks].Index; matchStack.Push(pluginMatch); nextMatchIndex = match.Groups[mgiAllText].Index + match.Groups[mgiAllText].Length; continue; } if (matchStack.Count == 0) { nextMatchIndex = match.Groups[mgiAllText].Index + match.Groups[mgiAllText].Length; continue; } PluginMatch preMatch = matchStack.Pop(); string markedupText = text.Substring (preMatch.index, match.Groups[mgiMarks].Index - preMatch.index); markedupText = markedupText.Replace(oldText, newText); text = KARAS.removeAndInsertText(text, preMatch.index, match.Groups[mgiMarks].Index - preMatch.index, markedupText); nextMatchIndex = preMatch.index + markedupText.Length; } return text; }