package com.gitee.sop.doc;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 生成_sidebar.md文件，直接运行即可
 * @author tanghc
 */
public class SidebarTest {

	static String format = "  * [%s](files/%s?t=%s)\r\n";
	static String sidebar_format = "* 文档目录\r\n\r\n%s";

	public static void main(String[] args) throws Exception {
		String path = SidebarTest.class.getClassLoader().getResource("").getPath();
		String root = path.substring(0, path.indexOf("doc")) + "doc";
		String fileDir = root + "/docs/files";
		File dir = new File(fileDir);
		File[] files = dir.listFiles();
		Stream<File> filesStream = Stream.of(files);
		List<File> fileList = filesStream
				.sorted(Comparator.comparing(File::getName))
				.collect(Collectors.toList());
		StringBuilder output = new StringBuilder();
		for (File file : fileList) {
			String filename = file.getName();
			String title = filename.substring(filename.indexOf("_") + 1, filename.length() - 3);
			String line = String.format(format, title, filename, System.currentTimeMillis());
			output.append(line);
		}

		String sidebarContent = String.format(sidebar_format, output.toString());

		System.out.println(sidebarContent);

		String sidebarFilepath = root + "/docs/_sidebar.md";

		FileOutputStream out = new FileOutputStream(new File(sidebarFilepath));
		out.write(sidebarContent.getBytes());
		out.close();
	}
}
