aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/libraries/MOBIClass/MOBIFile.php
diff options
context:
space:
mode:
authorThomas Citharel <tcit@tcit.fr>2015-02-18 19:17:31 +0100
committerThomas Citharel <tcit@tcit.fr>2015-02-18 19:17:31 +0100
commit4b1fa4c2febc7abbc6da3d65e4e760949a55843c (patch)
treea93ec906dbb03ec70e9cdc5dc876392c6d758e97 /inc/3rdparty/libraries/MOBIClass/MOBIFile.php
parent364953ede585b75fb29dc94b1c5f853053eaed0b (diff)
parentdf89c6f71adebfdb754ec3eb2fd775d8efbdb280 (diff)
downloadwallabag-4b1fa4c2febc7abbc6da3d65e4e760949a55843c.tar.gz
wallabag-4b1fa4c2febc7abbc6da3d65e4e760949a55843c.tar.zst
wallabag-4b1fa4c2febc7abbc6da3d65e4e760949a55843c.zip
Merge pull request #1081 from wallabag/dev1.9
Version 1.9.0
Diffstat (limited to 'inc/3rdparty/libraries/MOBIClass/MOBIFile.php')
-rw-r--r--inc/3rdparty/libraries/MOBIClass/MOBIFile.php157
1 files changed, 157 insertions, 0 deletions
diff --git a/inc/3rdparty/libraries/MOBIClass/MOBIFile.php b/inc/3rdparty/libraries/MOBIClass/MOBIFile.php
new file mode 100644
index 00000000..349227ae
--- /dev/null
+++ b/inc/3rdparty/libraries/MOBIClass/MOBIFile.php
@@ -0,0 +1,157 @@
1<?php
2/**
3 * This is the way MOBI files should be created if you want all features (TOC, images).
4 *
5 * File modified by Dawson for use in eBook Creator
6 * Added pagebreaks and a setting to remove table of contents.
7 */
8
9class MOBIFile extends ContentProvider {
10 const PARAGRAPH = 0;
11 const H2 = 1;
12 const H3 = 2;
13 const IMAGE = 3;
14 const PAGEBREAK = 4;
15
16 private $settings = array("title" => "Unknown Title", "toc" => true);
17 private $parts = array();
18 private $images = array();
19
20 /**
21 * Get the text data (the "html" code)
22 */
23 public function getTextData(){
24 $prefix = "<html><head><guide><reference title='CONTENT' type='toc' filepos=0000000000 /></guide></head><body>";
25
26 $title = "<h1>".$this->settings["title"]."</h1>";
27
28 list($text, $entries) = $this->generateText();
29
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
33 }
34
35 $suffix = "</body></html>";
36
37 return $prefix.$toc.$title.$text.$suffix;
38 }
39
40 /**
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.
44 */
45 public function generateText(){
46 $str = "";
47 $entries = array();
48
49 for($i = 0; $i < sizeof($this->parts); $i++){
50 list($type, $data) = $this->parts[$i];
51 $id = "title_".$i;
52 switch($type){
53 case self::PARAGRAPH:
54 $str .= "<p>".$data."</p>";
55 break;
56 case self::PAGEBREAK:
57 $str .= '<mbp:pagebreak/>';
58 break;
59 case self::H2:
60 $entries[] = array("level" => 2, "position" => strlen($str), "title" => $data, "id" => $id);
61 $str .= "<h2 id='" . $id . "'>".$data."</h2>";
62 break;
63 case self::H3:
64 $entries[] = array("level" => 3, "position" => strlen($str), "title" => $data, "id" => $id);
65 $str .= "<h3 id='" . $id . "'>".$data."</h3>";
66 break;
67 case self::IMAGE:
68 $str .= "<img recindex=".str_pad($data+1, 10, "0", STR_PAD_LEFT)." />";
69 break;
70 }
71 }
72 return array($str, $entries);
73 }
74
75 /**
76 * Generate a TOC
77 * @param $entries The entries array generated by generateText
78 * @param $base The zero position
79 */
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>";
87 }
88 return $toc."</tbody></b></table></blockquote><mbp:pagebreak/>";
89 }
90
91 /**
92 * Get the file records of the images
93 */
94 public function getImages(){
95 return $this->images;
96 }
97
98 /**
99 * Get the metadata
100 */
101 public function getMetaData(){
102 return $this->settings;
103 }
104
105 /**
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.
108 */
109 public function set($key, $value){
110 $this->settings[$key] = $value;
111 }
112
113 /**
114 * Get the file's settings.
115 */
116 public function get($key){
117 return $this->settings[$key];
118 }
119
120 /**
121 * Append a paragraph of text to the file.
122 * @param string $text The text to insert.
123 */
124 public function appendParagraph($text){
125 $this->parts[] = array(self::PARAGRAPH, $text);
126 }
127
128 /**
129 * Append a chapter title (H2)
130 * @param string $title The title to insert.
131 */
132 public function appendChapterTitle($title){
133 $this->parts[] = array(self::H2, $title);
134 }
135
136 /**
137 * Append a section title (H3)
138 * @param string $title The title to insert.
139 */
140 public function appendSectionTitle($title){
141 $this->parts[] = array(self::H3, $title);
142 }
143
144 public function appendPageBreak() {
145 $this->parts[] = array(self::PAGEBREAK, null);
146 }
147
148 /**
149 * Append an image.
150 * @param resource $img An image file (for example, created by `imagecreate`)
151 */
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);
156 }
157} \ No newline at end of file