Skip to content

Commit

Permalink
Add support for custom url formats specified in the config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpcwang committed Jan 13, 2016
1 parent 3cfe61a commit d4443b3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
9 changes: 9 additions & 0 deletions doc/examples/livegrep/index.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"name": "livegrep",
"fs_paths": [
{
"name": "livegrep/livegrep",
"path": "src/",
"metadata": {
"url-pattern": "https://github.com/{name}/blob/HEAD/src/{path}#L{lno}"
}
}
],
"repositories": [
{
"name": "livegrep",
Expand Down
20 changes: 16 additions & 4 deletions server/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"log"
"net/url"
"sync"
"time"

Expand All @@ -22,7 +23,7 @@ var (
type Tree struct {
Name string
Version string
Github string
Url string
}

type I struct {
Expand Down Expand Up @@ -120,12 +121,23 @@ func (bk *Backend) refresh(info *client.ServerInfo) {
if len(info.Trees) > 0 {
bk.I.Trees = nil
for _, r := range info.Trees {
gh := ""
pattern := ""
if v, ok := r.Metadata["url-pattern"]; ok {
pattern = v.(string)
}
if v, ok := r.Metadata["github"]; ok {
gh = v.(string)
value := v.(string)
base := ""
_, err := url.ParseRequestURI(value)
if err != nil {
base = "https://github.com/" + value
} else {
base = value
}
pattern = base + "/blob/{version}/{path}#L{lno}"
}
bk.I.Trees = append(bk.I.Trees,
Tree{r.Name, r.Version, gh})
Tree{r.Name, r.Version, pattern})
}
}
}
12 changes: 5 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,22 @@ func (s *server) ServeRoot(ctx context.Context, w http.ResponseWriter, r *http.R
}

func (s *server) ServeSearch(ctx context.Context, w http.ResponseWriter, r *http.Request) {
gh := make(map[string]map[string]string, len(s.bk))
urls := make(map[string]map[string]string, len(s.bk))
backends := make([]*Backend, 0, len(s.bk))
for _, bk := range s.bk {
backends = append(backends, bk)
bk.I.Lock()
m := make(map[string]string, len(bk.I.Trees))
gh[bk.Id] = m
urls[bk.Id] = m
for _, r := range bk.I.Trees {
if r.Github != "" {
m[r.Name] = r.Github
}
m[r.Name] = r.Url
}
bk.I.Unlock()
}
data := &struct {
GithubRepos map[string]map[string]string
RepoUrls map[string]map[string]string
Backends []*Backend
}{gh, backends}
}{urls, backends}

body, err := executeTemplate(s.T.Index, data)
if err != nil {
Expand Down
23 changes: 9 additions & 14 deletions web/htdocs/assets/js/codesearch_ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,21 @@ var Match = Backbone.Model.extend({

var repo_map;
var backend = Codesearch.in_flight.backend;
repo_map = CodesearchUI.github_repos[backend];
repo_map = CodesearchUI.repo_urls[backend];
if (!repo_map) {
return null;
}
if (!repo_map[name]) {
return null;
}

var base;
// If 'github' metadata is already a URL, pass it
// through, but otherwise asume it's a user/repo on
// the public github site.
try {
base = new URL(repo_map[name]).toString();
} catch(e) {
base = "https://github.com/" + repo_map[name];
}
return base +
"/blob/" + shorten(ref) + "/" + this.get('path') +
"#L" + this.get('lno');
// the order of these replacements is used to minimize conflicts
var url = repo_map[name];
url = url.replace('{lno}', this.get('lno'));
url = url.replace('{version}', shorten(ref));
url = url.replace('{name}', name);
url = url.replace('{path}', this.get('path'));
return url;
}
});

Expand Down Expand Up @@ -413,7 +408,7 @@ var CodesearchUI = function() {
search_done: function(search, time, why) {
CodesearchUI.state.handle_done(search, time, why);
},
github_repos: {}
repo_urls: {}
};
}();
CodesearchUI.onload();
Expand Down
2 changes: 1 addition & 1 deletion web/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script type="text/javascript">
$(function(){CodesearchUI.github_repos = {{.GithubRepos}};});
$(function(){CodesearchUI.repo_urls = {{.RepoUrls}};});
</script>
<div id='searcharea'>
<div id='searchinput'>
Expand Down

0 comments on commit d4443b3

Please sign in to comment.