Ejemplo n.º 1
0
        private Tuple <List <FileModel>, TreeItemRestructure> SplitModelToOperationGroup(FileModel model)
        {
            if (model.Type != DocumentType.Article)
            {
                return(null);
            }

            var content = (RestApiRootItemViewModel)model.Content;

            if (content.Tags.Count == 0 || content.Children.Count == 0)
            {
                return(null);
            }

            var tagModels = GenerateTagModels(content).ToList();

            if (tagModels.Count == 0)
            {
                return(null);
            }

            var treeItems      = new List <TreeItem>();
            var splittedModels = new List <FileModel>();

            foreach (var tagModel in tagModels)
            {
                tagModel.Metadata["_isSplittedToTag"] = true;
                var newModel = GenerateNewFileModel(model, tagModel);
                splittedModels.Add(newModel);
                treeItems.Add(ConvertToTreeItem(tagModel, newModel.Key));
            }

            // Only keep not tagged children in root model
            var groupedUids = splittedModels.SelectMany(m => m.Uids).Select(u => u.Name).ToList();

            content.Tags     = new List <RestApiTagViewModel>();
            content.Children = content.Children.Where(child => !groupedUids.Contains(child.Uid)).ToList();
            content.Metadata["_isSplittedByTag"] = true;
            model.Content = content;

            // Reset uid definition
            model.Uids = model.Uids.Where(u => !groupedUids.Contains(u.Name)).ToImmutableArray();

            var treeItemRestruction = new TreeItemRestructure
            {
                ActionType        = TreeItemActionType.AppendChild,
                Key               = model.Key,
                TypeOfKey         = TreeItemKeyType.TopicHref,
                RestructuredItems = treeItems.OrderBy(s => s.Metadata[Constants.PropertyName.Name]).ToImmutableList(),
                SourceFiles       = new FileAndType[] { model.OriginalFileAndType }.ToImmutableList(),
            };

            return(Tuple.Create(splittedModels, treeItemRestruction));
        }
Ejemplo n.º 2
0
        private bool Matches(TocItemViewModel item, TreeItemRestructure restruction)
        {
            switch (restruction.TypeOfKey)
            {
            case TreeItemKeyType.TopicUid:
                return(item.TopicUid == restruction.Key);

            case TreeItemKeyType.TopicHref:
                return(FilePathComparer.OSPlatformSensitiveStringComparer.Compare(item.TopicHref, restruction.Key) == 0);

            default:
                throw new NotSupportedException($"{restruction.TypeOfKey} is not a supported ComparerKeyType");
            }
        }
Ejemplo n.º 3
0
        private bool Matches(TocItemViewModel item, TreeItemRestructure restruction)
        {
            switch (restruction.TypeOfKey)
            {
            case TreeItemKeyType.TopicUid:
                // make sure TocHref is null so that TopicUid is not the resolved homepage in `href: api/` case
                return(item.TocHref == null && item.TopicUid == restruction.Key);

            case TreeItemKeyType.TopicHref:
                return(item.TocHref == null && FilePathComparer.OSPlatformSensitiveStringComparer.Compare(item.TopicHref, restruction.Key) == 0);

            default:
                throw new NotSupportedException($"{restruction.TypeOfKey} is not a supported ComparerKeyType");
            }
        }
Ejemplo n.º 4
0
        private void RestructureItem(TocItemViewModel item, List <TocItemViewModel> items, TreeItemRestructure restruction)
        {
            var index = items.IndexOf(item);

            if (index < 0)
            {
                Logger.LogWarning($"Unable to find {restruction.Key}, it is probably removed or replaced by other restructions.");
                return;
            }

            switch (restruction.ActionType)
            {
            case TreeItemActionType.ReplaceSelf:
            {
                if (restruction.RestructuredItems == null || restruction.RestructuredItems.Count == 0)
                {
                    return;
                }
                if (restruction.RestructuredItems.Count > 1)
                {
                    throw new InvalidOperationException($"{restruction.ActionType} does not allow multiple root nodes.");
                }

                var roots = GetRoots(restruction.RestructuredItems);
                items[index] = roots[0];
                break;
            }

            case TreeItemActionType.DeleteSelf:
            {
                items.RemoveAt(index);
                break;
            }

            case TreeItemActionType.AppendChild:
            {
                if (restruction.RestructuredItems == null || restruction.RestructuredItems.Count == 0)
                {
                    return;
                }
                if (item.Items == null)
                {
                    item.Items = new TocViewModel();
                }

                var roots = GetRoots(restruction.RestructuredItems);
                item.Items.AddRange(roots);
                break;
            }

            case TreeItemActionType.PrependChild:
            {
                if (restruction.RestructuredItems == null || restruction.RestructuredItems.Count == 0)
                {
                    return;
                }
                if (item.Items == null)
                {
                    item.Items = new TocViewModel();
                }

                var roots = GetRoots(restruction.RestructuredItems);
                item.Items.InsertRange(0, roots);
                break;
            }

            case TreeItemActionType.InsertAfter:
            {
                if (restruction.RestructuredItems == null || restruction.RestructuredItems.Count == 0)
                {
                    return;
                }
                var roots = GetRoots(restruction.RestructuredItems);
                items.InsertRange(index + 1, roots);
                break;
            }

            case TreeItemActionType.InsertBefore:
            {
                if (restruction.RestructuredItems == null || restruction.RestructuredItems.Count == 0)
                {
                    return;
                }
                var roots = GetRoots(restruction.RestructuredItems);
                items.InsertRange(index, roots);
                break;
            }

            default:
                break;
            }
        }