]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/MOBIClass/MultipleFileHandler.php
4 * Description of MultipleFileHandler
8 abstract class MultipleFileHandler
extends ContentProvider
{
12 private $files = array();
16 private $images = array();
20 private $metadata = array();
22 private $toc = array();
25 * Add a page to the file
26 * @param string $contents Contents of the chapter/page
27 * @param string $title Optional, title of the chapter/page. Will automatically add a h2
30 public function addPage($contents, $title = ""){
32 //TODO: Add in TOC (and add a way of generating it
33 $contents = "<h2>".$title."</h2>".$contents."<mbp:pagebreak>";
37 if(sizeof($this->toc
) > 0){
38 $lastToc = $this->toc
[sizeof($this->toc
)-1];
39 $lastFile = $this->files
[sizeof($this->files
)-1];
40 $pos = $lastToc["pos"] +
strlen($lastFile) +
1;
43 $this->files
[] = $contents;
44 $this->toc
[] = array("title"=>$title, "pos"=>$pos);
48 * Add an image to the file
49 * @param string $imageContents Data string containing the binary data of the image
50 * @return int The reference of the image
52 public function addImage($imageContents){
53 $this->images
[] = $imageContents;
54 return sizeof($this->images
)-1;
58 * Add an image to the file
59 * @param string $url Url to the image
60 * @return int The reference of the image, false if the image couldn't be downloaded
62 public function addImageFromUrl($url){
63 $image = ImageHandler
::DownloadImage($url);
65 if($image === false) return false;
66 return $this->addImage($image);
71 * @param string $key Key
72 * @param string $value Value
74 public function setMetadata($key, $value){
75 $this->metadata
[$key] = $value;
79 * Get the text data to be integrated in the MOBI file
82 public function getTextData(){
83 $data = implode("\n", $this->files
);
84 $begin = "<html><head><guide><reference title='CONTENT' type='toc' filepos=0000000000 /></guide></head><body>";
85 $beforeTOC = $begin.$data;
87 $tocPos = strlen($beforeTOC);
89 $toc = $this->generateTOC(strlen($begin));
91 $customBegin = "<html><head><guide><reference title='CONTENT' type='toc' filepos=".$this->forceLength($tocPos, 10)." /></guide></head><body>";
92 $data = $customBegin.$data.$toc."</body></html>";
96 public function forceLength($n, $l){
106 public function generateTOC($base = 0){
107 $toc = "<h2>Contents</h2>";
108 $toc .= "<blockquote><table summary='Table of Contents'><b><col/><col/><tbody>";
109 for($i = 0, $len = sizeof($this->toc
); $i < $len; $i++
){
110 $entry = $this->toc
[$i];
111 $position = $entry["pos"]+
$base;
112 $toc .= "<tr><td>".($i+
1).".</td><td><a filepos=".$position.">".$entry["title"]."</a></td></tr>";
114 $toc .= "</tbody></b></table></blockquote>";
119 * Get the images (an array containing the jpeg data). Array entry 0 will
120 * correspond to image record 0.
123 public function getImages(){
124 return $this->images
;
128 * Get the metadata in the form of a hashtable (for example, title or author).
131 public function getMetaData(){
132 return $this->metadata
;