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
コード例 #1
0
        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>");
        }
コード例 #2
0
        public void LinkSyntaxParser_Parses_Links()
        {
            var parser = new LinkSyntaxParser();
            var output = parser.Parse(@"<p>Lorem <a href=""" + HiveId.Empty.ToString() + @""" title=""ipsum"" data-rebel-link=""internal"">ipsum</a> dolar <a href=""" + HiveId.Empty.ToString() + @""" title=""sit"" data-rebel-link=""internal"">sit</a> amet.</p>",
                          (hiveId) => "replacement_link");

            Assert.IsTrue(output == @"<p>Lorem <a href=""replacement_link"" title=""ipsum"" >ipsum</a> dolar <a href=""replacement_link"" title=""sit"" >sit</a> amet.</p>");
        }
コード例 #3
0
ファイル: FieldRenderer.cs プロジェクト: RebelCMS/rebelcmsxu5
        public IHtmlString RenderField(IRoutableRequestContext routableRequestContext, ControllerContext controllerContext, Content item,
            string fieldAlias = "", string valueAlias = "", string altFieldAlias = "", string altValueAlias = "", string altText = "", string insertBefore = "", string insertAfter = "",
            bool recursive = false, bool convertLineBreaks = false, bool removeParagraphTags = false,
            RebelRenderItemCaseType casing = RebelRenderItemCaseType.Unchanged,
            RebelRenderItemEncodingType encoding = RebelRenderItemEncodingType.Unchanged,
            string formatString = "")
        {
            var sb = new StringBuilder();

            var valObj = GetFieldValue(item, fieldAlias, valueAlias, recursive);

            if ((valObj == null || valObj.ToString().IsNullOrWhiteSpace()) && !altFieldAlias.IsNullOrWhiteSpace())
            {
                valObj = GetFieldValue(item, altFieldAlias, altValueAlias, recursive);
            }

            if ((valObj == null || valObj.ToString().IsNullOrWhiteSpace()) && !altText.IsNullOrWhiteSpace())
            {
                valObj = altText;
            }

            if(!formatString.IsNullOrWhiteSpace())
                formatString = "{0:" + formatString.Replace("\\", "\\\\").Replace("\"", "\\\"") + "}";
            else
                formatString = "{0}";

            var val = string.Format(formatString, valObj);

            if(!val.IsNullOrWhiteSpace())
            {
                switch (casing)
                {
                    case RebelRenderItemCaseType.Upper:
                        val = val.ToUpper();
                        break;
                    case RebelRenderItemCaseType.Lower:
                        val = val.ToLower();
                        break;
                    case RebelRenderItemCaseType.Title:
                        val = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(val);
                        break;
                    default:
                        break;
                }

                switch (encoding)
                {
                    case RebelRenderItemEncodingType.Url:
                        val = HttpUtility.UrlEncode(val);
                        break;
                    case RebelRenderItemEncodingType.Html:
                        val = HttpUtility.HtmlEncode(val);
                        break;
                    default:
                        break;
                }

                if (convertLineBreaks)
                {
                    val = val.Replace(Environment.NewLine, "<br />");
                }

                if (removeParagraphTags)
                {
                    val = val.Trim().Trim("<p>").Trim("</p>");
                }

                sb.Append(HttpUtility.HtmlDecode(insertBefore));
                sb.Append(val);
                sb.Append(HttpUtility.HtmlDecode(insertAfter));
            }

            //now we need to parse the macro syntax out and replace it with the rendered macro content

            var macroRenderer = new MacroRenderer(routableRequestContext.RegisteredComponents, routableRequestContext);
            var macroParser = new MacroSyntaxParser();
            IEnumerable<MacroParserResult> parseResults;
            var parsed = macroParser.Parse(sb.ToString(),
                                           (macroAlias, macroParams)
                                           => macroRenderer.RenderMacroAsString(macroAlias,
                                                                                macroParams,
                                                                                controllerContext, false,
                                                                                () => item), out parseResults);

            //now we need to parse any internal links and replace with actual URLs
            var linkParse = new LinkSyntaxParser();
            parsed = linkParse.Parse(parsed, x => routableRequestContext.RoutingEngine.GetUrl(x));

            return new MvcHtmlString(parsed);
        }
コード例 #4
0
ファイル: FieldRenderer.cs プロジェクト: RebelCMS/rebelcmsxu5
        public IHtmlString RenderField(IRoutableRequestContext routableRequestContext, ControllerContext controllerContext, Content item,
                                       string fieldAlias = "", string valueAlias         = "", string altFieldAlias        = "", string altValueAlias = "", string altText = "", string insertBefore = "", string insertAfter = "",
                                       bool recursive    = false, bool convertLineBreaks = false, bool removeParagraphTags = false,
                                       RebelRenderItemCaseType casing       = RebelRenderItemCaseType.Unchanged,
                                       RebelRenderItemEncodingType encoding = RebelRenderItemEncodingType.Unchanged,
                                       string formatString = "")
        {
            var sb = new StringBuilder();

            var valObj = GetFieldValue(item, fieldAlias, valueAlias, recursive);

            if ((valObj == null || valObj.ToString().IsNullOrWhiteSpace()) && !altFieldAlias.IsNullOrWhiteSpace())
            {
                valObj = GetFieldValue(item, altFieldAlias, altValueAlias, recursive);
            }

            if ((valObj == null || valObj.ToString().IsNullOrWhiteSpace()) && !altText.IsNullOrWhiteSpace())
            {
                valObj = altText;
            }

            if (!formatString.IsNullOrWhiteSpace())
            {
                formatString = "{0:" + formatString.Replace("\\", "\\\\").Replace("\"", "\\\"") + "}";
            }
            else
            {
                formatString = "{0}";
            }

            var val = string.Format(formatString, valObj);

            if (!val.IsNullOrWhiteSpace())
            {
                switch (casing)
                {
                case RebelRenderItemCaseType.Upper:
                    val = val.ToUpper();
                    break;

                case RebelRenderItemCaseType.Lower:
                    val = val.ToLower();
                    break;

                case RebelRenderItemCaseType.Title:
                    val = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(val);
                    break;

                default:
                    break;
                }

                switch (encoding)
                {
                case RebelRenderItemEncodingType.Url:
                    val = HttpUtility.UrlEncode(val);
                    break;

                case RebelRenderItemEncodingType.Html:
                    val = HttpUtility.HtmlEncode(val);
                    break;

                default:
                    break;
                }

                if (convertLineBreaks)
                {
                    val = val.Replace(Environment.NewLine, "<br />");
                }

                if (removeParagraphTags)
                {
                    val = val.Trim().Trim("<p>").Trim("</p>");
                }

                sb.Append(HttpUtility.HtmlDecode(insertBefore));
                sb.Append(val);
                sb.Append(HttpUtility.HtmlDecode(insertAfter));
            }

            //now we need to parse the macro syntax out and replace it with the rendered macro content

            var macroRenderer = new MacroRenderer(routableRequestContext.RegisteredComponents, routableRequestContext);
            var macroParser   = new MacroSyntaxParser();
            IEnumerable <MacroParserResult> parseResults;
            var parsed = macroParser.Parse(sb.ToString(),
                                           (macroAlias, macroParams)
                                           => macroRenderer.RenderMacroAsString(macroAlias,
                                                                                macroParams,
                                                                                controllerContext, false,
                                                                                () => item), out parseResults);

            //now we need to parse any internal links and replace with actual URLs
            var linkParse = new LinkSyntaxParser();

            parsed = linkParse.Parse(parsed, x => routableRequestContext.RoutingEngine.GetUrl(x));

            return(new MvcHtmlString(parsed));
        }