private static int Process(string templatePath, string dataPath, string outputPath) { var ext = SupportedType.FindExtension(templatePath); if (ext == null) { Console.Error.WriteLine("Unsupported extension: " + templatePath); return(2); } if (dataPath != null && !File.Exists(dataPath)) { Console.Error.WriteLine("Unable to find specified data file: " + dataPath); return(2); } var json = dataPath == null ? Console.In : new StreamReader(new FileStream(dataPath, FileMode.Open, FileAccess.Read)); var newtonsoft = new Newtonsoft.Json.JsonSerializer(); newtonsoft.Converters.Add(new DictionaryConverter()); using (var fis = new FileStream(templatePath, FileMode.Open, FileAccess.Read)) using (var fos = outputPath == null ? Console.OpenStandardOutput() : new FileStream(outputPath, FileMode.Create, FileAccess.Write)) { var deser = newtonsoft.Deserialize <IDictionary <string, object> >(new JsonTextReader(json)); using (var td = Configuration.Factory.Open(fis, fos, ext)) td.Process(deser); } return(0); }
private static int Process(string templatePath, string dataPath, string outputPath) { var ext = SupportedType.FindExtension(templatePath); if (ext == null) { Console.Error.WriteLine("Unsupported extension: " + templatePath); return(2); } if (dataPath != null && !File.Exists(dataPath)) { Console.Error.WriteLine("Unable to find specified data file: " + dataPath); return(2); } var json = dataPath == null ? Console.In : new StreamReader(Open(dataPath)); var newtonsoft = new Newtonsoft.Json.JsonSerializer(); newtonsoft.Converters.Add(new DictionaryConverter()); using (var fis = Open(templatePath)) using (var fos = outputPath == null ? Console.OpenStandardOutput() : new FileStream(outputPath, FileMode.Create, FileAccess.Write)) { while (char.IsWhiteSpace((char)json.Peek())) { json.Read(); } //disable low level plugins so it works on more platforms without GDI library for System.Drawing var config = Configuration.Builder .Include(Base64Image) .Include(Xml) .BuiltInLowLevelPlugins(false); if (json.Peek() == '[') { var deser = newtonsoft.Deserialize <IDictionary <string, object>[]>(new JsonTextReader(json)); using (var td = config.Build().Open(fis, fos, ext)) td.Process(deser); } else { var deser = newtonsoft.Deserialize <IDictionary <string, object> >(new JsonTextReader(json)); using (var td = config.Build().Open(fis, fos, ext)) td.Process(deser); } } return(0); }
private static int ShowHelp(int result, TextWriter writer) { writer.WriteLine("Example usage: "); var location = typeof(Program).Assembly.Location; var name = location != null?Path.GetFileName(location) : "TemplaterJson.exe"; writer.WriteLine(" "+ name + " template.ext [data.json] [output.ext]"); writer.WriteLine(" template.ext: path to the template file (eg. document.docx)"); writer.WriteLine(" data.json: path to a file containing a JSON object or an array of JSON objects"); writer.WriteLine(); writer.WriteLine("Alternatively, you can use omit the [data.json] and [output.ext] arguments to read from stdin and write to stdout"); writer.WriteLine(" "+ name + " template.ext < [data.json] > [output.ext]"); SupportedType.ShowHelp(writer); writer.Flush(); return(result); }