Permalink
Browse files

Added search for Reader's corner

  • Loading branch information...
mhlakhani committed Feb 27, 2014
1 parent 30d97b7 commit ee7cc3a0744d0ec48cd04c9fa215d0cbad480bdf
@@ -70,12 +70,14 @@ def namefilter(forbidden_urls, forbidden_domains, names, extra_names, whitelist,
'tag' : '/blog/tag/{tag}/',
'resume' : '/resume/',
'readerscornerhome' : '/readers-corner/',
'readerscornerpage' : '/readers-corner/{year}/{month}/'
'readerscornerpage' : '/readers-corner/{year}/{month}/',
'readerscornersearch' : '/readers-corner/search/',
'readerscornerjsonitem' : '/readers-corner/search/{id}.json'
}

base_deps = ['templates/base.haml', 'templates/macros.haml']
post_deps = ['templates/base.haml', 'templates/macros.haml', 'templates/blogpost.haml', 'tags']
readers_corner_deps = base_deps + ['readerscorner', 'readerscornersidebar']
readers_corner_deps = base_deps + ['readerscorner', 'readerscornersidebar', 'readerscornerindex']

sources = [
('Page', 'content/index.haml', {}, base_deps),
@@ -91,6 +93,8 @@ def namefilter(forbidden_urls, forbidden_domains, names, extra_names, whitelist,
('StaticContent', 'static/img/*/*/*', {}, []),
('Page', 'content/readerscornerhome.haml', {'readerscornersidebar' : 'sidebar'}, readers_corner_deps),
('ReadersCornerPage', 'content/readerscornerpage.haml', {'readerscornersidebar' : 'sidebar', 'readerscorner' : 'readerscorner'}, readers_corner_deps),
('Page', 'content/readerscornersearch.haml', {'readerscornersidebar' : 'sidebar', 'readerscornerindex' : 'readerscornerindex'}, readers_corner_deps),
('ReadersCornerJSONItem', 'content/readerscornerjsonitem.json', {'readerscornersidebar' : 'sidebar', 'readerscorner' : 'readerscorner'}, readers_corner_deps),
]

processors = [
@@ -103,7 +107,7 @@ def namefilter(forbidden_urls, forbidden_domains, names, extra_names, whitelist,
('PostArchives', {'key' : 'blogarchives'}),
('RSSFeed', {'key' : 'rss', 'count' : 5, 'title' : "Hasnain Lakhani's Blog", 'link': 'http://mhlakhani.com/blog/', 'description': "Hasnain Lakhani's Blog", 'sortkey': 'date'}),
('Sitemap', {'key' : 'sitemap', 'root' : 'http://mhlakhani.com'}),
('ReadersCorner', {'key' : 'readerscorner', 'filename' : 'links.txt', 'sidebarkey' : 'readerscornersidebar', 'route' : 'readerscornerpage', 'filter' : _filter})
('ReadersCorner', {'key' : 'readerscorner', 'filename' : 'links.txt', 'sidebarkey' : 'readerscornersidebar', 'indexkey' : 'readerscornerindex', 'route' : 'readerscornerpage', 'filter' : _filter, 'homeroute' : 'readerscornerhome', 'searchroute' : 'readerscornersearch', 'stopwords' : filterdata.stopwords})
]

data = {
@@ -14,7 +14,10 @@
%p
This is a collection of links that I've shared over the years (along with my comments where present), as an easily browsable archive.

-for (header, links) in sidebar.items()
%a href="{{ url_for("readerscornersearch") }}"
You can also search the archive here.

-for (header, links) in sidebar.items() if header != 'Main'
%h2 << {{ header }}

%ul.nav.nav-pills
@@ -0,0 +1,4 @@
{
"route": "readerscornerjsonitem",
"readerscornerpage": true
}
@@ -0,0 +1,115 @@
{
"route": "readerscornersearch",
"title": "Hasnain Lakhani's Blog - Reader's Corner Search",
"readerscornerpage": true
}

-extends "base.haml"

-block script

%script src="/static/js/jquery-2.1.0.min.js"
%script src="/static/js/jquery-ui.min.js"

%script
$(function() {
var invertedIndex = {{ readerscornerindex }};

var data = []

for (var key in invertedIndex) {
data.push({label: key, value: invertedIndex[key]})
}

$( "#search" ).autocomplete({
minLength: 2,
source: data,
focus: function( event, ui ) {
$( "#search" ).val( ui.item.label );
return false;
},
select: function( event, ui ) {
$( "#search" ).val( ui.item.label );

var urls = [];
for (var i in ui.item.value) {
urls.push( "{{ url_for("readerscornersearch") }}" + "/" + ui.item.value[i] + ".json");
}

var jxhr = [];
var result = [];
$.each(urls, function (i, url) {
jxhr.push(
$.getJSON(url, function (json) {
result.push(json);
})
);
});

$.when.apply($, jxhr).done(function() {
results = $( "#results" );
results.empty();
for (var i in result) {
item = result[i];
var baseItem = $( "<div class=\"alert alert-info\">" );
var name = "";
if ("name" in item) {
name = item.name;
}
baseItem.append( "<h3><a class=\"permalink\" href=\"" + item.link + "\" title=\"Full article\" rel=\"nofollow\">&raquo; " + name + "</a></h3>" );

if ("caption" in item) {
baseItem.append( "<p>" + item.caption + "</p>" );
}

if ("description" in item) {
baseItem.append( "<p><em>" + item.description + "</em></p>" );
}

baseItem.appendTo(results);

if ("message" in item) {
var quoteItem = $( "<blockquote>" );
quoteItem.append( "<p>" + item.message + "</p>" );
quoteItem.appendTo(results);
}

}
});

return false;
},
search: function( event, ui ) {
results = $( "#results" );
results.empty();
}
}).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
query = $("#search").val();
pos = item.label.search(query);
left = item.label.substring(0, pos);
mid = item.label.substring(pos, pos+query.length);
right = item.label.substring(pos+query.length, item.label.length);
return $( "<li>" ).append( "<a>" + left + "<b>" + mid + "</b>" + right + " (" + item.value.length + ")" + "</a>" ).appendTo( ul );
};
});

-block css

%link rel="stylesheet" href="/static/css/jquery-ui.css"

-block content

.row-fluid

%h1 << Reader's Corner Search

%br

%input.span9.search-query#search type="text" placeholder="Enter Query"

.clearfix

%br
%br

#results
Oops, something went wrong.

0 comments on commit ee7cc3a

Please sign in to comment.