Rebel.Cms.Web.LinkSyntaxParser.Parse C# (CSharp) Method

Parse() public method

Finds an internal link amongst the input content (generally html) and renders the correct url into the output.
public Parse ( string input, string>.Func linkFoundCallback ) : string
input string The input.
linkFoundCallback string>.Func The link found callback.
return string
        public string Parse(string input,
            Func<HiveId, string> linkFoundCallback) 
        {
            var s = TagMatch.Replace(input, x =>
            {
                var strLink = x.ToString();

                // Links
                if(HrefMatch.IsMatch(strLink))
                {
                    var hiveId = new HiveId(HrefMatch.Match(strLink).Groups["Href"].Value);

                    var url = linkFoundCallback(hiveId);

                    strLink = HrefMatch.Replace(strLink, "href=\"" + url + "\"");
                    strLink = strLink.Replace(@"data-rebel-link=""internal""", "");
                }
                // Media
                else if (SrcMatch.IsMatch(strLink) && RebelSrcMatch.IsMatch(strLink))
                {
                    var hiveId = new HiveId(RebelSrcMatch.Match(strLink).Groups["Src"].Value);

                    var url = linkFoundCallback(hiveId);

                    if(!url.IsNullOrWhiteSpace())
                    {
                        strLink = SrcMatch.Replace(strLink, "src=\"" + url + "\"");
                        strLink = RebelSrcMatch.Replace(strLink, "");
                        strLink = strLink.Replace(@"data-rebel-link=""internal""", "");
                    }
                    else
                    {
                        strLink = "";
                    }
                }

                return strLink;
            });

            return s;
        }
    }

Usage Example

        public void LinkSyntaxParser_Does_Not_Parses_Links()
        {
            var parser = new LinkSyntaxParser();
            var output = parser.Parse("<p><a href=\"http://www.linkedsite.com\"><img src=\"../../../../Content/Media/e74f82351b0b4a35819e6373e3fbfddc/testimage.png\" alt=\"linked site\" width=\"170\" height=\"64\" data-rebel-link=\"internal\" data-rebel-src=\"media$empty_root$$_p__nhibernate$_v__guid$_c2f2015e7e724d2a822a9ff000f20224\" /></a></p>",
                          (hiveId) => "replacement_link");

            Assert.IsTrue(output == "<p><a href=\"http://www.linkedsite.com\"><img src=\"replacement_link\" alt=\"linked site\" width=\"170\" height=\"64\"   /></a></p>");
        }
All Usage Examples Of Rebel.Cms.Web.LinkSyntaxParser::Parse
LinkSyntaxParser