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); } } }
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); }
public void Process() { 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) { string SnippetProvider(string 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} in {file}"); return(FormatSnippet(selectedSnippet.Text)); } var originalText = File.ReadAllText(file); string text; switch (Path.GetExtension(file)) { case ".md": text = MarkdownProcessor.Process(originalText, SnippetProvider); break; case ".cs": text = CSharpProcessor.Process(originalText, SnippetProvider); break; default: throw new NotSupportedException(file); } if (text != originalText) { _utf8EncodingWithoutBOM = new UTF8Encoding(false); File.WriteAllText(file, text, _utf8EncodingWithoutBOM); } } }