Ejemplo n.º 1
0
        public Item ParseItem(string source, ref ContainerBlock.Enumerator enumerator)
        {
            var currentItem = GetNewItem(enumerator.Current);

            if (currentItem != null)
            {
                enumerator.MoveNext();
                while (enumerator.Current != null)
                {
                    var block = enumerator.Current;

                    if (block is HtmlBlock)
                    {
                        if (IsClosingItem(block))
                        {
                            currentItem.Id = GetNewAnchorId(source, currentItem.Name);
                            if (currentItem.Id != null)
                            {
                                _AllItems[currentItem.Id] = currentItem;
                            }
                            return(currentItem);
                        }
                        else if (IsNewItem(block))
                        {
                            var subItem = ParseItem(source, ref enumerator);

                            var propertyName = subItem.GetType().Name;

                            if (currentItem.GetType().GetProperty(propertyName) != null)
                            {
                                PropertyInfo prop = currentItem.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
                                if (null != prop && prop.CanWrite)
                                {
                                    prop.SetValue(currentItem, subItem, null);
                                }
                            }
                            else //if (currentItem is Items)
                            {
                                var items = currentItem; // as Items;
                                items.AddChild(subItem);
                            }
                            enumerator.MoveNext();
                        }
                        else
                        {
                            currentItem.Markdown += enumerator.Current.ToMarkdownString();
                            enumerator.MoveNext();
                        }
                    }
                    else
                    {
                        ParseItemProperties(source, currentItem, block);
                        currentItem.Markdown += enumerator.Current.ToMarkdownString();
                        enumerator.MoveNext();
                    }
                }
            }

            currentItem.Id            = GetNewAnchorId(source, currentItem.Name);
            _AllItems[currentItem.Id] = currentItem;
            return(currentItem);
        }
Ejemplo n.º 2
0
        public Item ParseItem(string source, ref ContainerBlock.Enumerator enumerator, Dictionary <string, Item> allItems)
        {
            var currentItem = GetNewItem(enumerator.Current);

            Debug.Assert(currentItem != null, $"Item invalide : {source} {enumerator.Current.ToMarkdownString()}");
            var currentProps = new Dictionary <string, PropertyInfo>();

            currentItem.Markdown     = string.Empty;
            currentProps["Markdown"] = currentItem.GetType().GetProperty("Markdown", BindingFlags.Public | BindingFlags.Instance);

            if (currentItem != null)
            {
                enumerator.MoveNext();
                while (enumerator.Current != null)
                {
                    var block = enumerator.Current;

                    if (block is HtmlBlock)
                    {
                        var htmlBlock = block as HtmlBlock;
                        if (htmlBlock.Type == HtmlBlockType.Comment)
                        {
                            var tag           = htmlBlock.Lines.Lines.FirstOrDefault().Slice.ToString();
                            var parsedComment = new ParsedComment(tag, withSigns: true);

                            if (parsedComment.Type == ParsedCommentType.Item)
                            {
                                if (parsedComment.IsClosing)
                                {
                                    currentItem.Id = GetNewAnchorId(source, currentItem.Name, allItems);
                                    if (currentItem.Id != null && allItems != null)
                                    {
                                        allItems[currentItem.Id] = currentItem;
                                    }
                                    return(currentItem);
                                }
                                else
                                {
                                    var subItem = ParseItem(source, ref enumerator, allItems);
                                    subItem.ParentLink = GetNewAnchorId(source, currentItem.Name, allItems);
                                    subItem.ParentName = currentItem.Name;
                                    subItem.Markdown   = $"> {subItem.ParentNameLink}\n\n---\n\n{subItem.Markdown}";

                                    var propertyName = subItem.GetType().Name;

                                    if (currentItem.GetType().GetProperty(propertyName) != null)
                                    {
                                        PropertyInfo prop = currentItem.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
                                        if (null != prop && prop.CanWrite)
                                        {
                                            prop.SetValue(currentItem, subItem, null);
                                        }
                                    }
                                    else
                                    {
                                        if (allItems != null && currentItem.GetNewFilterViewModel() == null)
                                        {
                                            var name  = subItem.Name;
                                            var level = Math.Max(1, Math.Min(6, subItem.NameLevel));
                                            var link  = (subItem is LinkItem ? (subItem as LinkItem).Link : subItem.Id);
                                            currentItem.Markdown += $"\n\n{new String('#', level)} [{name}]({link})";
                                            if (!string.IsNullOrEmpty(subItem.AltNameText))
                                            {
                                                var altname  = subItem.AltNameText;
                                                var altlevel = Math.Max(1, Math.Min(6, subItem.NameLevel + 3));
                                                currentItem.Markdown += $"\n\n{new String('#', altlevel)} _[{altname}]({link})_";
                                            }
                                            currentItem.Markdown += "\n\n";
                                            currentItem.AddChild(subItem);
                                        }
                                        else
                                        {
                                            currentItem.AddChild(subItem);
                                        }
                                    }
                                    enumerator.MoveNext();
                                }
                            }
                            else if (parsedComment.Type == ParsedCommentType.Property)
                            {
                                if (!parsedComment.IsClosing)
                                {
                                    PropertyInfo prop = currentItem.GetType().GetProperty(parsedComment.Name, BindingFlags.Public | BindingFlags.Instance);
                                    if (null != prop && prop.CanWrite)
                                    {
                                        prop.SetValue(currentItem, string.Empty, null);
                                        currentProps[parsedComment.Name] = prop;
                                    }
                                    enumerator.MoveNext();
                                }
                                else
                                {
                                    currentProps.Remove(parsedComment.Name);
                                    enumerator.MoveNext();
                                }
                            }
                            else // comment html différent de item et property
                            {
                                AddBlockContent(currentItem, currentProps, enumerator.Current);
                                enumerator.MoveNext();
                            }
                        }
                        else // autre chose qu'un comment html
                        {
                            AddBlockContent(currentItem, currentProps, enumerator.Current);
                            enumerator.MoveNext();
                        }
                    }
                    else // autre chose qu'un block html
                    {
                        ParseItemProperties(source, currentItem, block);
                        AddBlockContent(currentItem, currentProps, enumerator.Current);
                        enumerator.MoveNext();
                    }
                }
                currentItem.Id = GetNewAnchorId(source, currentItem.Name, allItems);
                if (allItems != null)
                {
                    allItems[currentItem.Id] = currentItem;
                }
            }

            return(currentItem);
        }