Esempio n. 1
0
        /// <summary>
        /// Gets the magic-word/variable/function-name/template-name and all params.
        /// </summary>
        /// <example>
        /// "#if:{{{lang|}}}|{{{{{lang}}}}}&nbsp;" should return
        /// "#if", {{{lang|}}}, {{{{{lang}}}}}&nbsp;
        /// </example>
        /// <param name="code">The code to parse.</param>
        /// <param name="args">The parameters, if any.</param>
        /// <param name="logger">An optional logger. May be null.</param>
        /// <returns>The command name.</returns>
        public static string GetMagicWordAndParams(string code, out List<KeyValuePair<string, string>> args, ILogger logger)
        {
            if (string.IsNullOrEmpty(code))
            {
                args = null;
                return null;
            }

            code = code.Trim(WhiteSpaceChars);

            int index = code.IndexOfAny(ParamDelimChars);
            if (index < 0)
            {
                args = null;
                return code;
            }

            List<string> parameters = new List<string>(4);

            if (code[index] == ':')
            {
                // If it's a colon, it's part of the command, include it.
                parameters.Add(code.Substring(0, index + 1).Trim(WhiteSpaceChars));
            }
            else
            {
                parameters.Add(code.Substring(0, index).Trim(WhiteSpaceChars));
            }

            // New-Line is considered a parameter, so don't trim.
            code = code.Substring(index + 1).Trim(WhiteSpaceChars);

            int curParamStart = 0;
            while (true)
            {
                index = FindParam(code, curParamStart);

                if (index >= 0)
                {
                    string param = code.Substring(curParamStart, index - curParamStart);
                    parameters.Add(param.Trim(WhiteSpaceChars));
                    curParamStart = index + 1;
                }
                else
                {
                    // Last param.
                    string param = code.Substring(curParamStart);
                    parameters.Add(param.Trim(WhiteSpaceChars));
                    break;
                }
            }

            if (parameters.Count == 0)
            {
                args = null;
                return null;
            }

            string command = parameters[0];
            parameters.RemoveAt(0);

            args = new List<KeyValuePair<string, string>>(parameters.Count);

            foreach (string parameter in parameters)
            {
                string name;
                string value = parameter.Trim(WhiteSpaceChars);

                // If parameter is whitespace.
                if (value.Length == 0)
                {
                    args.Add(new KeyValuePair<string, string>(string.Empty, parameter));
                    continue;
                }

                // Avoid finding nested magic params.
                int indexOfAssignment = StringUtils.UnboundIndexOfAny(parameter, new[] { '{', '|', '=' }, '<', '>');
                if (indexOfAssignment >= 0 && parameter[indexOfAssignment] == '=')
                {
                    name = parameter.Substring(0, indexOfAssignment);
                    name = name.Trim(WhiteSpaceChars);
                    value = parameter.Substring(indexOfAssignment + 1);
                }
                else
                {
                    name = null;
                    value = parameter;
                }

                value = value.Trim(WhiteSpaceChars);
                args.Add(new KeyValuePair<string, string>(name, value));
            }

            if (logger != null)
            {
                LogEntry logEntry = logger.CreateEntry(
                    Levels.Debug, "Parsed Magic:{0}{1}", Environment.NewLine, command);

                foreach (KeyValuePair<string, string> pair in args)
                {
                    if (!string.IsNullOrEmpty(pair.Key))
                    {
                        logEntry.Properties[pair.Key] = pair.Value;
                    }
                }

                logger.Log(logEntry);
            }

            return command;
        }