- 添加BookmarkExtractor类用于从FreePic2Pdf_bkmk文件提取书签内容 - 添加ContentFormatter类实现内容格式化处理 - 添加FileMerger类实现文件智能合并功能 - 更新主界面支持路径选择和处理进度显示 - 支持按文件名前缀自动合并(如CH-875 1-3和CH-875 4-6合并为CH-875.txt) - 输出格式符合需求:tableOfContents与subject之间插入格式化内容 - 支持UTF-8和GBK编码自动检测 - 添加详细的使用说明文档 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
137 lines
4.5 KiB
C#
137 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace SlideCombine
|
|
{
|
|
public class ProcessResult
|
|
{
|
|
public string BaseFileName { get; set; }
|
|
public List<string> SourceFiles { get; set; }
|
|
public string OutputContent { get; set; }
|
|
public bool Success { get; set; }
|
|
public string ErrorMessage { get; set; }
|
|
}
|
|
|
|
public class FileMerger
|
|
{
|
|
public static List<ProcessResult> ProcessAllFolders(string pdfRootPath, string txtOutputPath)
|
|
{
|
|
var results = new List<ProcessResult>();
|
|
|
|
try
|
|
{
|
|
// 获取所有包含FreePic2Pdf_bkmk文件的文件夹
|
|
var bkmkFiles = Directory.GetFiles(pdfRootPath, "FreePic2Pdf_bkmk", SearchOption.AllDirectories);
|
|
|
|
if (bkmkFiles.Length == 0)
|
|
{
|
|
throw new Exception($"在路径 {pdfRootPath} 下未找到任何 FreePic2Pdf_bkmk 文件");
|
|
}
|
|
|
|
// 按基础文件名分组(取文件夹名称的空格前缀)
|
|
var fileGroups = new Dictionary<string, List<string>>();
|
|
|
|
foreach (var bkmkFile in bkmkFiles)
|
|
{
|
|
var folderName = Path.GetDirectoryName(bkmkFile);
|
|
var folderNameOnly = new DirectoryInfo(folderName).Name;
|
|
|
|
// 获取空格前的基础名称
|
|
var baseName = GetBaseFileName(folderNameOnly);
|
|
|
|
if (!fileGroups.ContainsKey(baseName))
|
|
{
|
|
fileGroups[baseName] = new List<string>();
|
|
}
|
|
|
|
fileGroups[baseName].Add(bkmkFile);
|
|
}
|
|
|
|
// 处理每个分组
|
|
foreach (var group in fileGroups)
|
|
{
|
|
var result = ProcessFileGroup(group.Key, group.Value.OrderBy(f => f).ToList());
|
|
results.Add(result);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var errorResult = new ProcessResult
|
|
{
|
|
Success = false,
|
|
ErrorMessage = ex.Message
|
|
};
|
|
results.Add(errorResult);
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
private static string GetBaseFileName(string folderName)
|
|
{
|
|
// 获取空格前的部分作为基础名称
|
|
var spaceIndex = folderName.IndexOf(' ');
|
|
return spaceIndex > 0 ? folderName.Substring(0, spaceIndex) : folderName;
|
|
}
|
|
|
|
private static ProcessResult ProcessFileGroup(string baseName, List<string> bkmkFiles)
|
|
{
|
|
var result = new ProcessResult
|
|
{
|
|
BaseFileName = baseName,
|
|
SourceFiles = bkmkFiles,
|
|
Success = true
|
|
};
|
|
|
|
try
|
|
{
|
|
var allFormattedContents = new List<string>();
|
|
|
|
foreach (var bkmkFile in bkmkFiles)
|
|
{
|
|
// 提取书签
|
|
var bookmarks = BookmarkExtractor.ExtractBookmarksFromBkmk(bkmkFile);
|
|
|
|
// 格式化内容
|
|
var formattedContent = ContentFormatter.FormatBookmarks(bookmarks);
|
|
|
|
allFormattedContents.Add(formattedContent);
|
|
}
|
|
|
|
// 合并所有格式化的内容
|
|
var combinedContent = ContentFormatter.CombineFormattedContents(allFormattedContents);
|
|
result.OutputContent = combinedContent;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.ErrorMessage = $"处理文件组 {baseName} 时出错: {ex.Message}";
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static void SaveResults(List<ProcessResult> results, string outputPath)
|
|
{
|
|
if (!Directory.Exists(outputPath))
|
|
{
|
|
Directory.CreateDirectory(outputPath);
|
|
}
|
|
|
|
foreach (var result in results)
|
|
{
|
|
if (result.Success && !string.IsNullOrEmpty(result.OutputContent))
|
|
{
|
|
var outputFileName = $"{result.BaseFileName}.txt";
|
|
var outputFilePath = Path.Combine(outputPath, outputFileName);
|
|
|
|
// 使用UTF-8编码保存
|
|
File.WriteAllText(outputFilePath, result.OutputContent, Encoding.UTF8);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |