/// <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); }
/// <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); }
/// <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)); }
/// <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")); }
public static string StripHtml(string input) { return(input.IsNullOrWhiteSpace() ? input : MyRegex.Replace(input, @"<.*?>", string.Empty)); }