private Engine SetupEngine(ResourceCollection resourceCollection, TemplatePreprocessorResource scriptResource, DocumentBuildContext context) { var rootPath = (RelativePath)scriptResource.ResourceName; var engineCache = new Dictionary <string, Engine>(); var utility = new TemplateUtility(context); _utilityObject = new { resolveSourceRelativePath = new Func <string, string, string>(utility.ResolveSourceRelativePath) }; var engine = CreateDefaultEngine(); var requireAction = new Func <string, object>( s => { if (!s.StartsWith(RequireRelativePathPrefix)) { throw new ArgumentException($"Only relative path starting with `{RequireRelativePathPrefix}` is supported in require"); } var relativePath = (RelativePath)s.Substring(RequireRelativePathPrefix.Length); s = relativePath.BasedOn(rootPath); var script = resourceCollection?.GetResource(s); if (string.IsNullOrWhiteSpace(script)) { return(null); } Engine cachedEngine; if (!engineCache.TryGetValue(s, out cachedEngine)) { cachedEngine = CreateEngine(engine, RequireFuncVariableName); engineCache[s] = cachedEngine; cachedEngine.Execute(script); } return(cachedEngine.GetValue(ExportsVariableName)); }); engine.SetValue(RequireFuncVariableName, requireAction); engineCache[rootPath] = engine; engine.Execute(scriptResource.Content); var value = engine.GetValue(ExportsVariableName); if (value.IsObject()) { var exports = value.AsObject(); GetOptionsFunc = GetFunc(GetOptionsFuncVariableName, exports); TransformModelFunc = GetFunc(TransformFuncVariableName, exports); } else { throw new InvalidPreprocessorException("Invalid 'exports' variable definition. 'exports' MUST be an object."); } return(engine); }
private static IDictionary <string, string> LoadTokenJson(ResourceCollection resource) { var tokenJson = resource.GetResource("token.json"); if (string.IsNullOrEmpty(tokenJson)) { // also load `global.json` for backward compatibility // TODO: remove this tokenJson = resource.GetResource("global.json"); if (string.IsNullOrEmpty(tokenJson)) { return(null); } } return(JsonUtility.FromJsonString <Dictionary <string, string> >(tokenJson)); }
private object LoadGlobalJson(ResourceCollection resource) { var globalJson = resource.GetResource("global.json"); if (!string.IsNullOrEmpty(globalJson)) { return(JsonUtility.FromJsonString <object>(globalJson)); } return(null); }
private static IDictionary <string, object> LoadGlobalJson(ResourceCollection resource) { var globalJson = resource.GetResource("global.json"); if (string.IsNullOrEmpty(globalJson)) { return(null); } return(JsonUtility.FromJsonString <Dictionary <string, object> >(globalJson)); }
public string ReadTemplateFile(DotLiquid.Context context, string templateName) { if (_resourceProvider == null) { return(null); } return(_templateCache.GetOrAdd(templateName, s => { string resourceName; var slashIndex = templateName.LastIndexOf('/'); if (slashIndex > -1) { var fileName = templateName.Substring(slashIndex + 1); resourceName = $"{templateName.Substring(0, slashIndex)}/_{fileName}.liquid"; } else { resourceName = $"_{templateName}.liquid"; } return _resourceProvider.GetResource(resourceName); })); }
private static IDictionary<string, string> LoadTokenJson(ResourceCollection resource) { var tokenJson = resource.GetResource("token.json"); if (string.IsNullOrEmpty(tokenJson)) { // also load `global.json` for backward compatibility // TODO: remove this tokenJson = resource.GetResource("global.json"); if (string.IsNullOrEmpty(tokenJson)) { return null; } } return JsonUtility.FromJsonString<Dictionary<string, string>>(tokenJson); }