/// <summary>
 /// Dependent files are defined in following syntax in Mustache template leveraging Mustache Comments
 /// {{! include('file') }}
 /// file path can be wrapped by quote ' or double quote " or none
 /// </summary>
 /// <param name="template"></param>
 private IEnumerable <string> ExtractDependencyResourceNames(string template)
 {
     foreach (Match match in IncludeRegex.Matches(template))
     {
         var filePath = match.Groups["file"].Value;
         foreach (var name in ParseTemplateHelper.GetResourceName(filePath, Path, _reader))
         {
             yield return(name);
         }
     }
 }
Esempio n. 2
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);
                }
            }
        }
        public MustacheTemplateRenderer(IResourceFileReader reader, ResourceInfo info, string name = null)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

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

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

            Path    = info.Path;
            Name    = name ?? System.IO.Path.GetFileNameWithoutExtension(Path);
            _reader = reader;
            _resourceTemplateLocator = new ResourceTemplateLocator(reader);

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

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

            Dependencies = ExtractDependencyResourceNames(processedTemplate).ToList();
        }
Esempio n. 4
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();
        }
Esempio n. 5
0
 private IEnumerable <string> ParseDependencies(string templateName, ResourceCollection resource, IEnumerable <string> raw)
 {
     return(from item in raw
            from name in ParseTemplateHelper.GetResourceName(item, templateName, resource)
            select name);
 }
Esempio n. 6
0
 private IEnumerable <string> ParseDependencies(string path, IResourceFileReader reader, IEnumerable <string> raw)
 {
     return(from item in raw
            from name in ParseTemplateHelper.GetResourceName(item, path, reader)
            select name);
 }