Пример #1
0
        public ITemplateRenderer Load(string path)
        {
            var content = _reader.GetResource(path);

            if (content == null)
            {
                return(null);
            }

            return(Load(new ResourceInfo(path, content)));
        }
Пример #2
0
        public IEnumerable <ITemplatePreprocessor> LoadFromRenderer(ITemplateRenderer renderer)
        {
            var viewPath         = renderer.Path;
            var preproceesorPath = Path.ChangeExtension(viewPath, TemplateJintPreprocessor.Extension);
            var res          = _reader.GetResource(preproceesorPath);
            var preprocessor = Load(new ResourceInfo(preproceesorPath, res), renderer.Name);

            if (preprocessor != null)
            {
                yield return(preprocessor);
            }
        }
Пример #3
0
            public string ReadTemplateFile(DotLiquid.Context context, string templateName)
            {
                if (_reader == 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 _reader.GetResource(resourceName);
                }));
            }
Пример #4
0
        private Engine SetupEngine(IResourceFileReader resourceCollection, ResourceInfo scriptResource, DocumentBuildContext context)
        {
            var rootPath    = (RelativePath)scriptResource.Path;
            var engineCache = new Dictionary <string, Engine>();

            var utility = new TemplateUtility(context);

            _utilityObject = new
            {
                resolveSourceRelativePath = new Func <string, string, string>(utility.ResolveSourceRelativePath),
                getHrefFromRoot           = new Func <string, string, string>(utility.GetHrefFromRoot),
                markup = new Func <string, string, string>(utility.Markup),
            };

            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);
                }

                if (!engineCache.TryGetValue(s, out Engine 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);
                _transformFunc  = GetFunc(TransformFuncVariableName, exports);
            }
            else
            {
                throw new InvalidPreprocessorException("Invalid 'exports' variable definition. 'exports' MUST be an object.");
            }

            return(engine);
        }