From bd4b59060ceb65fdbf6413b2c927a9011733dfac Mon Sep 17 00:00:00 2001 From: Myron Turner Date: Tue, 10 Sep 2019 09:52:27 -0500 Subject: [PATCH] Imap (#31) * Adding support for image maps * working on regexes * Replaces area tag's href with url local to ebook * added some documentation * Fixed xml declaration at top of toc.ncx, no longer generates .ncx error and now reads image map in Readium --- scripts/ebook.php | 41 +++++++++++++++++++++++++++++++++++++++++ scripts/epub_utils.php | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/scripts/ebook.php b/scripts/ebook.php index 6d4a7fc..71bda36 100644 --- a/scripts/ebook.php +++ b/scripts/ebook.php @@ -73,6 +73,47 @@ function create($id, $user_title=false) { $xhtml = $Renderer->doc; $result .= $xhtml; + //handle image maps + if(strpos($result, 'usemap') !== false) { + $R = $Renderer; + $result = preg_replace_callback( + '||im', + function($matches) use($R) { + if(strpos( $matches[1],'?') !== false) { + list($pre, $img) = explode('=', $matches[1]); + } + else $img = basename($matches[1]); + + $name = '../'. $R->copy_media($img); + echo "Map image name = $name\n"; + return ''; + }, + $result + ); + //Convert internal links to localized epub links + $result = preg_replace_callback( + '||im', + function($matches) { + if(strpos($matches[0], 'http') !== false) return $matches[0]; //External link, no conversioon needed + $matches[0]= preg_replace_callback( + '|href\s*=\s*([\"\'])(.*?)\1|m', //test $matches[0] + function($m) { + if(stripos($m[0],'javascript:') !== false) { + return $m[0]; // we do no convert javascript links + } + $patterns = array('!^' . preg_quote(DOKU_BASE) . '!', "/^doku.php/","!^\?\s*id\s*=\s*!"); + $_REQUEST['epubid'] = preg_replace($patterns, "", $m[2]); + $id = getID('epubid') . '.html' ; + $id = "../Text/" . str_replace(':','_',$id) ; + echo "revised url: " . htmlentities($id)."\n"; + return "href='$id'"; + },$matches[0]); + return $matches[0]; + }, $result + + ); + + } $result .= "\n\n"; $result = preg_replace_callback("/&(\w+);/m", "epbub_entity_replace", $result ); $result = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/m", "\n", $result); diff --git a/scripts/epub_utils.php b/scripts/epub_utils.php index 3cbebe6..5899481 100644 --- a/scripts/epub_utils.php +++ b/scripts/epub_utils.php @@ -59,7 +59,7 @@ function epub_opf_header($user_title) { flush(); $ncx=<< +