func getPkgInfoWithQ(path, tag string, q *qbs.Qbs) (*hv.PkgInfo, error) { // Check path length to reduce connect times. if len(path) == 0 { return nil, errors.New("models.getPkgInfoWithQ -> Empty path as not found.") } pinfo := new(hv.PkgInfo) q.WhereEqual("import_path", path).Find(pinfo) proPath := utils.GetProjectPath(path) if utils.IsGoRepoPath(path) { proPath = "code.google.com/p/go" } beego.Trace("models.getPkgInfoWithQ -> proPath:", proPath) ptag := new(PkgTag) cond := qbs.NewCondition("path = ?", proPath).And("tag = ?", tag) err := q.Condition(cond).Find(ptag) if err != nil { pinfo.Ptag = "ptag" return pinfo, errors.New( fmt.Sprintf("models.getPkgInfoWithQ( %s:%s ) -> 'PkgTag': %s", path, tag, err)) } pinfo.Vcs = ptag.Vcs pinfo.Tags = ptag.Tags // Only 'PkgInfo' cannot prove that package exists, // we have to check 'PkgDecl' as well in case it was deleted by mistake. pdecl := new(PkgDecl) cond = qbs.NewCondition("pid = ?", pinfo.Id).And("tag = ?", tag) err = q.Condition(cond).Find(pdecl) if err != nil { // Basically, error means not found, so we set 'pinfo.PkgVer' to 0 // because server uses it to decide whether force update. pinfo.PkgVer = 0 pinfo.Ptag = "ptag" return pinfo, errors.New( fmt.Sprintf("models.getPkgInfoWithQ( %s:%s ) -> 'PkgDecl': %s", path, tag, err)) } docPath := path + utils.TagSuffix("-", tag) if !com.IsExist("." + utils.DocsJsPath + docPath + ".js") { pinfo.PkgVer = 0 pinfo.Ptag = "ptag" return pinfo, errors.New( fmt.Sprintf("models.getPkgInfoWithQ( %s:%s ) -> JS: File not found", path, tag)) } return pinfo, nil }
// generatePage genarates documentation page for project. // it returns false when it's a invaild(empty) project. func generatePage(this *HomeRouter, pdoc *hv.Package, q, tag string) bool { docPath := pdoc.ImportPath + utils.TagSuffix("-", tag) if pdoc.IsNeedRender { if !renderDoc(this, pdoc, q, tag, docPath) { return false } } else { pdecl, err := models.LoadProject(pdoc.Id, tag) if err != nil { beego.Error("HomeController.generatePage ->", err) return false } err = ConvertDataFormat(pdoc, pdecl) if err != nil { beego.Error("HomeController.generatePage -> ConvertDataFormat:", err) return false } this.Data["UtcTime"] = pdoc.Created.Add(4 * time.Hour) this.Data["TimeSince"] = calTimeSince(pdoc.Created.Add(4 * time.Hour)) } proName := path.Base(pdoc.ImportPath) if i := strings.Index(proName, "?"); i > -1 { proName = proName[:i] } this.Data["ProName"] = proName // Check if need to show Hacker View. f := this.Input().Get("f") hvJs := utils.HvJsPath + pdoc.ImportPath + "/" + f + ".js" if len(tag) == 0 && (pdoc.IsCmd || pdoc.IsGoRepo || pdoc.IsGoSubrepo) && len(f) > 0 && com.IsExist("."+hvJs) { this.TplNames = "hv.html" var query string if i := strings.Index(pdoc.ViewDirPath, "?"); i > -1 { query = pdoc.ViewDirPath[i:] pdoc.ViewDirPath = pdoc.ViewDirPath[:i] } this.Data["ViewDirPath"] = strings.TrimSuffix(pdoc.ViewDirPath, "/") this.Data["Query"] = query this.Data["FileName"] = f this.Data["HvJs"] = hvJs return true } // Set properties. this.TplNames = "docs.html" this.Data["Pid"] = pdoc.Id this.Data["IsGoRepo"] = pdoc.IsGoRepo if len(tag) == 0 && (pdoc.IsCmd || pdoc.IsGoRepo || pdoc.IsGoSubrepo) { this.Data["IsHasHv"] = true } // Refresh (within 10 seconds). this.Data["IsRefresh"] = pdoc.Created.UTC().Add(10 * time.Second).After(time.Now().UTC()) this.Data["VCS"] = pdoc.Vcs this.Data["ProPath"] = pdoc.ProjectPath this.Data["ProDocPath"] = path.Dir(pdoc.ImportPath) // Introduction. this.Data["ImportPath"] = pdoc.ImportPath lang := this.Data["Lang"].(string) byts, _ := base32.StdEncoding.DecodeString( models.LoadPkgDoc(pdoc.ImportPath, lang, "rm")) if len(byts) > 0 { this.Data["IsHasReadme"] = true this.Data["PkgDoc"] = string(byts) } // Index. this.Data["IsHasExports"] = pdoc.IsHasExport this.Data["IsHasConst"] = pdoc.IsHasConst this.Data["IsHasVar"] = pdoc.IsHasVar if !pdoc.IsCmd { this.Data["IsHasExams"] = pdoc.IsHasExample tags := strings.Split(pdoc.Tags, "|||") // Tags. if len(tag) == 0 { tag = tags[0] } this.Data["CurTag"] = tag this.Data["Tags"] = tags } else { this.Data["IsCmd"] = true } this.Data["Rank"] = pdoc.Rank this.Data["Views"] = pdoc.Views + 1 //this.Data["Labels"] = getLabels(pdoc.Labels) //this.Data["LabelDataSrc"] = labelSet this.Data["LabelDataSrc"] = "" this.Data["ImportPkgNum"] = len(pdoc.Imports) this.Data["IsHasSubdirs"] = pdoc.IsHasSubdir this.Data["IsHasFiles"] = pdoc.IsHasFile this.Data["IsHasImports"] = len(pdoc.Imports) > 0 this.Data["IsImported"] = pdoc.RefNum > 0 this.Data["ImportedNum"] = pdoc.RefNum this.Data["IsDocumentation"] = true docJS := make([]string, 0, pdoc.JsNum+1) docJS = append(docJS, utils.DocsJsPath+docPath+".js") for i := 1; i <= pdoc.JsNum; i++ { docJS = append(docJS, fmt.Sprintf( "%s%s-%d.js", utils.DocsJsPath, docPath, i)) } this.Data["DocJS"] = docJS return true }
// generatePage genarates documentation page for project. // it returns false when it's a invaild(empty) project. func generatePage(this *HomeRouter, pdoc *hv.Package, q, tag string) bool { docPath := pdoc.ImportPath + utils.TagSuffix("-", tag) if pdoc.IsNeedRender { if !renderDoc(this, pdoc, q, tag, docPath) { return false } } else { pdecl, err := models.LoadProject(pdoc.Id, tag) if err != nil { beego.Error("HomeController.generatePage ->", err) return false } err = ConvertDataFormat(pdoc, pdecl) if err != nil { beego.Error("HomeController.generatePage -> ConvertDataFormat:", err) return false } this.Data["UtcTime"] = time.Unix(pdoc.Created, 0).UTC() this.Data["TimeSince"] = calTimeSince(time.Unix(pdoc.Created, 0)) } // Add create time for JS files' link to prevent cache in case the page is recreated. this.Data["Timestamp"] = pdoc.Created proName := path.Base(pdoc.ImportPath) if i := strings.Index(proName, "?"); i > -1 { proName = proName[:i] } this.Data["ProName"] = proName // Check if need to show Hacker View. f := this.Input().Get("f") hvJs := utils.HvJsPath + pdoc.ImportPath + "/" + f + ".js" if len(tag) == 0 && (pdoc.IsCmd || pdoc.IsGoRepo || pdoc.IsGoSubrepo) && len(f) > 0 && com.IsExist("."+hvJs) { this.TplNames = "hv.html" var query string if i := strings.Index(pdoc.ViewDirPath, "?"); i > -1 { query = pdoc.ViewDirPath[i:] pdoc.ViewDirPath = pdoc.ViewDirPath[:i] } this.Data["ViewDirPath"] = strings.TrimSuffix(pdoc.ViewDirPath, "/") this.Data["Query"] = query this.Data["FileName"] = f this.Data["HvJs"] = hvJs return true } // Set properties. this.TplNames = "docs.html" this.Data["PkgDesc"] = pdoc.Synopsis this.Data["Pid"] = pdoc.Id this.Data["IsGoRepo"] = pdoc.IsGoRepo if len(tag) == 0 && (pdoc.IsCmd || pdoc.IsGoRepo || pdoc.IsGoSubrepo) { this.Data["IsHasHv"] = true } // Refresh (within 10 seconds). this.Data["IsRefresh"] = time.Unix(pdoc.Created, 0).Add(10 * time.Second).After(time.Now()) this.Data["VCS"] = pdoc.Vcs proPath := pdoc.ProjectPath // Introduction. this.Data["ImportPath"] = pdoc.ImportPath // README. lang := this.Data["Lang"].(string)[:2] readmePath := utils.DocsJsPath + docPath + "_RM_" + lang + ".js" if com.IsFile("." + readmePath) { this.Data["IsHasReadme"] = true this.Data["PkgDocPath"] = readmePath } // Index. this.Data["IsHasExports"] = pdoc.IsHasExport this.Data["IsHasConst"] = pdoc.IsHasConst this.Data["IsHasVar"] = pdoc.IsHasVar if !pdoc.IsCmd { this.Data["IsHasExams"] = pdoc.IsHasExample tags := strings.Split(pdoc.Tags, "|||") // Tags. if len(tag) == 0 { tag = tags[0] } this.Data["CurTag"] = tag this.Data["Tags"] = tags if strings.HasPrefix(proPath, "github.com") { offset := 6 if len(tag) == 0 { // Projects with no tag. offset = 0 } proPath = proPath[:strings.Index(proPath, "/tree/")+offset] + tag } else if strings.HasPrefix(proPath, "code.google.com") { proPath = proPath[:strings.Index(proPath, "?r=")+3] + tag } } else { this.Data["IsCmd"] = true } this.Data["IsCgo"] = pdoc.IsCgo this.Data["ProPath"] = proPath this.Data["ProDocPath"] = path.Dir(pdoc.ImportPath) this.Data["Rank"] = pdoc.Rank this.Data["Views"] = pdoc.Views + 1 //this.Data["Labels"] = getLabels(pdoc.Labels) //this.Data["LabelDataSrc"] = labelSet this.Data["LabelDataSrc"] = "" this.Data["ImportPkgNum"] = len(pdoc.Imports) this.Data["IsHasSubdirs"] = pdoc.IsHasSubdir this.Data["IsHasFiles"] = pdoc.IsHasFile this.Data["IsHasImports"] = len(pdoc.Imports) > 0 this.Data["IsImported"] = pdoc.RefNum > 0 this.Data["ImportedNum"] = pdoc.RefNum this.Data["IsDocumentation"] = true docJS := make([]string, 0, pdoc.JsNum+1) docJS = append(docJS, utils.DocsJsPath+docPath+".js") for i := 1; i <= pdoc.JsNum; i++ { docJS = append(docJS, fmt.Sprintf( "%s%s-%d.js", utils.DocsJsPath, docPath, i)) } this.Data["DocJS"] = docJS return true }
// GetPkgInfo returns 'PkgInfo' by given import path and tag. // It returns error when the package does not exist. func GetPkgInfo(path, tag string) (*hv.PkgInfo, error) { // Check path length to reduce connect times. if len(path) == 0 { return nil, errors.New("models.GetPkgInfo -> Empty path as not found.") } pinfo := &hv.PkgInfo{ImportPath: path} has, err := x.Get(pinfo) if !has || err != nil { return pinfo, errors.New( fmt.Sprintf("models.GetPkgInfo( %s:%s ) -> Get hv.PkgInfo: %v", path, tag, err)) } proPath := utils.GetProjectPath(path) if utils.IsGoRepoPath(path) { proPath = "code.google.com/p/go" } beego.Trace("models.GetPkgInfo -> proPath:", proPath) ptag := &PkgTag{ Path: proPath, Tag: tag, } has, err = x.Get(ptag) if !has || err != nil { pinfo.Ptag = "ptag" return pinfo, errors.New( fmt.Sprintf("models.GetPkgInfo( %s:%s ) -> Get PkgTag: %v", path, tag, err)) } pinfo.Vcs = ptag.Vcs pinfo.Tags = ptag.Tags // Only 'PkgInfo' cannot prove that package exists, // we have to check 'PkgDecl' as well in case it was deleted by mistake. pdecl := &PkgDecl{ Pid: pinfo.Id, Tag: tag, } has, err = x.Get(pdecl) if err != nil { return pinfo, errors.New( fmt.Sprintf("models.GetPkgInfo( %s:%s ) -> Get PkgDecl: %v", path, tag, err)) } if !has { pinfo.PkgVer = 0 pinfo.Ptag = "ptag" return pinfo, errors.New( fmt.Sprintf("models.GetPkgInfo( %s:%s ) -> PkgDecl not exist: %v", path, tag, err)) } docPath := path + utils.TagSuffix("-", tag) if !com.IsExist("." + utils.DocsJsPath + docPath + ".js") { pinfo.PkgVer = 0 pinfo.Ptag = "ptag" return pinfo, errors.New( fmt.Sprintf("models.GetPkgInfo( %s:%s ) -> JS: File not found", path, tag)) } return pinfo, nil }