public static ProcessResult Process(ProcessRequest req) { var result = new ProcessResult(); req.SourceFileName = req.SourceFileName.Replace("\\", "/"); //try to get a cached value first if (!string.IsNullOrEmpty(req.SourceFileName)) { var cached = _Tracker.GetResult(req.SourceFileName); if (cached != null) { Console.WriteLine("Processing: serving {0} from cache", req.SourceFileName.Replace(req.SiteConfig.WorkingDir, "")); var cachedText = new StringBuilder(cached.Text.ToString()); //even if a final page is cached, we still may need to mix vars into its output var cacheMatches = CommandProcessor.GetCommandMatchesFromText(cached.Text.ToString()) .Where(i => i.CommandName == "var" || i.CommandName == "vars" || i.CommandName == "getvar") .Where(i => !req.ExcludeCommands.Contains(i.CommandName)); var fvars = CompileVars(req.ItemConfig, req.Vars, cachedText, cacheMatches); fvars.MixIn(cached.Vars); foreach (var match in cacheMatches) { cachedText = ProcessMatch(req, cachedText, fvars, match); } result.Vars = fvars; result.Text = cachedText; return(result); } } //read the actual item's text string text = File.ReadAllText(req.SourceFileName); var sb = new StringBuilder(text); //get all matches var matches = CommandProcessor.GetCommandMatchesFromText(text); var finalVars = CompileVars(req.ItemConfig, req.Vars, sb, matches); foreach (var match in matches) { sb = ProcessMatch(req, sb, finalVars, match); } result.Text = sb; result.Vars = finalVars; if (!string.IsNullOrEmpty(req.SourceFileName)) { _Tracker.AddResult(req, result); } return(result); }
public static ProcessRequest FromExistingRequest(ProcessRequest req, string sourceFileName, Dictionary <string, string> vars = null) { var v = new Dictionary <string, string>(); v.MixIn(req.Vars); v.MixIn(vars); return(new ProcessRequest() { SiteConfig = req.SiteConfig, SourceFileName = sourceFileName, Vars = v }); }
private static StringBuilder ProcessMatch(ProcessRequest req, StringBuilder sb, Dictionary <string, string> finalVars, CommandMatch match) { Console.WriteLine("Processing {0} :: {1}", req.SourceFileName, match.Match.Value); if (_Commands.ContainsKey(match.CommandName.ToLower())) { sb = _Commands[match.CommandName.ToLower()].Run(new CommandContext(req, sb, match, finalVars)); } else { Console.WriteLine("Processing: {0} does not match a registered command", match.CommandName); } return(sb); }
public void AddResult(ProcessRequest req, ProcessResult res) { _Results[req.SourceFileName] = res; }