Skip to content

Commit e5753a3

Browse files
committed
修改输入逻辑
1 parent 9a253f2 commit e5753a3

File tree

1 file changed

+79
-27
lines changed

1 file changed

+79
-27
lines changed

Program.cs

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,101 @@ static void Main(string[] args)
1616
{
1717
Console.OutputEncoding = Encoding.UTF8;
1818

19-
// --- 侦探代码开始 ---
20-
Console.WriteLine("--- macOS Drag-and-Drop Detective ---");
21-
Console.WriteLine();
22-
Console.WriteLine($"Current Working Directory is: {Directory.GetCurrentDirectory()}");
23-
Console.WriteLine();
24-
Console.WriteLine($"Received {args.Length} argument(s):");
19+
// 获取输入路径
20+
string filePath = GetFilePath(args);
21+
if (!File.Exists(filePath))
22+
{
23+
Console.WriteLine("文件不存在,请检查路径是否正确");
24+
return;
25+
}
26+
27+
// 验证文件后缀
28+
if (!IsValidHtmlFile(filePath))
29+
{
30+
Console.WriteLine("错误:只支持HTML文件(.html 或 .htm 后缀)");
31+
Console.WriteLine("按任意键退出...");
32+
Console.ReadKey();
33+
return;
34+
}
35+
36+
// 选择转换模式
37+
ConversionMode mode = GetConversionMode();
2538

26-
if (args.Length > 0)
39+
try
2740
{
28-
for (int i = 0; i < args.Length; i++)
41+
// 读取文件
42+
string html = File.ReadAllText(filePath, Encoding.UTF8);
43+
44+
// 转换处理
45+
string result;
46+
string extension;
47+
48+
if (mode == ConversionMode.Markdown)
49+
{
50+
result = ConvertHtmlToMarkdown(html);
51+
extension = ".md";
52+
}
53+
else
54+
{
55+
result = ConvertHtmlToBBCode(html);
56+
extension = ".bbcode";
57+
}
58+
59+
// 生成输出路径
60+
string outputPath = Path.Combine(
61+
Path.GetDirectoryName(filePath) ?? "",
62+
$"{Path.GetFileNameWithoutExtension(filePath)}{extension}"
63+
);
64+
65+
// 保存文件
66+
File.WriteAllText(outputPath, result, Encoding.UTF8);
67+
Console.WriteLine($"转换完成!输出文件:{outputPath}");
68+
69+
// 询问是否复制到剪贴板
70+
if (AskToCopyToClipboard())
2971
{
30-
// 打印每一个参数的原始内容,不带任何处理
31-
Console.WriteLine($" args[{i}] = \"{args[i]}\"");
72+
if (CopyToClipboard(result))
73+
{
74+
Console.WriteLine("✓ 内容已复制到剪贴板");
75+
}
76+
else
77+
{
78+
Console.WriteLine("✗ 复制到剪贴板失败");
79+
}
3280
}
3381
}
34-
else
82+
catch (Exception ex)
3583
{
36-
Console.WriteLine(" (No arguments were received)");
84+
Console.WriteLine($"处理出错:{ex.Message}");
3785
}
3886

39-
Console.WriteLine();
40-
Console.WriteLine("---------------------------------------");
41-
Console.WriteLine("This is the end of the diagnostic report.");
42-
Console.WriteLine("Please take a screenshot of this entire window and send it back.");
43-
Console.WriteLine();
44-
Console.WriteLine("Press any key to close the window...");
87+
Console.WriteLine("按任意键退出...");
4588
Console.ReadKey();
46-
// --- 侦探代码结束 ---
47-
48-
// 我们暂时让程序在这里结束,不执行后面的逻辑,以避免任何干扰
49-
return;
5089
}
5190

5291
static string GetFilePath(string[] args)
5392
{
54-
if (args.Length > 0)
93+
// 优先处理通过命令行参数传入的路径
94+
if (args.Length > 0 && !string.IsNullOrWhiteSpace(args[0]))
5595
{
56-
// 将所有传入的参数用空格重新拼接,以正确处理未被引号包裹的带空格路径
57-
return string.Join(" ", args).Trim('"');
96+
return args[0].Trim('"'); // 处理带引号的路径
5897
}
5998

60-
Console.Write("请拖入HTML文件或输入路径:");
61-
return Console.ReadLine()?.Trim('"') ?? "";
99+
// 如果没有命令行参数,则循环提示用户输入,直到获得非空路径
100+
string filePath;
101+
do
102+
{
103+
Console.Write("请拖入HTML文件或输入路径:");
104+
filePath = Console.ReadLine()?.Trim('"') ?? ""; // 读取并去除可能存在的引号
105+
106+
if (string.IsNullOrWhiteSpace(filePath))
107+
{
108+
Console.WriteLine("路径不能为空,请重新输入。"); // 对空输入给出提示
109+
}
110+
111+
} while (string.IsNullOrWhiteSpace(filePath)); // 只要输入为空,就继续循环
112+
113+
return filePath;
62114
}
63115

64116
static bool IsValidHtmlFile(string filePath)

0 commit comments

Comments
 (0)