Example #1
0
        public Document Parse()
        {
            var groups     = new Stack <Group>();
            var infoGroup  = default(Group);
            var listStyles = new Dictionary <int, ListStyle>();

            foreach (var token in Tokens())
            {
                if (token is Group group)
                {
                    if (groups.Count < 1)
                    {
                        groups.Push(_document);
                    }
                    else
                    {
                        groups.Peek().Contents.Add(group);
                        groups.Push(group);
                    }
                }
                else if (token is GroupEnd)
                {
                    var lastGroup = groups.Count > 0 ? groups.Pop() : default(Group);
                    var dest      = lastGroup?.Destination;
                    if (dest is ListDefinition)
                    {
                        var style = new ListStyle(lastGroup);
                        listStyles[style.Id] = style;
                    }
                    else if (dest is ListOverride)
                    {
                        var listId = lastGroup.Contents.OfType <ListId>().FirstOrDefault()?.Value;
                        var refId  = lastGroup.Contents.OfType <ListStyleId>().FirstOrDefault()?.Value;
                        if (listId.HasValue && refId.HasValue && listStyles.TryGetValue(listId.Value, out var style))
                        {
                            _document.ListStyles[refId.Value] = new ListStyleReference(refId.Value, style);
                        }
                    }
                }
                else
                {
                    groups.Peek().Contents.Add(token);
                    if (token is Info)
                    {
                        infoGroup = groups.Peek();
                    }
                }
            }

            if (infoGroup != null)
            {
                ParseInfo(_document, infoGroup);
            }

            return(_document);
        }
Example #2
0
 internal ListStyleReference(int id, ListStyle style)
 {
     Id    = id;
     Style = style;
 }