コード例 #1
0
ファイル: Parser.cs プロジェクト: pavelkucherov/RtfPipe
        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);
        }
コード例 #2
0
ファイル: ListStyleReference.cs プロジェクト: shekky/RtfPipe
 internal ListStyleReference(int id, ListStyle style)
 {
     Id    = id;
     Style = style;
 }