/// <summary> /// Identifies the locations of macros within article or template content. /// </summary> /// <remarks> /// The macro syntax is inspired by Aaron Parecki's extensions to Markdown (https://aaronparecki.com/articles/2012/09/01/1/some-enhancements-to-markdown#macros) in the /// hope of maintaining some consistency and portability between dialects. The mechanics by which macros are implemented is rather different though. /// </remarks> public static List <MacroInvocation> LocateMarkdownMacros(string content) { List <MacroInvocation> invocations = new List <MacroInvocation>(); //Tested on regexr.com - their tool is great (except JavaScript doesn't support lookbehind) string macroRegex = @"(?<!\\)!\[:(\w+)(\s[\w\d]+)*\]\s*(\(([^\)]+)\))?"; Regex re = new Regex(macroRegex); MatchCollection matches = re.Matches(content); foreach (Match m in matches) { string macroName = m.Groups[1].Value; List <string> parms = new List <string>(); List <string> vals = new List <string>(); for (int i = 0; i < m.Groups[2].Captures.Count; i++) { //Push paramters into the list parms.Add(decodeParameterValue(m.Groups[2].Captures[i].Value.Trim())); } //Group 3 is the same as for (int i = 0; i < m.Groups[4].Captures.Count; i++) { //Push value(s?) into the list vals.Add(m.Groups[4].Captures[i].Value.Trim()); } MacroInvocation mi = new MacroInvocation() { MacroName = macroName, Parameters = parms, Values = vals, StartingCharIndex = m.Index, EndingCharIndex = m.Index + m.Length }; invocations.Add(mi); } return(invocations); }
/// <summary> /// Identifies the locations of macros within article or template content. /// </summary> /// <remarks> /// The macro syntax is inspired by Aaron Parecki's extensions to Markdown (https://aaronparecki.com/articles/2012/09/01/1/some-enhancements-to-markdown#macros) in the /// hope of maintaining some consistency and portability between dialects. The mechanics by which macros are implemented is rather different though. /// </remarks> public static List<MacroInvocation> LocateMarkdownMacros(string content) { List<MacroInvocation> invocations = new List<MacroInvocation>(); //Tested on regexr.com - their tool is great (except JavaScript doesn't support lookbehind) string macroRegex = @"(?<!\\)!\[:(\w+)(\s[\w\d]+)*\]\s*(\(([^\)]+)\))?"; Regex re = new Regex(macroRegex); MatchCollection matches = re.Matches(content); foreach (Match m in matches) { string macroName = m.Groups[1].Value; List<string> parms = new List<string>(); List<string> vals = new List<string>(); for (int i = 0; i < m.Groups[2].Captures.Count; i++) { //Push paramters into the list parms.Add(decodeParameterValue(m.Groups[2].Captures[i].Value.Trim())); } //Group 3 is the same as for (int i = 0; i < m.Groups[4].Captures.Count; i++) { //Push value(s?) into the list vals.Add(m.Groups[4].Captures[i].Value.Trim()); } MacroInvocation mi = new MacroInvocation() { MacroName = macroName, Parameters = parms, Values = vals, StartingCharIndex = m.Index, EndingCharIndex = m.Index + m.Length }; invocations.Add(mi); } return invocations; }