-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6243d97
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea | ||
*.epub | ||
*.txt | ||
*.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
## TmdTextEpub | ||
|
||
> 把txt文本转成epub电子书的命令行工具 | ||
### Usage | ||
```$xslt | ||
Usage of TmdTextEpub.exe: | ||
-author string | ||
作者 (default "YSTYLE") | ||
-filename string | ||
txt 文件名 | ||
-match string | ||
匹配标题的正则表达式, 例: -match 第.{1,8}章 表示第和章字之间可以有1-8个任意文字 (default "第.{1,8}章") | ||
``` | ||
|
||
### 示例 | ||
1. 把小说和`TmdTextEpub.exe`放到`D`盘 | ||
2. 按以下其中一种方法打开命令行 | ||
- 按`win + r` 输入 `cmd` 然后输入以下命令 | ||
- 按`win + x + i` 输入以下命令 | ||
|
||
|
||
```shell | ||
cd d:/ | ||
d:/TmdTextEpub.exe -author 乱 -filename d:/全职法师.txt -match 第.{1,4}章 | ||
``` | ||
|
||
### 手工构建 | ||
```$xslt | ||
go build -ldflags "-s -w" -o TmdTextEpub.exe main.go | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/ystyle/TmdTextEpub | ||
|
||
require github.com/bmaupin/go-epub v0.5.0 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/bmaupin/go-epub v0.5.0 h1:ptOZQuO3YBRytuhRPsHUJh5bRmvP66/GvB+NWTC2dyE= | ||
github.com/bmaupin/go-epub v0.5.0/go.mod h1:4RBr0Zo03mRGOyGAcc25eLOqIPCkMbfz+tINVmH6clQ= | ||
github.com/gofrs/uuid v3.1.0+incompatible h1:q2rtkjaKT4YEr6E1kamy0Ha4RtepWlQBedyHx0uzKwA= | ||
github.com/gofrs/uuid v3.1.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"github.com/bmaupin/go-epub" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var ( | ||
filename string // 目录 | ||
match string // 正则 | ||
author string // 正则 | ||
) | ||
|
||
// 解析程序参数 | ||
func init() { | ||
flag.StringVar(&filename, "filename", "", "txt 文件名") | ||
flag.StringVar(&author, "author", "YSTYLE", "作者") | ||
flag.StringVar(&match, "match", "第.{1,8}章", "匹配标题的正则表达式, 例: -match 第.{1,8}章 表示第和章字之间可以有1-8个任意文字") | ||
flag.Parse() | ||
} | ||
|
||
func readBuffer(filename string) *bufio.Reader { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
fmt.Println("读取文件出错: ", err.Error()) | ||
os.Exit(1) | ||
} | ||
buf := bufio.NewReader(f) | ||
return buf | ||
} | ||
|
||
func main() { | ||
start := time.Now() | ||
bookName := strings.Split(filepath.Base(filename), ".")[0] | ||
|
||
fmt.Println("转换信息:") | ||
fmt.Println("文件名:", filename) | ||
if author != "" { | ||
fmt.Println("作者:", author) | ||
} | ||
fmt.Println("匹配条件:", match) | ||
fmt.Println() | ||
|
||
// Create a ne EPUB | ||
e := epub.NewEpub(bookName) | ||
|
||
// Set the author | ||
e.SetAuthor(author) | ||
fmt.Println("正在读取txt文件...") | ||
|
||
// 编译正则表达式 | ||
reg, err := regexp.Compile(match) | ||
if err != nil { | ||
fmt.Printf("生成匹配规则出错: %s\n%s\n", match, err.Error()) | ||
return | ||
} | ||
|
||
buf := readBuffer(filename) | ||
var title string | ||
var content string | ||
|
||
for { | ||
line, err := buf.ReadString('\n') | ||
if err != nil { | ||
if err == io.EOF { | ||
e.AddSection(content, title, "", "") | ||
break | ||
} | ||
fmt.Println("读取文件出错:", err.Error()) | ||
os.Exit(1) | ||
} | ||
line = strings.TrimSpace(line) | ||
if reg.MatchString(line) { | ||
if title == "" { | ||
title = "说明" | ||
} | ||
e.AddSection(content, title, "", "") | ||
title = line | ||
content = line + "<br/>" | ||
} else { | ||
content = content + line + "<br/>" | ||
} | ||
} | ||
end := time.Now().Sub(start) | ||
fmt.Println("耗时:", end) | ||
|
||
// Write the EPUB | ||
fmt.Println("正在生成电子书...") | ||
err = e.Write(bookName + ".epub") | ||
if err != nil { | ||
// handle error | ||
} | ||
// 计算耗时 | ||
end = time.Now().Sub(start) | ||
fmt.Println("耗时:", end) | ||
fmt.Println("\n转换完成! 总耗时:", end) | ||
} |