// NOT thread-safe over a request because it modifies the
    // global UmbracoContext.Current.InPreviewMode status. So it
    // should never execute in // over the same UmbracoContext with
    // different preview modes.
    private string RenderRteMacros(string source, bool preview)
    {
        IUmbracoContext umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();

        using (umbracoContext.ForcedPreview(preview)) // force for macro rendering
        {
            var sb = new StringBuilder();

            MacroTagParser.ParseMacros(
                source,

                // callback for when text block is found
                textBlock => sb.Append(textBlock),

                // callback for when macro syntax is found
                (macroAlias, macroAttributes) => sb.Append(_macroRenderer.RenderAsync(
                                                               macroAlias,
                                                               umbracoContext.PublishedRequest?.PublishedContent,

                                                               // needs to be explicitly casted to Dictionary<string, object>
                                                               macroAttributes.ConvertTo(x => (string)x, x => x) !).GetAwaiter().GetResult().Text));

            return(sb.ToString());
        }
    }
Exemple #2
0
        // NOT thread-safe over a request because it modifies the
        // global UmbracoContext.Current.InPreviewMode status. So it
        // should never execute in // over the same UmbracoContext with
        // different preview modes.
        static string RenderRteMacros(string source, bool preview)
        {
            // save and set for macro rendering
            var inPreviewMode = UmbracoContext.Current.InPreviewMode;

            UmbracoContext.Current.InPreviewMode = preview;

            var sb = new StringBuilder();

            try
            {
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                MacroTagParser.ParseMacros(
                    source,
                    //callback for when text block is found
                    textBlock => sb.Append(textBlock),
                    //callback for when macro syntax is found
                    (macroAlias, macroAttributes) => sb.Append(umbracoHelper.RenderMacro(
                                                                   macroAlias,
                                                                   //needs to be explicitly casted to Dictionary<string, object>
                                                                   macroAttributes.ConvertTo(x => (string)x, x => x)).ToString()));
            }
            finally
            {
                // restore
                UmbracoContext.Current.InPreviewMode = inPreviewMode;
            }

            return(sb.ToString());
        }
Exemple #3
0
        /// <summary>
        /// Parses the macros inside the text, by creating child elements for each item.
        /// </summary>
        /// <param name="item">The item.</param>
        protected virtual void ParseMacros(Item item)
        {
            // do nothing if the macros have already been rendered
            if (item.Controls.Count > 0)
            {
                return;
            }

            string elementText = GetFieldContents(item);

            using (DisposableTimer.DebugDuration <ItemRenderer>("Parsing Macros"))
            {
                MacroTagParser.ParseMacros(
                    elementText,

                    //callback for when a text block is parsed
                    textBlock => item.Controls.Add(new LiteralControl(textBlock)),

                    //callback for when a macro is parsed:
                    (macroAlias, attributes) =>
                {
                    var macroControl = new Macro
                    {
                        Alias = macroAlias
                    };
                    foreach (var i in attributes.Where(i => macroControl.Attributes[i.Key] == null))
                    {
                        macroControl.Attributes.Add(i.Key, i.Value);
                    }
                    item.Controls.Add(macroControl);
                });
            }
        }
 public bool Init(int CurrentNodeId, string PropertyData, out object instance)
 {
     //we're going to send the string through the macro parser and create the output string.
     if (UmbracoContext.Current != null)
     {
         var sb            = new StringBuilder();
         var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
         MacroTagParser.ParseMacros(
             PropertyData,
             //callback for when text block is found
             textBlock => sb.Append(textBlock),
             //callback for when macro syntax is found
             (macroAlias, macroAttributes) => sb.Append(umbracoHelper.RenderMacro(
                                                            macroAlias,
                                                            //needs to be explicitly casted to Dictionary<string, object>
                                                            macroAttributes.ConvertTo(x => (string)x, x => (object)x)).ToString()));
         instance = new HtmlString(sb.ToString());
     }
     else
     {
         //we have no umbraco context, so best we can do is convert to html string
         instance = new HtmlString(PropertyData);
     }
     return(true);
 }
        /// <summary>
        /// Parses out media UDIs from an HTML string based on embedded macro parameter values.
        /// </summary>
        /// <param name="text">HTML string</param>
        /// <returns></returns>
        public IEnumerable <UmbracoEntityReference> FindUmbracoEntityReferencesFromEmbeddedMacros(string text)
        {
            // There may be more than one macro with the same alias on the page so using a tuple
            var foundMacros = new List <Tuple <string?, Dictionary <string, string> > >();

            // This legacy ParseMacros() already finds the macros within a Rich Text Editor using regexes
            // It seems to lowercase the macro parameter alias - so making the dictionary case insensitive
            MacroTagParser.ParseMacros(text, textblock => { }, (macroAlias, macroAttributes) => foundMacros.Add(new Tuple <string?, Dictionary <string, string> >(macroAlias, new Dictionary <string, string>(macroAttributes, StringComparer.OrdinalIgnoreCase))));
            foreach (var umbracoEntityReference in GetUmbracoEntityReferencesFromMacros(foundMacros))
            {
                yield return(umbracoEntityReference);
            }
        }
Exemple #6
0
        /// <summary>
        /// Return IHtmlString so devs doesn't need to decode html
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override Attempt <object> ConvertPropertyValue(object value)
        {
            //we're going to send the string through the macro parser and create the output string.
            var sb            = new StringBuilder();
            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);

            MacroTagParser.ParseMacros(
                value.ToString(),
                //callback for when text block is found
                textBlock => sb.Append(textBlock),
                //callback for when macro syntax is found
                (macroAlias, macroAttributes) => sb.Append(umbracoHelper.RenderMacro(
                                                               macroAlias,
                                                               //needs to be explicitly casted to Dictionary<string, object>
                                                               macroAttributes.ConvertTo(x => (string)x, x => (object)x)).ToString()));

            return(new Attempt <object>(true, new HtmlString(sb.ToString())));
        }
        /// <summary>
        /// Parses the macros inside the text, by creating child elements for each item.
        /// </summary>
        /// <param name="item">The item.</param>
        protected virtual void ParseMacros(Item item)
        {
            // do nothing if the macros have already been rendered
            if (item.Controls.Count > 0)
            {
                return;
            }

            var elementText = GetFieldContents(item);

            //Don't parse macros if there's a content item assigned since the content value
            // converters take care of that, just add the already parsed text
            if (item.ContentItem != null)
            {
                item.Controls.Add(new LiteralControl(elementText));
            }
            else
            {
                using (DisposableTimer.DebugDuration <ItemRenderer>("Parsing Macros"))
                {
                    MacroTagParser.ParseMacros(
                        elementText,

                        //callback for when a text block is parsed
                        textBlock => item.Controls.Add(new LiteralControl(textBlock)),

                        //callback for when a macro is parsed:
                        (macroAlias, attributes) =>
                    {
                        var macroControl = new Macro
                        {
                            Alias = macroAlias
                        };
                        foreach (var i in attributes.Where(i => macroControl.Attributes[i.Key] == null))
                        {
                            macroControl.Attributes.Add(i.Key, i.Value);
                        }
                        item.Controls.Add(macroControl);
                    });
                }
            }
        }
        // NOT thread-safe over a request because it modifies the
        // global UmbracoContext.Current.InPreviewMode status. So it
        // should never execute in // over the same UmbracoContext with
        // different preview modes.
        string RenderMacros(string source, bool preview)
        {
            var umbracoContext = _umbracoContextAccessor.UmbracoContext;

            using (umbracoContext.ForcedPreview(preview)) // force for macro rendering
            {
                var sb = new StringBuilder();

                var umbracoHelper = new UmbracoHelper(umbracoContext, _services);
                MacroTagParser.ParseMacros(
                    source,
                    //callback for when text block is found
                    textBlock => sb.Append(textBlock),
                    //callback for when macro syntax is found
                    (macroAlias, macroAttributes) => sb.Append(umbracoHelper.RenderMacro(
                                                                   macroAlias,
                                                                   //needs to be explicitly casted to Dictionary<string, object>
                                                                   macroAttributes.ConvertTo(x => (string)x, x => x)).ToString()));

                return(sb.ToString());
            }
        }