Skip to content

Commit

Permalink
Imap (#31)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
turnermm committed Sep 10, 2019
1 parent 13e4a89 commit bd4b590
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions scripts/ebook.php
Expand Up @@ -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(
'|<img\s+src=\"(.*?)\"(.*?usemap.*?)>|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 '<img src="' . $name . '"' . $matches[2] . '>';
},
$result
);
//Convert internal links to localized epub links
$result = preg_replace_callback(
'|<area(.*?)>|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</div></body></html>\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);
Expand Down
2 changes: 1 addition & 1 deletion scripts/epub_utils.php
Expand Up @@ -59,7 +59,7 @@ function epub_opf_header($user_title) {
flush();

$ncx=<<<NCX
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0"?>
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd"><ncx version="2005-1" xml:lang="en" xmlns="http://www.daisy.org/z3986/2005/ncx/">
<head>
Expand Down

0 comments on commit bd4b590

Please sign in to comment.