/// <summary>
        /// Loads a template
        /// </summary>
        /// <param name="templateName">Template name</param>
        /// <param name="callerSpan">Caller span</param>
        /// <returns>Code of the template</returns>
        private async Task <string> LoadTemplate(string templateName, SourceSpan callerSpan)
        {
            GoNorthProject project = await _cachedDbAccess.GetUserProject();

            IncludeExportTemplate includeExportTemplate = await _cachedDbAccess.GetIncludeTemplateByName(project.Id, templateName);

            if (includeExportTemplate == null)
            {
                _errorCollection.AddIncludeTemplateNotFoundError(templateName, ScribanErrorUtil.FormatScribanSpan(callerSpan));
                return("<<INCLUDE TEMPLATE NOT FOUND>>");
            }

            return(includeExportTemplate.Code);
        }
Exemple #2
0
        /// <summary>
        /// Loads an include template by name
        /// </summary>
        /// <param name="projectId">Id of the project to which the template belongs</param>
        /// <param name="templateName">Name of the template to load</param>
        /// <returns>Include template</returns>
        public async Task <IncludeExportTemplate> GetIncludeTemplateByName(string projectId, string templateName)
        {
            string cacheKey = projectId + "|" + templateName;

            if (_cachedIncludeExportTemplates.ContainsKey(cacheKey))
            {
                return(_cachedIncludeExportTemplates[cacheKey]);
            }

            IncludeExportTemplate exportTemplate = await _includeExportTemplateDbAccess.GetIncludeTemplateByName(projectId, templateName);

            _cachedIncludeExportTemplates.Add(cacheKey, exportTemplate);
            return(exportTemplate);
        }
        /// <summary>
        /// Parses the referenced include templates from a template
        /// </summary>
        /// <param name="template">Template to parse</param>
        /// <returns>List of referenced include</returns>
        private async Task <List <IncludeExportTemplateReference> > ParseIncludeTemplates(ExportTemplate template)
        {
            Template parsedTemplate = Template.Parse(template.Code);

            List <IncludeExportTemplateReference> includeTemplateRefs = new List <IncludeExportTemplateReference>();
            List <ScriptStatement> statementsToCheck = ScribanStatementExtractor.ExtractPlainNonTextStatements(parsedTemplate);

            foreach (ScriptStatement curStatement in statementsToCheck)
            {
                if (!(curStatement is ScriptExpressionStatement))
                {
                    continue;
                }

                ScriptExpressionStatement expressionStatement = (ScriptExpressionStatement)curStatement;
                if (expressionStatement.Expression == null || !(expressionStatement.Expression is ScriptFunctionCall))
                {
                    continue;
                }

                ScriptFunctionCall functionCall = (ScriptFunctionCall)expressionStatement.Expression;
                if (functionCall.Target == null || functionCall.Target.ToString() != "include")
                {
                    continue;
                }

                if (functionCall.Arguments.Count < 1)
                {
                    continue;
                }

                string templateName = functionCall.Arguments[0].ToString().Trim('\"').Trim('\'');
                IncludeExportTemplate existingTemplate = await _cachedDbAccess.GetIncludeTemplateByName(template.ProjectId, templateName);

                if (existingTemplate != null && !includeTemplateRefs.Any(e => e.IncludeTemplateId == existingTemplate.Id))
                {
                    includeTemplateRefs.Add(new IncludeExportTemplateReference {
                        IncludeTemplateId = existingTemplate.Id,
                        Name = existingTemplate.Name
                    });
                }
            }

            return(includeTemplateRefs);
        }