/// <summary> /// Main method. First argument should be a filename, otherwise it will prompt the user. /// A path ending with ".md" will be considered a single-file output. Otherwise, the output /// will be a directory with a .md file for each slide. In both cases, images will be output /// as separate files alongside the slide(s). If no argument was provided, the output will /// be printed, and no image files will be written. /// </summary> /// <param name="args">The arguments.</param> public static void Main(string[] args) { string file = GetFilename(args); string output = GetOutput(args); ISlideFormatter format = GetFormat(args); using (ISlideParser parser = GetParser(file)) { var slides = parser.Parse(); GenerateOutput(output, slides, format); } }
/// <summary> /// Generates the output with the specified formatter. /// </summary> /// <param name="output">The output path. May be null to represent STDOUT.</param> /// <param name="parsedSlides">The parsed slides.</param> /// <param name="formatter">The output specifier.</param> private static void GenerateOutput(string output, List <Model.Slide> parsedSlides, ISlideFormatter formatter) { // Scrub empty strings parsedSlides.ForEach(slide => { slide.Titles.RemoveAll(s => String.IsNullOrWhiteSpace(s)); slide.SubTitles.RemoveAll(s => String.IsNullOrWhiteSpace(s)); slide.Bullets.RemoveAll(b => String.IsNullOrWhiteSpace(b.Text)); }); bool consoleOut = output == null; bool singleFileOutput = false; string outputDirectory = null; if (!consoleOut) { singleFileOutput = output.EndsWith(".MD", StringComparison.CurrentCultureIgnoreCase); outputDirectory = singleFileOutput ? output.Substring(0, output.LastIndexOf(Path.DirectorySeparatorChar)) : output; Directory.CreateDirectory(outputDirectory); var imgOutputDirectory = outputDirectory + Path.DirectorySeparatorChar + "img"; Directory.CreateDirectory(imgOutputDirectory); // Dump the image files foreach (var slide in parsedSlides) { for (int i = 0; i < slide.Images.Count; i++) { string fileName = imgOutputDirectory + Path.DirectorySeparatorChar + $"{slide.ID.ToString()}-img{i + 1}.jpg"; slide.Images[i].Data.Save(fileName); } } } var lastDir = outputDirectory != null ? "/" + outputDirectory.Substring(outputDirectory.LastIndexOf(Path.DirectorySeparatorChar) + 1) : String.Empty; var imgPath = $".{lastDir}/img"; // One long dump of markdown, put it all in a StringBuilder and vomit if (consoleOut || singleFileOutput) { var markdownString = new StringBuilder(); parsedSlides.ForEach(slide => markdownString.Append(formatter.Convert(slide, imgPath))); if (consoleOut) { Console.Write(markdownString.ToString()); // Give the user a chance to copy it all out before the shell potentially dies. Console.WriteLine("END OF SLIDESHOW - PRESS ANY KEY TO EXIT"); Console.ReadKey(); } else { File.WriteAllText(output, markdownString.ToString()); } } else { // Dump each slide individually for (int i = 0; i < parsedSlides.Count; i++) { File.WriteAllText(output + Path.DirectorySeparatorChar + $"slide{i + 1}.MD", formatter.Convert(parsedSlides[i], imgPath)); } } }