1
1
package iterator
2
2
3
3
import (
4
- "net/url"
4
+ nurl "net/url"
5
5
"strings"
6
6
"unicode"
7
7
"unicode/utf8"
8
8
9
9
"github.com/readium/go-toolkit/pkg/content/element"
10
10
iutil "github.com/readium/go-toolkit/pkg/internal/util"
11
11
"github.com/readium/go-toolkit/pkg/manifest"
12
- "github.com/readium/go-toolkit/pkg/util"
12
+ "github.com/readium/go-toolkit/pkg/mediatype"
13
+ "github.com/readium/go-toolkit/pkg/util/url"
13
14
"golang.org/x/net/html"
14
15
"golang.org/x/net/html/atom"
15
16
)
@@ -72,14 +73,15 @@ func getAttr(n *html.Node, key string) string {
72
73
return ""
73
74
}
74
75
75
- func srcRelativeToHref (n * html.Node , base string ) * string {
76
+ func srcRelativeToHref (n * html.Node , base url. URL ) url. URL {
76
77
if n == nil {
77
78
return nil
78
79
}
79
80
80
81
if v := getAttr (n , "src" ); v != "" {
81
- h , _ := util .NewHREF (v , base ).String ()
82
- return & h
82
+ if u , _ := url .URLFromString (v ); u != nil {
83
+ return base .Resolve (u )
84
+ }
83
85
}
84
86
return nil
85
87
}
@@ -336,10 +338,10 @@ func (c *HTMLConverter) Head(n *html.Node, depth int) {
336
338
cssSelector = & cs
337
339
}
338
340
elementLocator := manifest.Locator {
339
- Href : c .baseLocator .Href ,
340
- Type : c .baseLocator .Type ,
341
- Title : c .baseLocator .Title ,
342
- Text : c .baseLocator .Text ,
341
+ Href : c .baseLocator .Href ,
342
+ MediaType : c .baseLocator .MediaType ,
343
+ Title : c .baseLocator .Title ,
344
+ Text : c .baseLocator .Text ,
343
345
Locations : manifest.Locations {
344
346
OtherLocations : map [string ]interface {}{
345
347
"cssSelector" : cssSelector ,
@@ -361,7 +363,7 @@ func (c *HTMLConverter) Head(n *html.Node, depth int) {
361
363
c .elements = append (c .elements , element .NewImageElement (
362
364
elementLocator ,
363
365
manifest.Link {
364
- Href : * href ,
366
+ Href : manifest . NewHREF ( href ) ,
365
367
},
366
368
"" , // FIXME: Get the caption from figcaption
367
369
atlist ,
@@ -372,18 +374,20 @@ func (c *HTMLConverter) Head(n *html.Node, depth int) {
372
374
var link * manifest.Link
373
375
if href != nil {
374
376
link = & manifest.Link {
375
- Href : * href ,
377
+ Href : manifest . NewHREF ( href ) ,
376
378
}
377
379
} else {
378
380
sourceNodes := childrenOfType (n , atom .Source , 1 )
379
381
sources := make ([]manifest.Link , len (sourceNodes ))
380
382
for _ , source := range sourceNodes {
381
383
if src := srcRelativeToHref (source , c .baseLocator .Href ); src != nil {
382
384
l := manifest.Link {
383
- Href : * src ,
385
+ Href : manifest . NewHREF ( href ) ,
384
386
}
385
387
if typ := getAttr (source , "type" ); typ != "" {
386
- l .Type = typ
388
+ if mt , err := mediatype .NewOfString (typ ); err == nil {
389
+ l .MediaType = & mt
390
+ }
387
391
}
388
392
sources = append (sources , l )
389
393
}
@@ -495,7 +499,7 @@ func (c *HTMLConverter) flushText() {
495
499
quote := element.Quote {}
496
500
for _ , at := range el .Attr {
497
501
if at .Key == "cite" {
498
- quote .ReferenceURL , _ = url .Parse (at .Val )
502
+ quote .ReferenceURL , _ = nurl .Parse (at .Val )
499
503
}
500
504
if at .Key == "title" {
501
505
quote .ReferenceTitle = at .Val
@@ -512,9 +516,9 @@ func (c *HTMLConverter) flushText() {
512
516
}
513
517
el := element .NewTextElement (
514
518
manifest.Locator {
515
- Href : c .baseLocator .Href ,
516
- Type : c .baseLocator .Type ,
517
- Title : c .baseLocator .Title ,
519
+ Href : c .baseLocator .Href ,
520
+ MediaType : c .baseLocator .MediaType ,
521
+ Title : c .baseLocator .Title ,
518
522
Locations : manifest.Locations {
519
523
OtherLocations : map [string ]interface {}{},
520
524
},
@@ -563,9 +567,9 @@ func (c *HTMLConverter) flushSegment() {
563
567
}
564
568
seg := element.TextSegment {
565
569
Locator : manifest.Locator {
566
- Href : c .baseLocator .Href ,
567
- Type : c .baseLocator .Type ,
568
- Title : c .baseLocator .Title ,
570
+ Href : c .baseLocator .Href ,
571
+ MediaType : c .baseLocator .MediaType ,
572
+ Title : c .baseLocator .Title ,
569
573
Locations : manifest.Locations {
570
574
// TODO fix: needs to use baseLocator locations too!
571
575
OtherLocations : map [string ]interface {}{},
0 commit comments