<commit_before>﻿using System.IO;
using System.Xml.Linq;

namespace specgen
{
    internal class MarkdownSpecificationWriter
    {
        private static void TitleSection(XDocument spec, TextWriter writer)
        {
            var head = spec.Element("specification")?.Element("head");

            writer.WriteLine($"# {head?.Element("title")?.Value}");
            writer.WriteLine();
            writer.WriteLine(head?.Element("version")?.Value);
            writer.WriteLine();
            writer.WriteLine(head?.Element("draft")?.Value ?? head?.Element("date")?.Value);
            writer.WriteLine();
            writer.WriteLine("<br/>");
        }

        public static void WriteSpecification(XDocument spec, string path)
        {
            using (var stream = new FileStream(path, FileMode.Create))
            {
                using (var writer = new StreamWriter(stream))
                {
                    TitleSection(spec, writer);
                }
            }
        }
    }
}<commit_msg>Markdown Table of Contents<commit_after>﻿using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace specgen
{
    internal class MarkdownSpecificationWriter
    {
        private static void TitleSection(XDocument spec, TextWriter writer)
        {
            var head = spec.Element("specification")?.Element("head");

            writer.WriteLine($"# {head?.Element("title")?.Value}");
            writer.WriteLine();
            writer.WriteLine(head?.Element("version")?.Value);
            writer.WriteLine();
            writer.WriteLine(head?.Element("draft")?.Value ?? head?.Element("date")?.Value);
            writer.WriteLine();
            writer.WriteLine(head?.Element("author")?.Value);
            writer.WriteLine();
            writer.WriteLine("<br/>");
            writer.WriteLine();
        }

        private static void TableOfContentsLevel(XElement level, TextWriter writer, int indent, string parentLevel)
        {
            var sections = level.Elements("section");

            var index = 1;
            foreach (var section in sections)
            {
                writer.WriteLine($"{new string(' ', indent * 2)}* [{parentLevel}{index} {section.Attribute("title").Value}](#{parentLevel}{index})");
                TableOfContentsLevel(section, writer, indent + 1, $"{parentLevel}{index}.");
                index++;
            }
        }

        private static void TableOfContents(XDocument spec, TextWriter writer)
        {
            writer.WriteLine("## Table of Contents");
            writer.WriteLine();
            TableOfContentsLevel(spec.Elements("specification").Elements("body").Single(), writer, 0, "");
        }

        public static void WriteSpecification(XDocument spec, string path)
        {
            using (var stream = new FileStream(path, FileMode.Create))
            {
                using (var writer = new StreamWriter(stream))
                {
                    TitleSection(spec, writer);
                    TableOfContents(spec, writer);
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.12818856958346786, 'fraction_numerical': 0.0012915724895059735, 'mean_word_length': 1.9932367149758454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1875825', 'n_tokens_mistral': 758, 'n_tokens_neox': 735, 'n_words': 155}