Skip to content

Commit 499c9ff

Browse files
committed
docs. prepare for GA
1 parent c7df796 commit 499c9ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+30964
-0
lines changed

docs/api/assets/2f6fad72/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Carsten Brandt
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

docs/api/assets/2f6fad72/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
js-search
2+
=========
3+
4+
This is a client side search engine for use on static pages.
5+
6+
It uses a pre-compiled search index to add a fulltext search to static HTML pages such as
7+
[github pages][] or offline API documentation. The index is built by a PHP script using a
8+
similar yet much more simplified and dump approach than the popular search engine [Lucene].
9+
10+
To see how it looks like, check out the [demo][].
11+
12+
[github pages]: https://pages.github.com/
13+
[Lucene]: http://lucene.apache.org/
14+
[demo]: http://cebe.github.io/js-search/#demo
15+
16+
17+
Installation
18+
------------
19+
20+
PHP 5.4 or higher is required to run the index generator.
21+
22+
Installation is recommended to be done via [composer][] by adding the following to the `require` section in your `composer.json`:
23+
24+
```json
25+
"cebe/js-search": "*"
26+
```
27+
28+
Run `composer update` afterwards.
29+
30+
31+
Usage
32+
-----
33+
34+
TODO.
35+
36+
See [example.html](example.html) for an implementation.
37+
38+
### Generate the index
39+
40+
Using the command line tool:
41+
```
42+
vendor/bin/jsindex <path-to-your-html-files>
43+
```
44+
45+
This will generate a `jssearch.index.js` file that you have to include in the Html header.

docs/api/assets/2f6fad72/bin/jsindex

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
// Send all errors to stderr
5+
ini_set('display_errors', 'stderr');
6+
7+
// setup composer autoloading
8+
$composerAutoload = [
9+
__DIR__ . '/../vendor/autoload.php', // standalone with "composer install" run
10+
__DIR__ . '/../../../autoload.php', // script is installed as a composer binary
11+
];
12+
foreach ($composerAutoload as $autoload) {
13+
if (file_exists($autoload)) {
14+
require($autoload);
15+
break;
16+
}
17+
}
18+
19+
if (!class_exists('cebe\jssearch\Indexer')) {
20+
error('Autoloading does not seem to work. Looks like you should run `composer install` first.');
21+
}
22+
23+
// check arguments
24+
$src = [];
25+
foreach($argv as $k => $arg) {
26+
if ($k == 0) {
27+
continue;
28+
}
29+
if ($arg[0] == '-') {
30+
$arg = explode('=', $arg);
31+
switch($arg[0]) {
32+
// TODO allow baseUrl to be set via arg
33+
case '-h':
34+
case '--help':
35+
echo "jssearch index builder\n";
36+
echo "----------------------\n\n";
37+
echo "by Carsten Brandt <[email protected]>\n\n";
38+
usage();
39+
break;
40+
default:
41+
error("Unknown argument " . $arg[0], "usage");
42+
}
43+
} else {
44+
$src[] = $arg;
45+
}
46+
}
47+
48+
if (empty($src)) {
49+
error("You have to give an input directory.", "usage");
50+
}
51+
52+
$indexer = new \cebe\jssearch\Indexer();
53+
54+
$files = [];
55+
foreach($src as $dir) {
56+
$files = array_merge($files, findFiles($dir));
57+
58+
if (empty($files)) {
59+
error("No files where found in $dir.");
60+
}
61+
62+
$indexer->indexFiles($files, $dir);
63+
}
64+
65+
$js = $indexer->exportJs();
66+
file_put_contents('jssearch.index.js', $js);
67+
68+
69+
// functions
70+
71+
/**
72+
* Display usage information
73+
*/
74+
function usage() {
75+
global $argv;
76+
$cmd = $argv[0];
77+
echo <<<EOF
78+
Usage:
79+
$cmd [src-directory]
80+
81+
--help shows this usage information.
82+
83+
creates and jssearch.index.js file in the current directory.
84+
85+
EOF;
86+
exit(1);
87+
}
88+
89+
/**
90+
* Send custom error message to stderr
91+
* @param $message string
92+
* @param $callback mixed called before script exit
93+
* @return void
94+
*/
95+
function error($message, $callback = null) {
96+
$fe = fopen("php://stderr", "w");
97+
fwrite($fe, "Error: " . $message . "\n");
98+
99+
if (is_callable($callback)) {
100+
call_user_func($callback);
101+
}
102+
103+
exit(1);
104+
}
105+
106+
function findFiles($dir, $ext = '.html')
107+
{
108+
if (!is_dir($dir)) {
109+
error("$dir is not a directory.");
110+
}
111+
$dir = rtrim($dir, DIRECTORY_SEPARATOR);
112+
$list = [];
113+
$handle = opendir($dir);
114+
if ($handle === false) {
115+
error('Unable to open directory: ' . $dir);
116+
}
117+
while (($file = readdir($handle)) !== false) {
118+
if ($file === '.' || $file === '..') {
119+
continue;
120+
}
121+
$path = $dir . DIRECTORY_SEPARATOR . $file;
122+
if (substr($file, -($l = strlen($ext)), $l) === $ext) {
123+
if (is_file($path)) {
124+
$list[] = $path;
125+
} else {
126+
$list = array_merge($list, findFiles($path, $ext));
127+
}
128+
}
129+
}
130+
closedir($handle);
131+
132+
return $list;
133+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "cebe/js-search",
3+
"description": "A client side search engine for use on static pages.",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Carsten Brandt",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {
12+
"php": ">=5.4.0"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"cebe\\jssearch\\": "lib/"
17+
}
18+
},
19+
"bin": [
20+
"bin/jsindex"
21+
]
22+
}

docs/api/assets/2f6fad72/example.html

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
6+
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
7+
<script src="./jssearch.js"></script>
8+
<script src="./jssearch.index.js"></script>
9+
10+
<script type="text/javascript">
11+
12+
$( document ).ready(function() {
13+
$('#searchbox').on("keyup", function() {
14+
var result = jssearch.search($(this).val());
15+
16+
$('#query').html(jssearch.queryWords.join(' '));
17+
18+
$('#results').html('');
19+
var i = 0;
20+
result.forEach(function(item) {
21+
if (i++ > 20) {
22+
return;
23+
}
24+
var div = $('#results');
25+
div.html(div.html() + '<li>"' + item.file.title + '" ' + item.file.url + ' w:' + item.weight + '</li>');
26+
});
27+
});
28+
});
29+
</script>
30+
31+
<title>Example</title>
32+
</head>
33+
<body>
34+
35+
<h1>Example</h1>
36+
37+
<label for="searchbox" style="display: inline-block; width: 160px;">Search: </label>
38+
<input id="searchbox" type="text" value="">
39+
40+
<br/>
41+
42+
<label for="query" style="display: inline-block; width: 160px;">Actual query: </label>
43+
<span id="query"></span>
44+
45+
<ul id="results">
46+
<li>No results</li>
47+
</ul>
48+
49+
</body>
50+
</html>

docs/api/assets/2f6fad72/jssearch.js

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// polyfills for IE<9
2+
(function(fn) {
3+
if (!fn.map) {
4+
fn.map = function(f/*, thisArg */) {
5+
if (this === void 0 || this === null)
6+
throw new TypeError();
7+
8+
var t = Object(this);
9+
var len = t.length >>> 0;
10+
if (typeof f !== "function")
11+
throw new TypeError();
12+
13+
var res = new Array(len);
14+
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
15+
for (var i = 0; i < len; i++) {
16+
if (i in t)
17+
res[i] = f.call(thisArg, t[i], i, t);
18+
}
19+
20+
return res;
21+
}
22+
}
23+
if (!fn.forEach) {
24+
fn.forEach = function (f/*, thisArg */) {
25+
if (this === void 0 || this === null)
26+
throw new TypeError();
27+
28+
var t = Object(this);
29+
var len = t.length >>> 0;
30+
if (typeof f !== "function")
31+
throw new TypeError();
32+
33+
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
34+
for (var i = 0; i < len; i++) {
35+
if (i in t)
36+
f.call(thisArg, t[i], i, t);
37+
}
38+
}
39+
}
40+
})(Array.prototype);
41+
42+
var jssearch = {
43+
44+
/**
45+
* the actual words finally used to query (set by last search call)
46+
*/
47+
queryWords: [],
48+
49+
search: function(query) {
50+
var words = jssearch.tokenizeString(query);
51+
var result = {};
52+
53+
jssearch.queryWords = words.map(function(i) { return i.t; });
54+
55+
// do not search when no words given
56+
if (!words.length) {
57+
return result;
58+
}
59+
60+
// result = jssearch.searchForWords(words);
61+
// if ($.isEmptyObject(result)) {
62+
words = jssearch.completeWords(words);
63+
jssearch.queryWords = words.map(function(i) { return i.t; });
64+
result = jssearch.searchForWords(words);
65+
// }
66+
67+
var res = [];
68+
for (var i in result) {
69+
res.push(result[i]);
70+
}
71+
res.sort(function(a,b) { return b.weight - a.weight; });
72+
return res;
73+
},
74+
75+
searchForWords: function(words) {
76+
var result = {};
77+
words.forEach(function(word) {
78+
if (jssearch.index[word.t]) {
79+
jssearch.index[word.t].forEach(function(file) {
80+
if (result[file.f]) {
81+
result[file.f].weight *= file.w * word.w;
82+
} else {
83+
result[file.f] = {
84+
file: jssearch.files[file.f],
85+
weight: file.w * word.w
86+
};
87+
}
88+
});
89+
}
90+
});
91+
return result;
92+
},
93+
94+
completeWords: function(words) {
95+
var result = [];
96+
97+
words.forEach(function(word) {
98+
if (!jssearch.index[word.t] && word.t.length > 2) {
99+
// complete words that are not in the index
100+
for(var w in jssearch.index) {
101+
if (w.substr(0, word.t.length) === word.t) {
102+
result.push({t: w, w: 1});
103+
}
104+
}
105+
} else {
106+
// keep existing words
107+
result.push(word);
108+
}
109+
});
110+
return result;
111+
},
112+
113+
tokenizeString: function(string)
114+
{
115+
if (console) {
116+
console.log('Error: tokenizeString should have been overwritten by index JS file.')
117+
}
118+
return [{t: string, w: 1}];
119+
}
120+
};

0 commit comments

Comments
 (0)