private Topic BuildTopics()
        {
            using (md5 = HashAlgorithm.Create("MD5"))
            {

                RootTopic = Topic.Create(TopicType.FeatureSet, CreateTopicId("Documentação Funcional"), "Documentação Funcional", gherkinFeaturesPath, gherkinFeaturesLanguage, builder.ProjectFolder);
                BuildTopicsTree(gherkinFeaturesPath, RootTopic);
            }
            RootTopic.Load();
            return RootTopic;
        }
 private static void LoadChildTopic(Topic topic)
 {
     try
     {
         topic.Load();
     }
     catch (SpecFlowParserException ex)
     {
         Exception newEx = new Exception(topic.SourcePath + ": " + ex.Message, ex);
         throw newEx;
     }
 }
        private void BuildTopicsTree(string parentPath, Topic parentTopic)
        {
            foreach (string featureSet in Directory.EnumerateDirectories(parentPath))
            {
                builder.ReportProgress("Feature set: " + featureSet);
                Topic currentTopic = Topic.Create(TopicType.FeatureSet, CreateTopicId(featureSet),Path.GetFileName(featureSet), featureSet, gherkinFeaturesLanguage, builder.ProjectFolder);
                parentTopic.Children.Add(currentTopic);
                BuildTopicsTree(featureSet, currentTopic);
            }

            foreach (string featureFile in Directory.EnumerateFiles(parentPath, "*.feature"))
            {
                if (Path.GetFileNameWithoutExtension(featureFile).ToLower() == "index") continue;
                builder.ReportProgress("Feature: " + featureFile);
                Topic currentTopic = Topic.Create(TopicType.Feature, CreateTopicId(featureFile), Path.GetFileNameWithoutExtension(featureFile), featureFile, gherkinFeaturesLanguage, builder.ProjectFolder);
                parentTopic.Children.Add(currentTopic);
            }
        }
        private void GenerateContentFile(Topic rootTopic)
        {
            builder.ReportProgress("Writing content file to: " + ContentFile + "..."); 
            
            var doc = new XmlDocument();
            var rootNode = doc.CreateElement("Topics");
            doc.AppendChild(rootNode);

            var topicElement = doc.CreateElement("Topic");
            topicElement.SetAttribute("id", rootTopic.Id);
            topicElement.SetAttribute("visible", XmlConvert.ToString(true));
            topicElement.SetAttribute("title", rootTopic.Title);
            rootNode.AppendChild(topicElement);

            GenerateContentFileElements(topicElement, rootTopic.Children);

            var directory = Path.GetDirectoryName(ContentFile);
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);
            doc.Save(ContentFile);
        }
 private static string GetAbsoluteFileName(string topicsFolder, Topic topic)
 {
     return Path.Combine(topicsFolder, Path.ChangeExtension(topic.Id, ".aml"));
 }
        private void AddTopicFile(Topic topic)
        {
            topic.FileName = GetAbsoluteFileName(TopicsFolder, topic);
            Topics.Add(topic);

            builder.ReportProgress("Adding topic file titled '{0}' to '{1}'...", topic.Title, topic.FileName);
        }
 private void WriteTopicFile(Topic topic, string topicContents)
 {
     builder.ReportProgress("Writing topic contents to {0}...", topic.FileName);
     File.WriteAllText(topic.FileName, topicContents);
 }
        private static void DeterminePreviousAndNextTopic(IList<Topic> topics, Topic parent, Topic parentsNextSibling, int topicsSiblingPosition, Topic topic, out Topic nextTopic, out Topic previousTopic)
        {
            var topicIsFirstSibling = topicsSiblingPosition == 0;
            var topicIsLastSibling = topicsSiblingPosition == topics.Count - 1;
            if (topicIsFirstSibling)
            {
                previousTopic = parent;
                if (topic.Children.Count > 0)
                {
                    nextTopic = topic.Children[0];
                }
                else if (topicIsLastSibling)
                {
                    nextTopic = parentsNextSibling;
                }
                else
                {
                    nextTopic = topics[topicsSiblingPosition + 1];
                }
            }
            else if (topicIsLastSibling)
            {
                if (topic.Children.Count > 0)
                {
                    nextTopic = topic.Children[0];
                }
                else
                {
                    nextTopic = parentsNextSibling;
                }
                previousTopic = topics[topicsSiblingPosition - 1];
            }
            else
            {
                if (topic.Children.Count > 0)
                {
                    nextTopic = topic.Children[0];
                }
                else
                {
                    nextTopic = topics[topicsSiblingPosition + 1];
                }

                var previousSibling = topics[topicsSiblingPosition - 1];
                if (previousSibling.Children.Count > 0)
                {
                    previousTopic = previousSibling.Children[previousSibling.Children.Count - 1];
                }
                else
                {
                    previousTopic = topics[topicsSiblingPosition - 1];
                }
            }
        }
        private void GenerateTopicFiles(IList<Topic> topics, Topic parent, Topic parentsNextSibling)
        {

            for (int i = 0; i < topics.Count; i++)
            {
                Topic topic = topics[i];
                topic.Children.Sort((x, y) => { return String.Compare(x.Title, y.Title); });
                Topic nextTopic;
                Topic previousTopic;
                DeterminePreviousAndNextTopic(topics, parent, parentsNextSibling, i, topic, out nextTopic, out previousTopic);

                AddTopicFile(topic);
                FeatureContentCreator creator = new FeatureContentCreator();
                string topicContents = creator.Visit((dynamic)topic, nextTopic, previousTopic);
                WriteTopicFile(topic, topicContents);

                var topicsNextSibling = i + 1 < topics.Count ? parent.Children[i + 1] : null;
                GenerateTopicFiles(topic.Children, topic, topicsNextSibling);
                
            }
        }