幕布笔记批量导入到anki

幕布是款亲测好用的大纲笔记,而anki是款flash card工具。
大段时间可将笔记整理到幕布,碎片时间则可以抽查记忆。

功能:支持三层大纲制作,比如
幕布笔记批量导入到anki_第1张图片

以下代码可直接使用

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.URLDecoder;
import java.util.*;

public class ParseMuBuOpml {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String path = "C:\\Users\\wxmgc\\Downloads\\设计模式.opml";
        Map<String, String> map = parseOpml(path);
        String outputPath = "C:\\Users\\wxmgc\\Desktop\\anki.txt";
        try (FileWriter fileWriter = new FileWriter(outputPath)) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                fileWriter.append(String.format("%s\t%s\n",entry.getKey(),entry.getValue()));
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Map<String,String> parseOpml(String filepath) throws UnsupportedEncodingException {
        Map<String,String> map = new HashMap<>();
        String htmlStr = toHtmlString(new File(filepath));
        //解析字符串为Document对象
        Document doc = Jsoup.parse(htmlStr);
        String title = doc.title();
        Elements outline = doc.body().getElementsByTag("outline");
        Set<String> keys = new HashSet<>();
        for (int i = 0; i < outline.size(); i++) {
            Element element = outline.get(i);
            if(element.childNodes().size() == 0){
                // 跳过没有答案
                String question = element.parent().attributes().get("text");
                if(question.equals(title)){
                    continue;
                }

                //根据key滤重,处理多行答案
                if(keys.contains(question)){
                    continue;
                }

                keys.add(question);
                List<Node> nodes = element.parent().childNodes();
                StringBuffer sb = new StringBuffer();
                for (Node node : nodes) {
                    if(node.attributes().get("text").trim().length() == 0){
                        continue;
                    }
                    String answer = URLDecoder.decode(node.attributes().get("_mubu_text"),"utf-8");
                    sb.append(answer).append("
"
); } map.put(question,sb.toString()); } } return map; } public static String toHtmlString(File file) { StringBuffer htmlSb = new StringBuffer(); try { BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(file), "utf-8"));//unicode while (br.ready()) { htmlSb.append(br.readLine()); } br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String htmlStr = htmlSb.toString(); return htmlStr; } }

引入pom依赖

<dependency>
    <groupId>org.jsoupgroupId>
    <artifactId>jsoupartifactId>
    <version>1.8.3version>
dependency>

你可能感兴趣的:(编程基础,java,幕布,anki)