/// <summary> /// parse command args return a object /// </summary> /// <param name="args">command line args</param> /// <returns>RunArgs</returns> private static RunArgs ParseArgs(string[] args) { RunArgs runArgs = new RunArgs(); bool showHelp = false; OptionSet p = new OptionSet { { "i|input=", "{INPUT} markdown file path.", v => runArgs.MarkdonwPath = v }, { "o|output=", "{OUTPUT} docx file path.", v => runArgs.DocxPath = v }, { "r|rules=", "{RULES} for pure text block", v => runArgs.RulesPath = v }, { "c|config=", "{CONFIG} file for coverting", v => runArgs.ConfigPath = v }, { "h|help", "show this message and exit", v => showHelp = v != null }, }; try { p.Parse(args); } catch (OptionException e) { Console.Write("md2docx: "); Console.WriteLine(e.Message); Console.WriteLine("Try`md2docx --help' for more information."); Environment.Exit(1); } if (showHelp) { Usage(p); Environment.Exit(0); } if (runArgs.GetType().GetProperties() .Any(p => string.IsNullOrWhiteSpace((p.GetValue(runArgs) as string)))) { Usage(p); Environment.Exit(1); } return(runArgs); }
static void Main(string[] args) { RunArgs runArgs = ParseArgs(args); // var markdown = Markdown.Parse(runArgs.MarkdonwPath); RuleJsonSerializer ruleJsonSerializer = new RuleJsonSerializer(); var rules = ruleJsonSerializer.ParseJson(System.IO.File.ReadAllText(runArgs.ConfigPath)); using WordprocessingDocument document = WordprocessingDocument.Create(runArgs.DocxPath, WordprocessingDocumentType.Document); MainDocumentPart mainPart = document.AddMainDocumentPart(); GenerateMainPart(mainPart, runArgs.MarkdonwPath); StyleDefinitionsPart styleDefinitionsPart = mainPart.AddNewPart <StyleDefinitionsPart>("Styles"); // TODO: latent config if needed GenerateStyleDefinitionsPartContent(styleDefinitionsPart, rules, true); FontTablePart fontTablePart1 = mainPart.AddNewPart <FontTablePart>("FontTable"); GeneratedCode.GenerateFontTablePartContent(fontTablePart1); SetPackageProperties(document); }