Skip to content

Commit

Permalink
Merge branch 'ystyle:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
dizicheng authored Apr 10, 2022
2 parents a5afc2d + d385c3f commit b5ac072
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 29 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.13
go-version: ^1.18
id: go

- name: Check out code into the Go module directory
Expand All @@ -32,6 +32,7 @@ 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"
go mod tidy
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
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
*.txt
*.exe
*.tmp
kaf-cli-*
*.zio
kaf-cli-*
samples
3 changes: 2 additions & 1 deletion analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"
)

func Analytics(version, secret, measurement string) {
func Analytics(version, secret, measurement, format string) {
if secret == "" || measurement == "" {
return
}
Expand All @@ -23,6 +23,7 @@ func Analytics(version, secret, measurement string) {
Params: map[string]interface{}{
"os": runtime.GOOS,
"arch": runtime.GOARCH,
"format": format,
"version": version,
},
},
Expand Down
6 changes: 3 additions & 3 deletions azw3.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (convert Azw3Converter) Build(book Book) error {
fmt.Println("使用第三方库生成azw3, 不保证所有样式都能正常显示")
fmt.Println("正在生成azw3...")
start := time.Now()
chunks := SectionSliceChunk(book.SectionList, 2700)
chunks := SectionSliceChunk(book.SectionList, 2000)
for i, chunk := range chunks {
index := i + 1
title := fmt.Sprintf("%s_%d", book.Bookname, index)
Expand All @@ -38,7 +38,7 @@ func (convert Azw3Converter) Build(book Book) error {
for _, section := range chunk {
ch := mobi.Chapter{
Title: section.Title,
Chunks: mobi.Chunks(convert.wrapMobiTitle(section.Title, section.Content, book.Align)),
Chunks: mobi.Chunks(convert.wrapTitle(section.Title, section.Content, book.Align)),
}
mb.Chapters = append(mb.Chapters, ch)
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func (convert Azw3Converter) Build(book Book) error {
return nil
}

func (convert Azw3Converter) wrapMobiTitle(title, content, align string) string {
func (convert Azw3Converter) wrapTitle(title, content, align string) string {
var buff bytes.Buffer
buff.WriteString(fmt.Sprintf(mobiTtmlTitleStart, align))
buff.WriteString(title)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func main() {
fmt.Printf("错误: %s\n", err.Error())
os.Exit(1)
}
kafcli.Analytics(version, secret, measurement, book.Format)
book.ToString()
if err := book.Parse(); err != nil {
fmt.Printf("错误: %s\n", err.Error())
os.Exit(2)
}
book.Convert()
kafcli.Analytics(version, secret, measurement)
}
2 changes: 1 addition & 1 deletion convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const (
htmlTitleStart = `<h3 class="title">`
mobiTtmlTitleStart = `<h3 style="text-align:%s;">`
htmlTitleEnd = "</h3>"
DefaultMatchTips = "^.{0,8}(第.{1,20}[章节卷]|[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}$|^引子$|^楔子$|^章节目录|^章节|^序章"
cssContent = `
.title {text-align:%s}
.content {
Expand Down
4 changes: 2 additions & 2 deletions epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type EpubConverter struct{}

func (convert EpubConverter) wrapEpubTitle(title, content string) string {
func (convert EpubConverter) wrapTitle(title, content string) string {
var buff bytes.Buffer
buff.WriteString(htmlTitleStart)
buff.WriteString(title)
Expand Down Expand Up @@ -56,7 +56,7 @@ func (convert EpubConverter) Build(book Book) error {
}

for _, section := range book.SectionList {
e.AddSection(convert.wrapEpubTitle(section.Title, section.Content), section.Title, "", css)
e.AddSection(convert.wrapTitle(section.Title, section.Content), section.Title, "", css)
}

// Write the EPUB
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module github.com/ystyle/kaf-cli
require (
github.com/766b/mobi v0.0.0-20200528201125-c87aa9e3c890
github.com/bmaupin/go-epub v0.11.0
github.com/leotaku/mobi v0.0.0-20211229161609-765f28ff1457
github.com/leotaku/mobi v0.0.0-20220405163106-82e29bde7964
github.com/ystyle/google-analytics v0.0.0-20210425064301-a7f754dd0649
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125
golang.org/x/text v0.3.7
)

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

go 1.16
go 1.18
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/gofrs/uuid v3.1.0+incompatible h1:q2rtkjaKT4YEr6E1kamy0Ha4RtepWlQBedy
github.com/gofrs/uuid v3.1.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang/text v0.3.2 h1:vDAeTQXl8YUdGoj2vMsMnzHi1xMJJ9S7iwnTBFL/pkA=
github.com/golang/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
github.com/leotaku/mobi v0.0.0-20211229161609-765f28ff1457 h1:kFV3RufaLq26MT6kPWUzUK+7K27kaBLaBZyZIzG50j8=
github.com/leotaku/mobi v0.0.0-20211229161609-765f28ff1457/go.mod h1:aPjSURhp6tj0b/dLHyjzeygcitkmvO2/SO368XiPtrs=
github.com/leotaku/mobi v0.0.0-20220405163106-82e29bde7964 h1:h7CxQNRMxzHmVvDV1TPudN9XUF0YxYnh966lQ3kqEoo=
github.com/leotaku/mobi v0.0.0-20220405163106-82e29bde7964/go.mod h1:aPjSURhp6tj0b/dLHyjzeygcitkmvO2/SO368XiPtrs=
github.com/vincent-petithory/dataurl v0.0.0-20191104211930-d1553a71de50 h1:uxE3GYdXIOfhMv3unJKETJEhw78gvzuQqRX/rVirc2A=
github.com/vincent-petithory/dataurl v0.0.0-20191104211930-d1553a71de50/go.mod h1:FHafX5vmDzyP+1CQATJn7WFKc9CvnvxyvZy6I1MrG/U=
github.com/ystyle/google-analytics v0.0.0-20210425064301-a7f754dd0649 h1:2uiow2Fw91jCIiYwMKvPF/lUMMdtVjgVpwqTcbC0fPU=
Expand Down
16 changes: 10 additions & 6 deletions mobil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ package kafcli

import (
"fmt"
mobi2 "github.com/766b/mobi"
"github.com/766b/mobi"
"time"
)

type MobiConverter struct{}

func (convert MobiConverter) Build(book Book) error {
m, err := mobi2.NewWriter(fmt.Sprintf("%s.mobi", book.Out))
fmt.Println("使用第三方库生成mobi, 不保证所有样式都能正常显示")
fmt.Println("正在生成mobi...")
start := time.Now()
m, err := mobi.NewWriter(fmt.Sprintf("%s.mobi", book.Out))
if err != nil {
panic(err)
}
m.Title(book.Bookname)
m.Compression(mobi2.CompressionNone)
m.Compression(mobi.CompressionNone)
if book.Cover != "" {
m.AddCover(book.Cover, book.Cover)
}
m.NewExthRecord(mobi2.EXTH_DOCTYPE, "EBOK")
m.NewExthRecord(mobi2.EXTH_AUTHOR, book.Author)

m.NewExthRecord(mobi.EXTH_DOCTYPE, "EBOK")
m.NewExthRecord(mobi.EXTH_AUTHOR, book.Author)
for _, section := range book.SectionList {
m.NewChapter(section.Title, []byte(section.Content))
}
m.Write()
fmt.Println("生成mobi电子书耗时:", time.Now().Sub(start))
return nil
}
17 changes: 8 additions & 9 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/exec"
"path"
"runtime"
"strings"
"time"
)
Expand All @@ -31,15 +30,15 @@ func run(command string, args ...string) error {

func lookKindlegen() string {
command := "kindlegen"
if runtime.GOOS == "windows" {
command = "kindlegen.exe"
}
kindlegen, _ := exec.LookPath(command)
if kindlegen == "" {
currentDir := path.Dir(os.Args[0])
kindlegen = path.Join(currentDir, command)
kindlegen, err := exec.LookPath(command)
if err != nil {
currentDir, err := os.Executable()
if err != nil {
return ""
}
kindlegen = path.Join(path.Dir(currentDir), command)
if exist, _ := isExists(kindlegen); !exist {
return kindlegen
return ""
}
}
return kindlegen
Expand Down
25 changes: 25 additions & 0 deletions tools_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package kafcli

import (
"fmt"
"os"
"os/exec"
"testing"
)

func TestLook(t *testing.T) {
kindlegen, err := exec.LookPath("kindlegen")
if err != nil {
t.Fatal(err)
}
fmt.Println(kindlegen)
}

func TestExe(t *testing.T) {
fmt.Println(os.Args)
path, err := os.Executable()
if err != nil {
t.Fatal(err)
}
fmt.Println(path)
}

0 comments on commit b5ac072

Please sign in to comment.