示例#1
0
        static void Main(string[] args)
        {
            if (!CommandLineSwitch.TryParse <CommandLineOptions>(ref args, out var cmdLineOptions, out var parserError))
            {
                Console.WriteLine(parserError.ToString());
                return;
            }
            if (cmdLineOptions.Version)
            {
                Console.WriteLine("ver." + typeof(Program).Assembly.GetName().Version.ToString(3));
                return;
            }

            var converterOptions = new MarkdownToHtmlConverterOptions
            {
                BaseDirectory = !string.IsNullOrEmpty(cmdLineOptions.InputPath) ? Path.GetDirectoryName(cmdLineOptions.InputPath) : null,
                EmbedImage    = cmdLineOptions.EmbedImage
            };

            var input  = Console.In;
            var output = Console.Out;

            if (!string.IsNullOrEmpty(cmdLineOptions.InputPath))
            {
                input = new StreamReader(cmdLineOptions.InputPath, encoding: Encoding.UTF8);
            }
            if (!string.IsNullOrEmpty(cmdLineOptions.OutputPath))
            {
                output = new StreamWriter(cmdLineOptions.OutputPath, append: false, encoding: Encoding.UTF8);
            }

            MarkdownToHtmlConverter.Convert(input, output, converterOptions);
        }
示例#2
0
        public static void Convert(TextReader input, TextWriter output, MarkdownToHtmlConverterOptions options)
        {
            var markdownText = input.ReadToEnd();

            var pipelineBulder = new MarkdownPipelineBuilder()
                                 .UseAdvancedExtensions();

            if (options.EmbedImage)
            {
                pipelineBulder.Use(new EmbeddedImageExtension(options.BaseDirectory));
            }

            var pipeline     = pipelineBulder.Build();
            var htmlBodyText = Markdown.ToHtml(markdownText, pipeline);

            var styleText = GetDefaultStyleText();

            output.WriteLine("<!DOCTYPE html>");
            output.WriteLine("<html>");
            output.WriteLine("<head>");
            output.WriteLine("<style>");
            output.WriteLine(styleText);
            output.WriteLine("</style>");
            output.WriteLine("</head>");
            output.WriteLine("<body class=\"markdown-body\">");

            output.WriteLine(htmlBodyText);

            output.WriteLine("</body>");
            output.WriteLine("</html>");
            output.Flush();
        }