fork of https://github.com/sourcegraph/zoekt
1// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package web
16
17import (
18 "html/template"
19 "log"
20)
21
22// Top provides the standard templates in parsed form
23var Top = template.New("top").Funcs(Funcmap)
24
25// TemplateText contains the text of the standard templates.
26var TemplateText = map[string]string{
27 "head": `
28<head>
29<meta charset="utf-8">
30<meta http-equiv="X-UA-Compatible" content="IE=edge">
31<meta name="viewport" content="width=device-width, initial-scale=1">
32<!-- Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) -->
33<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
34<style>
35 #navsearchbox { width: 350px !important; }
36 #maxhits { width: 100px !important; }
37 .label-dup {
38 border-width: 1px !important;
39 border-style: solid !important;
40 border-color: #aaa !important;
41 color: black;
42 }
43 .noselect {
44 user-select: none;
45 }
46 a.label-dup:hover {
47 color: black;
48 background: #ddd;
49 }
50 .result {
51 display: block;
52 content: " ";
53 visibility: hidden;
54 }
55 .container-results {
56 overflow: auto;
57 max-height: calc(100% - 72px);
58 }
59 .inline-pre {
60 border: unset;
61 background-color: unset;
62 margin: unset;
63 padding: unset;
64 overflow: unset;
65 }
66 :target { background-color: #ccf; }
67 table tbody tr td { border: none !important; padding: 2px !important; }
68</style>
69</head>
70 `,
71
72 "jsdep": `
73<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
74<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
75`,
76
77 // the template for the search box.
78 "searchbox": `
79<form action="search">
80 <div class="form-group form-group-lg">
81 <div class="input-group input-group-lg">
82 <input class="form-control" placeholder="Search for some code..." autofocus
83 {{if .Query}}
84 value={{.Query}}
85 {{end}}
86 id="searchbox" type="text" name="q">
87 <div class="input-group-btn">
88 <button class="btn btn-primary">Search</button>
89 </div>
90 </div>
91 </div>
92</form>
93`,
94
95 "navbar": `
96<nav class="navbar navbar-default">
97 <div class="container-fluid">
98 <div class="navbar-header">
99 <a class="navbar-brand" href="/">Zoekt</a>
100 <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false">
101 <span class="sr-only">Toggle navigation</span>
102 <span class="icon-bar"></span>
103 <span class="icon-bar"></span>
104 <span class="icon-bar"></span>
105 </button>
106 </div>
107 <div class="navbar-collapse collapse" id="navbar-collapse" aria-expanded="false" style="height: 1px;">
108 <form class="navbar-form navbar-left" action="search">
109 <div class="form-group">
110 <input class="form-control"
111 placeholder="Search for some code..." role="search"
112 id="navsearchbox" type="text" name="q" autofocus
113 {{if .Query}}
114 value={{.Query}}
115 {{end}}>
116 <div class="input-group">
117 <div class="input-group-addon">Max Results</div>
118 <input class="form-control" type="number" id="maxhits" name="num" value="{{.Num}}">
119 </div>
120 <button class="btn btn-primary">Search</button>
121 <!--Hack: we use a hidden form field to keep track of the debug flag across searches-->
122 {{if .Debug}}<input id="debug" name="debug" type="hidden" value="{{.Debug}}">{{end}}
123 </div>
124 </form>
125 </div>
126 </div>
127</nav>
128<script>
129document.onkeydown=function(e){
130 var e = e || window.event;
131 if (e.key == "/") {
132 var navbox = document.getElementById("navsearchbox");
133 if (document.activeElement !== navbox) {
134 navbox.focus();
135 return false;
136 }
137 }
138};
139</script>
140`,
141 // search box for the entry page.
142 "search": `
143<html>
144{{template "head"}}
145<title>Zoekt, en gij zult spinazie eten</title>
146<body>
147 <div class="jumbotron">
148 <div class="container">
149 {{template "searchbox" .Last}}
150 </div>
151 </div>
152
153 <div class="container">
154 <div class="row">
155 <div class="col-md-8">
156 <h3>Search examples:</h3>
157 <dl class="dl-horizontal">
158 <dt><a href="search?q=needle">needle</a></dt><dd>search for "needle"</dd>
159 <dt><a href="search?q=thread+or+needle">thread or needle</a></dt><dd>search for either "thread" or "needle"</dd>
160 <dt><a href="search?q=class+needle">class needle</a></span></dt><dd>search for files containing both "class" and "needle"</dd>
161 <dt><a href="search?q=class+Needle">class Needle</a></dt><dd>search for files containing both "class" (case insensitive) and "Needle" (case sensitive)</dd>
162 <dt><a href="search?q=class+Needle+case:yes">class Needle case:yes</a></dt><dd>search for files containing "class" and "Needle", both case sensitively</dd>
163 <dt><a href="search?q=%22class Needle%22">"class Needle"</a></dt><dd>search for files with the phrase "class Needle"</dd>
164 <dt><a href="search?q=needle+-hay">needle -hay</a></dt><dd>search for files with the word "needle" but not the word "hay"</dd>
165 <dt><a href="search?q=path+file:java">path file:java</a></dt><dd>search for the word "path" in files whose name contains "java"</dd>
166 <dt><a href="search?q=needle+lang%3Apython&num=50">needle lang:python</a></dt><dd>search for "needle" in Python source code</dd>
167 <dt><a href="search?q=f:%5C.c%24">f:\.c$</a></dt><dd>search for files whose name ends with ".c"</dd>
168 <dt><a href="search?q=path+-file:java">path -file:java</a></dt><dd>search for the word "path" excluding files whose name contains "java"</dd>
169 <dt><a href="search?q=foo.*bar">foo.*bar</a></dt><dd>search for the regular expression "foo.*bar"</dd>
170 <dt><a href="search?q=-%28Path File%29 Stream">-(Path File) Stream</a></dt><dd>search "Stream", but exclude files containing both "Path" and "File"</dd>
171 <dt><a href="search?q=-Path%5c+file+Stream">-Path\ file Stream</a></dt><dd>search "Stream", but exclude files containing "Path File"</dd>
172 <dt><a href="search?q=sym:data">sym:data</a></span></dt><dd>search for symbol definitions containing "data"</dd>
173 <dt><a href="search?q=phone+r:droid">phone r:droid</a></dt><dd>search for "phone" in repositories whose name contains "droid"</dd>
174 <dt><a href="search?q=phone+archived:no">phone archived:no</a></dt><dd>search for "phone" in repositories that are not archived</dd>
175 <dt><a href="search?q=phone+fork:no">phone fork:no</a></dt><dd>search for "phone" in repositories that are not forks</dd>
176 <dt><a href="search?q=phone+public:no">phone public:no</a></dt><dd>search for "phone" in repositories that are not public</dd>
177 <dt><a href="search?q=phone+b:master">phone b:master</a></dt><dd>for Git repos, find "phone" in files in branches whose name contains "master".</dd>
178 <dt><a href="search?q=phone+b:HEAD">phone b:HEAD</a></dt><dd>for Git repos, find "phone" in the default ('HEAD') branch.</dd>
179 </dl>
180 </div>
181 <div class="col-md-4">
182 <h3>To list repositories, try:</h3>
183 <dl class="dl-horizontal">
184 <dt><a href="search?q=r:droid">r:droid</a></dt><dd>list repositories whose name contains "droid".</dd>
185 <dt><a href="search?q=r:go+-r:google">r:go -r:google</a></dt><dd>list repositories whose name contains "go" but not "google".</dd>
186 </dl>
187 </div>
188 </div>
189 </div>
190 <nav class="navbar navbar-default navbar-bottom">
191 <div class="container">
192 {{template "footerBoilerplate"}}
193 <p class="navbar-text navbar-right">
194 Used {{HumanUnit .Stats.IndexBytes}} mem for
195 {{.Stats.Documents}} documents ({{HumanUnit .Stats.ContentBytes}})
196 from {{.Stats.Repos}} repositories.
197 </p>
198 </div>
199 </nav>
200</body>
201</html>
202`,
203 "footerBoilerplate": `<a class="navbar-text" href="about">About</a>`,
204 "results": `
205<html>
206{{template "head"}}
207<title>Results for {{.QueryStr}}</title>
208<script>
209 function zoektAddQ(atom) {
210 window.location.href = "/search?q=" + escape("{{.QueryStr}}" + " " + atom) +
211 "&" + "num=" + {{.Last.Num}};
212 }
213</script>
214<body id="results">
215 {{template "navbar" .Last}}
216 <div class="container-fluid container-results">
217 <h5>
218 {{if .Stats.Crashes}}<br><b>{{.Stats.Crashes}} shards crashed</b><br>{{end}}
219 {{ $fileCount := len .FileMatches }}
220 Found {{.Stats.MatchCount}} results in {{.Stats.FileCount}} files{{if or (lt $fileCount .Stats.FileCount) (or (gt .Stats.ShardsSkipped 0) (gt .Stats.FilesSkipped 0)) }},
221 showing top {{ $fileCount }} files (<a rel="nofollow"
222 href="search?q={{.Last.Query}}&num={{More .Last.Num}}">show more</a>).
223 {{else}}.{{end}}
224 </h5>
225 {{range .FileMatches}}
226 <table class="table table-hover table-condensed">
227 <thead>
228 <tr>
229 <th>
230 {{if .URL}}<a name="{{.ResultID}}" class="result"></a><a href="{{.URL}}" >{{else}}<a name="{{.ResultID}}">{{end}}
231 <small>
232 {{.Repo}}:{{.FileName}} {{if .ScoreDebug}}<i>({{.ScoreDebug}})</i>{{end}}</a>:
233 <span style="font-weight: normal">[ {{if .Branches}}{{range .Branches}}<span class="label label-default">{{.}}</span>,{{end}}{{end}} ]</span>
234 {{if .Language}}<button
235 title="restrict search to files written in {{.Language}}"
236 onclick="zoektAddQ('lang:"{{.Language}}"')" class="label label-primary">language {{.Language}}</button></span>{{end}}
237 {{if .DuplicateID}}<a class="label label-dup" href="#{{.DuplicateID}}">Duplicate result</a>{{end}}
238 </small>
239 </th>
240 </tr>
241 </thead>
242 {{if not .DuplicateID}}
243 <tbody>
244 {{range .Matches}}
245 {{if gt .LineNum 0}}
246 <tr>
247 <td style="background-color: rgba(238, 238, 255, 0.6);">
248 <pre class="inline-pre"><span class="noselect">{{if .URL}}<a href="{{.URL}}">{{end}}<u>{{.LineNum}}</u>{{if .URL}}</a>{{end}}: </span>{{range .Fragments}}{{LimitPre 100 .Pre}}<b>{{.Match}}</b>{{LimitPost 100 (TrimTrailingNewline .Post)}}{{end}} {{if .ScoreDebug}}<i>({{.ScoreDebug}})</i>{{end}}</pre>
249 </td>
250 </tr>
251 {{end}}
252 </tbody>
253 {{end}}
254 {{end}}
255 </table>
256 {{end}}
257
258 <nav class="navbar navbar-default navbar-bottom">
259 <div class="container">
260 {{template "footerBoilerplate"}}
261 <p class="navbar-text navbar-right">
262 Took {{.Stats.Duration}}{{if .Stats.Wait}} (queued: {{.Stats.Wait}}){{end}} for
263 {{HumanUnit .Stats.IndexBytesLoaded}}B index data,
264 {{.Stats.NgramMatches}} ngram matches,
265 {{.Stats.FilesConsidered}} docs considered,
266 {{.Stats.FilesLoaded}} docs ({{HumanUnit .Stats.ContentBytesLoaded}}B) loaded,
267 {{.Stats.ShardsScanned}} shards scanned,
268 {{.Stats.ShardsSkippedFilter}} shards filtered
269 {{- if or .Stats.FilesSkipped .Stats.ShardsSkipped -}}
270 , {{.Stats.FilesSkipped}} docs skipped, {{.Stats.ShardsSkipped}} shards skipped
271 {{- end -}}
272 .
273 </p>
274 </div>
275 </nav>
276 </div>
277 {{ template "jsdep"}}
278</body>
279</html>
280`,
281
282 "repolist": `
283<html>
284{{template "head"}}
285<body id="results">
286 <div class="container">
287 {{template "navbar" .Last}}
288 <div><b>
289 Found {{.Stats.Repos}} repositories ({{.Stats.Documents}} files, {{HumanUnit .Stats.ContentBytes}}B content)
290 </b></div>
291 <table class="table table-hover table-condensed">
292 <thead>
293 <tr>
294 {{- define "q"}}q={{.Last.Query}}{{if (gt .Last.Num 0)}}&num={{.Last.Num}}{{end}}{{end}}
295 <th>Name <a href="/search?{{template "q" .}}&order=name">▼</a><a href="/search?{{template "q" .}}&order=revname">▲</a></th>
296 <th>Last updated <a href="/search?{{template "q" .}}&order=revtime">▼</a><a href="/search?{{template "q" .}}&order=time">▲</a></th>
297 <th>Branches</th>
298 <th>Size <a href="/search?{{template "q" .}}&order=revsize">▼</a><a href="/search?{{template "q" .}}&order=size">▲</a></th>
299 <th>RAM <a href="/search?{{template "q" .}}&order=revram">▼</a><a href="/search?{{template "q" .}}&order=ram">▲</a></th>
300 </tr>
301 </thead>
302 <tbody>
303 {{range .Repos -}}
304 <tr>
305 <td>{{if .URL}}<a href="{{.URL}}">{{end}}{{.Name}}{{if .URL}}</a>{{end}}</td>
306 <td><small>{{.IndexTime.Format "Jan 02, 2006 15:04"}}</small></td>
307 <td style="vertical-align: middle;">
308 {{- range .Branches -}}
309 {{if .URL}}<tt><a class="label label-default small" href="{{.URL}}">{{end}}{{.Name}}{{if .URL}}</a> </tt>{{end}}
310 {{- end -}}
311 </td>
312 <td><small>{{HumanUnit .Files}} files ({{HumanUnit .Size}}B)</small></td>
313 <td><small>{{HumanUnit .MemorySize}}B</td>
314 </tr>
315 {{end}}
316 </tbody>
317 </table>
318 </div>
319
320 <nav class="navbar navbar-default navbar-bottom">
321 <div class="container">
322 {{template "footerBoilerplate"}}
323 <p class="navbar-text navbar-right">
324 </p>
325 </div>
326 </nav>
327
328 {{ template "jsdep"}}
329</body>
330</html>
331`,
332
333 "print": `
334<html>
335 {{template "head"}}
336 <title>{{.Repo}}:{{.Name}}</title>
337<body id="results">
338 {{template "navbar" .Last}}
339 <div class="container-fluid container-results" >
340 <div><b>{{.Name}}</b></div>
341 <div class="table table-hover table-condensed" style="overflow:auto; background: #eef;">
342 {{ range $index, $ln := .Lines}}
343 <pre id="l{{Inc $index}}" class="inline-pre"><span class="noselect"><a href="#l{{Inc $index}}">{{Inc $index}}</a>: </span>{{$ln}}</pre>
344 {{end}}
345 </div>
346 <nav class="navbar navbar-default navbar-bottom">
347 <div class="container">
348 {{template "footerBoilerplate"}}
349 <p class="navbar-text navbar-right">
350 </p>
351 </div>
352 </nav>
353 </div>
354 {{ template "jsdep"}}
355</body>
356</html>
357`,
358
359 "about": `
360
361<html>
362 {{template "head"}}
363 <title>About <em>zoekt</em></title>
364<body>
365
366
367 <div class="jumbotron">
368 <div class="container">
369 {{template "searchbox" .Last}}
370 </div>
371 </div>
372
373 <div class="container">
374 <p>
375 This is <a href="http://github.com/sourcegraph/zoekt"><em>zoekt</em> (IPA: /zukt/)</a>,
376 an open-source full text search engine. It's pronounced roughly as you would
377 pronounce "zooked" in English.
378 </p>
379 <p>
380 {{if .Version}}<em>Zoekt</em> version {{.Version}}, uptime{{else}}Uptime{{end}} {{.Uptime}}
381 </p>
382
383 <p>
384 Used {{HumanUnit .Stats.IndexBytes}} memory for
385 {{.Stats.Documents}} documents ({{HumanUnit .Stats.ContentBytes}})
386 from {{.Stats.Repos}} repositories.
387 </p>
388 </div>
389
390 <nav class="navbar navbar-default navbar-bottom">
391 <div class="container">
392 {{template "footerBoilerplate"}}
393 <p class="navbar-text navbar-right">
394 </p>
395 </div>
396 </nav>
397`,
398 "robots": `
399user-agent: *
400disallow: /search
401`,
402}
403
404func init() {
405 for k, v := range TemplateText {
406 _, err := Top.New(k).Parse(v)
407 if err != nil {
408 log.Panicf("parse(%s): %v:", k, err)
409 }
410 }
411}