Exemplo n.º 1
0
        public static void AssignSubMenu(List <Element> Links, RawMenu ParentMenu)
        {
            var commonparent = DomService.FindParent(Links);

            foreach (var item in Links)
            {
                var menu = new RawMenu();
                menu.Parent           = ParentMenu;
                menu.LinkElement      = item;
                menu.ContainerElement = FindLinkContainer(item, Links, commonparent);
                menu.Parent           = ParentMenu;
                ParentMenu.Children.Add(menu);
            }

            foreach (var item in ParentMenu.Children)
            {
                var sublinks = FindSubLinks(item);
                if (sublinks != null && sublinks.Count() > 0)
                {
                    var itemcontainer = DomService.FindParent(sublinks);
                    AssignSubMenu(sublinks, item);
                }
            }
            return;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Convert one group of links into menu, links must be within one group.
        /// </summary>
        /// <param name="links"></param>
        /// <param name="website"></param>
        /// <param name="IncludeParentInTemplate"></param>
        /// <param name="maxsiblingindex">if this links group share parents with another link group, should only check sub menu till end of next sibling start. </param>
        /// <returns></returns>
        public static Menu ConvertToMenu(List <Element> links, WebSite website)
        {
            bool includeparentintemplate = true;
            int  maxsilbingindex         = 9999;

            links = links.OrderBy(o => o.location.openTokenStartIndex).ToList();
            Element commonparent = DomService.FindParent(links);
            var     lastlink     = links.LastOrDefault();

            TestTemplate(ref includeparentintemplate, ref maxsilbingindex, commonparent, lastlink);

            return(ConvertToMenu(links, commonparent, includeparentintemplate, website, maxsilbingindex));
        }
Exemplo n.º 3
0
        private static Element _GetItemContainer(Document doc, MenuViewModel MenuViewModel)
        {
            // find the item container.
            if (!string.IsNullOrEmpty(MenuViewModel.ItemContainer))
            {
                var itemcontainer = DomService.GetElementByKoobooId(doc, MenuViewModel.ItemContainer) as Element;
                if (itemcontainer != null)
                {
                    return(itemcontainer);
                }
            }

            List <Element> containerlist = new List <Element>();

            foreach (var item in MenuViewModel.children)
            {
                if (!string.IsNullOrEmpty(item.ContainerElement))
                {
                    var element = DomService.GetElementByKoobooId(doc, item.ContainerElement) as Element;
                    if (element != null)
                    {
                        containerlist.Add(element);
                    }
                }
            }

            if (containerlist.Count == 0)
            {
                foreach (var item in MenuViewModel.children)
                {
                    var element = DomService.GetElementByKoobooId(doc, item.LinkElement) as Element;
                    if (element != null)
                    {
                        containerlist.Add(element);
                    }
                }
            }
            if (containerlist.Count > 1)
            {
                var element = DomService.FindParent(containerlist);
                if (element != null)
                {
                    MenuViewModel.ItemContainer = DomService.GetKoobooId(element);
                }
                return(element);
            }
            return(null);
        }
Exemplo n.º 4
0
        internal static List <Element> GetSiblingSubLinks(Element CurrentContainer, Element NextContainer)
        {
            List <Node> BetweenNodes = new List <Node>();

            int MaxSiblingIndex = 9999;

            if (NextContainer != null)
            {
                MaxSiblingIndex = NextContainer.siblingIndex;
                var parent = DomService.FindParent(CurrentContainer, NextContainer);

                var current = CurrentContainer;
                while (current.depth > parent.depth + 1)
                {
                    current = current.parentElement;
                }

                var next = NextContainer;
                while (next.depth > parent.depth + 1)
                {
                    next = next.parentElement;
                }
                var NodesInBetween = parent.childNodes.item.Where(o => o.siblingIndex > current.siblingIndex && o.siblingIndex < next.siblingIndex).ToList();

                var alllinks = DomService.GetElementsByTagName(NodesInBetween, "a").item;
                if (alllinks.Count() > 0)
                {
                    var group = GroupBy(alllinks);
                    return(group[0]);
                }
            }
            else
            {
                var parent = CurrentContainer.parentElement;

                var NodesInBetween = parent.childNodes.item.Where(o => o.siblingIndex > CurrentContainer.siblingIndex).ToList();

                var alllinks = DomService.GetElementsByTagName(NodesInBetween, "a").item;
                if (alllinks.Count() > 0)
                {
                    var group = GroupBy(alllinks);
                    return(group[0]);
                }
            }


            return(null);
        }
Exemplo n.º 5
0
        public static string DetectKoobooId(Document dom, List <string> SubLinks)
        {
            var  allinks         = dom.getElementsByTagName("a").item;
            var  GroupbyLinks    = MenuService.SimpleGroupBy(allinks);
            bool match           = true;
            bool onetimedismatch = false;

            foreach (var item in GroupbyLinks)
            {
                var allgrouplinks = item.Select(o => o.getAttribute("href"));

                match = false;
                foreach (var link in SubLinks)
                {
                    if (allgrouplinks.Any(o => IsSameUrl(o, link)))
                    {
                        match = true;
                    }
                    else
                    {
                        if (!onetimedismatch)
                        {
                            onetimedismatch = true;
                        }
                        else
                        {
                            match = false;
                            break;
                        }
                    }
                }

                if (match)
                {
                    var commoneparent = DomService.FindParent(item);
                    if (commoneparent != null)
                    {
                        return(DomService.GetKoobooId(commoneparent));
                    }
                }
            }

            return(null);
        }
Exemplo n.º 6
0
        /// <summary>
        /// convert one parent with sub links into a menu....
        /// </summary>
        /// <param name="links"></param>
        /// <param name="CommonParent"></param>
        /// <param name="IncludeParentInTemplate"></param>
        /// <param name="website"></param>
        /// <returns></returns>
        public static Menu ConvertToMenu(List <Element> links, Element CommonParent, bool IncludeParentInTemplate, WebSite website, int maxsiblingindex = 9999)
        {
            Menu menu = new Menu();

            links = links.OrderBy(o => o.location.openTokenStartIndex).ToList();

            List <Element> Parents = new List <Element>();

            int count = links.Count;

            for (int i = 0; i < count; i++)
            {
                var parent = links[i];
                while (!parent.parentElement.isEqualNode(CommonParent) && parent != null)
                {
                    parent = parent.parentElement;
                }
                Parents.Add(parent);
            }

            menu.tempdata.StartIndex = Parents[0].location.openTokenStartIndex;
            menu.tempdata.EndIndex   = Parents[count - 1].location.endTokenEndIndex;

            for (int i = 0; i < count; i++)
            {
                var parent      = Parents[i];
                var linkelement = links[i];

                Menu submenu = ConvertToMenu(parent, links[i], website);

                if (submenu.children.Count == 0)
                {
                    int nextSiblingEnds = maxsiblingindex;
                    if (i < count - 1)
                    {
                        var nextparent = Parents[i + 1];
                        nextSiblingEnds = nextparent.siblingIndex;
                    }

                    var tempsublinks = FindLinksAfter(CommonParent, links[i], nextSiblingEnds);

                    /// Check and make sure those sub links are not similar, same level links.
                    List <Element> sublinks = new List <Element>();

                    if (tempsublinks.Count > 0)
                    {
                        foreach (var item in tempsublinks)
                        {
                            if (isSimiliarLink(item, linkelement))
                            {
                                break;
                            }
                            sublinks.Add(item);
                        }

                        var groupbylinks = GroupBy(sublinks);

                        foreach (var item in groupbylinks)
                        {
                            var subcommonparent = DomService.FindParent(item);

                            bool SubIncludeParent = false;

                            if (!subcommonparent.isEqualNode(CommonParent) && subcommonparent.depth < CommonParent.depth)
                            {
                                SubIncludeParent = true;
                            }
                            Menu subsubmenu = ConvertToMenu(item, subcommonparent, SubIncludeParent, website);

                            submenu.AddSubMenu(subsubmenu);
                        }
                    }
                }

                menu.AddSubMenu(submenu);
            }

            string menuitemsstring = CommonParent.ownerDocument.HtmlSource.Substring(menu.tempdata.StartIndex, menu.tempdata.EndIndex - menu.tempdata.StartIndex + 1);

            if (IncludeParentInTemplate)
            {
                string tempTemplate = CommonParent.OuterHtml;
                menu.Template = tempTemplate.Replace(menuitemsstring, "{items}");

                if (CommonParent.location.openTokenStartIndex < menu.tempdata.StartIndex)
                {
                    menu.tempdata.StartIndex = CommonParent.location.openTokenStartIndex;
                }

                if (CommonParent.location.endTokenEndIndex > menu.tempdata.EndIndex)
                {
                    menu.tempdata.EndIndex = CommonParent.location.endTokenEndIndex;
                }
            }
            else
            {
                menu.Template = "{items}";
            }

            return(menu);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Convert one link item with its parent element into one menu item...
        /// example: Li, a, website..... Convert the a under li tag into one mentu....
        /// </summary>
        /// <param name="parentElement"></param>
        /// <param name="linkElement"></param>
        /// <param name="website"></param>
        /// <returns></returns>
        public static Menu ConvertToMenu(Element parentElement, Element linkElement, WebSite website)
        {
            Menu menu = new Menu();

            string template = parentElement.OuterHtml;

            string anchortext = linkElement.InnerHtml;
            string href       = linkElement.getAttribute("href");

            template = template.Replace(linkElement.OuterHtml, ConvertToTemplate(linkElement, href, anchortext));

            menu.Template = template;
            menu.Name     = anchortext;
            menu.Url      = href;

            // Check if there is a submenu....
            var alllinks = parentElement.getElementsByTagName("a").item;

            if (alllinks.Count > 1)
            {
                /// there is sub menu...
                List <Element> sublinks = new List <Element>();

                string mainhref = linkElement.getAttribute("href");

                foreach (var item in alllinks)
                {
                    string subhref = item.getAttribute("href");

                    if (subhref != mainhref)
                    {
                        sublinks.Add(item);
                    }
                }

                if (sublinks.Count() > 0)
                {
                    int SubGroupStartIndex;
                    int SubGroupEndIndex;

                    var groupby = GroupBy(sublinks);

                    if (groupby.Count == 1)
                    {
                        var submenulinks = groupby[0];

                        SubGroupStartIndex = submenulinks.First().location.openTokenStartIndex;
                        SubGroupEndIndex   = submenulinks.Last().location.endTokenEndIndex;

                        var  subcommonparent = DomService.FindParent(submenulinks);
                        bool includeparent   = true;

                        if (subcommonparent.isEqualNode(linkElement) || subcommonparent.isEqualNode(parentElement))
                        {
                            includeparent = false;
                        }

                        var submenu = ConvertToMenu(submenulinks, subcommonparent, includeparent, website);

                        if (submenu.tempdata.EndIndex > SubGroupEndIndex)
                        {
                            SubGroupEndIndex = submenu.tempdata.EndIndex;
                        }

                        if (submenu.tempdata.StartIndex < SubGroupStartIndex)
                        {
                            SubGroupStartIndex = submenu.tempdata.StartIndex;
                        }

                        menu.SubItemContainer = submenu.Template;
                        // menu.SubItemSeperator = submenu.SubItemSeperator;
                        menu.children = submenu.children;
                    }

                    else
                    {
                        SubGroupStartIndex = groupby.First().First().location.openTokenStartIndex;
                        SubGroupEndIndex   = groupby.Last().Last().location.endTokenEndIndex;

                        foreach (var item in groupby)
                        {
                            var  commonparent            = DomService.FindParent(item);
                            bool includeparentintemplate = false;

                            if (!commonparent.isEqualNode(parentElement) && commonparent.depth > parentElement.depth)
                            {
                                includeparentintemplate = true;
                            }

                            var submenu = ConvertToMenu(item, commonparent, includeparentintemplate, website);
                            // menu.Template = menu.Template.Replace(submenu.tempdata.TempOriginalText, placeholder);

                            if (submenu.tempdata.EndIndex > SubGroupEndIndex)
                            {
                                SubGroupEndIndex = submenu.tempdata.EndIndex;
                            }

                            if (submenu.tempdata.StartIndex < SubGroupStartIndex)
                            {
                                SubGroupStartIndex = submenu.tempdata.StartIndex;
                            }

                            menu.AddSubMenu(submenu);
                        }

                        // try to find the seperator....
                        List <Element> parents = new List <Element>();
                        parents.Add(groupby[0].Last());
                        parents.Add(groupby[1].First());
                        //var seperatorparent = FindCommonParent(parents);
                        //var seperator = FindSeperator(parents, seperatorparent);
                        //if (!string.IsNullOrEmpty(seperator))
                        //{
                        //    menu.SubItemSeperator = seperator;
                        //}
                    }

                    string subgrouptext = parentElement.ownerDocument.HtmlSource.Substring(SubGroupStartIndex, SubGroupEndIndex - SubGroupStartIndex + 1);

                    menu.Template = menu.Template.Replace(subgrouptext, "{items}");
                }
            }

            return(menu);
        }