コード例 #1
0
        /// <summary>
        /// Read the templates variables
        /// </summary>
        /// <returns>Returns true if changes happened</returns>
        private bool ReadTemplateFields()
        {
            if (Template == null || TemplateFields == null)
            {
                return(false);
            }
            if (!File.Exists(Template) && File.Exists(Path.Combine(Config.SlidesConfiguration.TemplatePath, Template)))
            {
                Template = Path.Combine(Config.SlidesConfiguration.TemplatePath, Template);
            }
            else if (!File.Exists(Template) && File.Exists(Path.Combine(Config.SlidesConfiguration.TemplatePath, Path.GetFileName(Template))))
            {
                Template = Path.Combine(Config.SlidesConfiguration.TemplatePath, Path.GetFileName(Template));
            }
            else if (!File.Exists(Template))
            {
                return(false);
            }

            var templateHtml = File.ReadAllText(Template);

            // Pattern used to replace template variables
            string pattern = "@@(?<variable>.*?)(=(?<default>.*))?@@";

            var changes = false;

            if (TemplateFields == null)
            {
                TemplateFields = new DispatchedObservableCollection <ManagerTemplateField>();
            }
            var matches = Regex.Matches(templateHtml, pattern);

            foreach (Match m in matches)
            {
                var    variable       = m.Groups["variable"].Value;
                var    defaultvalue   = m.Groups["default"].Value;
                var    isDefaultValue = false;
                string replaceValue   = null;
                if (Context != null && Context.HasValue(variable))
                {
                    replaceValue = Context.GetValue(variable);
                }
                else if (!string.IsNullOrEmpty(defaultvalue))
                {
                    replaceValue   = defaultvalue;
                    isDefaultValue = true;

                    //// Add to context if any
                    //if (Context != null)
                    //{
                    //    Context.Data.Add(new DataMessage()
                    //    {je
                    //        Data = null,
                    //        DataType = "string",
                    //        Key = variable,
                    //        Value = defaultvalue
                    //    });
                    //}
                }

                // Add all fields to template fields
                if (TemplateFields.Any(x => x.Title == variable))
                {
                    // If already in there check if value changed
                    var existing = TemplateFields.First(x => x.Title == variable);
                    if (!isDefaultValue && String.Compare(existing.Value, replaceValue, StringComparison.Ordinal) != 0)
                    {
                        // Update value
                        if (replaceValue != null)
                        {
                            existing.Value = replaceValue;
                            changes        = true;
                        }
                    }
                }
                else
                {
                    // Only add once to list
                    TemplateFields.Add(new ManagerTemplateField(variable, replaceValue));
                    changes = true;
                }
            }
            return(changes);
        }
コード例 #2
0
        /// <summary>
        /// Read the templates variables
        /// </summary>
        /// <returns>Returns true if changes happened</returns>
        private bool ReadTemplateFields()
        {
            if (Template == null || TemplateFields == null)
            {
                return(false);
            }
            if (!File.Exists(Template) && File.Exists(Path.Combine(Config.SlidesConfiguration.TemplatePath, Template)))
            {
                Template = Path.Combine(Config.SlidesConfiguration.TemplatePath, Template);
            }
            else if (!File.Exists(Template) && File.Exists(Path.Combine(Config.SlidesConfiguration.TemplatePath, Path.GetFileName(Template))))
            {
                Template = Path.Combine(Config.SlidesConfiguration.TemplatePath, Path.GetFileName(Template));
            }
            else if (!File.Exists(Template))
            {
                return(false);
            }

            var templateHtml = File.ReadAllText(Template);

            // Replace @@values@@ with context Values
            const string pattern = "@@(.*?)@@";

            var changes = false;

            if (TemplateFields == null)
            {
                TemplateFields = new DispatchedObservableCollection <ManagerTemplateField>();
            }
            foreach (Match m in Regex.Matches(templateHtml, pattern))
            {
                var    variable     = m.Groups[1].Value;
                string replaceValue = null;
                if (Context != null && Context.HasValue(variable))
                {
                    replaceValue = Context.GetValue(variable);
                }
                // Add all fields to template fields
                if (TemplateFields.Any(x => x.Title == variable))
                {
                    // If already in there check if value changed
                    var existing = TemplateFields.First(x => x.Title == variable);
                    if (String.Compare(existing.Value, replaceValue, StringComparison.Ordinal) != 0)
                    {
                        // Update value
                        if (replaceValue != null)
                        {
                            existing.Value = replaceValue;
                            changes        = true;
                        }
                    }
                }
                else
                {
                    // Only add once to list
                    TemplateFields.Add(new ManagerTemplateField(variable, replaceValue));
                    changes = true;
                }
            }
            return(changes);
        }