add search button to mallard pages

This commit is contained in:
Frédéric Péters 2015-11-28 17:23:04 +01:00
parent d92e30a1e1
commit 1acafe929c
6 changed files with 156 additions and 2 deletions

View File

@ -0,0 +1,46 @@
.header {
position: relative;
z-index: 10;
}
#search-input {
float: right;
display: none;
}
#search-button {
float: right;
text-indent: -10000px;
background: transparent url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAQAAAC1QeVaAAAAAnNCSVQICFXsRgQAAAAJcEhZcwAADdcAAA3XAUIom3gAAAAZdEVYdFNvZnR3YXJlAHd3dy5pbmtzY2FwZS5vcmeb7jwaAAAAx0lEQVQY033PTwqCQBQG8GkjdICENmVbpfISQoWffwJPVoJeIFDoBF2gtrloVZA30EAQhOkJjeOiesPAfPx4M28Yo8IMMXLUtCNPY/1yVniBd6t0rI6oq6WDqwcKDCR0LjZTgXFL8h6klEMRcnBX7z2yIHwIrMEDRWIwJKwEPikYEu0l5bvAiELS2QBHcHv3SZ6Gkjh158GQuojQeKYcwULR+2dDO/NVOf4EIR6ocHf2GxNX4tt2zL7VevSXfRUZ8YWxX3y2T2+KPGKNWhiWHQAAAABJRU5ErkJggg==) center center no-repeat;
border: none;
width: 26px;
height: 26px;
}
#search-results {
display: none;
position: absolute;
background: white;
max-width: 58em;
width: 100%;
min-height: 10em;
top: 3em;
}
.body.search-dim {
opacity: 0.05;
}
#search-results ul {
list-style: none;
}
#search-results ul li a {
display: block;
border: 1px solid #666;
padding: 1ex;
}
#search-results ul li a:hover {
text-decoration: none;
background: #ddd;
}

View File

@ -0,0 +1,58 @@
$(function() {
var search_button = $('<button id="search-button">/</button>');
var search_input = $('<input id="search-input" type="search"></input>');
var search_results = $('<div id="search-results"><ul><li><a href="#">sdsdsd </a></li></ul></div>');
var search_index = null;
var pages = Object();
search_results.prependTo($('div.header'));
search_input.prependTo($('div.header'));
search_button.prependTo($('div.header'));
$(search_button).click(function() {
search_input.toggle();
if ($('#search-input:visible').length == 0) {
search_results.hide();
search_input.val('');
} else {
search_input.focus();
}
$('.body').toggleClass('search-dim');
if (search_index === null) {
$('<script src="../../lunr.min.js"></script>').appendTo('body');
$('<script src="../../lunr.stemmer.support.min.js"></script>').appendTo('body');
$('<script src="../../lunr.fr.min.js"></script>').appendTo('body');
search_index = lunr(function () {
this.use(lunr.fr);
this.field('title', {boost: 10})
this.field('other_titles', {boost: 5})
this.field('text')
this.ref('id')
});
$.ajax({
type: 'GET',
url: 'pages.json',
success: function(data) {
$(data).each(function(index, elem) {
pages[elem.id] = elem.title;
search_index.add(elem);
});
},
dataType: 'json',
async: true
});
}
});
$(search_input).keyup(function() {
var search_txt = $(search_input).val();
$('#search-results').show();
$('#search-results ul').empty();
if (search_txt.length > 3 && search_index) {
console.log('search txt:', search_txt);
var results = search_index.search(search_txt);
$(results).each(function(index, elem) {
console.log(elem);
$('<li><a href="' + elem.ref + '.html">' + pages[elem.ref] + '</li>').appendTo('#search-results ul');
});
console.log(results);
}
});
});

18
js/lunr.fr.min.js vendored Normal file

File diff suppressed because one or more lines are too long

7
js/lunr.min.js vendored Normal file

File diff suppressed because one or more lines are too long

1
js/lunr.stemmer.support.min.js vendored Normal file
View File

@ -0,0 +1 @@
!function(r,t){"function"==typeof define&&define.amd?define(t):"object"==typeof exports?module.exports=t():t()(r.lunr)}(this,function(){return function(r){r.stemmerSupport={Among:function(r,t,i,s){if(this.toCharArray=function(r){for(var t=r.length,i=new Array(t),s=0;t>s;s++)i[s]=r.charCodeAt(s);return i},!r&&""!=r||!t&&0!=t||!i)throw"Bad Among initialisation: s:"+r+", substring_i: "+t+", result: "+i;this.s_size=r.length,this.s=this.toCharArray(r),this.substring_i=t,this.result=i,this.method=s},SnowballProgram:function(){var r;return{bra:0,ket:0,limit:0,cursor:0,limit_backward:0,setCurrent:function(t){r=t,this.cursor=0,this.limit=t.length,this.limit_backward=0,this.bra=this.cursor,this.ket=this.limit},getCurrent:function(){var t=r;return r=null,t},in_grouping:function(t,i,s){if(this.cursor<this.limit){var e=r.charCodeAt(this.cursor);if(s>=e&&e>=i&&(e-=i,t[e>>3]&1<<(7&e)))return this.cursor++,!0}return!1},in_grouping_b:function(t,i,s){if(this.cursor>this.limit_backward){var e=r.charCodeAt(this.cursor-1);if(s>=e&&e>=i&&(e-=i,t[e>>3]&1<<(7&e)))return this.cursor--,!0}return!1},out_grouping:function(t,i,s){if(this.cursor<this.limit){var e=r.charCodeAt(this.cursor);if(e>s||i>e)return this.cursor++,!0;if(e-=i,!(t[e>>3]&1<<(7&e)))return this.cursor++,!0}return!1},out_grouping_b:function(t,i,s){if(this.cursor>this.limit_backward){var e=r.charCodeAt(this.cursor-1);if(e>s||i>e)return this.cursor--,!0;if(e-=i,!(t[e>>3]&1<<(7&e)))return this.cursor--,!0}return!1},eq_s:function(t,i){if(this.limit-this.cursor<t)return!1;for(var s=0;t>s;s++)if(r.charCodeAt(this.cursor+s)!=i.charCodeAt(s))return!1;return this.cursor+=t,!0},eq_s_b:function(t,i){if(this.cursor-this.limit_backward<t)return!1;for(var s=0;t>s;s++)if(r.charCodeAt(this.cursor-t+s)!=i.charCodeAt(s))return!1;return this.cursor-=t,!0},find_among:function(t,i){for(var s=0,e=i,n=this.cursor,u=this.limit,o=0,h=0,c=!1;;){for(var f=s+(e-s>>1),a=0,l=h>o?o:h,_=t[f],b=l;b<_.s_size;b++){if(n+l==u){a=-1;break}if(a=r.charCodeAt(n+l)-_.s[b])break;l++}if(0>a?(e=f,h=l):(s=f,o=l),1>=e-s){if(s>0||e==s||c)break;c=!0}}for(;;){var _=t[s];if(o>=_.s_size){if(this.cursor=n+_.s_size,!_.method)return _.result;var m=_.method();if(this.cursor=n+_.s_size,m)return _.result}if(s=_.substring_i,0>s)return 0}},find_among_b:function(t,i){for(var s=0,e=i,n=this.cursor,u=this.limit_backward,o=0,h=0,c=!1;;){for(var f=s+(e-s>>1),a=0,l=h>o?o:h,_=t[f],b=_.s_size-1-l;b>=0;b--){if(n-l==u){a=-1;break}if(a=r.charCodeAt(n-1-l)-_.s[b])break;l++}if(0>a?(e=f,h=l):(s=f,o=l),1>=e-s){if(s>0||e==s||c)break;c=!0}}for(;;){var _=t[s];if(o>=_.s_size){if(this.cursor=n-_.s_size,!_.method)return _.result;var m=_.method();if(this.cursor=n-_.s_size,m)return _.result}if(s=_.substring_i,0>s)return 0}},replace_s:function(t,i,s){var e=s.length-(i-t),n=r.substring(0,t),u=r.substring(i);return r=n+s+u,this.limit+=e,this.cursor>=i?this.cursor+=e:this.cursor>t&&(this.cursor=t),e},slice_check:function(){if(this.bra<0||this.bra>this.ket||this.ket>this.limit||this.limit>r.length)throw"faulty slice operation"},slice_from:function(r){this.slice_check(),this.replace_s(this.bra,this.ket,r)},slice_del:function(){this.slice_from("")},insert:function(r,t,i){var s=this.replace_s(r,t,i);r<=this.bra&&(this.bra+=s),r<=this.ket&&(this.ket+=s)},slice_to:function(){return this.slice_check(),r.substring(this.bra,this.ket)},eq_v_b:function(r){return this.eq_s_b(r.length,r)}}}}}});

View File

@ -6,13 +6,17 @@ import shutil
import subprocess import subprocess
import tarfile import tarfile
import tempfile import tempfile
import re
import urllib2 import urllib2
import urlparse import urlparse
import xml.etree.ElementTree as ET
from django.template import Template, Context from django.template import Template, Context
from django.conf import settings from django.conf import settings
settings.configure() settings.configure()
MAL_NS = 'http://projectmallard.org/1.0/'
jhbuildrc = os.path.join(os.getcwd(), 'jhbuildrc') jhbuildrc = os.path.join(os.getcwd(), 'jhbuildrc')
checkouts_directory = os.path.join(os.getcwd(), '..', 'src') checkouts_directory = os.path.join(os.getcwd(), '..', 'src')
web_directory = os.path.join(os.getcwd(), '..', 'web') web_directory = os.path.join(os.getcwd(), '..', 'web')
@ -68,13 +72,14 @@ def publish_mallard(module, branch, directory):
if os.path.islink(index_html): if os.path.islink(index_html):
os.unlink(index_html) os.unlink(index_html)
page_files = [os.path.join(help_dir, x) for x in os.listdir(help_dir) if x.endswith('.page')]
cmd = ['jhbuild', '-f', jhbuildrc, 'run', 'yelp-build', 'html', cmd = ['jhbuild', '-f', jhbuildrc, 'run', 'yelp-build', 'html',
'-o', output_dir, '-x'] '-o', output_dir, '-x']
if module.get('branch') == 'dev': if module.get('branch') == 'dev':
cmd.append('mallard_extra_dev.xsl') cmd.append('mallard_extra_dev.xsl')
else: else:
cmd.append('mallard_extra.xsl') cmd.append('mallard_extra.xsl')
cmd.extend([os.path.join(help_dir, x) for x in os.listdir(help_dir) if x.endswith('.page')]) cmd.extend(page_files)
subprocess.call(cmd, **kws) subprocess.call(cmd, **kws)
for static in ('brainstew.js', 'brainstew.css'): for static in ('brainstew.js', 'brainstew.css'):
shutil.copy(static, output_dir) shutil.copy(static, output_dir)
@ -84,13 +89,29 @@ def publish_mallard(module, branch, directory):
if html_files: if html_files:
os.symlink(html_files[0], index_html) os.symlink(html_files[0], index_html)
def remove_tags(string):
return re.sub('<.*?>', '', string, flags=re.DOTALL)
pages = []
for page_file in page_files:
text = open(page_file).read()
titles = re.findall('<title.*>(.*)<\/title>', text)
pages.append({
'id': os.path.basename(page_file).rsplit('.', 1)[0],
'title': remove_tags(titles[0]),
'other_titles': remove_tags(' '.join(titles[1:])),
'text': remove_tags(text),
})
json.dump(pages, open(os.path.join(output_dir, 'pages.json'), 'w'), indent=2)
if not os.path.exists('../mal2latex'): if not os.path.exists('../mal2latex'):
return return
temp_dir = tempfile.mkdtemp() temp_dir = tempfile.mkdtemp()
cache_file = os.path.join(temp_dir, '%s.cache' % module_name) cache_file = os.path.join(temp_dir, '%s.cache' % module_name)
cmd = ['jhbuild', '-f', jhbuildrc, 'run', 'yelp-build', 'cache', '-o', cache_file] cmd = ['jhbuild', '-f', jhbuildrc, 'run', 'yelp-build', 'cache', '-o', cache_file]
cmd.extend([os.path.join(help_dir, x) for x in os.listdir(help_dir) if x.endswith('.page')]) cmd.extend(page_files)
subprocess.call(cmd, **kws) subprocess.call(cmd, **kws)
if os.path.exists(cache_file): if os.path.exists(cache_file):
@ -162,4 +183,7 @@ for module in modules:
module['branch'] = 'dev' module['branch'] = 'dev'
publish_mallard(module, 'master', 'dev') publish_mallard(module, 'master', 'dev')
for js_file in os.listdir('js'):
shutil.copy('js/' + js_file, web_directory)
create_index(modules) create_index(modules)