Exemplo n.º 1
0
 private static StringBuffer RenderSections(IMarkdownRenderer renderer, DfmTabGroupBlockToken token, StringBuffer sb, string groupId)
 {
     for (int i = 0; i < token.Items.Length; i++)
     {
         var item = token.Items[i];
         sb += @"<section id=""tabpanel_";
         sb  = AppendGroupId(sb, groupId, item);
         sb += @""" role=""tabpanel"" data-tab=""";
         sb += item.Id;
         if (!string.IsNullOrEmpty(item.Condition))
         {
             sb += @""" data-condition=""";
             sb += item.Condition;
         }
         if (i == token.ActiveTabIndex)
         {
             sb += "\">\n";
         }
         else
         {
             sb += "\" aria-hidden=\"true\" hidden=\"hidden\">\n";
         }
         sb += renderer.Render(item.Content);
         sb += "</section>\n";
     }
     return(sb);
 }
Exemplo n.º 2
0
 private static StringBuffer RenderTabHeaders(IMarkdownRenderer renderer, DfmTabGroupBlockToken token, StringBuffer sb, string groupId)
 {
     sb += "<ul role=\"tablist\">\n";
     for (int i = 0; i < token.Items.Length; i++)
     {
         var item = token.Items[i];
         sb += "<li role=\"presentation\">\n";
         sb += @"<a href=""#tabpanel_";
         sb  = AppendGroupId(sb, groupId, item);
         sb += @""" role=""tab"" aria-controls=""tabpanel_";
         sb  = AppendGroupId(sb, groupId, item);
         sb += @""" data-tab=""";
         sb += item.Id;
         if (!string.IsNullOrEmpty(item.Condition))
         {
             sb += @""" data-condition=""";
             sb += item.Condition;
         }
         if (i == token.ActiveTabIndex)
         {
             sb += "\" tabindex=\"0\" aria-selected=\"true\"";
         }
         else
         {
             sb += "\" tabindex=\"-1\"";
         }
         sb  = AppendSourceInfo(sb, renderer, item.Title);
         sb += ">";
         sb += renderer.Render(item.Title);
         sb += "</a>\n";
         sb += "</li>\n";
     }
     sb += "</ul>\n";
     return(sb);
 }
Exemplo n.º 3
0
        public virtual StringBuffer Render(IMarkdownRenderer renderer, DfmTabGroupBlockToken token, MarkdownBlockContext context)
        {
            var result = StringBuffer.Empty;
            foreach (var item in token.Items)
            {
                result += "#### [";
                foreach (var contentItem in item.Title.Content.Tokens)
                {
                    result += renderer.Render(contentItem);
                }
                result += "](#tab/";
                result += item.Id;
                if (string.IsNullOrEmpty(item.Condition))
                {
                    result += "/";
                    result += item.Condition;
                }
                result += ")\n";

                foreach (var contentItem in item.Content.Content)
                {
                    result += renderer.Render(contentItem);
                }
            }

            result += "* * *\n";
            return result;
        }
Exemplo n.º 4
0
        private static DfmTabGroupBlockToken RewriteActiveAndVisible(DfmTabGroupBlockToken token, List <string[]> tabSelectionInfo)
        {
            var items           = token.Items.ToList();
            int firstVisibleTab = ApplyTabVisible(tabSelectionInfo, items);
            var idAndCountList  = GetTabIdAndCountList(items).ToList();

            if (idAndCountList.Any(g => g.Item2 > 1))
            {
                Logger.LogWarning($"Duplicate tab id: {string.Join(",", idAndCountList.Where(g => g.Item2 > 1))}.", line: token.SourceInfo.LineNumber.ToString(), code: WarningCodes.Markdown.DuplicateTabId);
            }
            var active = GetTabActive(token, tabSelectionInfo, items, firstVisibleTab, idAndCountList);

            return(new DfmTabGroupBlockToken(token.Rule, token.Context, token.Id, items.ToImmutableArray(), active, token.SourceInfo));
        }
Exemplo n.º 5
0
        private static int GetTabActive(DfmTabGroupBlockToken token, List <string[]> tabSelectionInfo, List <DfmTabItemBlockToken> items, int firstVisibleTab, List <Tuple <string, int> > idAndCountList)
        {
            int  active          = -1;
            bool hasDifferentSet = false;

            foreach (var info in tabSelectionInfo)
            {
                var set = info.Intersect(from pair in idAndCountList select pair.Item1).ToList();
                if (set.Count > 0)
                {
                    if (set.Count == info.Length && set.Count == idAndCountList.Count)
                    {
                        active = FindActiveIndex(items, info);
                        break;
                    }
                    else
                    {
                        hasDifferentSet = true;
                        active          = FindActiveIndex(items, info);
                        if (active != -1)
                        {
                            break;
                        }
                    }
                }
            }

            if (hasDifferentSet)
            {
                Logger.LogWarning("Tab group with different tab id set.", line: token.SourceInfo.LineNumber.ToString(), code: WarningCodes.Markdown.DifferentTabIdSet);
            }

            if (active == -1)
            {
                if (firstVisibleTab != -1)
                {
                    active = firstVisibleTab;
                    tabSelectionInfo.Add((from pair in idAndCountList select pair.Item1).ToArray());
                }
                else
                {
                    active = 0;
                    Logger.LogWarning("All tabs are hidden in the tab group.", file: token.SourceInfo.File, line: token.SourceInfo.LineNumber.ToString(), code: WarningCodes.Markdown.NoVisibleTab);
                }
            }

            return(active);
        }
Exemplo n.º 6
0
        public virtual StringBuffer Render(IMarkdownRenderer renderer, DfmTabGroupBlockToken token, MarkdownBlockContext context)
        {
            StringBuffer sb      = @"<div class=""tabGroup"" id=""tabgroup_";
            var          groupId = StringHelper.Escape(token.Id);

            sb += groupId;
            sb += "\"";
            sb  = AppendSourceInfo(sb, renderer, token);
            sb += ">\n";

            sb = RenderTabHeaders(renderer, token, sb, groupId);

            sb = RenderSections(renderer, token, sb, groupId);

            sb += "</div>\n";
            return(sb);
        }
Exemplo n.º 7
0
        private static DfmTabGroupBlockToken RewriteActiveAndVisible(DfmTabGroupBlockToken token, HashSet <string> selectedTabIds)
        {
            var items           = token.Items;
            int firstVisibleTab = -1;
            int active          = -1;

            for (int i = 0; i < items.Length; i++)
            {
                var tab     = items[i];
                var visible = string.IsNullOrEmpty(tab.Condition) || selectedTabIds.Contains(tab.Condition);
                if (visible && firstVisibleTab == -1)
                {
                    firstVisibleTab = i;
                }
                if (active == -1 && visible && selectedTabIds.Contains(tab.Id))
                {
                    active = i;
                }
                if (tab.Visible != visible)
                {
                    items = items.SetItem(i, new DfmTabItemBlockToken(tab.Rule, tab.Context, tab.Id, tab.Condition, tab.Title, tab.Content, visible, tab.SourceInfo));
                }
            }
            if (active == -1)
            {
                if (firstVisibleTab != -1)
                {
                    active = firstVisibleTab;
                    selectedTabIds.Add(items[firstVisibleTab].Id);
                }
                else
                {
                    active = 0;
                    Logger.LogWarning("All tabs are hidden in the tab group.", file: token.SourceInfo.File, line: token.SourceInfo.LineNumber.ToString(), code: WarningCodes.Markdown.NoVisibleTab);
                }
            }
            return(new DfmTabGroupBlockToken(token.Rule, token.Context, token.Id, items, active, token.SourceInfo));
        }
Exemplo n.º 8
0
        private static DfmTabGroupBlockToken RewriteGroupId(DfmTabGroupBlockToken token, Dictionary <string, int> dict)
        {
            var groupId = token.Id;

            while (true)
            {
                if (!dict.TryGetValue(groupId, out int index))
                {
                    dict.Add(groupId, 1);
                    break;
                }
                else
                {
                    dict[groupId]++;
                    groupId = groupId + "-" + index.ToString();
                }
            }
            if (token.Id == groupId)
            {
                return(token);
            }
            return(new DfmTabGroupBlockToken(token.Rule, token.Context, groupId, token.Items, token.ActiveTabIndex, token.SourceInfo));
        }