[fix] server word count, [add] client word count
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
b9b9ad2098
commit
1b8b5060f1
@ -45,9 +45,15 @@ class EBookReader {
|
|||||||
* Load progress and generate locations
|
* Load progress and generate locations
|
||||||
**/
|
**/
|
||||||
async setupReader() {
|
async setupReader() {
|
||||||
|
// Get Word Count (If Needed)
|
||||||
|
if (this.bookState.words == 0)
|
||||||
|
this.bookState.words = await this.countWords();
|
||||||
|
|
||||||
// Load Progress
|
// Load Progress
|
||||||
let { cfi } = await this.getCFIFromXPath(this.bookState.progress);
|
let { cfi } = await this.getCFIFromXPath(this.bookState.progress);
|
||||||
if (!cfi) this.bookState.currentWord = 0;
|
this.bookState.currentWord = cfi
|
||||||
|
? this.bookState.percentage * (this.bookState.words / 100)
|
||||||
|
: 0;
|
||||||
|
|
||||||
let getStats = function () {
|
let getStats = function () {
|
||||||
// Start Timer
|
// Start Timer
|
||||||
@ -1038,6 +1044,21 @@ class EBookReader {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Count the words of the book. Useful for keeping a more accurate track
|
||||||
|
* of progress percentage. Implementation returns the same number as the
|
||||||
|
* server side implementation.
|
||||||
|
**/
|
||||||
|
countWords() {
|
||||||
|
// Iterate over each item in the spine, render, and count words.
|
||||||
|
return this.book.spine.spineItems.reduce(async (totalCount, item) => {
|
||||||
|
let currentCount = await totalCount;
|
||||||
|
let newDoc = await item.load(this.book.load.bind(this.book));
|
||||||
|
let itemCount = newDoc.innerText.trim().split(/\s+/).length;
|
||||||
|
return currentCount + itemCount;
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save settings to localStorage
|
* Save settings to localStorage
|
||||||
**/
|
**/
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
package metadata
|
package metadata
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/PuerkitoBio/goquery"
|
||||||
"github.com/taylorskalyo/goreader/epub"
|
"github.com/taylorskalyo/goreader/epub"
|
||||||
"golang.org/x/net/html"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func getEPUBMetadata(filepath string) (*MetadataInfo, error) {
|
func getEPUBMetadata(filepath string) (*MetadataInfo, error) {
|
||||||
@ -32,33 +31,9 @@ func countEPUBWords(filepath string) (int64, error) {
|
|||||||
var completeCount int64
|
var completeCount int64
|
||||||
for _, item := range rf.Spine.Itemrefs {
|
for _, item := range rf.Spine.Itemrefs {
|
||||||
f, _ := item.Open()
|
f, _ := item.Open()
|
||||||
tokenizer := html.NewTokenizer(f)
|
doc, _ := goquery.NewDocumentFromReader(f)
|
||||||
newCount, err := countTokenizerWords(*tokenizer)
|
completeCount = completeCount + int64(len(strings.Fields(doc.Text())))
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
completeCount = completeCount + newCount
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return completeCount, nil
|
return completeCount, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func countTokenizerWords(tokenizer html.Tokenizer) (int64, error) {
|
|
||||||
var err error
|
|
||||||
var totalWords int64
|
|
||||||
for {
|
|
||||||
tokenType := tokenizer.Next()
|
|
||||||
token := tokenizer.Token()
|
|
||||||
if tokenType == html.TextToken {
|
|
||||||
currStr := string(token.Data)
|
|
||||||
totalWords = totalWords + int64(len(strings.Fields(currStr)))
|
|
||||||
} else if tokenType == html.ErrorToken {
|
|
||||||
err = tokenizer.Err()
|
|
||||||
}
|
|
||||||
if err == io.EOF {
|
|
||||||
return totalWords, nil
|
|
||||||
} else if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestGetWordCount(t *testing.T) {
|
func TestGetWordCount(t *testing.T) {
|
||||||
var want int64 = 30477
|
var want int64 = 30080
|
||||||
wordCount, err := countEPUBWords("../_test_files/alice.epub")
|
wordCount, err := countEPUBWords("../_test_files/alice.epub")
|
||||||
|
|
||||||
if wordCount != want {
|
if wordCount != want {
|
||||||
|
Loading…
Reference in New Issue
Block a user