func SearchJSON(ctx *middleware.Context) { q := ctx.Query("q") // Clean up keyword. q = strings.TrimFunc(q, func(c rune) bool { return unicode.IsSpace(c) || c == '"' }) fmt.Println(q) pinfos, err := models.SearchPkgInfo(7, q) if err != nil { log.ErrorD(4, "SearchPkgInfo '%s': %v", q, err) return } results := make([]*searchResult, len(pinfos)) for i := range pinfos { results[i] = &searchResult{ Title: pinfos[i].ImportPath, Description: pinfos[i].Synopsis, } } ctx.JSON(200, map[string]interface{}{ "results": results, }) }
func RefreshSearchContent() { items := make([]searchItem, 0, len(pathFlags)) for name := range pathFlags { items = append(items, searchItem{Title: name}) } data, err := json.Marshal(&items) if err != nil { log.ErrorD(4, "Fail to marshal search content: %v", err) return } SearchContent = string(data) }
// Handle handles and logs error by given status. func (ctx *Context) Handle(status int, title string, err error) { if err != nil { log.ErrorD(4, "%s: %v", title, err) if macaron.Env != macaron.PROD { ctx.Data["ErrorMsg"] = err } } switch status { case 404: ctx.Data["Title"] = "Page Not Found" case 500: ctx.Data["Title"] = "Internal Server Error" } ctx.HTML(status, base.TplName(fmt.Sprintf("status/%d", status))) }
func SearchJSON(ctx *middleware.Context) { q := ctx.QueryEscape("q") pinfos, err := models.SearchPkgInfo(7, q) if err != nil { log.ErrorD(4, "SearchPkgInfo '%s': %v", q, err) return } results := make([]*searchResult, len(pinfos)) for i := range pinfos { results[i] = &searchResult{ Title: pinfos[i].ImportPath, Description: pinfos[i].Synopsis, } } ctx.JSON(200, map[string]interface{}{ "results": results, }) }
// SavePkgDoc saves readered readme.md file data. func SavePkgDoc(docPath string, readmes map[string][]byte) { for lang, data := range readmes { if len(data) == 0 { continue } if data[0] == '\n' { data = data[1:] } data = com.Html2JS(data) localeDocPath := setting.DocsJsPath + docPath + "_RM_" + lang os.MkdirAll(path.Dir(localeDocPath), os.ModePerm) buf := new(bytes.Buffer) buf.WriteString("document.write(\"") buf.Write(data) buf.WriteString("\")") if err := ioutil.WriteFile(localeDocPath+".js", buf.Bytes(), 0655); err != nil { log.ErrorD(4, "SavePkgDoc( %s ): %v", localeDocPath, err) } } }
// SaveDocPage saves doc. content to JS file(s), // it returns max index of JS file(s); // it returns -1 when error occurs. func SaveDocPage(docPath string, data []byte) int { data = com.Html2JS(data) docPath = setting.DocsJsPath + docPath buf := new(bytes.Buffer) count := 0 d := string(data) l := len(d) if l < 80000 { buf.WriteString("document.write(\"") buf.Write(data) buf.WriteString("\")") os.MkdirAll(path.Dir(docPath+".js"), os.ModePerm) if err := ioutil.WriteFile(docPath+".js", buf.Bytes(), 0655); err != nil { log.ErrorD(4, "SaveDocPage( %s ): %v", docPath, err) return -1 } } else { // Too large, need to sperate. start := 0 end := start + 40000 for { if end >= l { end = l } else { // Need to break in space. for { if d[end-3:end] == "/b>" { break } end += 1 if end >= l { break } } } buf.WriteString("document.write(\"") buf.WriteString(d[start:end]) buf.WriteString("\")\n") p := docPath if count != 0 { p += fmt.Sprintf("-%d", count) } os.MkdirAll(path.Dir(p+".js"), os.ModePerm) if err := ioutil.WriteFile(p+".js", buf.Bytes(), 0655); err != nil { log.ErrorD(4, "SaveDocPage( %s ): %v", p, err) return -1 } if end >= l { break } buf.Reset() start = end end += 204800 count++ } } return count }