func Home(ctx *middleware.Context) { ctx.Data["PageIsHome"] = true ctx.Data["NumOfPackages"] = base.FormatNumString(models.NumOfPackages()) ctx.Data["SearchContent"] = doc.SearchContent ctx.Data["BrowseHistory"] = getHistory(ctx) ctx.HTML(200, HOME) }
// getHistory returns browse history. func getHistory(ctx *middleware.Context) []*models.PkgInfo { pairs := strings.Split(ctx.GetCookie("user_history"), "|") pkgs := make([]*models.PkgInfo, 0, len(pairs)) for _, pair := range pairs { infos := strings.Split(pair, ":") if len(infos) != 2 { continue } pid := com.StrTo(infos[0]).MustInt64() if pid == 0 { continue } pinfo, _ := models.GetPkgInfoById(pid) if pinfo == nil { continue } pinfo.LastView = com.StrTo(infos[1]).MustInt64() pkgs = append(pkgs, pinfo) } return pkgs }
// updateHistory updates browser history. func updateHistory(ctx *middleware.Context, id int64) { pairs := make([]string, 1, 10) pairs[0] = com.ToStr(id) + ":" + com.ToStr(time.Now().UTC().Unix()) count := 0 for _, pair := range strings.Split(ctx.GetCookie("user_history"), "|") { infos := strings.Split(pair, ":") if len(infos) != 2 { continue } pid := com.StrTo(infos[0]).MustInt64() if pid == 0 || pid == id { continue } pairs = append(pairs, pair) count++ if count == 9 { break } } ctx.SetCookie("user_history", strings.Join(pairs, "|"), 9999999) }
func specialHandles(ctx *middleware.Context, pinfo *models.PkgInfo) bool { // Only show imports. if strings.HasSuffix(ctx.Req.RequestURI, "?imports") { ctx.Data["PageIsImports"] = true ctx.Data["Packages"] = models.GetPkgInfosByPaths(strings.Split(pinfo.ImportPaths, "|")) ctx.HTML(200, DOCS_IMPORTS) return true } // Only show references. if strings.HasSuffix(ctx.Req.RequestURI, "?refs") { ctx.Data["PageIsRefs"] = true ctx.Data["Packages"] = pinfo.GetRefs() ctx.HTML(200, DOCS_IMPORTS) return true } // Refresh documentation. if strings.HasSuffix(ctx.Req.RequestURI, "?refresh") { if !pinfo.CanRefresh() { ctx.Flash.Info(ctx.Tr("docs.refresh.too_often")) } else { importPath := ctx.Params("*") _, err := doc.CheckPackage(importPath, ctx.Render, doc.REQUEST_TYPE_REFRESH) if err != nil { handleError(ctx, err) return true } } ctx.Redirect(ctx.Data["Link"].(string)) return true } return false }
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 Search(ctx *middleware.Context) { q := ctx.Query("q") if ctx.Query("auto_redirect") == "true" && (base.IsGoRepoPath(q) || base.IsGAERepoPath(q) || base.IsValidRemotePath(q)) { ctx.Redirect("/" + q) return } var ( results []*models.PkgInfo err error ) switch q { case "gorepos": results, err = models.GetGoRepos() case "gosubrepos": results, err = models.GetGoSubepos() case "gaesdk": results, err = models.GetGAERepos() default: results, err = models.SearchPkgInfo(100, q) } if err != nil { ctx.Flash.Error(err.Error(), true) } else { ctx.Data["Results"] = results } ctx.Data["Keyword"] = q ctx.HTML(200, SEARCH) }
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, }) }
func handleError(ctx *middleware.Context, err error) { importPath := ctx.Params("*") if err == doc.ErrInvalidRemotePath { ctx.Redirect("/search?q=" + importPath) return } ctx.Flash.Error(importPath+": "+err.Error(), true) ctx.Flash.Info(ctx.Tr("form.click_to_search", importPath), true) Home(ctx) }
func handleError(ctx *middleware.Context, err error) { importPath := ctx.Params("*") if err == doc.ErrInvalidRemotePath { ctx.Redirect("/search?q=" + importPath) return } ctx.Flash.Error(importPath+": "+strings.Replace(err.Error(), setting.GitHubCredentials, "<GitHubCredentials>", -1), true) ctx.Flash.Info(ctx.Tr("form.click_to_search", importPath), true) Home(ctx) }
func Search(ctx *middleware.Context) { q := ctx.Query("q") if ctx.Query("auto_redirect") == "true" && (base.IsGoRepoPath(q) || base.IsValidRemotePath(q)) { ctx.Redirect("/" + q) return } results, err := models.SearchPkgInfo(100, q) if err != nil { ctx.Flash.Error(err.Error(), true) } else { ctx.Data["Results"] = results } ctx.Data["Keyword"] = q ctx.HTML(200, SEARCH) }
func handleError(ctx *middleware.Context, err error) { importPath := ctx.Params("*") if err == doc.ErrInvalidRemotePath { ctx.Redirect("/search?q=" + importPath) return } if strings.Contains(err.Error(), "<meta> not found") || strings.Contains(err.Error(), "resource not found") { models.DeletePackageByPath(importPath) } ctx.Flash.Error(importPath+": "+strings.Replace(err.Error(), setting.GitHubCredentials, "<GitHubCredentials>", -1), true) ctx.Flash.Info(ctx.Tr("form.click_to_search", importPath), true) Home(ctx) }
func Home(ctx *middleware.Context) { ctx.Data["PageIsHome"] = true ctx.Data["SearchContent"] = doc.SearchContent ctx.Data["BrowseHistory"] = getHistory(ctx) ctx.HTML(200, HOME) }
func Docs(ctx *middleware.Context) { importPath := ctx.Params("*") pinfo, err := doc.CheckPackage(importPath, ctx.Render, doc.REQUEST_TYPE_HUMAN) if err != nil { handleError(ctx, err) return } ctx.Data["PageIsDocs"] = true ctx.Data["Title"] = pinfo.ImportPath ctx.Data["ParentPath"] = path.Dir(pinfo.ImportPath) ctx.Data["ProjectName"] = path.Base(pinfo.ImportPath) ctx.Data["ProjectPath"] = pinfo.ProjectPath if specialHandles(ctx, pinfo) { return } if pinfo.IsGoRepo { ctx.Flash.Info(ctx.Tr("docs.turn_into_search", importPath), true) } ctx.Data["PkgDesc"] = pinfo.Synopsis // README. lang := ctx.Data["Lang"].(string)[:2] readmePath := setting.DocsJsPath + pinfo.ImportPath + "_RM_" + lang + ".js" if com.IsFile(readmePath) { ctx.Data["IsHasReadme"] = true ctx.Data["ReadmePath"] = readmePath } else { readmePath := setting.DocsJsPath + pinfo.ImportPath + "_RM_en.js" if com.IsFile(readmePath) { ctx.Data["IsHasReadme"] = true ctx.Data["ReadmePath"] = readmePath } } // Documentation. docJS := make([]string, 0, pinfo.JsNum+1) docJS = append(docJS, setting.DocsJsPath+importPath+".js") for i := 1; i <= pinfo.JsNum; i++ { docJS = append(docJS, fmt.Sprintf("%s%s-%d.js", setting.DocsJsPath, importPath, i)) } ctx.Data["DocJS"] = docJS ctx.Data["Timestamp"] = pinfo.Created if time.Now().UTC().Add(-5*time.Second).Unix() < pinfo.Created { ctx.Flash.Success(ctx.Tr("docs.generate_success"), true) } // Notes. if len(pinfo.Subdirs) > 0 { ctx.Data["IsHasSubdirs"] = true ctx.Data["ViewDirPath"] = pinfo.ViewDirPath ctx.Data["Subdirs"] = models.GetSubPkgs(pinfo.ImportPath, strings.Split(pinfo.Subdirs, "|")) } ctx.Data["ImportNum"] = len(strings.Split(pinfo.ImportPaths, "|")) // Tools. ctx.Data["CanRefresh"] = pinfo.CanRefresh() updateHistory(ctx, pinfo.Id) ctx.HTML(200, DOCS) }
func Badge(ctx *middleware.Context) { ctx.Redirect("https://img.shields.io/badge/Go%20Walker-API%20Documentation-green.svg?style=flat-square") }