Skip to content

Commit

Permalink
Added an option to choose epub v3 with html5.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-KM committed Oct 21, 2018
1 parent 9a7c8de commit 78546d9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"phpzip/phpzip": "~2.0.7",
"grandt/phpresizegif": "~1.0.3",
"grandt/relativepath": "~1.0.1",
"masterminds/html5": "^2.1"
"masterminds/html5": "~2.3"
},
"autoload": {
"psr-4": {
Expand Down
43 changes: 36 additions & 7 deletions src/PHPePub/Core/EPub.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class EPub {
const BOOK_VERSION_EPUB2 = '2.0';
const BOOK_VERSION_EPUB3 = '3.0';

const FORMAT_XHTML = 'xhtml';
const FORMAT_HTML5 = 'html5';

public $viewportMap = array(
"small" => array('width' => 600, 'height' => 800),
"medium" => array('width' => 720, 'height' => 1280),
Expand Down Expand Up @@ -127,6 +130,7 @@ class EPub {
private $tocCssFileName = null;
private $fileList = array();
private $writingDirection = EPub::DIRECTION_LEFT_TO_RIGHT;
private $htmlFormat = EPub::FORMAT_XHTML;
private $languageCode = 'en';
private $dateformat = 'Y-m-d\TH:i:s.000000P';
private $dateformatShort = 'Y-m-d';
Expand All @@ -150,11 +154,18 @@ class EPub {
* @param string $bookVersion
* @param string $languageCode
* @param string $writingDirection
*/
function __construct($bookVersion = EPub::BOOK_VERSION_EPUB2, $languageCode = 'en', $writingDirection = EPub::DIRECTION_LEFT_TO_RIGHT) {
* @param string $htmlFormat
*/
function __construct(
$bookVersion = EPub::BOOK_VERSION_EPUB2,
$languageCode = 'en',
$writingDirection = EPub::DIRECTION_LEFT_TO_RIGHT,
$htmlFormat = EPub::FORMAT_XHTML
) {
$this->bookVersion = $bookVersion;
$this->writingDirection = $writingDirection;
$this->languageCode = $languageCode;
$this->htmlFormat = $htmlFormat;

$this->log = new Logger('EPub', $this->isLogging);

Expand Down Expand Up @@ -247,7 +258,7 @@ function addChapter($chapterName, $fileName, $chapterData = null, $autoSplit = f

$chapter = $chapterData;
if ($autoSplit && is_string($chapterData) && mb_strlen($chapterData) > $this->splitDefaultSize) {
$splitter = new EPubChapterSplitter();
$splitter = new EPubChapterSplitter($this->htmlFormat);
$splitter->setSplitSize($this->splitDefaultSize);

$chapterArray = $splitter->splitChapter($chapterData);
Expand Down Expand Up @@ -355,8 +366,17 @@ function addChapter($chapterName, $fileName, $chapterData = null, $autoSplit = f
* @return array
*/
function findIdAttributes($chapterData) {
$html5 = new HTML5();
$xmlDoc = $html5->loadHTML($chapterData);
switch ($this->htmlFormat) {
case EPub::FORMAT_HTML5;
$html5 = new HTML5();
$xmlDoc = $html5->loadHTML($chapterData);
break;
case EPub::FORMAT_XHTML:
default:
$xmlDoc = new DOMDocument();
@$xmlDoc->loadHTML($chapterData);
break;
}

$xpath = new DomXpath($xmlDoc);

Expand Down Expand Up @@ -417,8 +437,17 @@ protected function processChapterExternalReferences(&$doc, $externalReferences =
if ($isDocAString) {
$doc = StringHelper::removeComments($doc);

$html5 = new HTML5();
$xmlDoc = $html5->loadHTML($doc);
switch ($this->htmlFormat) {
case EPub::FORMAT_HTML5;
$html5 = new HTML5();
$xmlDoc = $html5->loadHTML($doc);
break;
case EPub::FORMAT_XHTML:
default:
$xmlDoc = new DOMDocument();
@$xmlDoc->loadHTML($doc);
break;
}
} else {
$xmlDoc = $doc;
}
Expand Down
34 changes: 32 additions & 2 deletions src/PHPePub/Core/EPubChapterSplitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
class EPubChapterSplitter {
private $splitDefaultSize = 250000;
private $bookVersion = EPub::BOOK_VERSION_EPUB2;
private $htmlFormat = EPub::FORMAT_XHTML;

/**
* Class constructor.
*
* @param string $htmlFormat
*/
function __construct($htmlFormat = EPub::FORMAT_XHTML) {
$this->setHtmlFormat($htmlFormat);
}

/**
*
Expand All @@ -29,6 +39,17 @@ function setVersion($bookVersion) {
$this->bookVersion = is_string($bookVersion) ? trim($bookVersion) : EPub::BOOK_VERSION_EPUB2;
}

/**
* Set the html format of the epub.
*
* @param string $htmlFormat
*/
public function setHtmlFormat($htmlFormat) {
$this->htmlFormat = in_array($htmlFormat, [EPub::FORMAT_XHTML, EPub::FORMAT_HTML5])
? $htmlFormat
: EPub::FORMAT_XHTML;
}

/**
* Set default chapter target size.
* Default is 250000 bytes, and minimum is 10240 bytes.
Expand Down Expand Up @@ -78,8 +99,17 @@ function splitChapter($chapter, $splitOnSearchString = false, $searchString = '/
);
}

$html5 = new HTML5();
$xmlDoc = $html5->loadHTML($chapter);
switch ($this->htmlFormat) {
case EPub::FORMAT_HTML5;
$html5 = new HTML5();
$xmlDoc = $html5->loadHTML($chapter);
break;
case EPub::FORMAT_XHTML:
default:
$xmlDoc = new DOMDocument();
@$xmlDoc->loadHTML($chapter);
break;
}

$head = $xmlDoc->getElementsByTagName("head");
$body = $xmlDoc->getElementsByTagName("body");
Expand Down
2 changes: 1 addition & 1 deletion tests/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"require": {
"php": ">=5.3.0",
"phpzip/phpzip": ">=2.0.7",
"masterminds/html5": "^2.1"
"masterminds/html5": "~2.3"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 78546d9

Please sign in to comment.