/*****************************************
* Search Manager
*****************************************
* Author: mikael.lofjard@gmail.com
* Website: http://lofjard.se
* License: MIT License
****************************************/
var
SearchManager = (
function
() {
env = require(
'./environmentManager'
).EnvironmentManager;
misc = require(
'./misc'
).Misc;
lunr = require(
'./forked/lunr'
);
db =
null
;
searchIndex =
allPosts =
createOuttake =
(text) {
// remove <del> content
outtake = text.replace(/<del.*?>.*?<\/del>/g,
""
// remove html
outtake = outtake.replace(/<.*?>/g,
// remove markdown headers
outtake = outtake.replace(/
####.*/g, "");
// remove markdown links
outtake = outtake.replace(/\[(.*?)\]\(.*?\)/g,
"$1"
// fixup and shorten
outtake = outtake.replace(/\.\n/g,
'. '
).replace(/[\n\t]/g,
' '
// remove markdown code
outtake = outtake.replace(/```.*?```/g,
// shorten
outtake = outtake.substring(0,250) +
'...'
return
outtake;
};
scan =
searchIndex = lunr(
this
.field(
'title'
, { boost: 10 });
'post'
'imagetext'
});
env.info(
"SearchManager: Building index"
// init lunr index
db.view(
'posts/byDateTime'
, {},
(err, doc) {
if
(err) {
"CouchDB: DB Read error"
}
allPosts = {};
for
(
i
in
doc) {
dbDoc = doc[i].value;
searchDoc = {
id: dbDoc.readableKey,
title: dbDoc.title,
post: dbDoc.htmlContent,
imagetext: dbDoc.pictureSubText +
''
postDoc = {
readableKey: dbDoc.readableKey,
outtake: createOuttake(dbDoc.htmlContent),
readableDateTime: misc.prettyDate(dbDoc.dateTime),
author: dbDoc.author
" - Indexing: "
+ searchDoc.id);
searchIndex.add(searchDoc);
allPosts[dbDoc.readableKey] = postDoc;
{
init:
(dbInit) {
db = dbInit;
scan();
},
rescan:
search:
(term) {
'SearchManager: Searching for "'
+ term +
'"'
result = searchIndex.search(term);
docResults = [];
// populate docResults with docs from allPosts
i = 0; i < result.length; i++) {
docResults.push(allPosts[result[i][
"ref"
]]);
docResults;
}());
typeof
(exports) !=
'undefined'
? exports.SearchManager = SearchManager :
/*****************************************
* Search Manager
*****************************************
* Author: mikael.lofjard@gmail.com
* Website: http://lofjard.se
* License: MIT License
****************************************/
var
SearchManager = (
function
() {
var
env = require(
'./environmentManager'
).EnvironmentManager;
var
misc = require(
'./misc'
).Misc;
var
lunr = require(
'./forked/lunr'
);
var
db =
null
;
var
searchIndex =
null
;
var
allPosts =
null
;
var
createOuttake =
function
(text) {
// remove <del> content
var
outtake = text.replace(/<del.*?>.*?<\/del>/g,
""
);
// remove html
outtake = outtake.replace(/<.*?>/g,
""
);
// remove markdown headers
outtake = outtake.replace(/
####.*/g, "");
// remove markdown links
outtake = outtake.replace(/\[(.*?)\]\(.*?\)/g,
"$1"
);
// fixup and shorten
outtake = outtake.replace(/\.\n/g,
'. '
).replace(/[\n\t]/g,
' '
);
// remove markdown code
outtake = outtake.replace(/```.*?```/g,
""
);
// shorten
outtake = outtake.substring(0,250) +
'...'
;
return
outtake;
};
var
scan =
function
() {
searchIndex = lunr(
function
() {
this
.field(
'title'
, { boost: 10 });
this
.field(
'post'
);
this
.field(
'imagetext'
);
});
env.info(
"SearchManager: Building index"
);
// init lunr index
db.view(
'posts/byDateTime'
, {},
function
(err, doc) {
if
(err) {
env.info(
"CouchDB: DB Read error"
);
return
;
}
allPosts = {};
for
(
var
i
in
doc) {
var
dbDoc = doc[i].value;
var
searchDoc = {
id: dbDoc.readableKey,
title: dbDoc.title,
post: dbDoc.htmlContent,
imagetext: dbDoc.pictureSubText +
''
};
var
postDoc = {
readableKey: dbDoc.readableKey,
title: dbDoc.title,
outtake: createOuttake(dbDoc.htmlContent),
readableDateTime: misc.prettyDate(dbDoc.dateTime),
author: dbDoc.author
}
env.info(
" - Indexing: "
+ searchDoc.id);
searchIndex.add(searchDoc);
allPosts[dbDoc.readableKey] = postDoc;
}
});
}
return
{
init:
function
(dbInit) {
db = dbInit;
scan();
},
rescan:
function
() {
scan();
},
search:
function
(term) {
env.info(
'SearchManager: Searching for "'
+ term +
'"'
);
var
result = searchIndex.search(term);
var
docResults = [];
// populate docResults with docs from allPosts
for
(
var
i = 0; i < result.length; i++) {
docResults.push(allPosts[result[i][
"ref"
]]);
}
return
docResults;
}
};
}());
typeof
(exports) !=
'undefined'
? exports.SearchManager = SearchManager :
null
;