幕布笔记批量导入到anki

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

功能:支持三层大纲制作,比如
在这里插入图片描述

以下代码可直接使用

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("<br/>");
                }
                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.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.8.3</version>
</dependency>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值