예제 #1
0
 public static IList <Group> GroupConsecutiveSameStyleBlocks(IList <Group> groups,
                                                             bool header = true, bool codeBlocks = true, bool blockquotes = true)
 {
     return(GroupGroup.GroupConsecutiveElementsWhile(groups,
                                                     (Group g, Group gPrev) =>
     {
         if (g is BlockGroup bg && gPrev is BlockGroup bgPrev)
         {
             return (codeBlocks && AreBothCodeblocks(bg, bgPrev)) ||
             (blockquotes && AreBothBlockquotesWithSameAdi(bg, bgPrev)) ||
             (header && AreBothSameHeadersWithSameAdi(bg, bgPrev));
         }
         return false;
     }));
예제 #2
0
        public static IList <Group> Nest(IList <Group> groups)
        {
            var listBlocked         = ConvertListBlocksToListGroups(groups);
            var groupedByListGroups = GroupConsecutiveListGroups(listBlocked);

            // convert grouped ones into listgroup
            var nested = groupedByListGroups.SelectMany(g => g is GroupGroup gg ?
                                                        NestListSection(gg.Groups.Cast <ListGroup>().ToList()) :
                                                        Enumerable.Repeat(g, 1)).ToList();

            var groupRootLists = GroupGroup.GroupConsecutiveElementsWhile(nested,
                                                                          (curr, prev) =>
            {
                if (curr is ListGroup currLg && prev is ListGroup prevLg)
                {
                    return(currLg.Items[0].Item.Op.IsSameListAs(
                               prevLg.Items[0].Item.Op));
                }
                return(false);
            });
예제 #3
0
        ///
        /// Returns a new array by putting consecutive elements satisfying predicate into a new
        /// array and returning others as they are.
        /// Ex: [1, "ha", 3, "ha", "ha"] => [1, "ha", 3, ["ha", "ha"]]
        ///      where predicate: (v, vprev) => typeof v === typeof vPrev
        ///
        public static IList <Group> GroupConsecutiveElementsWhile(IList <Group> arr,
                                                                  Func <Group, Group, bool> predicate)
        {
            var groups = new List <GroupGroup>();

            for (int i = 0; i < arr.Count; ++i)
            {
                var currElm = arr[i];
                if (i > 0 && predicate(currElm, arr[i - 1]))
                {
                    groups.Last().Groups.Add(currElm);
                }
                else
                {
                    var group = new GroupGroup();
                    group.Groups.Add(currElm);
                    groups.Add(group);
                }
            }
            return(groups.Select(g => g.Groups.Count == 1 ? g.Groups[0] : g).ToList());
        }