Ejemplo n.º 1
0
        /// <summary>
        /// Uses the <tt>Liquid::TemplateParser</tt> regexp to tokenize the passed source
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        internal static List <string> Tokenize(string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return(new List <string>());
            }

            // Trim leading whitespace.
            source = MyRegex.Replace(source, string.Format(@"([ \t]+)?({0}|{1})-", Liquid.VariableStart, Liquid.TagStart), "$2");

            // Trim trailing whitespace.
            source = MyRegex.Replace(source, string.Format(@"-({0}|{1})(\n|\r\n|[ \t]+)?", Liquid.VariableEnd, Liquid.TagEnd), "$1");

            List <string> tokens = Regex.Split(source, Liquid.TemplateParser).ToList();

            // Trim any whitespace elements from the end of the array.
            for (int i = tokens.Count - 1; i > 0; --i)
            {
                if (tokens[i] == string.Empty)
                {
                    tokens.RemoveAt(i);
                }
            }

            // Removes the rogue empty element at the beginning of the array
            if (tokens[0] != null && tokens[0] == string.Empty)
            {
                tokens.Shift();
            }

            return(tokens);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Remove all newlines from the string
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string StripNewlines(string input)
        {
            return(input.IsNullOrWhiteSpace()
                                ? input
                : MyRegex.Replace(input, @"(\r?\n)", String.Empty));

            //: Regex.Replace(input, Environment.NewLine, string.Empty);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Replace occurrences of a string with another
        /// </summary>
        /// <param name="input"></param>
        /// <param name="string"></param>
        /// <param name="replacement"></param>
        /// <returns></returns>
        public static string Replace(string input, string @string, string replacement = "")
        {
            if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(@string))
            {
                return(input);
            }

            return(string.IsNullOrEmpty(input)
                                ? input
                                : MyRegex.Replace(input, @string, replacement));
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Add <br /> tags in front of all newlines in input string
 /// </summary>
 /// <param name="input"></param>
 /// <returns></returns>
 public static string NewlineToBr(string input)
 {
     return(input.IsNullOrWhiteSpace()
             ? input
             : MyRegex.Replace(input, @"(\r?\n)", "<br />$1"));
 }
Ejemplo n.º 5
0
 public static string StripHtml(string input)
 {
     return(input.IsNullOrWhiteSpace()
                         ? input
                         : MyRegex.Replace(input, @"<.*?>", string.Empty));
 }