动机
gitbook
根据文件夹层及目录自动生成gitbook目录
源码
主方法
public static void main(String[] args) throws IOException {
ClassPathResource resource = new ClassPathResource("markdown/doc");
File file = resource.getFile();
if (file.isDirectory()) {
String[] list = file.list();
ancestorPath = file.getPath();
File[] files = file.listFiles();
List<String> concepts = new ArrayList<>();
for (File currentFile : files) {
String structure = getFileStructure(currentFile, "", " ");
if (structure != null) {
concepts.add(structure);
}
}
File summary = new File("SUMMARY.md");
StringBuffer buffer = new StringBuffer();
String context = "# Summary\n\n";
buffer.append(context);
for (String concept : concepts) {
System.out.println(concept);
System.out.println();
buffer.append(concept);
buffer.append("\n");
}
String summaryContext = buffer.toString();
byte[] contextBytes = summaryContext.getBytes("UTF-8");
FileOutputStream fileOutputStream = new FileOutputStream(summary);
fileOutputStream.write(contextBytes);
fileOutputStream.close();
}
}
迭代递归方法
private static String getFileStructure(File currentFile, String father, String tab) {
String name = currentFile.getName();
if (currentFile.isFile() && ".md".equals(name.substring(name.lastIndexOf(".")))) {
return father + "* [" + name.substring(0, name.lastIndexOf(".")) + "](" + setRelativeFilePath(currentFile.getAbsolutePath()) + ")";
} else if (currentFile.isDirectory() && !"assets".equals(name)) {
String fatherOrigin = "* [" + name + "]()";
File[] files = currentFile.listFiles();
if (files.length == 0) {
return null;
}
List<String> subStructure = new ArrayList<>();
for (File file : files) {
String newTab = tab + " ";
String fileStructure = getFileStructure(file, "", newTab);
if (fileStructure != null) {
subStructure.add(fileStructure);
}
}
if (subStructure.size() == 0) {
return null;
}
for (int i = 0; i < subStructure.size(); i++) {
fatherOrigin = fatherOrigin + "\n" + tab + subStructure.get(i);
}
return fatherOrigin;
}
return null;
}
绝对路径转相对路径
/**
* 更改md文件为相对路径
*
* @param absolutePath 根层级绝对路径
* @return 番号相对路径
*/
private static String setRelativeFilePath(String absolutePath) {
int length = ancestorPath.length();
return absolutePath.substring(length + 1);
}
下一步计划
目录下有跟目录同名的markdown文件则设为目录的引用文档
md文件中本地文件的批量上传图床并替换
所有方法替换为python脚本形式
评论前必须登录!
注册