Example #1
0
        public static Template FromText(string pattern)
        {
            var template = new Template();

            template.RawPattern = pattern;
            template.Placeholders = GetPlaceholdersFromPattern(pattern);

            return template;
        }
        public string ProcessTemplate(Template template, TextTemplateProcessingContext context = null)
        {
            if (context != null)
            {
                // make sure provided context has access to all default substitutions
                foreach (var sub in InternalContext.Substitutions)
                {
                    context.Substitutions.AddOrUpdateSubstitution(sub);
                }

                // make sure provided context has access to all default functions
                foreach (var function in InternalContext.Functions)
                {
                    if (!context.Functions.Contains(function))
                        context.Functions.AddFunction(function);
                }
            }

            var sb = new StringBuilder(template.RawPattern);

            var placeholders = template.GetPlaceholders();

            for (int i = placeholders.Count - 1; i >= 0; i--)
            {
                var ph = placeholders[i];

                object value = null;

                if (context != null && ph.TryEvaluate(context, out value))
                {
                    sb.Replace(ph.RawValue, value.ToString(valueWhenNull: "[NULL]", valueWhenEmpty: ""), ph.StartIndex, ph.Length);
                }
                else if (ph.TryEvaluate(InternalContext, out value))
                {
                    sb.Replace(ph.RawValue, value.ToString(valueWhenNull: "[NULL]", valueWhenEmpty: ""), ph.StartIndex, ph.Length);
                }
                else
                {
                    Diagnostics.Log.Warning(() => "Unable to process placeholder '{0}'".FormatWith(ph.RawValue));
                    sb.Replace(ph.RawValue, "");
                }
            }

            return sb.ToString();
        }
        void Initialize()
        {
            DataRequestCache.Clear();

            var lines = Pattern.GetLines(keepLineBreaks: false);
            var trimmedPattern = string.Concat(lines.Select(l => l.Trim()));

            Template = Template.FromText(trimmedPattern);

            var placeholders = Template.GetPlaceholders();

            for (int i = 0; i < placeholders.Count; i++)
            {
                var ph = placeholders[i];

                if (!ph.SubstitutionPattern.StartsWith("'") && !ph.SubstitutionPattern.EndsWith("'"))
                {
                    var dr = new DataRequest();
                    dr.Data = ph.SubstitutionPattern;
                    DataRequestCache.Add(dr);

                    var cph = ph as ConditionalPlaceholder;
                    if (cph != null)
                    {

                        if (!cph.TrueOutcomeSubstitutionPattern.StartsWith("'") && !cph.TrueOutcomeSubstitutionPattern.EndsWith("'"))
                        {
                            dr = new DataRequest();
                            dr.Data = cph.TrueOutcomeSubstitutionPattern;
                            DataRequestCache.Add(dr);
                        }
                    }
                }
            }
        }