示例#1
0
        public static string ExpandMasterPage(ResourceCollection resource, TemplateRendererResource info, Regex masterRegex, Regex bodyRegex)
        {
            var template               = info.Content;
            var templateName           = info.TemplateName;
            var masterPageResourceName = ExtractMasterPageResourceName(resource, info, masterRegex).FirstOrDefault();

            template = masterRegex.Replace(template, string.Empty);
            if (masterPageResourceName != null)
            {
                using (var stream = resource.GetResourceStream(masterPageResourceName))
                {
                    if (stream != null)
                    {
                        using (var sr = new StreamReader(stream))
                        {
                            var master = sr.ReadToEnd();
                            if (bodyRegex.IsMatch(master))
                            {
                                return(bodyRegex.Replace(master, template));
                            }
                            else
                            {
                                Logger.LogInfo($"Master page {masterPageResourceName} does not contain {{{{!body}}}} element, content in current template {templateName} is ignored.");
                                return(master);
                            }
                        }
                    }
                }
            }

            return(template);
        }
        public MustacheTemplateRenderer(ResourceCollection resourceProvider, TemplateRendererResource info)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (info.Content == null)
            {
                throw new ArgumentNullException(nameof(info.Content));
            }

            if (info.TemplateName == null)
            {
                throw new ArgumentNullException(nameof(info.TemplateName));
            }

            _templateName = info.TemplateName;

            _resourceTemplateLocator = new ResourceTemplateLocator(resourceProvider);
            _resource = resourceProvider;

            _template = new Nustache.Core.Template();

            using (var reader = new StringReader(info.Content))
            {
                _template.Load(reader);
            }

            Dependencies = ExtractDependencyResourceNames(info.Content).ToList();
        }
示例#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);
        }
示例#4
0
文件: Template.cs 项目: vicancy/docfx
        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);
        }
示例#5
0
        public MustacheTemplateRenderer(ResourceCollection resourceProvider, TemplateRendererResource info)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (info.Content == null)
            {
                throw new ArgumentNullException(nameof(info.Content));
            }

            if (info.TemplateName == null)
            {
                throw new ArgumentNullException(nameof(info.TemplateName));
            }

            _templateName = info.TemplateName;

            _resource = resourceProvider;
            _resourceTemplateLocator = new ResourceTemplateLocator(resourceProvider);

            _template = new Nustache.Core.Template();
            var processedTemplate = ParseTemplateHelper.ExpandMasterPage(resourceProvider, info, MasterPageRegex, MasterPageBodyRegex);

            using (var reader = new StringReader(processedTemplate))
            {
                try
                {
                    _template.Load(reader);
                }
                catch (Nustache.Core.NustacheException e)
                {
                    throw new DocfxException($"Error in mustache template {info.TemplateName}: {e.Message}", e);
                }
            }

            Dependencies = ExtractDependencyResourceNames(processedTemplate).ToList();
        }
示例#6
0
文件: Template.cs 项目: vicancy/docfx
 private static ITemplateRenderer CreateRenderer(ResourceCollection resourceCollection, TemplateRendererResource templateResource)
 {
     if (templateResource.Type == TemplateRendererType.Liquid)
     {
         return LiquidTemplateRenderer.Create(resourceCollection, templateResource.Content);
     }
     else
     {
         return new MustacheTemplateRenderer(resourceCollection, templateResource.Content);
     }
 }
示例#7
0
        private static Dictionary <string, TemplateBundle> ReadTemplate(ResourceCollection resource, DocumentBuildContext context, int maxParallelism)
        {
            // type <=> list of template with different extension
            var dict = new Dictionary <string, List <Template> >(StringComparer.OrdinalIgnoreCase);

            if (resource == null || resource.IsEmpty)
            {
                return(new Dictionary <string, TemplateBundle>());
            }

            // Template file ends with .tmpl(Mustache) or .liquid(Liquid)
            // Template file naming convention: {template file name}.{file extension}.(tmpl|liquid)
            // Only files under root folder is searched
            var templates = resource.GetResources(@"[^/]*\.(tmpl|liquid|js)$").ToList();

            if (templates != null)
            {
                foreach (var group in templates.GroupBy(s => Path.GetFileNameWithoutExtension(s.Key), StringComparer.OrdinalIgnoreCase))
                {
                    var currentTemplates =
                        (from i in @group
                         select new
                    {
                        item = i.Value,
                        extension = Path.GetExtension(i.Key),
                        name = i.Key,
                    } into item
                         where IsSupportedTemplateFile(item.extension)
                         select item).ToArray();
                    var currentScripts =
                        (from i in @group
                         select new
                    {
                        item = i.Value,
                        extension = Path.GetExtension(i.Key),
                        name = i.Key,
                    } into item
                         where IsSupportedScriptFile(item.extension)
                         select item).ToArray();

                    if (currentTemplates.Length == 0 && currentScripts.Length == 0)
                    {
                        continue;
                    }

                    // If template file does not exists, while a js script ends with .tmpl.js exists
                    // we consider .tmpl.js file as a standalone preprocess file
                    var name = group.Key;
                    if (currentTemplates.Length == 0)
                    {
                        if (name.EndsWith(ScriptTemplateExtension, StringComparison.OrdinalIgnoreCase))
                        {
                            name = name.Substring(0, name.Length - ScriptTemplateExtension.Length);
                        }
                        else
                        {
                            continue;
                        }
                    }

                    var currentTemplate = currentTemplates.FirstOrDefault();
                    var currentScript   = currentScripts.FirstOrDefault();
                    if (currentTemplates.Length > 1)
                    {
                        Logger.Log(LogLevel.Warning, $"Multiple templates for type '{name}'(case insensitive) are found, the one from '{currentTemplate.item + currentTemplate.extension}' is taken.");
                    }

                    if (currentScripts.Length > 1)
                    {
                        Logger.Log(LogLevel.Warning, $"Multiple template scripts for type '{name}'(case insensitive) are found, the one from '{currentScript.item + currentScript.extension}' is taken.");
                    }

                    TemplateRendererResource templateResource =
                        currentTemplate == null ?
                        null :
                        new TemplateRendererResource(currentTemplate.name, currentTemplate.item, name);
                    TemplatePreprocessorResource templatePrepocessorResource =
                        currentScript == null ?
                        null :
                        new TemplatePreprocessorResource(currentScript.name, currentScript.item);
                    var template = new Template(name, context, templateResource, templatePrepocessorResource, resource, maxParallelism);
                    if (dict.TryGetValue(template.Type, out List <Template> templateList))
                    {
                        templateList.Add(template);
                    }
                    else
                    {
                        dict[template.Type] = new List <Template> {
                            template
                        };
                    }
                }
            }

            return(dict.ToDictionary(s => s.Key, s => new TemplateBundle(s.Key, s.Value)));
        }
示例#8
0
 private static ITemplateRenderer CreateRenderer(ResourceCollection resourceCollection, TemplateRendererResource templateResource)
 {
     if (templateResource.Type == TemplateRendererType.Liquid)
     {
         return(LiquidTemplateRenderer.Create(resourceCollection, templateResource));
     }
     else
     {
         return(new MustacheTemplateRenderer(resourceCollection, templateResource));
     }
 }
示例#9
0
        public static LiquidTemplateRenderer Create(ResourceCollection resourceProvider, TemplateRendererResource info)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (info.Content == null)
            {
                throw new ArgumentNullException(nameof(info.Content));
            }

            if (info.TemplateName == null)
            {
                throw new ArgumentNullException(nameof(info.TemplateName));
            }

            var processedTemplate = ParseTemplateHelper.ExpandMasterPage(resourceProvider, info, MasterPageRegex, MasterPageBodyRegex);

            // Guarantee that each time returns a new renderer
            // As Dependency is a globally shared object, allow one entry at a time
            lock (_locker)
            {
                try
                {
                    DotLiquid.Template.RegisterTag <Dependency>("ref");
                    Dependency.PopDependencies();
                    var liquidTemplate = DotLiquid.Template.Parse(processedTemplate);
                    var dependencies   = Dependency.PopDependencies();

                    liquidTemplate.Registers.Add("file_system", new ResourceFileSystem(resourceProvider));

                    return(new LiquidTemplateRenderer(liquidTemplate, processedTemplate, info.TemplateName, resourceProvider, dependencies));
                }
                catch (DotLiquid.Exceptions.SyntaxException e)
                {
                    throw new DocfxException($"Syntax error for template {info.TemplateName}: {e.Message}", e);
                }
            }
        }
示例#10
0
        private static IEnumerable <string> ExtractMasterPageResourceName(ResourceCollection resource, TemplateRendererResource info, Regex masterRegex)
        {
            var template     = info.Content;
            var templateName = info.TemplateName;

            foreach (Match match in masterRegex.Matches(template))
            {
                var filePath = match.Groups["file"].Value;
                foreach (var name in GetResourceName(filePath, templateName, resource))
                {
                    yield return(name);

                    Logger.LogWarning($"Multiple definitions for master page found, only the first one {match.Groups[0].Value} takes effect.");
                    yield break;
                }
            }
        }