Skip to content

Commit e43752b

Browse files
committed
feat: 自动识别第x幕、番外、完本感言类的章节,优化券识别使之更符合习惯
1 parent c2542d8 commit e43752b

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- 自动识别书名和章节(示例中所有用法都会自动识别)
1010
- 自动识别字符编码(自动解决中文乱码)
1111
- 自定义章节标题识别规则
12-
- 自定义卷的标题识别规则(卷的识别需要在标题识别规则里能识别出来)
12+
- 自定义卷的标题识别规则
1313
- 自动给章节正文生成加粗居中的标题
1414
- 自定义标题对齐方式
1515
- 段落自动识别
@@ -20,7 +20,7 @@
2020
- 自定义书籍语言
2121
- epub格式支持嵌入字体
2222
- 知轩藏书格式文件名会自动提取书名和作者, 例: `《希灵帝国》(校对版全本)作者:远瞳.txt`
23-
- 超快速(130章/s以上速度, 4000章30s不到)
23+
- 超快速(epub格式生成300章/s以上速度, 4000章15s不到)
2424
- 自动转为mobi格式
2525

2626
### 下载
@@ -48,6 +48,7 @@
4848

4949
```text
5050
Usage of kaf-cli:
51+
```shell
5152
-align string
5253
标题对齐方式: left、center、righ。环境变量KAF_CLI_ALIGN可修改默认值 (default "center")
5354
-author string
@@ -67,7 +68,7 @@ Usage of kaf-cli:
6768
-font string
6869
嵌入字体, 之后epub的正文都将使用该字体
6970
-format string
70-
书籍格式: all、epub、mobi、azw3。环境变量KAF_CLI_FORMAT可修改默认值 (default "all")
71+
书籍格式: all、epub、mobi、azw3。环境变量KAF_CLI_FORMAT可修改默认值 (default "epub")
7172
-indent uint
7273
段落缩进字数 (default 2)
7374
-lang string
@@ -85,7 +86,7 @@ Usage of kaf-cli:
8586
-unknow-title string
8687
未知章节默认名称 (default "章节正文")
8788
-volume-match string
88-
卷匹配规则 (default "^第[0-9一二三四五六七八九十零〇百千两 ]+[卷部]")
89+
卷匹配规则,设置为false可以禁用卷识别 (default "^第[0-9一二三四五六七八九十零〇百千两 ]+[卷部]")
8990
```
9091

9192
>PS: 在darwin(mac、osx)上`-tips`参数要设置为false的方法 `kaf-cli -filename 小说.txt -tips=0`
@@ -115,6 +116,9 @@ kaf-cli ~/全职法师.txt
115116
116117
>规则支持[正则表达式](http://deerchao.net/tutorials/regex/regex.htm), 要自定义标题格式参考以下几个例子, 以下例子小说都在D盘
117118
119+
快速入门
120+
>注意`.`是指任意字,`{1,8}`里边的数值代表1-8个字, `\d+`是指纯数字,`\w`是指中英文的字
121+
118122

119123

120124
自定义章节匹配, 章节格式为`第x节`:

internal/core/parser.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,26 @@ func Parse(book *model.Book) error {
8383
if len(line) == 0 {
8484
continue
8585
}
86-
// 处理标题
87-
if utf8.RuneCountInString(line) <= int(book.Max) &&
88-
(book.Reg.MatchString(line) || book.VolumeReg.MatchString(line)) {
89-
if title == "" {
90-
title = book.UnknowTitle
91-
}
92-
if content.Len() > 0 || title != book.UnknowTitle {
93-
contentList = append(contentList, model.Section{
94-
Title: title,
95-
Content: content.String(),
96-
})
86+
// 处理标题(优先匹配卷)
87+
if utf8.RuneCountInString(line) <= int(book.Max) {
88+
isVolume := book.VolumeReg.MatchString(line)
89+
isChapter := book.Reg.MatchString(line)
90+
91+
if isVolume || isChapter {
92+
if title == "" {
93+
title = book.UnknowTitle
94+
}
95+
if content.Len() > 0 || title != book.UnknowTitle {
96+
contentList = append(contentList, model.Section{
97+
Title: title,
98+
Content: content.String(),
99+
})
100+
}
101+
title = line
102+
content.Reset()
103+
continue
97104
}
98-
title = line
99-
content.Reset()
100-
continue
105+
101106
}
102107
utils.AddPart(&content, line)
103108
}
@@ -121,6 +126,12 @@ func Parse(book *model.Book) error {
121126
}
122127
temp := section
123128
volumeSection = &temp
129+
} else if strings.HasPrefix(section.Title, "完本感言") || strings.HasPrefix(section.Title, "番外") {
130+
if volumeSection != nil {
131+
sectionList = append(sectionList, *volumeSection)
132+
volumeSection = nil
133+
}
134+
sectionList = append(sectionList, section)
124135
} else {
125136
if volumeSection == nil {
126137
sectionList = append(sectionList, section)

internal/model/book.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616

1717
const (
1818
VolumeMatch = "^第[0-9一二三四五六七八九十零〇百千两 ]+[卷部]"
19-
DefaultMatchTips = "^第[0-9一二三四五六七八九十零〇百千两 ]+[章回节集卷部]|^[Ss]ection.{1,20}$|^[Cc]hapter.{1,20}$|^[Pp]age.{1,20}$|^\\d{1,4}$|^\\d+、$|^引子$|^楔子$|^章节目录|^章节|^序章"
19+
DefaultMatchTips = "^第[0-9一二三四五六七八九十零〇百千两 ]+[章回节集幕卷部]|^[Ss]ection.{1,20}$|^[Cc]hapter.{1,20}$|^[Pp]age.{1,20}$|^\\d{1,4}$|^\\d+、$|^引子$|^楔子$|^章节目录|^章节|^序章|^最终章 \\w{1,20}$|^番外\\d?\\w{0,20}|^完本感言.{0,4}$"
2020
Tutorial = `本书由kaf-cli生成: <br/>
2121
制作教程: <a href='https://ystyle.top/2019/12/31/txt-converto-epub-and-mobi/'>https://ystyle.top/2019/12/31/txt-converto-epub-and-mobi</a>
2222
`

0 commit comments

Comments
 (0)