You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.6 KiB

package UnitTest.Swdt;
import java.io.*;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RuntimeUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.File;
import java.util.Random;
/*
https://markmap.js.org/repl
https://github.com/markmap/markmap
*/
public class Swdt {
public static String workingPath = "D:\\dsWork\\QingLong\\src\\main\\java\\UnitTest\\Swdt\\";
public static String RED = "#ff5722";
public static String YELLOW = "#ffb800";
public static String GREEN = "#07C491";
/**
* 功能:生成一个指定颜色+数字
*
* @param txt
* @param numStr
* @return
*/
public static String getColorTxt(String txt, String numStr, String key) {
int num = Integer.parseInt(numStr.replace("+", ""));
if (num == 0) return getGrayTxt(txt, num);
String color;
if (num <= 30) color = RED;
else if (num <= 60) color = YELLOW;
else color = GREEN;
String content = FileUtil.readUtf8String(workingPath + "Swdt_Color.txt");
content = content.replace("\r", "");
content = content.replace("\n", "");
content = content.replace("{{color}}", color);
content = content.replace("{{txt}}", cut(txt));
content = content.replace("{{key}}", key);
if (num < 99) content = content.replace("{{num}}", String.valueOf(num));
else content = content.replace("{{num}}", num + "+");
return content;
}
/**
* 功能:生成一个灰颜色+数字
*
* @param txt
* @param num
* @return
*/
public static String getGrayTxt(String txt, int num) {
String content = FileUtil.readUtf8String(workingPath + "Swdt_Gray.txt");
content = content.replace("\r", "");
content = content.replace("\n", "");
content = content.replace("{{txt}}", cut(txt));
content = content.replace("{{num}}", String.valueOf(num));
return content;
}
public static String cut(String str) {
if (str.length() > 11)
return str.substring(0, 11) + "...";
return str;
}
public static void main(String[] args) throws IOException {
String km = "初中物理八年级上(人教版)";
String sourceJson = "Swdt.json";
String targetMd = "Swdt.md";
String targetHtml = "Swdt.html";
int rowCount = 3;//每行3个
String json = FileUtil.readUtf8String(new File(workingPath + sourceJson));
JSONObject jsonObject = JSONObject.parseObject(json);
JSONArray jTree = jsonObject.getJSONObject("data").getJSONArray("tree");
String res = "# " + km + "\n";
for (int i = 0; i < jTree.size(); i++) {
String key = jTree.getJSONObject(i).getString("key");
String title = jTree.getJSONObject(i).getString("title");
res += "## " + getColorTxt(title, "99+", key) + "\n";
JSONArray jChildren = jTree.getJSONObject(i).getJSONArray("children");
if (jChildren != null) {
for (int j = 0; j < jChildren.size(); j++) {
for (int k = 0; k <= j % rowCount; k++) res += "#";
res += "## ";
JSONObject jo = jChildren.getJSONObject(j);
//生成一个随机数
Random random = new Random();
int number = random.nextInt(100);
res += getColorTxt(jo.getString("title"), String.valueOf(number), jo.getString("key")) + "\n";
}
}
res += "\n";
}
//写入md文件
FileUtil.writeUtf8String(res, workingPath + targetMd);
//调用命令行生成html
//markmap Swdt.md -o Swdt.html --no-open
String osName = System.getProperty("os.name");
String cmd = "markmap " + workingPath + targetMd + " -o " + workingPath + targetHtml + " --no-open";
if (osName.startsWith("Windows")) {
cmd = "cmd /c " + cmd;
}
String str = RuntimeUtil.execForStr(cmd);
System.out.println(str);
// 修改html文件
File file = new File(workingPath + targetHtml);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
StringBuilder content = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
String a = "<title>Markmap</title>";
String b = "https://cdn.jsdelivr.net/npm/markmap-view@0.16.0/dist/browser/index.js";
if (line.indexOf(a) >= 0) {
String modifiedLine = line.replaceAll(a, "<title>课程建设情况</title>");
//读取字体文件,填充内容
String fontStr = FileUtil.readUtf8String(new File(workingPath + "Swdt_Font.txt"));
content.append(modifiedLine).append("\n");
content.append(fontStr).append("\n");
} else if (line.indexOf(b) >= 0) {
String modifiedLine = line.replaceAll(b, "https://dsideal.obs.cn-north-1.myhuaweicloud.com/wb/index.js");
content.append(modifiedLine).append("\n");
} else {
content.append(line).append("\n");
}
}
br.close();
//写入html文件
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content.toString());
bw.close();
//完成
System.out.println("恭喜,所有操作成功完成!");
}
}