]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/MOBIClass/MOBIFile.php
3 * This is the way MOBI files should be created if you want all features (TOC, images).
5 * File modified by Dawson for use in eBook Creator
6 * Added pagebreaks and a setting to remove table of contents.
9 class MOBIFile
extends ContentProvider
{
16 private $settings = array("title" => "Unknown Title", "toc" => true);
17 private $parts = array();
18 private $images = array();
21 * Get the text data (the "html" code)
23 public function getTextData(){
24 $prefix = "<html><head><guide><reference title='CONTENT' type='toc' filepos=0000000000 /></guide></head><body>";
26 $title = "<h1>".$this->settings
["title"]."</h1>";
28 list($text, $entries) = $this->generateText();
30 if($this->settings
["toc"]) {
31 $toc = $this->generateTOC($entries); //Generate TOC to get the right length
32 $toc = $this->generateTOC($entries, strlen($prefix)+
strlen($toc)+
strlen($title)); //Generate the real TOC
35 $suffix = "</body></html>";
37 return $prefix.$toc.$title.$text.$suffix;
41 * Generate the body's text and the chapter entries
42 * @return array($string, $entries) $string is the html data, $entries
43 * contains the level, the title and the position of the titles.
45 public function generateText(){
49 for($i = 0; $i < sizeof($this->parts
); $i++
){
50 list($type, $data) = $this->parts
[$i];
54 $str .= "<p>".$data."</p>";
57 $str .= '<mbp:pagebreak/>';
60 $entries[] = array("level" => 2, "position" => strlen($str), "title" => $data, "id" => $id);
61 $str .= "<h2 id='" . $id . "'>".$data."</h2>";
64 $entries[] = array("level" => 3, "position" => strlen($str), "title" => $data, "id" => $id);
65 $str .= "<h3 id='" . $id . "'>".$data."</h3>";
68 $str .= "<img recindex=".str_pad($data+
1, 10, "0", STR_PAD_LEFT
)." />";
72 return array($str, $entries);
77 * @param $entries The entries array generated by generateText
78 * @param $base The zero position
80 public function generateTOC($entries, $base = 0){
81 $toc = "<h2>Contents</h2>";
82 $toc .= "<blockquote><table summary='Table of Contents'><col/><tbody>";
83 for($i = 0, $len = sizeof($entries); $i < $len; $i++
){
84 $entry = $entries[$i];
85 $pos = str_pad($entry["position"]+
$base, 10, "0", STR_PAD_LEFT
);
86 $toc .= "<tr><td><a href='#".$entry["id"]."' filepos='".$pos."'>".$entry["title"]."</a></td></tr>";
88 return $toc."</tbody></b></table></blockquote><mbp:pagebreak/>";
92 * Get the file records of the images
94 public function getImages(){
101 public function getMetaData(){
102 return $this->settings
;
106 * Change the file's settings. For example set("author", "John Doe") or set("title", "The adventures of John Doe").
107 * @param $key Key of the setting to insert.
109 public function set($key, $value){
110 $this->settings
[$key] = $value;
114 * Get the file's settings.
116 public function get($key){
117 return $this->settings
[$key];
121 * Append a paragraph of text to the file.
122 * @param string $text The text to insert.
124 public function appendParagraph($text){
125 $this->parts
[] = array(self
::PARAGRAPH
, $text);
129 * Append a chapter title (H2)
130 * @param string $title The title to insert.
132 public function appendChapterTitle($title){
133 $this->parts
[] = array(self
::H2
, $title);
137 * Append a section title (H3)
138 * @param string $title The title to insert.
140 public function appendSectionTitle($title){
141 $this->parts
[] = array(self
::H3
, $title);
144 public function appendPageBreak() {
145 $this->parts
[] = array(self
::PAGEBREAK
, null);
150 * @param resource $img An image file (for example, created by `imagecreate`)
152 public function appendImage($img){
153 $imgIndex = sizeof($this->images
);
154 $this->images
[] = new FileRecord(new Record(ImageHandler
::CreateImage($img)));
155 $this->parts
[] = array(self
::IMAGE
, $imgIndex);