|
| 1 | +package epub2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "strconv" |
| 6 | +) |
| 7 | + |
| 8 | +// AddMetadataSubject adds subject to metadata |
| 9 | +func (e *EPUB) AddMetadataSubject(subj string) { |
| 10 | + e.Metadata += "<dc:subject>" + subj + "</dc:subject>\n" |
| 11 | +} |
| 12 | + |
| 13 | +// AddMetadataAuthor adds author to metadata |
| 14 | +func (e *EPUB) AddMetadataAuthor(name, sort string) { |
| 15 | + e.Metadata += `<dc:creator opf:file-as="` + sort + `" opf:role="aut" xmlns:opf="http://www.idpf.org/2007/opf">` + name + `</dc:creator>` + "\n" |
| 16 | +} |
| 17 | + |
| 18 | +// AddMetadataDescription adds description to metadata |
| 19 | +func (e *EPUB) AddMetadataDescription(desc string) { |
| 20 | + e.Metadata += "<dc:description>" + desc + "</dc:description>\n" |
| 21 | +} |
| 22 | + |
| 23 | +// AddMetadataTitle adds title to metadata |
| 24 | +func (e *EPUB) AddMetadataTitle(title string) { |
| 25 | + e.Title = title |
| 26 | + e.Metadata += "<dc:title>" + title + "</dc:title>\n" |
| 27 | +} |
| 28 | + |
| 29 | +// AddMetadataLanguage adds language to metadata |
| 30 | +func (e *EPUB) AddMetadataLanguage(lang string) { |
| 31 | + e.Lang = lang |
| 32 | + e.Metadata += `<dc:language xsi:type="dcterms:RFC3066">` + lang + `</dc:language>` + "\n" |
| 33 | +} |
| 34 | + |
| 35 | +// AddMetadataCover add cover to metadata |
| 36 | +func (e *EPUB) AddMetadataCover(imageName string) { |
| 37 | + e.Metadata += `<meta name="cover" content="` + imageName + `" />` + "\n" |
| 38 | +} |
| 39 | + |
| 40 | +func (e *EPUB) AddItem(itemName, guideType, content string) error { |
| 41 | + e.Manifest += `<item id="` + itemName + `" href="` + itemName + `.xhtml" media-type="application/xhtml+xml" />` + "\n" |
| 42 | + e.Spine += `<itemref idref="` + itemName + `" />` + "\n" |
| 43 | + |
| 44 | + data := struct { |
| 45 | + Title string |
| 46 | + Content string |
| 47 | + Type string |
| 48 | + }{ |
| 49 | + Title: e.Title, |
| 50 | + Content: content, |
| 51 | + Type: guideType, |
| 52 | + } |
| 53 | + return e.execTemplate("OEBPS/"+itemName+".xhtml", "page.tmpl", data) |
| 54 | +} |
| 55 | + |
| 56 | +func (e *EPUB) AddBinary(id, contentType, base64Content string) error { |
| 57 | + e.Manifest += `<item id="` + id + `" href="` + id + `" media-type="` + contentType + `" />` + "\n" |
| 58 | + data, err := base64.StdEncoding.DecodeString(base64Content) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + f, err := e.zw.Create("OEBPS/" + id) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + _, err = f.Write(data) |
| 69 | + return err |
| 70 | +} |
| 71 | + |
| 72 | +func (e *EPUB) AddOPF() error { |
| 73 | + return e.execTemplate("OEBPS/content.opf", "content.tmpl", e) |
| 74 | +} |
| 75 | + |
| 76 | +func (e *EPUB) AddTOC() error { |
| 77 | + toc := "" |
| 78 | + prevDepth := 1 |
| 79 | + for i, t := range e.Toc { |
| 80 | + // fmt.Println(t) |
| 81 | + switch { |
| 82 | + case i == 0 || t.Depth > prevDepth: |
| 83 | + prevDepth = t.Depth |
| 84 | + case t.Depth == prevDepth: |
| 85 | + toc += ` |
| 86 | +</navPoint> |
| 87 | +` |
| 88 | + case t.Depth < prevDepth: |
| 89 | + toc += ` |
| 90 | +</navPoint> |
| 91 | +</navPoint> |
| 92 | +` |
| 93 | + prevDepth = t.Depth |
| 94 | + } |
| 95 | + toc += ` |
| 96 | +<navPoint playOrder="` + strconv.Itoa(t.Order) + `" id="` + t.Id + `"> |
| 97 | + <navLabel> |
| 98 | + <text>` + t.Text + `</text> |
| 99 | + </navLabel> |
| 100 | + <content src="` + t.Src + `" /> |
| 101 | +` |
| 102 | + } |
| 103 | + |
| 104 | + for i := 1; i <= prevDepth; i++ { |
| 105 | + toc += ` |
| 106 | +</navPoint> |
| 107 | +` |
| 108 | + } |
| 109 | + |
| 110 | + data := struct { |
| 111 | + Lang string |
| 112 | + Title string |
| 113 | + Toc string |
| 114 | + }{ |
| 115 | + Lang: e.Lang, |
| 116 | + Title: e.Title, |
| 117 | + Toc: toc, |
| 118 | + } |
| 119 | + return e.execTemplate("OEBPS/toc.ncx", "toc.tmpl", data) |
| 120 | +} |
0 commit comments