From 78546d9a49d59ad9a32b13f3d95cc2f548add1c3 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Mon, 22 Oct 2018 00:00:00 +0200 Subject: [PATCH] Added an option to choose epub v3 with html5. --- composer.json | 2 +- src/PHPePub/Core/EPub.php | 43 ++++++++++++++++++++---- src/PHPePub/Core/EPubChapterSplitter.php | 34 +++++++++++++++++-- tests/composer.json | 2 +- 4 files changed, 70 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index f015749..da39c7d 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/PHPePub/Core/EPub.php b/src/PHPePub/Core/EPub.php index f7248ea..39a480f 100644 --- a/src/PHPePub/Core/EPub.php +++ b/src/PHPePub/Core/EPub.php @@ -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), @@ -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'; @@ -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); @@ -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); @@ -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); @@ -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; } diff --git a/src/PHPePub/Core/EPubChapterSplitter.php b/src/PHPePub/Core/EPubChapterSplitter.php index 5f5ad4c..5c45925 100644 --- a/src/PHPePub/Core/EPubChapterSplitter.php +++ b/src/PHPePub/Core/EPubChapterSplitter.php @@ -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); + } /** * @@ -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. @@ -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"); diff --git a/tests/composer.json b/tests/composer.json index 32a7147..cd3f14c 100644 --- a/tests/composer.json +++ b/tests/composer.json @@ -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": {