Skip to content

Commit

Permalink
解决翻页自定义字体失效问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ystyle committed Mar 29, 2020
1 parent ad6b7c1 commit a3e36d5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
9 changes: 9 additions & 0 deletions model/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
uuid "github.com/satori/go.uuid"
"github.com/ystyle/kas/util/array"
"github.com/ystyle/kas/util/config"
"os"
"path"
)

Expand All @@ -22,6 +23,7 @@ type TextInfo struct {
Format []string // 格式
CacheEpub string // epub 缓存目录
CacheMobi string // mobi 缓存目录
CacheCSS string // CSS保存目录
StoreEpub string // epub保存目录
StoreMobi string // mobi保存目录
}
Expand Down Expand Up @@ -55,6 +57,7 @@ func (text *TextInfo) SetDefault() {
if !array.IncludesString(Aligns, text.Align) {
text.Align = "center"
}
text.CacheCSS = path.Join(config.CacheDir, "text", fmt.Sprintf("%s.css", text.ID))
text.CacheEpub = path.Join(config.CacheDir, "text", fmt.Sprintf("%s.epub", text.ID))
text.CacheMobi = path.Join(config.CacheDir, "text", fmt.Sprintf("%s.mobi", text.ID))
text.StoreEpub = path.Join(config.StoreDir, "text", fmt.Sprintf("[epub]%s.zip", text.ID))
Expand All @@ -67,3 +70,9 @@ func (text *TextInfo) AddSection(title, content string) {
Content: content,
})
}

func (text *TextInfo) ClearCache() {
os.Remove(text.CacheEpub)
os.Remove(text.CacheMobi)
os.Remove(text.CacheCSS)
}
61 changes: 44 additions & 17 deletions services/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ystyle/kas/core"
"github.com/ystyle/kas/model"
"github.com/ystyle/kas/util/array"
"github.com/ystyle/kas/util/config"
"github.com/ystyle/kas/util/env"
"github.com/ystyle/kas/util/file"
"github.com/ystyle/kas/util/kindlegen"
Expand All @@ -25,12 +26,16 @@ import (
)

const (
htmlPStart = `<p style="text-indent: %dem">`
htmlPStart = `<p class="content">`
htmlPEnd = "</p>"
htmlTitleStart = `<h3 style="text-align:%s">`
htmlTitleStart = `<h3 class="title">`
htmlTitleEnd = "</h3>"
Tutorial = `本书由KAF生成: <br/>
制作教程: <a href='https://ystyle.top/2019/12/31/txt-converto-epub-and-mobi/'>https://ystyle.top/2019/12/31/txt-converto-epub-and-mobi</a>
`
cssContent = `
.title {text-align:%s}
.content {text-indent: %dem}
`
)

Expand Down Expand Up @@ -63,13 +68,26 @@ func TextUpload(client *core.WsClient, message core.Message) {
buff.Write(out)
}

// 写入样式
bookcss := fmt.Sprintf(cssContent, bookinfo.Align, bookinfo.Indent)
err = ioutil.WriteFile(bookinfo.CacheCSS, []byte(bookcss), config.Perm)
if err != nil {
panic(fmt.Sprintf("无法写入样式文件: %s", err))
}

// 转换小说
var title string
var content bytes.Buffer

for {
line, err := buff.ReadString('\n')
if err != nil {
if err == io.EOF {
if line != "" {
if line = strings.TrimSpace(line); line != "" {
AddPart(&content, line)
}
}
bookinfo.AddSection(title, content.String())
break
}
Expand All @@ -85,25 +103,17 @@ func TextUpload(client *core.WsClient, message core.Message) {
if utf8.RuneCountInString(line) <= bookinfo.MaxLen && reg.MatchString(line) {
if title == "" {
title = "说明"
AddPart(&content, Tutorial)
}
bookinfo.AddSection(title, content.String())
title = line
content.Reset()
content.WriteString(fmt.Sprintf(htmlTitleStart, bookinfo.Align))
content.WriteString(htmlTitleStart)
content.WriteString(title)
content.WriteString(htmlTitleEnd)
continue
}
if strings.HasSuffix(line, "==") ||
strings.HasSuffix(line, "**") ||
strings.HasSuffix(line, "--") ||
strings.HasSuffix(line, "//") {
content.WriteString(line)
continue
}
content.WriteString(fmt.Sprintf(htmlPStart, bookinfo.Indent))
content.WriteString(line)
content.WriteString(htmlPEnd)
AddPart(&content, line)
}
// 没识别到章节又没识别到 EOF 时,把所有的内容写到最后一章
if content.Len() != 0 {
Expand All @@ -118,6 +128,19 @@ func TextUpload(client *core.WsClient, message core.Message) {
client.WsSend <- core.NewMessage("text:uploaded", bookinfo.ID)
}

func AddPart(buff *bytes.Buffer, content string) {
if strings.HasSuffix(content, "==") ||
strings.HasSuffix(content, "**") ||
strings.HasSuffix(content, "--") ||
strings.HasSuffix(content, "//") {
buff.WriteString(content)
return
}
buff.WriteString(htmlPStart)
buff.WriteString(content)
buff.WriteString(htmlPEnd)
}

func TextPreView(client *core.WsClient, message core.Message) {
id := message.GetString()
if _, ok := client.Caches[id]; !ok {
Expand Down Expand Up @@ -146,11 +169,16 @@ func TextConvert(client *core.WsClient, message core.Message) {
client.WsSend <- core.NewMessage("info", "正在生成生成epub文件...")
e := epub.NewEpub(book.BookName)
e.SetAuthor(book.Author)
css, err := e.AddCSS(book.CacheCSS, "")
if err != nil {
client.WsSend <- core.NewMessage("Error", "写入CSS文件失败")
return
}
for _, section := range book.Sections {
e.AddSection(section.Content, section.Title, "", "")
e.AddSection(section.Content, section.Title, "", css)
}
file.CheckDir(path.Dir(book.CacheEpub))
err := e.Write(book.CacheEpub)
err = e.Write(book.CacheEpub)
if err != nil {
client.WsSend <- core.NewMessage("Error", "生成epub文件错误")
return
Expand Down Expand Up @@ -183,8 +211,7 @@ func TextConvert(client *core.WsClient, message core.Message) {
}
// 下载mobi文件
bookDownload(client, book, "mobi")
os.Remove(book.CacheEpub)
os.Remove(book.CacheMobi)
book.ClearCache()
if env.GetBool("DISABLED_STORAGE", false) {
os.Remove(book.StoreEpub)
os.Remove(book.StoreMobi)
Expand Down
2 changes: 2 additions & 0 deletions util/kindlegen/kindlegen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kindlegen

import (
"github.com/labstack/gommon/log"
"github.com/ystyle/kas/util/file"
"os"
"os/exec"
Expand All @@ -16,6 +17,7 @@ func Run(command string, args ...string) error {
}

func Conver(source string, bookname string, onlyKF8 bool) error {
log.Info("转换文件名: ", source)
command := "kindlegen"
if runtime.GOOS == "windows" {
command = "kindlegen.exe"
Expand Down

0 comments on commit a3e36d5

Please sign in to comment.