Skip to content

Commit

Permalink
update: find chapter by regex and more setting
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Aug 21, 2019
1 parent 71d9137 commit aaddfe8
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 102 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ Working yet

Install: [#1](https://github.com/MUedsa/TextReaderSiderTool/issues/1)

## Setting

- 解析限制: 章节的标题如果过长可以适量增加此限制
- 章节前缀: 章节的标题前缀
- 章节后缀: 章节的标题后缀
- 固定标题: 如果存在如‘前言’‘引子’等标题可以放在此列表中,使用`|`分隔

*以上设置修改后,清空‘正则设置’,然后点击'file'选择文件*

- 正则设置: 匹配章节标题的正则,可自定义正则,如果不存在,会把上面的设置解析为正则

## note

#### 【Dev】.iml something
Expand Down
25 changes: 16 additions & 9 deletions src/com/muedsa/intellij/textReader/Chapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import java.io.*;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Chapter {
private int startOffset;
Expand Down Expand Up @@ -53,23 +55,24 @@ public String toString() {
return "●" + title;
}

public static Vector<Chapter> getChapters(TextFile textFile, String chapterPrefix, String chapterSuffix){
public static Vector<Chapter> getChapters(TextFile textFile, int maxLineSize, Pattern pattern) {
Vector<Chapter> list = new Vector<>();
int offset = 0;
try{
try {
InputStreamReader reader = new InputStreamReader(textFile.getInputStream(), textFile.getCharset());
MyBufferedReader bufferedReader = new MyBufferedReader(reader);
String lineContent;
Chapter previousChapter = null;
while ((lineContent = bufferedReader.readLineWithCRLF()) != null){
int startIndex = lineContent.indexOf(chapterPrefix);
int endIndex = lineContent.indexOf(chapterSuffix);
if (startIndex >= 0 && endIndex > 0 && endIndex > startIndex && endIndex - startIndex < 10 && lineContent.length()-endIndex < 50 && Pattern.matches("[0-9]+|[零一二三四五六七八九十百千万]+", lineContent.substring(startIndex + 1, endIndex))) {
if(previousChapter != null){
previousChapter.setLength(offset - previousChapter.getStartOffset());
if(lineContent.length() <= maxLineSize){
Matcher matcher = pattern.matcher(lineContent);
if(matcher.find()){
if(previousChapter != null){
previousChapter.setLength(offset - previousChapter.getStartOffset());
}
previousChapter = new Chapter(offset, lineContent.trim());
list.add(previousChapter);
}
previousChapter = new Chapter(offset, lineContent.trim());
list.add(previousChapter);
}
offset += lineContent.getBytes(textFile.getCharset()).length;
}
Expand All @@ -82,6 +85,10 @@ public static Vector<Chapter> getChapters(TextFile textFile, String chapterPrefi
e.printStackTrace();
NotificationFactory.sendNotify("加载失败", e.getLocalizedMessage(), NotificationType.WARNING);
}
catch (PatternSyntaxException error){
error.printStackTrace();
NotificationFactory.sendNotify("正则错误", error.getLocalizedMessage(), NotificationType.ERROR);
}
return list;
}

Expand Down
65 changes: 0 additions & 65 deletions src/com/muedsa/intellij/textReader/Page.java

This file was deleted.

Loading

0 comments on commit aaddfe8

Please sign in to comment.