1 module epub.books; 2 3 @safe: 4 5 import std.conv; 6 import std.uuid; 7 import std.experimental.logger; 8 9 /** 10 * A Book is the object model for an epub file. 11 */ 12 class Book 13 { 14 /** The ID of the book. 15 * If you don't specify one, we will create it for you. 16 */ 17 string id; 18 19 /// The title of the book. 20 string title; 21 22 /// The author of the book. 23 string author; 24 25 /// The fileid of the cover image, which should be present as an attachment. 26 string coverid; 27 28 /// The chapters of the book, in the order to present them. 29 Chapter[] chapters; 30 31 /// Attachments (extra files) to include in the epub. 32 Attachment[] attachments; 33 34 /// The attachment to use as a cover image. 35 Attachment coverImage; 36 37 /** 38 * Preferred fonts for the book's cover. 39 * 40 * These should be ordered by priority: the first option will be used if possible, falling back 41 * to the second, falling back to the third, etc. 42 * 43 * The last resort is hard-coded as Sans. 44 */ 45 string[] coverFontPreferences; 46 47 /** 48 * Target size for generated covers. 49 * 50 * The defaults are taken from Kindle Direct Publishing's recommendations. 51 */ 52 uint width = 1600, height = 2560; 53 54 /** The name of the program that generated this ebook. */ 55 string generator; 56 } 57 58 /** 59 * An Attachment is a file to include in the epub document. 60 * 61 * For instance, if you want to include an image or stylesheet in the epub, 62 * you should create an Attachment for it. 63 */ 64 struct Attachment 65 { 66 /// The ID of the file. Generated if you don't provide it. 67 string fileid; 68 69 /// The path in the epub to this file. 70 string filename; 71 72 /// The mime type of the file. 73 string mimeType; 74 75 /// The file contents. 76 const(ubyte[]) content; 77 } 78 79 /** 80 * A Chapter is like an Attachment, but it appears in the main content of 81 * the book. 82 * 83 * Chapter content must be a valid XHTML document. The content type is 84 * always "application/xhtml+xml". If you specify invalid XHTML, it is 85 * unlikely that your epub will work. 86 */ 87 struct Chapter 88 { 89 /// The title of this chapter, if it's in the table of contents. 90 string title; 91 92 /// Whether to show this chapter in the table of contents. 93 bool showInTOC; 94 95 /// The contents of this chapter. 96 const(char)[] content; 97 98 package int index; 99 100 package string fileid() 101 { 102 return `chapter` ~ index.to!string; 103 } 104 105 package string filename() 106 { 107 return `chapter` ~ index.to!string ~ `.html`; 108 } 109 110 package string id() 111 { 112 return title.sha1UUID().to!string; 113 } 114 } 115