Skip to content

Commit

Permalink
feat: 添加新的章节匹配规则,添加wasm版本
Browse files Browse the repository at this point in the history
  • Loading branch information
ystyle committed Aug 31, 2023
1 parent ec900e6 commit fbd8138
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.19
go-version: ^1.21
id: go

- name: Check out code into the Go module directory
Expand Down Expand Up @@ -42,6 +42,7 @@ jobs:
GOOS=linux GOARCH=loong64 go build -ldflags "$flag" -o build/linux-loong64/kaf-cli cmd/cli.go
GOOS=darwin GOARCH=amd64 go build -ldflags "$flag" -o build/darwin-amd64/kaf-cli cmd/cli.go
GOOS=darwin GOARCH=arm64 go build -ldflags "$flag" -o build/darwin-arm64/kaf-cli cmd/cli.go
GOOS=wasip1 GOARCH=wasm go build -ldflags "$flag" -o build/wasm-wasip1/kaf-cli.wasm cmd/cli.go
${GOBIN}/rsrc -arch amd64 -manifest kaf-cli.exe.manifest -ico ./assets/kaf.ico -o kaf-cli.syso
GOOS=windows GOARCH=386 go build -ldflags "$flag" -o build/windows-386/kaf-cli.exe cmd/cli.go
GOOS=windows GOARCH=amd64 go build -ldflags "$flag" -o build/windows-amd64/kaf-cli.exe cmd/cli.go
Expand Down
16 changes: 16 additions & 0 deletions README_wasi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### 环境准备
- 安装`wasmtime`运行时, 或其它支持go 1.21版本生成的wasip1 wasm的运行时
- 经测试wasmer不支持运行gov1.21.0生成的wasm

### 运行
```shell
wasmtime run --mapdir /tmp::/tmp --mapdir /txt::. ./kaf-cli.wasm -- -filename /txt/《诡秘之主》(精校版全本)作者:爱潜水的乌贼.txt -format epub
```
- `--mapdir` 需要把运行时里的`/tmp`和txt存放位置映射到物理机的目录
- 运行时的参数和`kaf-cli`的参数需要用`--`分隔开
- `kaf-cli`的小说路径参数,需要指定运行时里映射的路径

### 源码构建
1. 需要提前安装[`go编译器`](https://go.dev)
2. 下载:https://github.com/ystyle/kaf-cli
3. 编译`wasm/wasi`版本: `OARCH=wasm GOOS=wasip1 go build -o kaf-cli.wasm cmd/cli.go`
64 changes: 28 additions & 36 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const (
mobiTtmlTitleStart = `<h3 style="text-align:%s;">`
htmlTitleEnd = "</h3>"
VolumeMatch = "^第[0-9一二三四五六七八九十零〇百千两 ]+[卷部]"
DefaultMatchTips = "^第[0-9一二三四五六七八九十零〇百千两 ]+[章回节集卷部]|^[Ss]ection.{1,20}$|^[Cc]hapter.{1,20}$|^[Pp]age.{1,20}$|^\\d{1,4}$|^引子$|^楔子$|^章节目录|^章节|^序章"
DefaultMatchTips = "^第[0-9一二三四五六七八九十零〇百千两 ]+[章回节集卷部]|^[Ss]ection.{1,20}$|^[Cc]hapter.{1,20}$|^[Pp]age.{1,20}$|^\\d{1,4}$|^\\d+、|^引子$|^楔子$|^章节目录|^章节|^序章"
cssContent = `
.title {text-align:%s}
.content {
Expand All @@ -85,62 +85,51 @@ const (

func NewBookSimple(filename string) (*Book, error) {
book := Book{
Filename: filename,
Bookname: "",
Match: DefaultMatchTips,
VolumeMatch: VolumeMatch,
Author: "YSTYLE",
UnknowTitle: "章节正文",
Max: 35,
Indent: 2,
Align: GetEnv("KAF_CLI_ALIGN", "center"),
Cover: "cover.png",
Bottom: "1em",
Tips: true,
Lang: GetEnv("KAF_CLI_LANG", "zh"),
Out: "",
Format: GetEnv("KAF_CLI_FORMAT", "all"),
SectionList: nil,
Decoder: nil,
PageStylesFile: "",
Reg: nil,
}
if os.Getenv("KAF_CLI_LANG") != "" {
book.Lang = os.Getenv("KAF_CLI_LANG")
Filename: filename,
}
book.SetDefault()
return &book, nil
}

func NewBookArgs() *Book {
var book Book
flag.StringVar(&book.Filename, "filename", "", "txt 文件名")
flag.StringVar(&book.Author, "author", "YSTYLE", "作者")
flag.StringVar(&book.Bookname, "bookname", "", "书名: 默认为txt文件名")
flag.UintVar(&book.Max, "max", 35, "标题最大字数")
flag.StringVar(&book.Author, "author", "YSTYLE", "作者")
flag.StringVar(&book.Match, "match", "", "匹配标题的正则表达式, 不写可以自动识别, 如果没生成章节就参考教程。例: -match 第.{1,8}章 表示第和章字之间可以有1-8个任意文字")
flag.StringVar(&book.VolumeMatch, "volume-match", VolumeMatch, "卷匹配规则")
flag.StringVar(&book.VolumeMatch, "volume-match", VolumeMatch, "卷匹配规则,设置为false可以禁用卷识别")
flag.StringVar(&book.UnknowTitle, "unknow-title", "章节正文", "未知章节默认名称")
flag.UintVar(&book.Indent, "indent", 2, "段落缩进字数")
flag.StringVar(&book.Align, "align", GetEnv("KAF_CLI_ALIGN", "center"), "标题对齐方式: left、center、righ。环境变量KAF_CLI_ALIGN可修改默认值")
flag.StringVar(&book.Cover, "cover", "cover.png", "封面图片可为: 本地图片, 和orly。 设置为orly时生成orly风格的封面, 需要连接网络。")
flag.StringVar(&book.CoverOrlyColor, "cover-orly-color", "", "orly封面的主题色, 可以为1-16和hex格式的颜色代码, 不填时随机")
flag.IntVar(&book.CoverOrlyIdx, "cover-orly-idx", -1, "orly封面的动物, 可以为0-41, 不填时随机, 具体图案可以查看: https://orly.nanmu.me")
flag.UintVar(&book.Max, "max", 35, "标题最大字数")
flag.UintVar(&book.Indent, "indent", 2, "段落缩进字数")
flag.StringVar(&book.Align, "align", GetEnv("KAF_CLI_ALIGN", "center"), "标题对齐方式: left、center、righ。环境变量KAF_CLI_ALIGN可修改默认值")
flag.StringVar(&book.Bottom, "bottom", "1em", "段落间距(单位可以为em、px)")
flag.StringVar(&book.LineHeight, "line-height", "", "行高(用于设置行间距, 默认为1.5rem)")
flag.StringVar(&book.Font, "font", "", "嵌入字体, 之后epub的正文都将使用该字体")
flag.StringVar(&book.Format, "format", GetEnv("KAF_CLI_FORMAT", "all"), "书籍格式: all、epub、mobi、azw3。环境变量KAF_CLI_FORMAT可修改默认值")
flag.StringVar(&book.Lang, "lang", GetEnv("KAF_CLI_LANG", "zh"), "设置语言: en,de,fr,it,es,zh,ja,pt,ru,nl。环境变量KAF_CLI_LANG可修改默认值")
flag.StringVar(&book.Format, "format", GetEnv("KAF_CLI_FORMAT", "all"), "书籍格式: all、epub、mobi、azw3。环境变量KAF_CLI_FORMAT可修改默认值")
flag.StringVar(&book.Out, "out", "", "输出文件名,不需要包含格式后缀")
flag.BoolVar(&book.Tips, "tips", true, "添加本软件教程")
flag.Parse()
return &book
}

func (book *Book) SetDefault() {
book.Match = defaultString(book.Match, DefaultMatchTips)
book.VolumeMatch = defaultString(book.VolumeMatch, VolumeMatch)
book.Author = defaultString(book.Author, "YSTYLE")
book.UnknowTitle = defaultString(book.UnknowTitle, "章节正文")
book.Max = defalutInt(book.Max, 35)
book.Indent = defalutInt(book.Indent, 2)
book.Align = defaultString(book.Align, GetEnv("KAF_CLI_ALIGN", "center"))
book.Cover = defaultString(book.Cover, "cover.png")
book.Bottom = defaultString(book.Bottom, "1em")
book.Lang = defaultString(book.Lang, GetEnv("KAF_CLI_LANG", "zh"))
book.Format = defaultString(book.Format, GetEnv("KAF_CLI_FORMAT", "all"))
}
func (book *Book) Check(version string) error {
book.version = version
if !strings.HasSuffix(book.Filename, ".txt") {
return errors.New("不是txt文件")
}
if book.Filename == "" {
fmt.Println("错误: 文件名不能为空")
fmt.Println("软件版本: \t", version)
Expand All @@ -153,8 +142,11 @@ func (book *Book) Check(version string) error {
}
os.Exit(0)
}
if !strings.HasSuffix(book.Filename, ".txt") {
return errors.New("不是txt文件")
}
// 通过文件名解析书名
reg, _ := regexp.Compile(`《(.*)》.*作者(.*).txt`)
reg, _ := regexp.Compile(`《(.*)》.*作者[::](.*).txt`)
if reg.MatchString(book.Filename) {
group := reg.FindAllStringSubmatch(book.Filename, -1)
if len(group) == 1 && len(group[0]) >= 3 {
Expand Down Expand Up @@ -236,7 +228,7 @@ func (book *Book) readBuffer(filename string) *bufio.Reader {
}
}

func (book Book) ToString() {
func (book *Book) ToString() {
fmt.Println("转换信息:")
fmt.Println("软件版本:", book.version)
fmt.Println("文件名:\t", book.Filename)
Expand Down Expand Up @@ -319,7 +311,7 @@ func (book *Book) Parse() error {
var sectionList []Section
var volumeSection *Section
for _, section := range contentList {
if book.VolumeReg.MatchString(section.Title) {
if book.VolumeMatch != "false" && book.VolumeReg.MatchString(section.Title) {
if volumeSection != nil {
sectionList = append(sectionList, *volumeSection)
volumeSection = nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ require (

replace golang.org/x/text => github.com/golang/text v0.3.2

go 1.19
go 1.21
36 changes: 36 additions & 0 deletions lib/lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import "C"
import (
"encoding/json"
kafcli "github.com/ystyle/kaf-cli"
)

var (
secret string
measurement string
version string
)

//export KafConvert
func KafConvert(params *C.char) int64 {
var bookArg kafcli.Book
err := json.Unmarshal([]byte(C.GoString(params)), &bookArg)
if err != nil {
return 1
}
bookArg.SetDefault()
if err := bookArg.Check(version); err != nil {
return 2
}
kafcli.Analytics(version, secret, measurement, bookArg.Format)
if err := bookArg.Parse(); err != nil {
return 3
}
bookArg.Convert()
return 0
}

func main() {

}
28 changes: 24 additions & 4 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ func converToMobi(bookname, lang string) {
fmt.Printf("\n检测到Kindle格式转换器: %s,正在把书籍转换成Kindle格式...\n", command)
fmt.Println("转换mobi比较花时间, 大约耗时1-10分钟, 请等待...")
start := time.Now()
err := run(command, "-dont_append_source", "-locale", lang, "-c1", bookname)
if err != nil {
panic(err)
}
run(command, "-dont_append_source", "-locale", lang, "-c1", bookname)
// 计算耗时
end := time.Now().Sub(start)
fmt.Println("转换为mobi格式耗时:", end)
Expand Down Expand Up @@ -178,3 +175,26 @@ func GenCover(title, author, color string, img int) (string, error) {
}
return coverfile, nil
}

type Number interface {
~int | ~uint
}

func defaultString(src, dst string) string {
if src == "" {
return dst
}
return src
}
func defalutInt[T Number](src, dst T) T {
if src == 0 {
return dst
}
return src
}
func defaultBool(src, dst bool) bool {
if src {
return src
}
return dst
}

0 comments on commit fbd8138

Please sign in to comment.