예제 #1
0
        public bool Process()
        {
            var consoleColor = Console.ForegroundColor;

            var packager = new Westwind.HtmlPackager.HtmlPackager();

            if (Parser.Verbose)
            {
                packager.WriteMessage += WriteConsole;
            }

            if (string.IsNullOrEmpty(Parser.TargetFile))
            {
                string output = packager.PackageHtml(Parser.SourceUrl);
                if (output == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error: Failed to create HTML output. ");
                    Console.WriteLine(packager.ErrorMessage);
                    Console.ForegroundColor = consoleColor;
                    return(false);
                }
                Console.WriteLine(output);
                return(true);
            }

            var version = Assembly.GetEntryAssembly().GetName().Version;
            var ver     = version.Major + "." + version.Minor + (version.Build > 0 ? "." + version.Build : string.Empty);

            ConsoleHelper.WriteWrappedHeader($"West Wind HTML Packager v{ver}");

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"Processing {Parser.SourceUrl}...");


            if (Parser.ZipDependencies)
            {
                if (!packager.PackageHtmlToZipFile(Parser.SourceUrl, Parser.TargetFile))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error: Failed to create Zip file");
                    Console.WriteLine(packager.ErrorMessage);
                    Console.ForegroundColor = consoleColor;
                    return(false);
                }

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine($"Done. Created {Parser.TargetFile} Zip file output.");
                Console.ForegroundColor = consoleColor;

                if (Parser.DisplayHtml)
                {
                    Utils.GoUrl(Parser.TargetFile);
                }
                return(true);
            }

            if (Parser.ExternalDependencies)
            {
                if (!packager.PackageHtmlToFolder(Parser.SourceUrl, Parser.TargetFile, null, false))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error: Failed to create HTML output.");
                    Console.WriteLine(packager.ErrorMessage);
                    Console.ForegroundColor = consoleColor;
                    return(false);
                }

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine($"Done. Created {Parser.TargetFile} plus dependencies.");
                Console.ForegroundColor = consoleColor;

                if (Parser.DisplayHtml)
                {
                    Utils.GoUrl(Parser.TargetFile);
                }

                return(true);
            }


            if (!packager.PackageHtmlToFile(Parser.SourceUrl, Parser.TargetFile, null, Parser.ExternalDependencies))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Error: Failed to create HTML output.");
                Console.ForegroundColor = consoleColor;
                return(false);
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Done. Created {Parser.TargetFile}.");
            Console.ForegroundColor = consoleColor;

            if (Parser.DisplayHtml)
            {
                Utils.GoUrl(Parser.TargetFile);
            }

            return(true);
        }
        /// <summary>
        ///
        /// mm markdowntohtml <inputfile> <outputFile> <rendermode> -open
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="outputFile"></param>
        /// <param name="openOutputFile"></param>
        /// <param name="fileMode">html,packagedhtml,zip</param>
        public void MarkdownToHtml()
        {
            Processor.ConsoleHeader();

            string inputFile  = Arguments.InputFile;
            string outputFile = Arguments.OutputFile;

            if (string.IsNullOrEmpty(inputFile) || !File.Exists(inputFile))
            {
                var fd = new OpenFileDialog
                {
                    DefaultExt = ".md",
                    Filter     = "Markdown files (*.md,*.markdown)|*.md;*.markdown|" +
                                 "All files (*.*)|*.*",
                    CheckFileExists  = true,
                    RestoreDirectory = true,
                    Title            = "Open Markdown File",
                    InitialDirectory = Environment.CurrentDirectory
                };
                var res = fd.ShowDialog();
                if (res == null)
                {
                    return;
                }
                inputFile = fd.FileName;
            }

            if (string.IsNullOrEmpty(outputFile))
            {
                var fd = new SaveFileDialog
                {
                    DefaultExt = ".html",
                    Filter     = "HTML files (*.html,*.htm)|*.html;*.htm|" +
                                 "All files (*.*)|*.*",
                    CheckFileExists  = false,
                    RestoreDirectory = true,
                    Title            = "Save as HTML File",
                    InitialDirectory = Path.GetDirectoryName(inputFile),
                    FileName         = Path.ChangeExtension(Path.GetFileName(inputFile), "html")
                };
                var res = fd.ShowDialog();
                if (res == null)
                {
                    return;
                }
                outputFile = fd.FileName;
            }

            string html;
            var    doc = new MarkdownDocument();

            if (!string.IsNullOrEmpty(Arguments.Theme))
            {
                mmApp.Configuration.PreviewTheme = Arguments.Theme;
            }

            try
            {
                if (!doc.Load(inputFile))
                {
                    throw new AccessViolationException();
                }
            }
            catch
            {
                ColorConsole.WriteError("Failed: Couldn't read input file.");
                Processor.ConsoleFooter();
                return;
            }

            try
            {
                string renderMode = Arguments.HtmlRenderMode?.ToLower();

                if (renderMode == "fragment" || renderMode == "raw")
                {
                    try
                    {
                        var parser = new MarkdownParserMarkdig();
                        html = parser.Parse(doc.CurrentText);

                        File.WriteAllText(outputFile, html, new UTF8Encoding(false));
                    }
                    catch
                    {
                        ColorConsole.WriteError("Failed: Couldn't convert Markdown document or generate output file.");
                        Processor.ConsoleFooter();
                        return;
                    }
                }
                else
                {
                    if (doc.RenderHtmlToFile(filename: outputFile) == null)
                    {
                        throw new AccessViolationException();
                    }
                }

                if (renderMode == "packagedhtml")
                {
                    var    packager   = new Westwind.HtmlPackager.HtmlPackager();
                    string outputHtml = packager.PackageHtml(outputFile);
                    try
                    {
                        File.WriteAllText(outputFile, outputHtml);
                    }
                    catch
                    {
                        ColorConsole.WriteError("Failed: Couldn't write output file.");
                        Processor.ConsoleFooter();
                        return;
                    }
                }
                else if (renderMode == "htmlfiles")
                {
                    var packager = new Westwind.HtmlPackager.HtmlPackager();
                    if (!packager.PackageHtmlToFolder(outputFile, outputFile))
                    {
                        ColorConsole.WriteLine("Failed: Create output folder.", ConsoleColor.Red);
                    }
                }
                else if (renderMode == "zip")
                {
                    var packager = new Westwind.HtmlPackager.HtmlPackager();
                    if (!packager.PackageHtmlToZipFile(Path.ChangeExtension(outputFile, "html"), Path.ChangeExtension(outputFile, "zip")))
                    {
                        ColorConsole.WriteError("Failed: Couldn't create Packaged HTML Zip files.");
                    }

                    try
                    {
                        File.Delete(Path.ChangeExtension(outputFile, "html"));
                    }catch {}
                }
            }

            catch
            {
                ColorConsole.WriteError("Failed: Couldn't write output file.");
                Processor.ConsoleFooter();
                return;
            }

            if (Arguments.OpenOutputFile)
            {
                ShellUtils.GoUrl($"{outputFile}");
            }

            ColorConsole.WriteSuccess($"Created file: {outputFile}");
            ColorConsole.WriteSuccess($" Output Mode: {Arguments.HtmlRenderMode}");

            Processor.ConsoleFooter();
        }