public void Process()
        {
            foreach (var mdFile in Directory.EnumerateFiles(_directory, "*.md", SearchOption.AllDirectories))
            {
                Console.WriteLine($"Processing {mdFile}");

                var  text    = File.ReadAllText(mdFile);
                bool changed = false;

                text = MarkdownProcessor.Process(text, s =>
                {
                    var selectedSnippets = _snippets.Value.Where(snip => snip.Name == s).ToArray();
                    if (selectedSnippets.Length > 1)
                    {
                        throw new InvalidOperationException($"Multiple snippets with the name '{s}' defined '{_directory}'");
                    }
                    if (selectedSnippets.Length == 0)
                    {
                        throw new InvalidOperationException($"Snippet '{s}' not found in directory '{_directory}'");
                    }

                    var selectedSnippet = selectedSnippets.Single();
                    Console.WriteLine($"Replaced {selectedSnippet.Name}");
                    changed = true;
                    return(FormatSnippet(selectedSnippet.Text));
                });

                if (changed)
                {
                    _utf8EncodingWithoutBOM = new UTF8Encoding(false);
                    File.WriteAllText(mdFile, text, _utf8EncodingWithoutBOM);
                }
            }
        }
Пример #2
0
        public async Task OnExecuteAsync()
        {
            Console.WriteLine($"Processing {Markdown}");

            var text     = File.ReadAllText(Markdown);
            var snippets = await GetSnippetsInDirectory(Snippets);

            Console.WriteLine($"Discovered snippets:");

            foreach (var snippet in snippets)
            {
                Console.WriteLine($" {snippet.Name}");
            }

            text = MarkdownProcessor.Process(text, s => {
                var selectedSnippets = snippets.Where(snip => snip.Name == s).ToArray();
                if (selectedSnippets.Length > 1)
                {
                    throw new InvalidOperationException($"Multiple snippets with the name '{s}' defined '{Snippets}'");
                }
                if (selectedSnippets.Length == 0)
                {
                    throw new InvalidOperationException($"Snippet '{s}' not found in directory '{Snippets}'");
                }

                var selectedSnippet = selectedSnippets.Single();
                Console.WriteLine($"Replaced {selectedSnippet.Name}");
                return(FormatSnippet(selectedSnippet.Text));
            });

            File.WriteAllText(Markdown, text);
        }
Пример #3
0
        public async Task ProcessAsync()
        {
            List <string> files = new List <string>();

            files.AddRange(Directory.EnumerateFiles(_directory, "*.md", SearchOption.AllDirectories));
            files.AddRange(Directory.EnumerateFiles(_directory, "*.cs", SearchOption.AllDirectories));

            foreach (var file in files)
            {
                async ValueTask <string> SnippetProvider(string s)
                {
                    var snippets         = await _snippets.Value;
                    var selectedSnippets = snippets.Where(snip => snip.Name == s).ToArray();

                    if (selectedSnippets.Length > 1)
                    {
                        throw new InvalidOperationException($"Multiple snippets with the name '{s}' defined '{_directory}'");
                    }

                    if (selectedSnippets.Length == 0)
                    {
                        throw new InvalidOperationException($"Snippet '{s}' not found in directory '{_directory}'");
                    }

                    var selectedSnippet = selectedSnippets.Single();

                    Console.WriteLine($"Replaced {selectedSnippet.Name} in {file}");
                    return(FormatSnippet(selectedSnippet.Text));
                }

                var originalText = await File.ReadAllTextAsync(file);

                string text;
                switch (Path.GetExtension(file))
                {
                case ".md":
                    text = await MarkdownProcessor.ProcessAsync(originalText, SnippetProvider);

                    break;

                case ".cs":
                    text = await CSharpProcessor.ProcessAsync(originalText, SnippetProvider);

                    break;

                default:
                    throw new NotSupportedException(file);
                }

                if (text != originalText)
                {
                    _utf8EncodingWithoutBOM = new UTF8Encoding(false);
                    await File.WriteAllTextAsync(file, text, _utf8EncodingWithoutBOM);
                }
            }
        }