private void RemoveDocDirectoryAttribute(Document document)
        {
            var directoryAttribute = document.Attributes.FirstOrDefault(a => a.Name == "docdir");

            if (directoryAttribute != null)
            {
                document.Attributes.Remove(directoryAttribute);
            }
        }
        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 Document Convert(Document document)
 {
     _document = document;
     document.Accept(this);
     return(_newDocument);
 }