Skip to content

Commit

Permalink
Merge pull request hound-search#152 from morria/master
Browse files Browse the repository at this point in the history
Adding open search description to enable search from the URL bar
  • Loading branch information
kellegous committed Sep 4, 2015
2 parents 3537d31 + c0d0ba5 commit 71e2150
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 26 deletions.
3 changes: 3 additions & 0 deletions ui/assets/index.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<title>Hound</title>
<link rel="stylesheet" href="css/octicons/octicons.css">
<link rel="stylesheet" href="css/hound.css">
<link rel="search" href="//{{ .Host }}/open_search.xml"
type="application/opensearchdescription+xml"
title="Hound" />
</head>
<body>
<div id="root">
Expand Down
10 changes: 10 additions & 0 deletions ui/assets/open_search.tpl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>

<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>Hound</ShortName>
<Description>Search code with Hound</Description>
<Tags>Hound</Tags>
<Url type="text/html"
method="get"
template="http://{{ .Host }}/?q={searchTerms}" />
</OpenSearchDescription>
68 changes: 46 additions & 22 deletions ui/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ui/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func init() {
},
},

"/open_search.xml": &content{
template: "open_search.tpl.xml",
},

"/excluded_files.html": &content{
template: "excluded_files.tpl.html",
sources: []string{
Expand Down
10 changes: 6 additions & 4 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ func (h *devHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// If so, render the HTML
w.Header().Set("Content-Type", "text/html;charset=utf-8")
if err := renderForDev(w, h.root, cr, h.cfg); err != nil {
if err := renderForDev(w, h.root, cr, h.cfg, r); err != nil {
log.Panic(err)
}
}

// Renders a templated asset in dev-mode. This simply embeds external script tags
// for the source elements.
func renderForDev(w io.Writer, root string, c *content, cfg *config.Config) error {
func renderForDev(w io.Writer, root string, c *content, cfg *config.Config, r *http.Request) error {
t, err := template.ParseFiles(
filepath.Join(root, c.template))
if err != nil {
Expand Down Expand Up @@ -89,6 +89,7 @@ func renderForDev(w io.Writer, root string, c *content, cfg *config.Config) erro
"jQueryVersion": JQueryVersion,
"ReposAsJson": json,
"Source": template.HTML(buf.String()),
"Host": r.Host,
})
}

Expand Down Expand Up @@ -117,7 +118,7 @@ func (h *prdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ct := h.content[p]
if ct != nil {
// if so, render it
if err := renderForPrd(w, ct, h.cfgJson); err != nil {
if err := renderForPrd(w, ct, h.cfgJson, r); err != nil {
log.Panic(err)
}
return
Expand All @@ -131,7 +132,7 @@ func (h *prdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// Renders a templated asset in prd-mode. This strategy will embed
// the sources directly in a script tag on the templated page.
func renderForPrd(w io.Writer, c *content, cfgJson string) error {
func renderForPrd(w io.Writer, c *content, cfgJson string, r *http.Request) error {

var buf bytes.Buffer
buf.WriteString("<script>")
Expand All @@ -149,6 +150,7 @@ func renderForPrd(w io.Writer, c *content, cfgJson string) error {
"jQueryVersion": JQueryVersion,
"ReposAsJson": cfgJson,
"Source": template.HTML(buf.String()),
"Host": r.Host,
})
}

Expand Down

0 comments on commit 71e2150

Please sign in to comment.