public static string Replace(string input, TextFilterCommand command) { // If they didn't pass in a second argument, then we're using String.Empty (so, less "replace" and more "remove" var replaceWith = command.CommandArgs.Count > 1 ? command.CommandArgs[1] : String.Empty; return(input.Replace(command.CommandArgs[0], replaceWith)); }
public static string AddQuerysgtringArg(string input, TextFilterCommand command) { var uriBuilder = new UriBuilder(input); NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query); query[command.CommandArgs["1"]] = command.CommandArgs["2"]; uriBuilder.Query = query.ToString(); return(uriBuilder.Uri.AbsoluteUri); }
public static string ExtractFromXml(string input, TextFilterCommand command) { var doc = new XmlDocument(); doc.LoadXml(input); XmlNode node = doc.DocumentElement.SelectSingleNode(command.CommandArgs[0]); return(node != null ? node.Value : String.Empty); }
public static string Get(string input, TextFilterCommand command) { string url = command.CommandArgs.ContainsKey(0) ? command.CommandArgs[0] : input; if (!url.ToLower().StartsWith("http")) { url = String.Concat("http://", url); } var client = new WebClient(); return(client.DownloadString(url)); }
public void PopulateCommand() { var command = new TextFilterCommand(); command.CommandName = "Replace"; command.CommandArgs.Add("1", "a"); command.CommandArgs.Add("2", "b"); Assert.AreEqual("Replace", command.CommandName); Assert.AreEqual("replace", command.NormalizedCommandName); Assert.AreEqual("a", command.CommandArgs.First().Value); Assert.AreEqual("b", command.CommandArgs.Last().Value); }
public void AddCommandByObject() { var command = new TextFilterCommand(); command.CommandName = "Replace"; command.CommandArgs.Add("a", "b"); var pipeline = new TextFilterPipeline(); pipeline.AddCommand(command); Assert.AreEqual(1, pipeline.Commands.Count()); Assert.AreEqual("Replace", pipeline.Commands.First().CommandName); Assert.AreEqual("b", pipeline.Commands.First().CommandArgs.First().Value); }
public static string Extract(string input, TextFilterCommand command) { var doc = new HtmlDocument(); doc.LoadHtml(input); HtmlNode node = doc.DocumentNode.SelectSingleNode(command.CommandArgs[0]); if (node == null) { node = doc.DocumentNode.QuerySelector(command.CommandArgs[0]); } return(node != null ? node.InnerHtml : String.Empty); }
public static string Format(string input, TextFilterCommand command) { string template = command.CommandArgs.ContainsKey(0) ? command.CommandArgs[0] : String.Empty; template = command.CommandArgs.ContainsKey("template") && !String.IsNullOrWhiteSpace(command.CommandArgs["template"]) ? command.CommandArgs["template"] : template; foreach (var variable in command.Pipeline.Variables) { template = template.Replace(String.Concat("{", variable.Key, "}"), variable.Value.ToString()); } // Escape any remaining {string} patterns, because these will break String.Format... template = Regex.Replace(template, "{([^0-9]+)}", "{{$1}}"); return(String.Format(template, input)); }
public static string TransformXml(string input, TextFilterCommand command) { var arguments = new XsltArgumentList(); // We're going to do this transform using the templae as XSL var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(input); var transform = new XslCompiledTransform(); transform.Load(XmlReader.Create(new StringReader(command.Pipeline.GetVariable(command.CommandArgs[0]).ToString()))); var writer = new StringWriter(); transform.Transform(xmlDoc, arguments, writer); return(writer.ToString()); }
public static string ExtractFromJson(string input, TextFilterCommand command) { var jobject = (JToken)JObject.Parse(input); foreach (string segment in command.CommandArgs["1"].Split('.')) { if (segment[0] == '~') { jobject = jobject[Convert.ToInt32(segment.Replace("~", String.Empty))]; continue; } if (jobject[segment] is JValue) { return(jobject[segment].ToString()); } jobject = jobject[segment]; } return(String.Empty); }
public static string WrapInTag(string input, TextFilterCommand command) { var stringWriter = new StringWriter(); var tagBuilder = new HtmlTextWriter(stringWriter); // The second argument should be the class if (command.CommandArgs.ContainsKey(1)) { tagBuilder.AddAttribute("class", command.CommandArgs[1]); } // The third argument should be the id if (command.CommandArgs.ContainsKey(2)) { tagBuilder.AddAttribute("id", command.CommandArgs[2]); } tagBuilder.RenderBeginTag(command.CommandArgs[0]); tagBuilder.Write(input); tagBuilder.RenderEndTag(); return(stringWriter.ToString()); }
public static string Read(string input, TextFilterCommand command) { string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, command.CommandArgs.First().Value.Replace("/", @"\\").TrimStart(@"\\".ToCharArray())); return(File.ReadAllText(fullPath)); }
public static string Append(string input, TextFilterCommand command) { return(String.Concat(input, "BAZ")); }
public static string Clear(string input, TextFilterCommand command) { return(String.Empty); }
public static string Trim(string input, TextFilterCommand command) { return(input.Trim()); }
public static string ReplaceAll(string input, TextFilterCommand command) { return(command.DefaultArgument); }
public static string Append(string input, TextFilterCommand command) { return(String.Concat(input, command.DefaultArgument)); }
public static string MyMethod(string input, TextFilterCommand command) { return("MyMethod"); }