Example #1
0
        public Template(string template, string templateName, string script, ResourceCollection resourceCollection)
        {
            if (string.IsNullOrEmpty(templateName))
            {
                throw new ArgumentNullException(nameof(templateName));
            }
            if (string.IsNullOrEmpty(template))
            {
                throw new ArgumentNullException(nameof(template));
            }
            Name = templateName;
            var typeAndExtension = GetTemplateTypeAndExtension(templateName);

            Extension = typeAndExtension.Item2;
            Type      = typeAndExtension.Item1;
            IsPrimary = typeAndExtension.Item3;
            _script   = script;
            if (script != null)
            {
                ScriptName  = templateName + ".js";
                _enginePool = ResourcePool.Create(() => CreateEngine(script), Constants.DefaultParallelism);
            }

            if (resourceCollection != null)
            {
                _rendererPool = ResourcePool.Create(() => CreateRenderer(resourceCollection, templateName, template), Constants.DefaultParallelism);
            }

            Resources = ExtractDependentResources();
        }
        public RendererWithResourcePool(Func <ITemplateRenderer> creater, int maxParallelism)
        {
            _rendererPool = ResourcePool.Create(creater, maxParallelism);

            using var lease = _rendererPool.Rent();
            var inner = lease.Resource;

            Raw          = inner.Raw;
            Dependencies = inner.Dependencies;
            Path         = inner.Path;
            Name         = inner.Name;
        }
Example #3
0
        public Template(string name, DocumentBuildContext context, TemplateRendererResource templateResource, TemplatePreprocessorResource scriptResource, ResourceCollection resourceCollection, int maxParallelism)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            Name = name;
            var templateInfo = GetTemplateInfo(Name);

            Extension    = templateInfo.Extension;
            Type         = templateInfo.DocumentType;
            TemplateType = templateInfo.TemplateType;
            _script      = scriptResource?.Content;
            if (!string.IsNullOrWhiteSpace(_script))
            {
                ScriptName        = Name + ".js";
                _preprocessorPool = ResourcePool.Create(() => CreatePreprocessor(resourceCollection, scriptResource, context), maxParallelism);
                try
                {
                    using (var preprocessor = _preprocessorPool.Rent())
                    {
                        ContainsGetOptions          = preprocessor.Resource.GetOptionsFunc != null;
                        ContainsModelTransformation = preprocessor.Resource.TransformModelFunc != null;
                    }
                }
                catch (Exception e)
                {
                    _preprocessorPool = null;
                    Logger.LogWarning($"{ScriptName} is not a valid template preprocessor, ignored: {e.Message}");
                }
            }

            if (!string.IsNullOrEmpty(templateResource?.Content) && resourceCollection != null)
            {
                _rendererPool            = ResourcePool.Create(() => CreateRenderer(resourceCollection, templateResource), maxParallelism);
                ContainsTemplateRenderer = true;
            }

            if (!ContainsGetOptions && !ContainsModelTransformation && !ContainsTemplateRenderer)
            {
                Logger.LogWarning($"Template {name} contains neither preprocessor to process model nor template to render model. Please check if the template is correctly defined. Allowed preprocessor functions are [exports.getOptions] and [exports.transform].");
            }

            Resources = ExtractDependentResources(Name);
        }
 public PreprocessorWithResourcePool(Func <ITemplatePreprocessor> creater, int maxParallelism)
 {
     _preprocessorPool = ResourcePool.Create(creater, maxParallelism);
     try
     {
         using (var preprocessor = _preprocessorPool.Rent())
         {
             var inner = preprocessor.Resource;
             ContainsGetOptions          = inner.ContainsGetOptions;
             ContainsModelTransformation = inner.ContainsModelTransformation;
             Path = inner.Path;
             Name = inner.Name;
         }
     }
     catch (Exception e)
     {
         _preprocessorPool = null;
         Logger.LogWarning($"Not a valid template preprocessor, ignored: {e.Message}");
     }
 }