Пример #1
0
        private static string CompileSingleFile(string inputFileName, string outputFileName, string templateFileName, string templatePlaceholder, CompilerConfiguration config)
        {
            // Read source
            var source = TryDo(
                $"Reading source from {inputFileName}...",
                () => File.ReadAllText(inputFileName)
                );

            // Read template
            var templateHtml = Properties.Resources.DefaultTemplate;

            if (!string.IsNullOrWhiteSpace(templateFileName))
            {
                TryDo(
                    $"Reading template from {templateFileName}...",
                    () => File.ReadAllText(templateFileName)
                    );
            }

            // Compile
            var parser = new TmdParser(config.ParserOptions, config.RenderOptions);
            var html   = TryDo(
                "Compiling...",
                () => parser.Render(source)
                );

            // Use template
            html = templateHtml.Replace(templatePlaceholder, html);

            // Save to output file
            if (string.IsNullOrWhiteSpace(outputFileName))
            {
                outputFileName = Path.Combine(Path.GetDirectoryName(inputFileName), Path.GetFileNameWithoutExtension(inputFileName) + ".html");
            }
            TryDo(
                $"Saving to {outputFileName}...",
                () => File.WriteAllText(outputFileName, html)
                );
            return(outputFileName);
        }
Пример #2
0
        public static void Compile(
            [Required(Description = "Input file or folder name")] string inputPath,
            [Optional(null, "o", Description = "Output file name")] string outputFileName,
            [Optional(false, "os", Description = "Open output file in default browser")] bool startOutputFile,
            [Optional(null, "c", Description = "Path to configuration file")] string configFileName,
            [Optional(null, "t", Description = "Document template file name")] string templateFileName,
            [Optional("<!--body-->", "tp", Description = "Template placeholder for body")] string templatePlaceholder,
            [Optional(false, Description = "Show detailed exception messages")] bool debug
            )
        {
            debugMode = debug;

            // Read or write configuration
            var renderOptions = new TmdRenderOptions {
                InformationTemplate = "<tr class=\"information\">\r\n\t<th>&#9432;</th>\r\n\t<td>{0}</td>\r\n</tr>",
                WarningTemplate     = "<tr class=\"warning\">\r\n\t<th>&#9888;</th>\r\n\t<td>{0}</td>\r\n</tr>",
                DownloadTemplate    = "<tr class=\"download\">\r\n\t<th>&#128426;</th>\r\n\t<td>{0}</td>\r\n</tr>"
            };
            var parserOptions = new TmdParserOptions();
            var config        = new CompilerConfiguration {
                RenderOptions = renderOptions, ParserOptions = parserOptions
            };

            if (!string.IsNullOrWhiteSpace(configFileName))
            {
                if (File.Exists(configFileName))
                {
                    TryDo(
                        $"Reading configuration from {configFileName}...",
                        () => {
                        var json = File.ReadAllText(configFileName);
                        config   = JsonSerializer.Deserialize <CompilerConfiguration>(json);
                    }
                        );
                }
                else
                {
                    TryDo(
                        $"Writing default configuration to {configFileName}...",
                        () => {
                        var json = JsonSerializer.Serialize(config, new JsonSerializerOptions {
                            WriteIndented = true
                        });
                        File.WriteAllText(configFileName, json);
                    }
                        );
                }
            }

            if (File.Exists(inputPath))
            {
                // Compile single file
                outputFileName = CompileSingleFile(inputPath, outputFileName, templateFileName, templatePlaceholder, config);
                // Start output file
                if (startOutputFile)
                {
                    TryDo(
                        "Opening output file...",
                        () => Process.Start(new ProcessStartInfo {
                        FileName = outputFileName, UseShellExecute = true
                    })
                        );
                }
            }
            else if (Directory.Exists(inputPath))
            {
                // Compile directory
                if (!string.IsNullOrEmpty(outputFileName))
                {
                    Console.WriteLine("WARNING: -o option ignored for folders");
                }
                if (startOutputFile)
                {
                    Console.WriteLine("WARNING: -os option ignored for folders");
                }
                CompileFolder(inputPath, templateFileName, templatePlaceholder, config);
            }
            else
            {
                // Neither file or folder not found
                CrashExit("Specified input path was not found.");
            }
            Environment.Exit(ERRORLEVEL_SUCCESS);
        }
Пример #3
0
        private static void CompileFolder(string folderName, string templateFileName, string templatePlaceholder, CompilerConfiguration config)
        {
            var di = new DirectoryInfo(folderName);

            // Process all folders
            foreach (var item in di.GetDirectories())
            {
                CompileFolder(item.FullName, templateFileName, templatePlaceholder, config);
            }

            // Process all files
            foreach (var item in di.GetFiles("*.md"))
            {
                CompileSingleFile(item.FullName, null, templateFileName, templatePlaceholder, config);
            }
        }