public override void VisitDocument(Document document)
        {
            _newDocument = new Document
            {
                Title   = document.Title,
                DocType = document.DocType
            };

            foreach (var authorInfo in document.Authors)
            {
                _newDocument.Authors.Add(authorInfo);
            }

            RemoveDocDirectoryAttribute(_newDocument);
            RemoveDocDirectoryAttribute(document);

            foreach (var attributeEntry in document.Attributes)
            {
                _newDocument.Attributes.Add(attributeEntry);
            }

            if (document.Attributes.All(a => a.Name != "ref_current"))
            {
                _newDocument.Attributes.Add(new AttributeEntry("ref_current",
                                                               $"https://www.elastic.co/guide/en/elasticsearch/reference/{Program.DocVersion}"));
            }

            var github = "https://github.com/elastic/elasticsearch-net";

            if (document.Attributes.All(a => a.Name != "github"))
            {
                _newDocument.Attributes.Add(new AttributeEntry("github", github));
            }

            if (document.Attributes.All(a => a.Name != "nuget"))
            {
                _newDocument.Attributes.Add(new AttributeEntry("nuget", "https://www.nuget.org/packages"));
            }

            var originalFile = Regex.Replace(_source.FullName.Replace("\\", "/"), @"^(.*Tests/)",
                                             $"{github}/tree/{Program.BranchName}/src/Tests/");

            _newDocument.Insert(0, new Comment
            {
                Style = CommentStyle.MultiLine,
                Text  = $"IMPORTANT NOTE\r\n==============\r\nThis file has been generated from {originalFile}. \r\n" +
                        "If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,\r\n" +
                        "please modify the original csharp file found at the link and submit the PR with that change. Thanks!"
            });

            _topSectionTitleLevel = _source.Directory.Name.Equals("request", StringComparison.OrdinalIgnoreCase) &&
                                    _source.Directory.Parent != null &&
                                    _source.Directory.Parent.Name.Equals("search", StringComparison.OrdinalIgnoreCase)
                                ? 2
                                : 3;

            // see if the document has some kind of top level title and add one with an anchor if not.
            // Used to add titles to *Usage test files
            if (document.Title == null && document.Count > 0)
            {
                var sectionTitle = document[0] as SectionTitle;

                // capture existing top level
                if (sectionTitle != null && sectionTitle.Level <= 3)
                {
                    _topSectionTitleLevel = sectionTitle.Level;
                }

                if (sectionTitle == null || (sectionTitle.Level > 3))
                {
                    var id    = Path.GetFileNameWithoutExtension(_destination.Name);
                    var title = id.LowercaseHyphenToPascal();
                    sectionTitle = new SectionTitle(title, _topSectionTitleLevel);
                    sectionTitle.Attributes.Add(new Anchor(id));
                    _newDocument.Add(sectionTitle);
                }
            }

            base.VisitDocument(document);
        }
        public override void VisitContainer(Container elements)
        {
            if (_topLevel)
            {
                _topLevel = false;
                for (var index = 0; index < elements.Count; index++)
                {
                    var element = elements[index];
                    var source  = element as Source;

                    if (source != null)
                    {
                        // remove empty source blocks
                        if (string.IsNullOrWhiteSpace(source.Text))
                        {
                            continue;
                        }

                        var method = source.Attributes.OfType <NamedAttribute>().FirstOrDefault(a => a.Name == "method");
                        if (method == null)
                        {
                            _newDocument.Add(source);
                            continue;
                        }

                        // if there is a section title since the last source block, don't add one
                        var lastSourceBlock  = _newDocument.LastOrDefault(e => e is Source);
                        var lastSectionTitle = _newDocument.OfType <SectionTitle>().LastOrDefault(e => e.Level == _topSectionTitleLevel + 1);
                        if (lastSourceBlock != null && lastSectionTitle != null)
                        {
                            var lastSectionTitleIndex = _newDocument.IndexOf(lastSectionTitle);
                            var lastSourceBlockIndex  = _newDocument.IndexOf(lastSourceBlock);
                            if (lastSectionTitleIndex > lastSourceBlockIndex)
                            {
                                _newDocument.Add(source);
                                continue;
                            }
                        }

                        switch (method.Value)
                        {
                        case "fluent":
                        case "queryfluent":
                        case "fluentaggs":
                            if (!LastSectionTitleMatches(text => text.StartsWith("Fluent DSL", StringComparison.OrdinalIgnoreCase)))
                            {
                                _newDocument.Add(CreateSubsectionTitle("Fluent DSL example"));
                            }

                            _newDocument.Add(source);
                            break;

                        case "initializer":
                        case "queryinitializer":
                        case "initializeraggs":
                            _newDocument.Add(CreateSubsectionTitle("Object Initializer syntax example"));
                            _newDocument.Add(source);
                            break;

                        case "expectresponse":
                            // Don't add the Handlng Response section title if it was the last title (it might be defined in the doc already)
                            if (!LastSectionTitleMatches(text => text.Equals("Handling responses", StringComparison.OrdinalIgnoreCase)))
                            {
                                _newDocument.Add(CreateSubsectionTitle("Handling Responses"));
                            }
                            _newDocument.Add(source);
                            break;

                        default:
                            _newDocument.Add(source);
                            break;
                        }
                    }
                    else
                    {
                        _newDocument.Add(element);
                    }
                }
            }

            base.VisitContainer(elements);
        }