|
| 1 | +using System.Diagnostics; |
| 2 | +using AIStudio.Chat; |
| 3 | +using AIStudio.Tools.PluginSystem; |
| 4 | +using AIStudio.Tools.Services; |
| 5 | + |
| 6 | +namespace AIStudio.Tools; |
| 7 | + |
| 8 | +public static class PandocExport |
| 9 | +{ |
| 10 | + private static readonly ILogger LOGGER = Program.LOGGER_FACTORY.CreateLogger(nameof(PandocExport)); |
| 11 | + |
| 12 | + private static string TB(string fallbackEn) => I18N.I.T(fallbackEn, typeof(PandocExport).Namespace, nameof(PandocExport)); |
| 13 | + |
| 14 | + public static async Task<bool> ToMicrosoftWord(RustService rustService, string dialogTitle, IContent markdownContent) |
| 15 | + { |
| 16 | + var response = await rustService.SaveFile(dialogTitle, new("Microsoft Word", ["docx"])); |
| 17 | + if (response.UserCancelled) |
| 18 | + { |
| 19 | + LOGGER.LogInformation("User cancelled the save dialog."); |
| 20 | + return false; |
| 21 | + } |
| 22 | + |
| 23 | + LOGGER.LogInformation($"The user chose the path '{response.SaveFilePath}' for the Microsoft Word export."); |
| 24 | + |
| 25 | + var tempMarkdownFile = Guid.NewGuid().ToString(); |
| 26 | + var tempMarkdownFilePath = Path.Combine(Path.GetTempPath(), tempMarkdownFile); |
| 27 | + |
| 28 | + try |
| 29 | + { |
| 30 | + // Extract text content from chat: |
| 31 | + var markdownText = markdownContent switch |
| 32 | + { |
| 33 | + ContentText text => text.Text, |
| 34 | + ContentImage _ => "Image export to Microsoft Word not yet possible", |
| 35 | + |
| 36 | + _ => "Unknown content type. Cannot export to Word." |
| 37 | + }; |
| 38 | + |
| 39 | + // Write text content to a temporary file: |
| 40 | + await File.WriteAllTextAsync(tempMarkdownFilePath, markdownText); |
| 41 | + |
| 42 | + // Ensure that Pandoc is installed and ready: |
| 43 | + var pandocState = await Pandoc.CheckAvailabilityAsync(rustService); |
| 44 | + if (!pandocState.IsAvailable) |
| 45 | + return false; |
| 46 | + |
| 47 | + // Call Pandoc to create the Word file: |
| 48 | + var pandoc = await PandocProcessBuilder |
| 49 | + .Create() |
| 50 | + .UseStandaloneMode() |
| 51 | + .WithInputFormat("markdown") |
| 52 | + .WithOutputFormat("docx") |
| 53 | + .WithOutputFile(response.SaveFilePath) |
| 54 | + .WithInputFile(tempMarkdownFilePath) |
| 55 | + .BuildAsync(rustService); |
| 56 | + |
| 57 | + using var process = Process.Start(pandoc.StartInfo); |
| 58 | + if (process is null) |
| 59 | + { |
| 60 | + LOGGER.LogError("Failed to start Pandoc process."); |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + await process.WaitForExitAsync(); |
| 65 | + if (process.ExitCode is not 0) |
| 66 | + { |
| 67 | + var error = await process.StandardError.ReadToEndAsync(); |
| 68 | + LOGGER.LogError($"Pandoc failed with exit code {process.ExitCode}: {error}"); |
| 69 | + await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.Cancel, TB("Error during Microsoft Word export"))); |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + LOGGER.LogInformation("Pandoc conversion successful."); |
| 74 | + await MessageBus.INSTANCE.SendSuccess(new(Icons.Material.Filled.CheckCircle, TB("Microsoft Word export successful"))); |
| 75 | + |
| 76 | + return true; |
| 77 | + } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + LOGGER.LogError(ex, "Error during Word export."); |
| 81 | + await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.Cancel, TB("Error during Microsoft Word export"))); |
| 82 | + return false; |
| 83 | + } |
| 84 | + finally |
| 85 | + { |
| 86 | + // Try to remove the temp file: |
| 87 | + try |
| 88 | + { |
| 89 | + File.Delete(tempMarkdownFilePath); |
| 90 | + } |
| 91 | + catch |
| 92 | + { |
| 93 | + LOGGER.LogWarning($"Was not able to delete temporary file: '{tempMarkdownFilePath}'"); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments