コード例 #1
0
        public async Task ParseParameters_MultipleLine()
        {
            IContextMetadata data = null;

            var mock = new Mock <IDynamicContentTransformer>();

            mock.Setup(m => m.Transform(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <ContextModel>()))
            .Returns(new Func <string, string, IContextModel, Task <string> >(
                         (c, k, m) =>
            {
                data = m.Data;
                return(Task.FromResult(""));
            }));

            var handler = new IncludesHandler(mock.Object, new Mock <IIncludesHandlerExtension>().Object);

            var s = new Site("", "", new PageMock("", ""), null);

            s.Includes.Add(new TemplateMock("include", ""));

            await handler.ResolveAll("{% include a1: A\r\na2: B\r\na3:\r\n    - X\r\n    - Y %}", s, s.MainPage, "");

            Assert.AreEqual(3, data.Count);
            Assert.AreEqual("A", data["a1"]);
            Assert.AreEqual("B", data["a2"]);
            Assert.AreEqual(2, (data["a3"] as List <object>).Count);
            Assert.AreEqual("X", (data["a3"] as List <object>)[0]);
            Assert.AreEqual("Y", (data["a3"] as List <object>)[1]);
        }
コード例 #2
0
        /// <summary>
        /// Extracts the title from this page
        /// </summary>
        /// <param name="page">This page</param>
        /// <param name="data">Page metadata</param>
        /// <returns>Title</returns>
        public static string GetTitle(IContextPage page, IContextMetadata data)
        {
            var title = page.Data.GetOrDefault <string>(DEFAULT_TITLE_ATT);

            if (string.IsNullOrEmpty(title))
            {
                title = page.Url.Split('/', StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
            }

            return(title);
        }
コード例 #3
0
        public async Task ParseParameters_SingleLine()
        {
            IContextMetadata data = null;

            var mock = new Mock <IDynamicContentTransformer>();

            mock.Setup(m => m.Transform(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <ContextModel>()))
            .Returns(new Func <string, string, IContextModel, Task <string> >(
                         (c, k, m) =>
            {
                data = m.Data;
                return(Task.FromResult(""));
            }));

            var handler = new IncludesHandler(mock.Object, new Mock <IIncludesHandlerExtension>().Object);

            var s = new Site("", "", new PageMock("", ""), null);

            s.Includes.Add(new TemplateMock("include", ""));

            IContextMetadata p1, p2, p3;

            await handler.ResolveAll("{% include a1: A %}", s, s.MainPage, "");

            p1 = data;

            await handler.ResolveAll("{%  include  a1: A %}", s, s.MainPage, "");

            p2 = data;

            await handler.ResolveAll("{% include { a1: A, a2: 0.2 } %}", s, s.MainPage, "");

            p3 = data;

            Assert.AreEqual(1, p1.Count);
            Assert.AreEqual("A", p1["a1"]);

            Assert.AreEqual(1, p2.Count);
            Assert.AreEqual("A", p2["a1"]);

            Assert.AreEqual(2, p3.Count);
            Assert.AreEqual("A", p3["a1"]);
            Assert.AreEqual("0.2", p3["a2"]);
        }
コード例 #4
0
        /// <summary>
        /// Returns the caption (short title) of the page
        /// </summary>
        /// <param name="page">This page</param>
        /// <param name="data">Page metadata</param>
        /// <returns>Caption</returns>
        public static string GetCaption(IContextPage page, IContextMetadata data)
        {
            var caption = "";

            if (data != null)
            {
                string titleAtt;

                if (data.TryGet(TITLE_ATT, out titleAtt) && !string.IsNullOrEmpty(titleAtt))
                {
                    caption = page.Data.GetOrDefault <string>(titleAtt);
                }
            }

            if (string.IsNullOrEmpty(caption))
            {
                caption = GetTitle(page, data);
            }

            return(caption);
        }
コード例 #5
0
        public async Task ParseParameters_NoParameters()
        {
            IContextMetadata data = null;

            var mock = new Mock <IDynamicContentTransformer>();

            mock.Setup(m => m.Transform(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <ContextModel>()))
            .Returns(new Func <string, string, IContextModel, Task <string> >(
                         (c, k, m) =>
            {
                data = m.Data;
                return(Task.FromResult(""));
            }));

            var handler = new IncludesHandler(mock.Object, new Mock <IIncludesHandlerExtension>().Object);

            var s = new Site("", "", new PageMock("", ""), null);

            s.Includes.Add(new TemplateMock("include", ""));

            await handler.ResolveAll("{% include %}", s, s.MainPage, "");

            Assert.AreEqual(0, data.Count);
        }
コード例 #6
0
        private static void ParsePages(List <object> menuList, List <MenuPage> menuPagesList, IEnumerable <IContextPage> allPages, IContextMetadata data)
        {
            MenuPage CreateMenuPage(string val)
            {
                const string URL_PATTERN = @"\[(.*)\]\((.*)\)(?:{(.+)})?";

                var match = Regex.Match(val, URL_PATTERN);

                string url   = "";
                string title = "";

                string[] scope = null;

                var overwriteTitle = match.Success;

                if (overwriteTitle)
                {
                    title = match.Groups[1].Value;
                    url   = match.Groups[2].Value;

                    var scopeAtt = match.Groups[3].Value;

                    if (!string.IsNullOrEmpty(scopeAtt))
                    {
                        scope = scopeAtt.Split(',', StringSplitOptions.RemoveEmptyEntries)
                                .Select(f => f.Trim()).ToArray();
                    }
                }

                var page = allPages?.FirstOrDefault(p => string.Equals(p.Url,
                                                                       !string.IsNullOrEmpty(url) ? url : val, StringComparison.CurrentCultureIgnoreCase));

                if (page != null)
                {
                    url = page.Url;
                }
                else
                {
                    if (!overwriteTitle)
                    {
                        title = val;
                    }
                }

                var menuPage = new MenuPage()
                {
                    Url   = url,
                    Scope = scope
                };

                menuPage.DataDictionary["title"] = val;

                if (page != null)
                {
                    menuPage.FullUrl = page.FullUrl;

                    foreach (var att in page.Data)
                    {
                        menuPage.DataDictionary[att.Key] = att.Value;
                    }
                }

                if (overwriteTitle)
                {
                    menuPage.DataDictionary["title"]   = title;
                    menuPage.DataDictionary["caption"] = title;
                }

                return(menuPage);
            }

            foreach (var menuItem in menuList)
            {
                if (menuItem is string)
                {
                    menuPagesList.Add(CreateMenuPage((string)menuItem));
                }
                else if (menuItem is Dictionary <string, object> )
                {
                    foreach (var parentMenuItem in menuItem as Dictionary <string, object> )
                    {
                        var menuPage = CreateMenuPage(parentMenuItem.Key);
                        menuPagesList.Add(menuPage);
                        ParsePages(parentMenuItem.Value as List <object>, menuPage.SubPagesList, allPages, data);
                    }
                }
                else
                {
                    throw new NotSupportedException("Invalid type of menu item");
                }
            }
        }
コード例 #7
0
        /// <summary>
        /// Build the menu based on predefined parameter
        /// </summary>
        /// <param name="menuOptName">Name of the menu parameter</param>
        /// <param name="site">Site</param>
        /// <param name="data">Metadata or configuration</param>
        /// <returns>Predefined menu</returns>
        public static IEnumerable <IContextPage> BuildPredefinedMenu(string menuOptName, IContextSite site, IContextMetadata data)
        {
            List <object> menu;

            if (data.TryGet(menuOptName, out menu) && menu != null)
            {
                var allPages      = PageHelper.GetAllPages(site.MainPage);
                var menuPagesList = new List <MenuPage>();
                ParsePages(menu, menuPagesList, allPages, data);
                return(menuPagesList);
            }
            else
            {
                return(null);
            }
        }