Created
March 31, 2015 10:11
-
-
Save fbstj/643a949a2ac2a82aac7d to your computer and use it in GitHub Desktop.
A search utility for unzipped epubs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> | |
<style> | |
#view { float: right; width: 75vw; height: 98vh; } | |
nav { float: left; width: 20vw; height: 98vh; } | |
#nav { height: 100%; max-height: 90vh; overflow-y: scroll; } | |
.active { font-weight: bold; } | |
</style> | |
<title>Book search</title> | |
<iframe name=bob id=view src=blank.html></iframe> | |
<nav> | |
<form method=get> | |
<input name=search value="<?=$_GET['search']?>" /> | |
<button type=submit>Search</button> | |
<input label="words" type=checkbox name=only <?=array_key_exists('only',$_GET)?'checked=true':''?> /> | |
</form> | |
<div id=nav> | |
<?php | |
function find_files($path, $type = 'html') | |
{ | |
$paths = []; | |
if (is_file(__DIR__.'/'.$path)) | |
{ | |
$paths[] = [ | |
'name' => basename($path, '.'.$type), | |
'path' => __DIR__.'/'.$path, | |
'link' => './'.$path | |
]; | |
} | |
else foreach (glob(__DIR__.'/'.$path.'/'.'*') as $file) | |
{ | |
$paths[] = [ | |
'name' => basename($file, '.'.$type), | |
'path' => $file, | |
'link' => str_replace(__DIR__, '.', $file) | |
]; | |
} | |
return $paths; | |
} | |
function search($paths, $term = "", $type = 'html') | |
{ | |
$matches = []; | |
foreach ($paths as $p) | |
{ | |
$text = file_get_contents($p['path']); | |
if (array_key_exists('only', $_GET)) | |
{ | |
if(preg_match("/\b{$term}\b/", $text) === 1) | |
$matches[] = [ $p['link'], $p['name'] ]; | |
} | |
elseif (stripos($text, $term) !== false) | |
$matches[] = [ $p['link'], $p['name'] ]; | |
} | |
return $matches; | |
} | |
$file = file_get_contents("./stories.json"); | |
$books = json_decode($file, true); | |
foreach ($books["books"] as $book) | |
{ | |
if(array_key_exists('tag', $_GET)) | |
{ | |
if (!in_array($_GET['tag'], $book['tags'])) | |
continue; | |
} | |
$name = $book["title"]; | |
$files = find_files($book["path"]); | |
$results = search($files, $_GET['search']); | |
if (count($results) == 0) | |
continue; | |
print "<h5>{$name}</h5>"; | |
foreach ($results as $link) | |
print "<a target=bob href=\"{$link[0]}\">{$link[1]}</a><br>"; | |
} | |
?> | |
</div> | |
</nav> | |
<script> | |
$(function () { | |
$('a').click( function () { | |
$(this).addClass('active').siblings().removeClass('active'); | |
}); | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"books": [ | |
{ | |
"title": "Book title", | |
"path": "path/to/OEBPS/Text", | |
"tags": [ "some", "keyword", "tags" ] | |
}, | |
{ | |
"title": "sequel", | |
"path": "path/for/OEBPS/Text", | |
"tags": [ "other", "keyword", "tags" ] | |
} | |
], | |
"exclude": [ | |
"bibliography.html", | |
"copywrite.html", | |
"cover.html", | |
"dedication.html", | |
"title.html", | |
"title.xml", | |
"postscript.html" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment