/// <summary>
        /// Register a shortcode that is defined and implemented in code.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="shortcodeFactoryMethod"></param>
        public void RegisterShortcode(string name, Func <string, ILavaShortcode> shortcodeFactoryMethod)
        {
            var instance = shortcodeFactoryMethod(name);

            if (instance == null)
            {
                throw new Exception($"Shortcode factory could not provide a valid instance for \"{name}\" .");
            }

            // Get a decorated name for the shortcode that will not collide with a regular tag name.
            var registrationKey = LavaUtilityHelper.GetLiquidElementNameFromShortcodeName(name);

            if (instance.ElementType == LavaShortcodeTypeSpecifier.Inline)
            {
                var tagFactoryMethod = shortcodeFactoryMethod as Func <string, ILavaTag>;

                RegisterTag(registrationKey, tagFactoryMethod);
            }
            else
            {
                RegisterBlock(registrationKey, (blockName) =>
                {
                    // Get a shortcode instance using the provided shortcut factory.
                    var shortcode = shortcodeFactoryMethod(registrationKey);

                    // Return the shortcode instance as a RockLavaBlock
                    return(shortcode as ILavaBlock);
                });
            }
        }
Exemple #2
0
        private ILavaService GetService(Type serviceType, object configuration)
        {
            var key = LavaUtilityHelper.GetContextKeyFromType(serviceType);

            var service = this.GetInternalField(key);

            return(service as ILavaService);
        }
        /// <summary>
        /// Remove the registration entry for a Tag with the specified name.
        /// </summary>
        /// <param name="name"></param>
        public bool DeregisterTag(string name)
        {
            var registrationKey = LavaUtilityHelper.GetLiquidElementNameFromShortcodeName(name);

            if (!_lavaElements.ContainsKey(registrationKey))
            {
                return(false);
            }

            _lavaElements.Remove(registrationKey);

            return(true);
        }
        private T GetShortcodeFromFactory <T>(string shortcodeInternalName, Func <string, DynamicShortcodeDefinition> shortcodeFactoryMethod)
            where T : DynamicShortcode, new()
        {
            // Call the factory method we have been passed to retrieve the definition of the shortcode.
            // The definition may change at runtime, so we need to execute the factory method every time we create a new shortcode instance.
            var shortCodeName = LavaUtilityHelper.GetShortcodeNameFromLiquidElementName(shortcodeInternalName);

            var shortcodeDefinition = shortcodeFactoryMethod(shortCodeName);

            var shortcodeInstance = new T();

            shortcodeInstance.Initialize(shortcodeDefinition, this);

            return(shortcodeInstance);
        }
        /// <summary>
        /// Initializes a new template context.
        /// </summary>
        /// <returns></returns>
        protected void InitializeRenderContext(ILavaRenderContext context, IEnumerable <string> enabledCommands = null)
        {
            if (context == null)
            {
                return;
            }

            if (enabledCommands != null)
            {
                context.SetEnabledCommands(enabledCommands);
            }

            // Set a reference to the current Lava Engine.
            context.SetInternalField(LavaUtilityHelper.GetContextKeyFromType(typeof(ILavaEngine)), this);
        }
        /// <summary>
        /// Register a shortcode that is defined in the data store.
        /// The definition of a dynamic shortcode can be changed at runtime.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="shortcodeFactoryMethod"></param>
        public void RegisterShortcode(DynamicShortcodeDefinition shortcodeDefinition)
        {
            if (shortcodeDefinition == null)
            {
                throw new Exception("Shortcode definition is required.");
            }

            if (shortcodeDefinition.ElementType == LavaShortcodeTypeSpecifier.Inline)
            {
                // Create a new factory method that returns an initialized Shortcode Tag element.
                Func <string, ILavaTag> tagFactoryMethod = (tagName) =>
                {
                    // Call the factory method we have been passed to retrieve the definition of the shortcode.
                    // The definition may change at runtime, so we need to execute the factory method every time we create a new shortcode instance.
                    var shortCodeName = LavaUtilityHelper.GetShortcodeNameFromLiquidElementName(tagName);

                    var shortcodeInstance = new DynamicShortcodeTag();

                    shortcodeInstance.Initialize(shortcodeDefinition, this);

                    return(shortcodeInstance);
                };

                // Register the shortcode as a custom tag, but use a decorated registration name that will not collide with a regular element name.
                var registrationKey = LavaUtilityHelper.GetLiquidElementNameFromShortcodeName(shortcodeDefinition.Name);

                RegisterTag(registrationKey, tagFactoryMethod);
            }
            else
            {
                // Create a new factory method that returns an initialized Shortcode Block element.
                Func <string, ILavaBlock> blockFactoryMethod = (blockName) =>
                {
                    // Call the factory method we have been passed to retrieve the definition of the shortcode.
                    // The definition may change at runtime, so we need to execute the factory method for each new shortcode instance.
                    var shortCodeName = LavaUtilityHelper.GetShortcodeNameFromLiquidElementName(blockName);

                    var shortcodeInstance = new DynamicShortcodeBlock(shortcodeDefinition, this);

                    return(shortcodeInstance);
                };

                // Register the shortcode as a custom block, but use a decorated registration name that will not collide with a regular element name.
                var registrationKey = LavaUtilityHelper.GetLiquidElementNameFromShortcodeName(shortcodeDefinition.Name);

                RegisterBlock(registrationKey, blockFactoryMethod);
            }
        }
        /// <summary>
        /// Register a shortcode that is defined in the data store.
        /// The definition of a dynamic shortcode can be changed at runtime.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="shortcodeFactoryMethod"></param>
        public void RegisterShortcode(string name, Func <string, DynamicShortcodeDefinition> shortcodeFactoryMethod)
        {
            // Create a default instance so we can retrieve the properties of the shortcode.
            var instance = shortcodeFactoryMethod(name);

            if (instance == null)
            {
                throw new Exception($"Shortcode factory could not provide a valid instance for \"{name}\" .");
            }

            if (instance.ElementType == LavaShortcodeTypeSpecifier.Inline)
            {
                // Create a new factory method that returns an initialized Shortcode Tag element.
                Func <string, ILavaTag> tagFactoryMethod = (tagName) =>
                {
                    var shortcodeInstance = GetShortcodeFromFactory <DynamicShortcodeTag>(tagName, shortcodeFactoryMethod);

                    return(shortcodeInstance);
                };

                // Register the shortcode as a custom tag, but use a decorated registration name that will not collide with a regular element name.
                var registrationKey = LavaUtilityHelper.GetLiquidElementNameFromShortcodeName(name);

                RegisterTag(registrationKey, tagFactoryMethod);
            }
            else
            {
                // Create a new factory method that returns an initialized Shortcode Block element.
                Func <string, ILavaBlock> blockFactoryMethod = (blockName) =>
                {
                    // Call the factory method we have been passed to retrieve the definition of the shortcode.
                    // The definition may change at runtime, so we need to execute the factory method for each new shortcode instance.
                    var shortCodeName = LavaUtilityHelper.GetShortcodeNameFromLiquidElementName(blockName);

                    var shortcodeDefinition = shortcodeFactoryMethod(shortCodeName);

                    var shortcodeInstance = new DynamicShortcodeBlock(shortcodeDefinition, this);

                    return(shortcodeInstance);
                };

                // Register the shortcode as a custom block, but use a decorated registration name that will not collide with a regular element name.
                var registrationKey = LavaUtilityHelper.GetLiquidElementNameFromShortcodeName(name);

                RegisterBlock(registrationKey, blockFactoryMethod);
            }
        }
        /// <summary>
        /// Register a shortcode that is defined and implemented by a provider component.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="shortcodeFactoryMethod"></param>
        public void RegisterShortcode(string name, ILavaShortcodeProvider provider)
        {
            // Get a decorated name for the shortcode that will not collide with a regular tag name.
            var registrationKey = LavaUtilityHelper.GetLiquidElementNameFromShortcodeName(name);

            var info = provider.GetShortcodeDefinition(name);

            if (info == null)
            {
                throw new Exception($"Shortcode provider could not provide a valid instance for \"{name}\" .");
            }

            if (info.ElementType == LavaShortcodeTypeSpecifier.Inline)
            {
                RegisterTag(registrationKey, (tagName) => { return(GetLavaShortcodeInstanceFromProvider <ILavaTag>(provider, tagName, this)); });
            }
            else
            {
                RegisterBlock(registrationKey, (blockName) => { return(GetLavaShortcodeInstanceFromProvider <ILavaBlock>(provider, blockName, this)); });
            }
        }