Skip to content

Commit

Permalink
fix: 书籍名称会为空的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
ystyle committed Jan 17, 2022
1 parent 4723a15 commit 17b1221
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
45 changes: 19 additions & 26 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,27 @@ jobs:
hash=`git rev-list --tags --max-count=1`
version=`git describe --tags $hash`
flag="-s -w -X main.secret=${{ secrets.API_SECRET }} -X main.measurement=${{ secrets.MEASUREMENT }} -X main.version=$version"
GOOS=windows GOARCH=amd64 go build -ldflags "$flag" -o kaf-cli.exe cmd/cli.go
GOOS=windows GOARCH=386 go build -ldflags "$flag" -o kaf-cli_32.exe cmd/cli.go
GOOS=linux GOARCH=amd64 go build -ldflags "$flag" -o kaf-cli-linux cmd/cli.go
GOOS=darwin GOARCH=amd64 go build -ldflags "$flag" -o kaf-cli-darwin cmd/cli.go
GOOS=windows GOARCH=amd64 go build -ldflags "$flag" -o build/windows-amd64/kaf-cli.exe cmd/cli.go
GOOS=windows GOARCH=386 go build -ldflags "$flag" -o build/windows-386/kaf-cli.exe cmd/cli.go
GOOS=linux GOARCH=amd64 go build -ldflags "$flag" -o build/linux-amd64/kaf-cli cmd/cli.go
GOOS=linux GOARCH=arm64 go build -ldflags "$flag" -o build/linux-arm64/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
- name: compression zip
run: |
zip -j kaf-cli-windows.zip windows/kindlegen.exe kaf-cli.exe kaf-cli_32.exe
zip -j kaf-cli-darwin.zip darwin/kindlegen kaf-cli-darwin
zip kaf-cli-linux.zip kaf-cli-linux
- name: Upload windows to release
uses: JasonEtco/upload-to-release@master
with:
args: kaf-cli-windows.zip application/octet-stream
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Upload linux to release
uses: JasonEtco/upload-to-release@master
with:
args: kaf-cli-linux.zip application/octet-stream
hash=`git rev-list --tags --max-count=1`
version=`git describe --tags $hash`
zip -j kaf-cli_${version}_windows_amd64.zip build/windows-amd64/kaf-cli.exe windows/kindlegen.exe
zip -j kaf-cli_${version}_windows_386.zip build/windows-386/kaf-cli.exe windows/kindlegen.exe
zip -j kaf-cli_${version}_darwin_amd64.zip build/darwin-amd64/kaf-cli darwin/kindlegen
zip -j kaf-cli_${version}_darwin_arm64.zip build/darwin-arm64/kaf-cli darwin/kindlegen
zip -j kaf-cli_${version}_linux_amd64.zip build/linux-amd64/kaf-cli
zip -j kaf-cli_${version}_linux_arm64.zip build/linux-arm64/kaf-cli
- name: Upload release binaries
uses: alexellis/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Upload mac to release
uses: JasonEtco/upload-to-release@master
GITHUB_TOKEN: ${{ github.token }}
with:
args: kaf-cli-darwin.zip application/octet-stream
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
asset_paths: '["./kaf-cli_*.zip"]'
12 changes: 8 additions & 4 deletions cmd/cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"github.com/ystyle/kaf-cli"
"os"
"strings"
Expand All @@ -18,17 +19,20 @@ func main() {
if len(os.Args) == 2 && strings.HasSuffix(os.Args[1], ".txt") {
book, err = kafcli.NewBookSimple(os.Args[1])
if err != nil {
panic(err)
fmt.Printf("错误: %s\n", err.Error())
os.Exit(3)
}
} else {
book = kafcli.NewBookArgs()
}
if err := book.Check(); err != nil {
panic(err)
if err := book.Check(version); err != nil {
fmt.Printf("错误: %s\n", err.Error())
os.Exit(1)
}
book.ToString()
if err := book.Parse(); err != nil {
panic(err)
fmt.Printf("错误: %s\n", err.Error())
os.Exit(2)
}
book.Convert()
kafcli.Analytics(version, secret, measurement)
Expand Down
28 changes: 20 additions & 8 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
Expand All @@ -40,6 +41,7 @@ type Book struct {
Decoder *encoding.Decoder
PageStylesFile string
Reg *regexp.Regexp
version string
}

type Section struct {
Expand Down Expand Up @@ -72,9 +74,6 @@ const (
)

func NewBookSimple(filename string) (*Book, error) {
if !strings.HasSuffix(filename, ".txt") {
return nil, errors.New("不是txt文件")
}
book := Book{
Filename: filename,
Bookname: "",
Expand Down Expand Up @@ -120,9 +119,14 @@ func NewBookArgs() *Book {
return &book
}

func (book *Book) Check() error {
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)
fmt.Println("简洁模式: \t把文件拖放到kaf-cli上")
fmt.Println("命令行简单模式: kaf-cli ebook.txt")
fmt.Println("\n以下为kaf-cli的全部参数")
Expand All @@ -132,22 +136,29 @@ func (book *Book) Check() error {
}
os.Exit(0)
}
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 {
book.Bookname = group[0][1]
book.Author = group[0][2]
if book.Bookname == "" {
book.Bookname = group[0][1]
}
if book.Author == "" || book.Author == "YSTYLE" {
book.Author = group[0][2]
}
}
}
if book.Bookname == "" {
book.Bookname = strings.Split(filepath.Base(book.Filename), ".")[0]
}
if book.Out == "" {
book.Out = book.Bookname
}
book.Lang = parseLang(book.Lang)
if exists, _ := isExists(book.Cover); !exists {
book.Cover = ""
}

// 编译正则表达式
if book.Match == "" {
book.Match = DefaultMatchTips
Expand Down Expand Up @@ -193,6 +204,7 @@ func (book *Book) readBuffer(filename string) *bufio.Reader {

func (book Book) ToString() {
fmt.Println("转换信息:")
fmt.Println("软件版本:", book.version)
fmt.Println("文件名:\t", book.Filename)
fmt.Println("书籍书名:", book.Bookname)
fmt.Println("书籍作者:", book.Author)
Expand Down

0 comments on commit 17b1221

Please sign in to comment.