mirror of
https://codeberg.org/scip/epuppy.git
synced 2026-02-04 17:50:58 +01:00
Compare commits
8 Commits
github
...
fix-mobi-c
| Author | SHA1 | Date | |
|---|---|---|---|
| ecbfba8809 | |||
| 372a7b1b00 | |||
| 02c99da8e9 | |||
| 4ca12b907b | |||
| 807a2712e5 | |||
| 0d80f0ef42 | |||
| 120b88803c | |||
| fc9ff4a23f |
30
README.md
30
README.md
@@ -1,5 +1,5 @@
|
|||||||
[](https://ci.codeberg.org/repos/15473)
|
[](https://ci.codeberg.org/repos/15473)
|
||||||
[](https://codeberg.org/scip/epuppy/raw/branch/master/LICENSE)
|
[](https://codeberg.org/scip/epuppy/raw/branch/main/LICENSE)
|
||||||
[](https://goreportcard.com/report/codeberg.org/scip/epuppy)
|
[](https://goreportcard.com/report/codeberg.org/scip/epuppy)
|
||||||
|
|
||||||
|
|
||||||
@@ -20,16 +20,20 @@ long run.
|
|||||||
|
|
||||||
## Screenshots
|
## Screenshots
|
||||||
|
|
||||||
- Viewing an ebook in dark mode
|
### Viewing an ebook in dark mode
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
- Viewing an ebook in light mode
|
### Viewing an ebook in light mode
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
- You can interactively adjust text width
|
### You can interactively adjust text width
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
- Showing the help
|
### Showing the help
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -42,7 +46,7 @@ progress.
|
|||||||
Sometimes you may be unhappy with the colors. Depending on your
|
Sometimes you may be unhappy with the colors. Depending on your
|
||||||
terminal style you can enable dark mode with `-D`, light mode is the
|
terminal style you can enable dark mode with `-D`, light mode is the
|
||||||
default. You can also configure custom colors in a config file in
|
default. You can also configure custom colors in a config file in
|
||||||
`$HOME/.config/epuppy/confit.toml`:
|
`$HOME/.config/epuppy/config.toml`:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
# color setting for dark mode
|
# color setting for dark mode
|
||||||
@@ -92,6 +96,16 @@ Options:
|
|||||||
-v --version show program version
|
-v --version show program version
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Reading mobi files
|
||||||
|
|
||||||
|
`epuppy` doesn't support mobi files, but you can install
|
||||||
|
[mobitool](https://github.com/bfabiszewski/libmobi/) and use it to
|
||||||
|
convert mobi files to epub. The ubuntu package is `libmobi-tools`. To convert, execute:
|
||||||
|
|
||||||
|
```default
|
||||||
|
mobitool -e somebook.epub
|
||||||
|
```
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
The tool does not have any dependencies. Just download the binary for
|
The tool does not have any dependencies. Just download the binary for
|
||||||
@@ -104,7 +118,7 @@ You can use [stew](https://github.com/marwanhawari/stew) to install epuppy:
|
|||||||
stew install https://codeberg.org/scip/epuppy
|
stew install https://codeberg.org/scip/epuppy
|
||||||
```
|
```
|
||||||
|
|
||||||
Or go to the [latest release page](https://codeberg.org/scip/epuppy/releases/latest)
|
Or go to the [latest release page](https://codeberg.org/scip/epuppy/releases/)
|
||||||
and look for your OS and platform. There are two options to install the binary:
|
and look for your OS and platform. There are two options to install the binary:
|
||||||
|
|
||||||
Directly download the binary for your platform,
|
Directly download the binary for your platform,
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version string = `v0.0.7`
|
Version string = `v0.0.8`
|
||||||
Usage string = `This is epuppy, a terminal ui ebook viewer.
|
Usage string = `This is epuppy, a terminal ui ebook viewer.
|
||||||
|
|
||||||
Usage: epuppy [options] <epub file>
|
Usage: epuppy [options] <epub file>
|
||||||
|
|||||||
@@ -8,11 +8,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cleanentitles = regexp.MustCompile(`&[a-z]+;`)
|
cleanenTitles = regexp.MustCompile(`&[a-z]+;`)
|
||||||
empty = regexp.MustCompile(`(?s)^[\s ]*$`)
|
isEmpty = regexp.MustCompile(`(?s)^[\s ]*$`)
|
||||||
newlines = regexp.MustCompile(`[\r\n\s]+`)
|
cleanNewlines = regexp.MustCompile(`[\r\n\s]+`)
|
||||||
cleansvg = regexp.MustCompile(`(<svg.+</svg>|<!\[CDATA\[.+\]\]>)`)
|
cleanSVG = regexp.MustCompile(`(<svg.+</svg>|<!\[CDATA\[.+\]\]>)`)
|
||||||
cleanmarkup = regexp.MustCompile(`<[^<>]+>`)
|
cleanMarkup = regexp.MustCompile(`<[^<>]+>`)
|
||||||
|
cleanMobiPageBreaks = regexp.MustCompile(`<mbp:pagebreak/>`)
|
||||||
)
|
)
|
||||||
|
|
||||||
// Content nav-point content
|
// Content nav-point content
|
||||||
@@ -25,15 +26,30 @@ type Content struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parse XML, look for title and <p>.*</p> stuff
|
// parse XML, look for title and <p>.*</p> stuff
|
||||||
func (c *Content) String(content []byte) error {
|
func (c *Content) Extract(content []byte) error {
|
||||||
doc, err := xmlquery.Parse(
|
rawXML := cleanSVG.ReplaceAllString(
|
||||||
strings.NewReader(
|
cleanenTitles.ReplaceAllString(string(content), " "), "")
|
||||||
cleansvg.ReplaceAllString(
|
|
||||||
cleanentitles.ReplaceAllString(string(content), " "), "")))
|
var doc *xmlquery.Node
|
||||||
|
var err error
|
||||||
|
|
||||||
|
doc, err = xmlquery.Parse(strings.NewReader(rawXML))
|
||||||
|
if err != nil {
|
||||||
|
if strings.Contains(err.Error(), `namespace mbp is missing`) {
|
||||||
|
fixedmbp := strings.NewReader(
|
||||||
|
cleanMobiPageBreaks.ReplaceAllString(
|
||||||
|
rawXML, `<span style="page-break-after: always" />`))
|
||||||
|
|
||||||
|
doc, err = xmlquery.Parse(fixedmbp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if c.Title == "" {
|
if c.Title == "" {
|
||||||
// extract the title
|
// extract the title
|
||||||
for _, item := range xmlquery.Find(doc, "//title") {
|
for _, item := range xmlquery.Find(doc, "//title") {
|
||||||
@@ -47,9 +63,9 @@ func (c *Content) String(content []byte) error {
|
|||||||
txt := strings.Builder{}
|
txt := strings.Builder{}
|
||||||
var have_p bool
|
var have_p bool
|
||||||
for _, item := range xmlquery.Find(doc, "//p") {
|
for _, item := range xmlquery.Find(doc, "//p") {
|
||||||
if !empty.MatchString(item.InnerText()) {
|
if !isEmpty.MatchString(item.InnerText()) {
|
||||||
have_p = true
|
have_p = true
|
||||||
txt.WriteString(newlines.ReplaceAllString(item.InnerText(), " ") + "\n\n")
|
txt.WriteString(cleanNewlines.ReplaceAllString(item.InnerText(), " ") + "\n\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,9 +73,9 @@ func (c *Content) String(content []byte) error {
|
|||||||
// try <div></div>, which some ebooks use, so get all divs,
|
// try <div></div>, which some ebooks use, so get all divs,
|
||||||
// remove markup and paragraphify the parts
|
// remove markup and paragraphify the parts
|
||||||
for _, item := range xmlquery.Find(doc, "//div") {
|
for _, item := range xmlquery.Find(doc, "//div") {
|
||||||
if !empty.MatchString(item.InnerText()) {
|
if !isEmpty.MatchString(item.InnerText()) {
|
||||||
cleaned := cleanmarkup.ReplaceAllString(item.InnerText(), "")
|
cleaned := cleanMarkup.ReplaceAllString(item.InnerText(), "")
|
||||||
txt.WriteString(newlines.ReplaceAllString(cleaned, " ") + "\n\n")
|
txt.WriteString(cleanNewlines.ReplaceAllString(cleaned, " ") + "\n\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,6 +112,8 @@ func (bk *Book) getSections() error {
|
|||||||
|
|
||||||
// we have ncx points from the TOC, try those
|
// we have ncx points from the TOC, try those
|
||||||
if len(bk.Ncx.Points) > 0 {
|
if len(bk.Ncx.Points) > 0 {
|
||||||
|
known := map[string]int{}
|
||||||
|
|
||||||
for _, block := range bk.Ncx.Points {
|
for _, block := range bk.Ncx.Points {
|
||||||
sect := Section{
|
sect := Section{
|
||||||
File: "OEBPS/" + block.Content.Src,
|
File: "OEBPS/" + block.Content.Src,
|
||||||
@@ -128,7 +130,13 @@ func (bk *Book) getSections() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, haveFile := known[sect.File]; !haveFile {
|
||||||
|
// sometimes epub's have many sections but they all
|
||||||
|
// point to the same file. To avoid duplicate content
|
||||||
|
// we ignore sections (thus files) we have already seen.
|
||||||
sections = append(sections, sect)
|
sections = append(sections, sect)
|
||||||
|
known[sect.File] = 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(sections) < manifestcount {
|
if len(sections) < manifestcount {
|
||||||
@@ -189,7 +197,7 @@ func (bk *Book) readSectionContent() error {
|
|||||||
ct := Content{Src: section.File, Title: section.Title}
|
ct := Content{Src: section.File, Title: section.Title}
|
||||||
|
|
||||||
if types.MatchString(section.MediaType) {
|
if types.MatchString(section.MediaType) {
|
||||||
if err := ct.String(content); err != nil {
|
if err := ct.Extract(content); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user