Skip to content

Commit

Permalink
add google custom search support
Browse files Browse the repository at this point in the history
fix gist style
  • Loading branch information
yangjian committed Feb 5, 2014
1 parent 8640537 commit 38c2dc4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
5 changes: 5 additions & 0 deletions _config.yml
Expand Up @@ -84,3 +84,8 @@ google_analytics:
## You MUST upgrade to Universal Analytics first!
## https://developers.google.com/analytics/devguides/collection/upgrade/?hl=zh_CN

#### Custom Search
google_cse:
enable: true
cx: 000561263943549425496:mrzrm0gr4kg ## e.g. 000561263943549425496:mrzrm0gr4kg your Custom Search ID.
## https://www.google.com/cse/
15 changes: 12 additions & 3 deletions layout/_partial/header.ejs
Expand Up @@ -16,11 +16,20 @@
<% for (var i in theme.menu){ %>
<li><a href="<%- theme.menu[i] %>"><%= i %></a></li>
<% } %>
<li><form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
<li>
<% if (theme.google_cse.enable){ %>
<form class="search" action="<%- config.root %>search/index.html" method="get" accept-charset="utf-8">
<label>Search</label>
<input type="search" id="search" autocomplete="off" maxlength="20" placeholder="Search" />
<input type="search" id="search" autocomplete="off" name="q" maxlength="20" placeholder="Search" />
</form>
<% } else { %>
<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
<label>Search</label>
<input type="search" id="search" name="q" autocomplete="off" maxlength="20" placeholder="Search" />
<input type="hidden" name="q" value="site:<%- config.url.replace(/^https?:\/\//, '') %>">
</form></li>
</form>
<% } %>
</li>
</ul>
</nav>
</div>
21 changes: 21 additions & 0 deletions layout/_partial/search.ejs
@@ -0,0 +1,21 @@
<% if(theme.google_cse.enable) { %>
<div id="main" class="page">
<div id='cse' style='width:100%;text-align:center'>正在加载搜索结果,请稍等。</div>
</div>
<script src="http://www.google.com.hk/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1', {language : 'zh-CN',style: google.loader.themes.MINIMALIST});
google.setOnLoadCallback(function(){
var customSearchControl = new google.search.CustomSearchControl('<%= theme.google_cse.cx %>');
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
var options = new google.search.DrawOptions();
options.setAutoComplete(true);
customSearchControl.draw('cse', options);
var match = location.search.match(/q=([^&]*)(&|$)/);
if(match && match[1]){
var search = decodeURIComponent(match[1]);
customSearchControl.execute(search);
}
},true);
</script>
<% } %>
12 changes: 12 additions & 0 deletions layout/layout.ejs
Expand Up @@ -29,6 +29,18 @@
<% }else{ %>
<%- page.content %>
<% } %>
<% } else if(page.layout=='search'){ %>
<%- partial('_partial/head') %>
<body>
<header>
<%- partial('_partial/header') %>
</header>
<div id="container">
<%- partial('_partial/search')%>
</div>
<%- partial('_partial/after_footer') %>
</body>
</html>
<% } else if(page.layout=='tags'){ %>
<%- partial('_partial/head') %>
<body>
Expand Down
1 change: 1 addition & 0 deletions source/css/_base/code.styl
Expand Up @@ -92,6 +92,7 @@ $line-numbers
font 0.85em font-serif
padding 0
margin-top 1em
text-shadow none
a
color color-blue
font-weight normal
Expand Down

0 comments on commit 38c2dc4

Please sign in to comment.