예제 #1
0
파일: ILoader.cs 프로젝트: dwarry/Mindmap
        internal IMindMapRoot ParsePageData(XDocument doc)
        {
            var styles = doc.Root.Elements(OneNoteElements.QuickStyleDef)
                         .ToDictionary(x => x.Attribute(OneNoteAttributes.Index)?.Value,
                                       x => x.Attribute(OneNoteAttributes.Name)?.Value);

            var headingStyles = styles.Where(x => x.Value.StartsWith("h"))
                                .ToDictionary(x => x.Key, x => int.Parse(x.Value.Substring(1)));

            bool isHeading(XElement x)
            {
                var styleIndex = x.Attribute(OneNoteAttributes.QuickStyleIndex)?.Value ?? "";

                return(!headingStyles.ContainsKey(styleIndex));
            }

            var titleElement = doc.Root.Descendants(OneNoteElements.Title).FirstOrDefault();

            var title = GetTextContents(titleElement.Descendants(OneNoteElements.T).FirstOrDefault());

            var outline = doc.Root.Elements(OneNoteElements.Outline).FirstOrDefault();

            if (outline == null)
            {
                return(new MindMapRoot(title, Enumerable.Empty <XElement>()));
            }

            var children = outline.Elements(OneNoteElements.OeChildren).First().Elements().ToList();

            var rootContents = children.TakeWhile(isHeading);

            var root = new MindMapRoot(title, rootContents);

            var nodeData = ParseOutlineChildren(children.SkipWhile(x => !isHeading(x)), headingStyles);

            IMindMapData parent = root;

            var ancestors = new Stack <IMindMapData>();

            ancestors.Push(root);

            foreach (var(objectId, depth, nodeTitle, contents) in nodeData)
            {
                while (ancestors.Peek().Depth >= depth)
                {
                    parent = ancestors.Pop();
                }

                parent = ancestors.Peek();

                var element = new MindMapElement(nodeTitle, parent, new ReactiveList <XElement>(contents), depth, objectId);

                ancestors.Push(element);
            }

            return(root);
        }
예제 #2
0
        public MindMapElement(string title, IMindMapData parent, ReactiveList <XElement> content, int depth, string objectId)
        {
            Parent = parent ?? throw new ArgumentNullException(nameof(parent));

            Content = content ?? throw new ArgumentNullException(nameof(content));

            _title = title ?? throw new ArgumentNullException(nameof(title));

            if (depth < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(depth), "must be positive");
            }

            _depth = depth;

            _objectId = objectId;

            _index = parent.Children.Count;

            parent.Children.Add(this);
        }