|
| 1 | +package com.eugene.kindle.service.impl; |
| 2 | + |
| 3 | +import com.eugene.kindle.constant.NoteType; |
| 4 | +import com.eugene.kindle.bean.Note; |
| 5 | +import com.eugene.kindle.service.IConvertService; |
| 6 | +import com.eugene.kindle.utils.DateFormatter; |
| 7 | +import com.eugene.kindle.utils.DateUtils; |
| 8 | +import com.eugene.kindle.utils.FileUtils; |
| 9 | +import java.time.LocalDateTime; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashSet; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Set; |
| 14 | + |
| 15 | +public class ConvertServiceImpl implements IConvertService { |
| 16 | + |
| 17 | + @Override |
| 18 | + public List<Note> getNotes(String path, String bookName) { |
| 19 | + List<String> lines = FileUtils.readLine(path); |
| 20 | + List<Note> result = new ArrayList<>(); |
| 21 | + for (int i = 1; i <= lines.size(); i = i + 5) { |
| 22 | + // 书名不匹配 |
| 23 | + if (!lines.get(i - 1).equals(bookName)) { |
| 24 | + continue; |
| 25 | + } |
| 26 | + Note note = new Note(); |
| 27 | + note.setName(lines.get(i - 1)); |
| 28 | + setNodeInfo(note, lines.get(i)); |
| 29 | + note.setContent(lines.get(i + 2)); |
| 30 | + result.add(note); |
| 31 | + } |
| 32 | + return result; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * 翻译标注的位置、时间行 |
| 37 | + * |
| 38 | + * @param line - 您在位置 #1877-1878的标注 | 添加于 2022年5月30日星期一 下午6:19:37 |
| 39 | + */ |
| 40 | + private void setNodeInfo(Note node, String line) { |
| 41 | +// System.out.println(line); |
| 42 | + if (!line.startsWith("-")) { |
| 43 | + return; |
| 44 | + } |
| 45 | + String[] split = line.replaceFirst("-", "").trim().split("\\|"); |
| 46 | + // 0:您在位置 #1877-1878的标注 |
| 47 | + // 1:添加于 2022年5月30日星期一 下午6:19:37 |
| 48 | + String locationAndType = split[0].replace("您在位置 #", "").trim(); |
| 49 | + // 1877-1878的标注 |
| 50 | + String[] local = locationAndType.split("的"); |
| 51 | + int location = Integer.parseInt(local[0].split("-")[0].trim()) |
| 52 | + + Integer.parseInt(local[0].split("-")[0].trim()); |
| 53 | + node.setLocation((float) location / 2); |
| 54 | + node.setType(NoteType.fromVal(local[1].trim())); |
| 55 | + String dateTimeStr = split[1].replace("添加于", "").trim(); |
| 56 | + // 2022年5月30日星期一 下午6:19:37 |
| 57 | + dateTimeStr = DateUtils.makeUpZero(dateTimeStr); |
| 58 | + LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, DateFormatter.RFC_1123_DATE_TIME); |
| 59 | + node.setDateTime(dateTime); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public List<String> getBookNames(String path) { |
| 64 | + List<String> lines = FileUtils.readLine(path); |
| 65 | + Set<String> names = new HashSet<>(); |
| 66 | + for (int i = 1; i <= lines.size(); i = i + 5) { |
| 67 | + names.add(lines.get(i - 1)); |
| 68 | + } |
| 69 | + return new ArrayList<>(names); |
| 70 | + } |
| 71 | +} |
0 commit comments