Beispiel #1
0
            private DynamicShortcodeDefinition GetShortcodeDefinitionFromShortcodeEntity(LavaShortcode shortcode)
            {
                if (shortcode == null)
                {
                    return(null);
                }

                var newShortcode = new DynamicShortcodeDefinition();

                newShortcode.Name           = shortcode.Name;
                newShortcode.TemplateMarkup = shortcode.Markup;

                var parameters = RockSerializableDictionary.FromUriEncodedString(shortcode.Parameters);

                newShortcode.Parameters = new Dictionary <string, string>(parameters.Dictionary);

                newShortcode.EnabledLavaCommands = shortcode.EnabledLavaCommands.SplitDelimitedValues(",", StringSplitOptions.RemoveEmptyEntries).ToList();

                if (shortcode.TagType == TagType.Block)
                {
                    newShortcode.ElementType = LavaShortcodeTypeSpecifier.Block;
                }
                else
                {
                    newShortcode.ElementType = LavaShortcodeTypeSpecifier.Inline;
                }

                return(newShortcode);
            }
        /// <summary>
        /// Parses the markup.
        /// </summary>
        /// <param name="markup">The markup.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        private Dictionary <string, object> ParseMarkup(string markup, Context context)
        {
            var parms = new Dictionary <string, object>();

            LoadBlockMergeFields(context, parms);

            // create all the parameters from the shortcode with their default values
            var shortcodeParms = RockSerializableDictionary.FromUriEncodedString(_shortcode.Parameters);

            foreach (var shortcodeParm in shortcodeParms.Dictionary)
            {
                parms.AddOrReplace(shortcodeParm.Key, shortcodeParm.Value);
            }

            // first run lava across the inputted markup
            var resolvedMarkup = markup.ResolveMergeFields(_internalMergeFields);

            var markupItems = Regex.Matches(resolvedMarkup, @"(\S*?:'[^']+')")
                              .Cast <Match>()
                              .Select(m => m.Value)
                              .ToList();

            foreach (var item in markupItems)
            {
                var itemParts = item.ToString().Split(new char[] { ':' }, 2);
                if (itemParts.Length > 1)
                {
                    parms.AddOrReplace(itemParts[0].Trim().ToLower(), itemParts[1].Trim().Substring(1, itemParts[1].Length - 2));
                }
            }

            // OK, now let's look for any passed variables ala: name:variable
            var variableTokens = Regex.Matches(resolvedMarkup, @"\w*:\w+")
                                 .Cast <Match>()
                                 .Select(m => m.Value)
                                 .ToList();

            foreach (var item in variableTokens)
            {
                var itemParts = item.Trim().Split(new char[] { ':' }, 2);
                if (itemParts.Length > 1)
                {
                    var scopeKey = itemParts[1].Trim();

                    // context.Scopes is a weird beast can't find a cleaner way to get the object than to iterate over it
                    foreach (var scopeItem in context.Scopes)
                    {
                        var scopeObject = scopeItem.Where(x => x.Key == scopeKey).FirstOrDefault();

                        if (scopeObject.Value != null)
                        {
                            parms.AddOrReplace(itemParts[0].Trim().ToLower(), scopeObject.Value);
                            break;
                        }
                    }
                }
            }

            return(parms);
        }
Beispiel #3
0
        public void RockSerializableDictionary_UriEncodedString_CanRoundtrip()
        {
            var sd1 = GetComplexDictionary();

            var uriString = sd1.ToUriEncodedString();

            var sd2 = RockSerializableDictionary.FromUriEncodedString(uriString);

            Assert.AreEqual("Value1a|Value1b^Value1c,Value1d", sd2.Dictionary["Key1|With^Special,Characters"]);
        }
Beispiel #4
0
        private RockSerializableDictionary GetComplexDictionary()
        {
            var dict = new Dictionary <string, object>();

            dict.Add("Key1|With^Special,Characters", "Value1a|Value1b^Value1c,Value1d");
            dict.Add("Key2|With^Special,Characters", "Value2a|Value2b^Value2c,Value2d");

            var sd = new RockSerializableDictionary(dict);

            return(sd);
        }
Beispiel #5
0
        public void RockSerializableDictionary_SerializedSingleEntry_HasCorrectFormat()
        {
            var dict = new Dictionary <string, object>();

            dict.Add("Key1", "Value1");

            var sd = new RockSerializableDictionary(dict);

            var uriString = sd.ToUriEncodedString();

            Assert.AreEqual("Key1^Value1", uriString);
        }
Beispiel #6
0
        public void RockSerializableDictionary_ToUriEncodedString_EscapesSpecialCharacters()
        {
            const string testRegExDecoded = @",/([!-/:-@[-`{-~]|\sand\s)+/i";
            const string testRegExEncoded = @"%2C%2F%28%5B%21-%2F%3A-%40%5B-%60%7B-~%5D%7C%5Csand%5Cs%29%2B%2Fi";

            var dict = new Dictionary <string, object>();

            dict.Add("RegEx1", testRegExDecoded);

            var sd = new RockSerializableDictionary(dict);

            var uriString = sd.ToUriEncodedString();

            Assert.AreEqual("RegEx1^" + testRegExEncoded, uriString);
        }
        private static void RegisterDynamicShortcodes(ILavaEngine engine)
        {
            // Register dynamic shortcodes with a factory method to ensure that the latest definition is retrieved from the global cache each time the shortcode is used.
            Func <string, DynamicShortcodeDefinition> shortCodeFactory = (shortcodeName) =>
            {
                var shortcodeDefinition = LavaShortcodeCache.All().Where(c => c.TagName == shortcodeName).FirstOrDefault();

                if (shortcodeDefinition == null)
                {
                    return(null);
                }

                var newShortcode = new DynamicShortcodeDefinition();

                newShortcode.Name           = shortcodeDefinition.Name;
                newShortcode.TemplateMarkup = shortcodeDefinition.Markup;

                var parameters = RockSerializableDictionary.FromUriEncodedString(shortcodeDefinition.Parameters);

                newShortcode.Parameters = new Dictionary <string, string>(parameters.Dictionary);

                newShortcode.EnabledLavaCommands = shortcodeDefinition.EnabledLavaCommands.SplitDelimitedValues(",", StringSplitOptions.RemoveEmptyEntries).ToList();

                if (shortcodeDefinition.TagType == TagType.Block)
                {
                    newShortcode.ElementType = LavaShortcodeTypeSpecifier.Block;
                }
                else
                {
                    newShortcode.ElementType = LavaShortcodeTypeSpecifier.Inline;
                }

                return(newShortcode);
            };

            var shortCodes = LavaShortcodeCache.All();

            foreach (var shortcode in shortCodes)
            {
                engine.RegisterShortcode(shortcode.TagName, shortCodeFactory);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Renders the base control.
        /// </summary>
        /// <param name="writer">The writer.</param>
        public override void RenderBaseControl(HtmlTextWriter writer)
        {
            Dictionary <string, string> values = null;

            if (DefinedTypeId.HasValue)
            {
                values = new Dictionary <string, string>();
                new DefinedValueService(new RockContext())
                .GetByDefinedTypeId(DefinedTypeId.Value)
                .ToList()
                .ForEach(v => values.Add(v.Id.ToString(), v.Value));
            }
            else if (CustomValues != null)
            {
                values = CustomValues;
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "key-value-list " + this.CssClass);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.WriteLine();

            _hfValue.RenderControl(writer);
            _hfValueDisableVrm.RenderControl(writer);

            writer.WriteLine();

            StringBuilder html = new StringBuilder();

            html.Append(@"<div class=""controls controls-row form-control-group"">");

            // write key/value html
            if (this.DisplayValueFirst)
            {
                WriteValueHtml(html, values);
                WriteKeyHtml(html);
            }
            else
            {
                WriteKeyHtml(html);
                WriteValueHtml(html, values);
            }


            html.Append(@"<a href=""#"" class=""btn btn-sm btn-danger key-value-remove""><i class=""fa fa-times""></i></a></div>");

            var hfValueHtml = new HtmlInputHidden();

            hfValueHtml.AddCssClass("js-value-html");
            hfValueHtml.Value = html.ToString();
            hfValueHtml.RenderControl(writer);

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "key-value-rows");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.WriteLine();

            var nameValues = RockSerializableDictionary.FromUriEncodedString(this.Value);

            foreach (var nameValuePair in nameValues.Dictionary)
            {
                var nameAndValue = new string[] { nameValuePair.Key, nameValuePair.Value.ToStringSafe() };

                writer.AddAttribute(HtmlTextWriterAttribute.Class, "controls controls-row form-control-group");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.WriteLine();

                if (DisplayValueFirst)
                {
                    WriteValueControls(writer, nameAndValue, values);
                    WriteKeyControls(writer, nameAndValue);
                }
                else
                {
                    WriteKeyControls(writer, nameAndValue);
                    WriteValueControls(writer, nameAndValue, values);
                }

                // Write Remove Button
                writer.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "btn btn-sm btn-danger key-value-remove");
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "fa fa-times");
                writer.RenderBeginTag(HtmlTextWriterTag.I);
                writer.RenderEndTag();
                writer.RenderEndTag();
                writer.WriteLine();

                writer.RenderEndTag();
                writer.WriteLine();
            }

            writer.RenderEndTag();
            writer.WriteLine();

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "actions");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            var addButtonCssClass = "btn btn-action btn-xs key-value-add";

            if (!this.Enabled)
            {
                addButtonCssClass += " aspNetDisabled disabled";
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Class, addButtonCssClass);
            writer.AddAttribute(HtmlTextWriterAttribute.Href, "#");
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "fa fa-plus-circle");
            writer.RenderBeginTag(HtmlTextWriterTag.I);

            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.WriteLine();

            writer.RenderEndTag();
            writer.WriteLine();
        }