Skip to content

Commit

Permalink
generate thumbail automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
zhnxin committed Nov 5, 2019
0 parents commit 2b4f2db
Show file tree
Hide file tree
Showing 10 changed files with 599 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.mobi
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# TXT2MOBI

[README_ZH_cn.md](./examples/README_ZH_cn.md)

`txt2mobi` is a simple tool for converting txt file to Mobi format for kindle. It's Based on golang package [mobi](https://github.com/766b/mobi/).

`Note`: All testing were done on Kindle App for windows and Kindle Paperwhite (10th Gen)
_PS: Any html element in text file would make effect._

## Build

```
go get -u github.com/zhnxin/txt2mobi
```

## Usage

1. Prepare the cover,thumbnail and txt file

2. Create a config file witch you can refer to [example.toml](./examples/example.toml)

3. Run the command:
```sh
[linux]$./txt2mobi -f example.toml [- o output_file_name.mobi] [-p]
#for Windows
[windows]$ txt2mobi.exe -f example.toml [- o output_file_name.mobi] [-p]
```

## termimal parameter
require:
- `-config`: config file

`or`

require:

- `-f`: input file
- `-title`: book title
- `-author`: book author
- `-cover`: book cover and thumbnail
- `-chapter`: chater title regexp pattern

options:

- `-subchapter`: subchapter title regexp pattern
- `compress`: is to compress the result
- `encoding:`: use gb18030 as default


other options:

- `-o`: output file name
- `-p`: is to use '\<p\>\</p\>' to pack every paragrahes.
- `-escape`: to disable html escape

## config file

```toml
title="Example"
author="zhengxin"
file="example.txt"
cover="cover_example.jpg"
chapter="^Chapter\\.\\d+.*$"
subchapter='^Chapter\\.\\d+\-\\d+ .*$'
compress=false
//default cover size
//cover_width=860
//cover_hight=1200
```

_Note:_
- `title`: If the output_filename is not given, the `title` would be used as default.
- `chapter`: A regexp pattern to determin the chapter line title.
- `compress`: If it's true, the output would be compressed witch make the file smaller but take mush time to finish.
- `encoding`: Options. Use `utf-8` as default. Also support `gb18030` and `gbk`.

## Need GUI?

This tool is ready simple but modifis all need for me. Email me if you really desire it.

## About BLANK

The prefixed blank would be ignore because of html decoding.
104 changes: 104 additions & 0 deletions chapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"bytes"
"html"

"github.com/zhnxin/mobi"
)

var (
isEscape = true
isParagraph = false
LineFeed = []byte("<br/>")
PStart = []byte("<p>")
PEnd = []byte("</p>")
Blank = []byte{}
)

func SetIsEscape(flag bool) {
isEscape = flag
}
func SetIsParagraph(flag bool) {
isParagraph = flag
}

type chapterContent struct {
Title string
Content []byte
}

func (c *chapterContent) Append(content []byte) {
if !isEscape {
content = []byte(html.EscapeString(string(content)))
}
if isParagraph {
if len(content) > 1 {
c.Content = append(c.Content, bytes.Join([][]byte{PStart, content, PEnd}, Blank)...)
} else {
c.Content = append(c.Content, LineFeed...)
}
} else {
c.Content = append(c.Content, LineFeed...)
if len(content) > 1 {
c.Content = append(c.Content, content...)
}
}

}

func (c *chapterContent) SetTitle(title string) {
c.Title = title
}

func (c *chapterContent) Restore(title string) {
c.Title = title
c.Content = make([]byte, 0)
}

type Chapter struct {
content chapterContent
subChapterList []chapterContent
subLength uint
}

func NewChapter(title string) Chapter {
return Chapter{
content: chapterContent{
Title: title,
Content: []byte{},
},
subChapterList: make([]chapterContent, 0),
}
}

func (c *Chapter) Restore(title string) {
c.subLength = 0
c.content.Title = title
c.content.Content = make([]byte, 0)
c.subChapterList = make([]chapterContent, 0)
}

func (c *Chapter) AddSubChapter(title string) {
subChapter := chapterContent{
Title: title,
Content: make([]byte, 0),
}
c.subChapterList = append(c.subChapterList, subChapter)
c.subLength += 1
}

func (c *Chapter) Append(content []byte) {
if c.subLength < 1 {
c.content.Append(content)
} else {
c.subChapterList[c.subLength-1].Append(content)
}
}

func (c *Chapter) Flush(mobiWriter mobi.Builder) {
chapter := mobiWriter.NewChapter(c.content.Title, c.content.Content)
for _, content := range c.subChapterList {
chapter.AddSubChapter(content.Title, content.Content)
}
}
73 changes: 73 additions & 0 deletions examples/README_ZH_cn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# TXT2Mobi

一个基于golang package [mobi](https://github.com/766b/mobi/)的简单命令行工具。能够将txt文本文件转换为Mobi格式

`注意`: 所有测试都是使用KindleForPC(windows)和Paperwhite (10th Gen)

_ps: 文档中的所有html元素都会生效_

## 编译


```
go get -u github.com/zhnxin/txt2mobi
```

## Usage

1. 准备好封面(.jpg),文本文档(.txt)
2. 根据[示例](./examples/example.toml)创建配置文件(.toml)
3. 运行
```sh
[linux]$./txt2mobi -f example.toml [- o output_file_name.mobi] [-p]
[windows]$ txt2mobi.exe -f example.toml [- o output_file_name.mobi] [-p]
```

## termimal parameter
require:
- `-config`: config file

or

require:
- `-f`: 文档
- `-title`: 书名
- `-author`: 作者
- `-cover`: 封面及缩略图
- `-chapter`: 一级标题的正则表达式
options:
- `-subchapter`: 二级标题的正则表达式,默认为空
- `compress`: 是否压缩,默认不使用
- `encoding`: 文件编码,默认为gb18030


other options:

- `-o`: 输出文件名
- `-p`: 是否使用 '\<p\>\</p\>' 装饰段落.
- `-escape`: 关闭html转义

## 配置文件
```toml
title="Example"
author="zhengxin"
file="example.txt"
cover="cover_example.jpg"
chapter="^第.*卷 .*$"
subchapter="^第\\d+章 .*$"
compress=false
```

_Note_:
- `title` : 如果可选的输出文件名没有提供,就会使用title作为默认文件名
- `chapter`: 正则表达式,用于识别章节
- `compress`: 被置为true时,会压缩生成结果,但回消耗更多的运行时间
- `encoding`: 可选参数,默认值为`utf-8`,也支持`gb18030``gbk`

## 需要GUI?

这个命令行工具虽然简单(简陋?),但已经能够满足我的所有需求。如果你非常有需求,可以email联系我,让我有足够的¥动力¥来完成它。

## 关于开头空格

由于html内部解析,英文的段首空格将被忽略。
Binary file added examples/cover_example.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions examples/example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
title="example"
author="zhengxin"
file="examples/example.txt"
cover="examples/cover_example.jpg"
chapter="^第.*卷$"
subchapter="^第\\d+章 .*$"
compress=false
encoding="GB18030"
lang="zh-CN"
//default cover size
//cover_width=860
//cover_hight=1200
26 changes: 26 additions & 0 deletions examples/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
���ݼ�飺
��������һ�ɺ��ԣ�Ҳ�Ǽ����ұ�д������ߵ���·���̡�

��һ��

��1�� ����Ҫ�µĹ���

�������������ҵ��Ľ�txt�ĵ�ת��ΪMobi��ʽ��;�����࣬��������ʹ��Calibre��kindlegen�����������߶������Ե����⡪����Ҫ��תΪhtml���ٶ���������CPU��˳��һ�ᣬͨ����Calibre�Ĵ�����Է��֣�mobiģ��Ҳ����kindlegen��������Mobi���ɹ�������
�����ĵ�ת����ҪԤ�����������½ڱ���ij�<h1></h1>��ǩ��������Ҫ��<p></p>������һ��8M���ĵ��ڸij�html����������Ҫ5���ӽ���ת������ֻ����Ҫ�򵥵���ʽת��������������ʵ����̫�������Խ����ˡ����Ǿ������˱�дһ�����ߵ���ͷ��

��2�� �Ұ�rust

����û�����Ұ�rust ��rust�����㹻�����ҵ�Ҫ����Ȼ�һ�C/C++����ʵ����������������rustû���ֳɵ�Mobi�������⣬����ֻ��Ѱ���������ԡ�
����ʲô������˵����Mobi�ٷ��ĵ��Լ�д���ߺߣ������ǵ����Dz���������ӵģ���

��3�� golang��������

��������mobi�Ŀ⣬ʵ�����١����ҵ�C���԰汾�ģ���ԭ���ᡣpython��û���������golang��ֻ�ҵ�һ�����Ǿ������ˡ�

�ڶ���

��4�� ���ڶ���

�������俪ͷ�ո�����⣬���Һ�Ϊ�ѡ�������ֱ�����ص�С˵�ļ������俪ͷ�����Դ����Ŀո�ġ�ʹ��<p>��ǩ���ҵ�kindle�ͻ��������������ڿ����������Ҫ��������<p>��ǩ�����Լ���'-p'������
����
����Mobi��ʽ֧��html��ʽ��������txt�ļ�תΪmobi��ʽ����ʽҲӦ�ñ���ԭ�������������á����仰˵����ת����ʽ�����лض������htmlת�塣�����Ҫ�ر�ת�壬����ʹ��'-escape'������
13 changes: 13 additions & 0 deletions img_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"testing"
)

func TestGImg(t *testing.T) {
config := Config{CoverHight: 1200, CoverWidth: 860}
_, _, err := config.generateCover("examples/cover_example.jpg")
if err != nil {
t.Fatal(err)
}
}
Loading

0 comments on commit 2b4f2db

Please sign in to comment.