Exemple #1
0
        public List <IShortcode> GetShortcodes(string content)
        {
            ParseInternal(content);

            if (!ParseInstructions.Any())
            {
                return(null);
            }

            return(ParseInstructions
                   .Where(pi => !string.IsNullOrEmpty(pi.Tag))
                   .Select(pi => ShortcodeProvider.Create(pi.Tag, pi.Attributes, pi.Content, false))
                   .ToList());
        }
Exemple #2
0
        public string Parse(string content)
        {
            ParseInternal(content);

            var contentBuilder = new StringBuilder(content);

            if (ParseInstructions.Any())
            {
                var contentPositionAdjust = 0;
                var initialContentLength  = contentBuilder.Length;

                foreach (var shortcodeInfo in ParseInstructions.OrderBy(pi => pi.BeginPosition))
                {
                    string generatedText = null;

                    if (shortcodeInfo.Tag != null)
                    {
                        var shortcode = ShortcodeProvider.Create(shortcodeInfo.Tag, shortcodeInfo.Attributes, shortcodeInfo.Content);
                        generatedText = shortcode.Generate(new ShortcodeContext {
                            Tag = shortcodeInfo.Tag, Parser = this
                        });
                    }
                    else
                    {
                        generatedText = shortcodeInfo.PreCompiledContent;
                    }

                    if (generatedText == null)
                    {
                        generatedText = string.Empty;
                    }

                    var startIndex = shortcodeInfo.BeginPosition + contentPositionAdjust;
                    if (startIndex < 0)
                    {
                        startIndex = 0;
                    }

                    contentBuilder.Remove(startIndex, shortcodeInfo.EndPosition - shortcodeInfo.BeginPosition);
                    contentBuilder.Insert(startIndex, generatedText);

                    contentPositionAdjust = contentBuilder.Length - initialContentLength;
                }
            }

            return(contentBuilder.ToString());
        }