using System;
using System.IO;
using System.Diagnostics;
using System.Text;
public static class ClipboardFusionHelper
{
public static string ProcessText(string text)
{
if (string.IsNullOrWhiteSpace(text))
return text;
string pandocPath = @"D:\Tools\开发设计\pandoc\pandoc.exe";
if (!File.Exists(pandocPath))
{
BFS.Dialog.ShowMessageError("Pandoc 未找到,路径:" + pandocPath);
return text;
}
string tempMd = Path.GetTempFileName() + ".md";
string rtfOutput = "";
try
{
// 写入临时文件:UTF-8 with BOM,确保正确处理中文和换行
File.WriteAllText(tempMd, text, new UTF8Encoding(true));
// 启用 hard_line_breaks 保留单个换行
string args = $"\"{tempMd}\" -f markdown+hard_line_breaks -t rtf --standalone --wrap=none";
var psi = new ProcessStartInfo
{
FileName = pandocPath,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8
};
using (var process = Process.Start(psi))
{
rtfOutput = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0)
{
BFS.Dialog.ShowMessageError("Pandoc Conversion failed:\n" + error);
return rtfOutput;
}
}
// 写入剪贴板:纯文本 + RTF
var data = new System.Windows.Forms.DataObject();
data.SetText(text, System.Windows.Forms.TextDataFormat.UnicodeText);
data.SetText(rtfOutput, System.Windows.Forms.TextDataFormat.Rtf);
System.Windows.Forms.Clipboard.SetDataObject(data, true);
// 成功提示(可选,如需静默可删除)
BFS.Dialog.ShowTrayMessage("Markdown → RTF Conversion successful!");
}
catch (Exception ex)
{
BFS.Dialog.ShowMessageError("Error:\n" + ex.Message + "\n" + ex.StackTrace);
}
finally
{
if (File.Exists(tempMd))
File.Delete(tempMd);
}
return text;
}
}