Skip to content

Commit

Permalink
feat: 更新格式指定的方式: all、epub、mobi、azw3
Browse files Browse the repository at this point in the history
  • Loading branch information
ystyle committed Aug 7, 2021
1 parent a94dd12 commit 0544218
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Usage of kaf-cli.exe:
-filename string
txt 文件名
-format string
书籍格式: both、epub、mobi (default "both")
书籍格式: all、epub、mobi、azw3 (default "all")
-indent uint
段落缩进字数 (default 2)
-lang string
Expand Down
71 changes: 45 additions & 26 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,22 @@ type Section struct {
}

var (
filename string // 目录
bookname string // 书名
match string // 正则
author string // 作者
max uint // 标题最大字数
indent uint // 段落缩进字段
align string // 标题对齐方式
cover string // 封面图片
bottom string // 段阿落间距
Tips bool // 是否添加教程文本
lang string // 设置语言
out string // 输出文件名
format string // 书籍格式
decoder *encoding.Decoder
pageStylesFile string
reg *regexp.Regexp
installKindlegen bool
filename string // 目录
bookname string // 书名
match string // 正则
author string // 作者
max uint // 标题最大字数
indent uint // 段落缩进字段
align string // 标题对齐方式
cover string // 封面图片
bottom string // 段阿落间距
Tips bool // 是否添加教程文本
lang string // 设置语言
out string // 输出文件名
format string // 书籍格式
decoder *encoding.Decoder
pageStylesFile string
reg *regexp.Regexp
)

const (
Expand Down Expand Up @@ -97,14 +96,20 @@ func init() {
flag.StringVar(&align, "align", "center", "标题对齐方式: left、center、righ")
flag.StringVar(&cover, "cover", "cover.png", "封面图片")
flag.StringVar(&bottom, "bottom", "1em", "段落间距(单位可以为em、px)")
flag.StringVar(&format, "format", "both", "书籍格式: both、epub、mobi")
flag.StringVar(&format, "format", "all", "书籍格式: all、epub、mobi、azw3")
flag.StringVar(&lang, "lang", "zh", "设置语言: en,de,fr,it,es,zh,ja,pt,ru,nl。 支持使用环境变量KAF-CLI-LANG设置")
flag.BoolVar(&Tips, "tips", true, "添加本软件教程")
flag.Parse()
}

if filename == "" {
fmt.Println("文件名不能为空")
fmt.Println("简洁模式: 直接把文件播放到kaf-cli上")
fmt.Println("命令行简单模式: kaf-cli ebook.txt")
fmt.Println("查看命令行参数: kaf-cli -h")
fmt.Println("以下为kaf-cli的全部参数")
flag.PrintDefaults()
time.Sleep(time.Second * 10)
os.Exit(1)
}

Expand Down Expand Up @@ -266,10 +271,21 @@ func Convert() {
fmt.Println()

// 判断要生成的格式
var isEpub, isMobi = true, true
if format == "epub" {
isMobi = false
var isEpub, isMobi, isAzw3 bool
switch format {
case "epub":
isEpub = true
case "mobi":
isEpub = true
isMobi = true
case "azw3":
isAzw3 = true
default:
isEpub = true
isMobi = true
isAzw3 = true
}

hasKinldegen := lookKindlegen()
if isMobi && hasKinldegen == "" {
isEpub = false
Expand All @@ -279,10 +295,13 @@ func Convert() {
buildEpub(sectionList)
fmt.Println()
}
// 生成kindle格式
if isMobi {
// 生成azw3格式
if isAzw3 {
// 生成kindle格式
buildMobi(sectionList)
buildAzw3(sectionList)
}
// 生成mobi格式
if isMobi {
if hasKinldegen != "" {
converToMobi(fmt.Sprintf("%s.epub", out))
}
Expand All @@ -300,7 +319,7 @@ func wrapMobiTitle(title, content string) string {
return buff.String()
}

func buildMobi(sectionList []Section) {
func buildAzw3(sectionList []Section) {
fmt.Println("使用第三方库生成azw3, 不保证所有样式都能正常显示")
fmt.Println("正在生成azw3...")
start := time.Now()
Expand Down Expand Up @@ -343,7 +362,7 @@ func buildMobi(sectionList []Section) {
if err != nil {
panic(err)
}
fmt.Println("生成Mobi电子书耗时:", time.Now().Sub(start))
fmt.Println("生成azw3电子书耗时:", time.Now().Sub(start))
}

func wrapEpubTitle(title, content string) string {
Expand Down
2 changes: 1 addition & 1 deletion tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func converToMobi(bookname string) {
run(command, "-dont_append_source", "-locale", lang, "-c1", bookname)
// 计算耗时
end := time.Now().Sub(start)
fmt.Println("转换为Kindle格式耗时:", end)
fmt.Println("转换为mobi格式耗时:", end)
}

func isExists(path string) (bool, error) {
Expand Down

0 comments on commit 0544218

Please sign in to comment.