Skip to content

Commit 8197426

Browse files
author
Bastien Quelen
committedJan 10, 2017
related #2, better handling of contributor in epub3, and add first test suit with goconvey
1 parent 87e2ae9 commit 8197426

File tree

5 files changed

+115
-10
lines changed

5 files changed

+115
-10
lines changed
 

‎parser/epub.go

+34-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/feedbooks/webpub-streamer/models"
1212
)
1313

14+
const epub3 = "3.0"
15+
1416
func init() {
1517
parserList = append(parserList, List{fileExt: "epub", parser: EpubParser})
1618
}
@@ -55,7 +57,9 @@ func EpubParser(filePath string, selfURL string) (models.Publication, error) {
5557
publication.Metadata.Language = book.Opf.Metadata.Language
5658
addIdentifier(&publication, book, epubVersion)
5759
publication.Metadata.Right = strings.Join(book.Opf.Metadata.Rights, " ")
58-
publication.Metadata.Description = book.Opf.Metadata.Description[0]
60+
if len(book.Opf.Metadata.Description) > 0 {
61+
publication.Metadata.Description = book.Opf.Metadata.Description[0]
62+
}
5963

6064
if len(book.Opf.Metadata.Publisher) > 0 {
6165
for _, pub := range book.Opf.Metadata.Publisher {
@@ -80,6 +84,10 @@ func EpubParser(filePath string, selfURL string) (models.Publication, error) {
8084
}
8185
}
8286

87+
if epubVersion == epub3 {
88+
findContributorInMeta(&publication, book, epubVersion)
89+
}
90+
8391
fillSpineAndResource(&publication, book)
8492
addCoverRel(&publication, book)
8593

@@ -141,12 +149,35 @@ func findInManifestByID(book *epub.Book, ID string) models.Link {
141149
return models.Link{}
142150
}
143151

152+
func findContributorInMeta(publication *models.Publication, book *epub.Book, epubVersion string) {
153+
154+
for _, meta := range book.Opf.Metadata.Meta {
155+
if meta.Property == "dcterms:creator" || meta.Property == "dcterms:contributor" {
156+
cont := epub.Author{}
157+
cont.Data = meta.Data
158+
cont.ID = meta.ID
159+
addContributor(publication, book, epubVersion, cont)
160+
161+
}
162+
}
163+
164+
}
165+
144166
func addContributor(publication *models.Publication, book *epub.Book, epubVersion string, cont epub.Author) {
145167
var contributor models.Contributor
168+
var role string
146169

147170
contributor.Name = cont.Data
171+
if epubVersion == "3.0" {
172+
meta := findMetaByRefineAndProperty(book, cont.ID, "role")
173+
if meta.Property == "role" {
174+
role = meta.Data
175+
}
176+
} else {
177+
role = cont.Role
178+
}
148179

149-
switch contributor.Role {
180+
switch role {
150181
case "aut":
151182
publication.Metadata.Author = append(publication.Metadata.Author, contributor)
152183
case "trl":
@@ -170,14 +201,7 @@ func addContributor(publication *models.Publication, book *epub.Book, epubVersio
170201
case "pbl":
171202
publication.Metadata.Publisher = append(publication.Metadata.Publisher, contributor)
172203
default:
173-
if epubVersion == "3.0" {
174-
meta := findMetaByRefineAndProperty(book, cont.ID, "role")
175-
if meta.Property == "role" {
176-
contributor.Role = meta.Data
177-
}
178-
} else {
179-
contributor.Role = cont.Role
180-
}
204+
contributor.Role = role
181205
publication.Metadata.Contributor = append(publication.Metadata.Contributor, contributor)
182206
}
183207
}

‎parser/epub_test.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package parser
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
. "github.com/smartystreets/goconvey/convey"
8+
)
9+
10+
type testDataStruct struct {
11+
filepath string
12+
err error
13+
title string
14+
authorName string
15+
identifier string
16+
thirdChapter string
17+
tocChildren bool
18+
}
19+
20+
func TestPublication(t *testing.T) {
21+
testData := []testDataStruct{
22+
{"../test/empty.epub", errors.New("can't open or parse epub file with err : open ../test/empty.epub: no such file or directory"), "", "", "", "", false},
23+
{"../test/moby-dick.epub", nil, "Moby-Dick", "Herman Melville", "code.google.com.epub-samples.moby-dick-basic", "ETYMOLOGY.", false},
24+
{"../test/kusamakura.epub", nil, "草枕", "夏目 漱石", "http://www.aozora.gr.jp/cards/000148/card776.html", "三", false},
25+
{"../test/feedbooks_book_6816.epub", nil, "Mémoires d'Outre-tombe", "François-René de Chateaubriand", "urn:uuid:47f6aaf6-aa7e-11e6-8357-4c72b9252ec6", "Partie 1", true},
26+
}
27+
28+
for _, d := range testData {
29+
Convey("Given "+d.title+" book", t, func() {
30+
publication, err := Parse(d.filepath, "http://localhost/")
31+
Convey("There no exception parsing", func() {
32+
if d.err != nil {
33+
So(err.Error(), ShouldEqual, d.err.Error())
34+
} else {
35+
So(err, ShouldEqual, nil)
36+
}
37+
})
38+
Convey("The title is good", func() {
39+
So(publication.Metadata.Title, ShouldEqual, d.title)
40+
})
41+
42+
if err == nil {
43+
Convey("There must be an author", func() {
44+
So(len(publication.Metadata.Author), ShouldBeGreaterThanOrEqualTo, 1)
45+
})
46+
}
47+
48+
if d.authorName != "" && len(publication.Metadata.Author) > 0 {
49+
Convey("first author is good", func() {
50+
So(publication.Metadata.Author[0].Name, ShouldEqual, d.authorName)
51+
})
52+
}
53+
54+
Convey("Identifier is good", func() {
55+
So(publication.Metadata.Identifier, ShouldEqual, d.identifier)
56+
})
57+
58+
Convey("The third chapter is good", func() {
59+
if len(publication.TOC) > 3 {
60+
So(publication.TOC[2].Title, ShouldEqual, d.thirdChapter)
61+
}
62+
})
63+
64+
Convey("There Chapter with children", func() {
65+
emptyChildren := false
66+
for _, toc := range publication.TOC {
67+
if len(toc.Children) > 0 {
68+
emptyChildren = true
69+
}
70+
}
71+
if d.tocChildren == true {
72+
So(emptyChildren, ShouldBeTrue)
73+
} else {
74+
So(emptyChildren, ShouldBeFalse)
75+
}
76+
})
77+
78+
})
79+
}
80+
81+
}

‎test/feedbooks_book_6816.epub

3 MB
Binary file not shown.

‎test/kusamakura.epub

17.1 MB
Binary file not shown.

‎test/moby-dick.epub

1.55 MB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.