Esempio n. 1
0
// generatePage genarates documentation page for project.
// it returns false when its a invaild(empty) project.
func generatePage(this *HomeController, pdoc *doc.Package, q string, lang string) bool {
	// Set properties.
	this.TplNames = "docs_" + lang + ".html"

	// Refresh (within 10 seconds).
	this.Data["IsRefresh"] = pdoc.Created.Add(10 * time.Second).UTC().After(time.Now().UTC())

	// Get project name.
	lastIndex := strings.LastIndex(q, "/")
	proName := q[lastIndex+1:]
	if i := strings.Index(proName, "?"); i > -1 {
		proName = proName[:i]
	}
	this.Data["ProName"] = proName

	// Project VCS home page.
	switch {
	case q[:4] == "code": // code.google.com
		if strings.Index(q, "source/") == -1 {
			this.Data["ProPath"] = strings.Replace(q, pdoc.ProjectName, pdoc.ProjectName+"/source/browse", 1)
		} else {
			this.Data["ProPath"] = q
		}
	case q[:3] == "git": // github.com
		if proName != pdoc.ProjectName {
			// Not root.
			this.Data["ProPath"] = strings.Replace(q, proName, "tree/master/"+proName, 1)
		} else {
			this.Data["ProPath"] = q + "/tree/master"
		}
	}

	this.Data["Views"] = pdoc.Views + 1

	// Remove last "/".
	if urlLen := len(q); q[urlLen-1] == '/' {
		q = q[:urlLen-1]
	}

	if utils.IsGoRepoPath(pdoc.ImportPath) {
		this.Data["IsGoRepo"] = true
	}
	pkgDocPath := q[:lastIndex]
	this.Data["ProDocPath"] = pkgDocPath // Upper level project URL.

	// Introduction.
	this.Data["ImportPath"] = pdoc.ImportPath
	// Load project data from database.
	pdecl, err := models.LoadProject(pdoc.ImportPath)
	if err != nil {
		beego.Error("SearchController.generatePage(): models.LoadProject()", err)
		return false
	}
	var buf bytes.Buffer
	godoc.ToHTML(&buf, pdecl.Doc, nil)
	pkgInfo := buf.String()
	pkgInfo = strings.Replace(pkgInfo, "<p>", "<p><b>", 1)
	pkgInfo = strings.Replace(pkgInfo, "</p>", "</b></p>", 1)
	this.Data["PkgFullIntro"] = pkgInfo
	// Convert data format.
	err = ConvertDataFormat(pdoc, pdecl)
	if err != nil {
		beego.Error("SearchController.generatePage(): ConvertDataFormat", err)
		return false
	}

	links := make([]*utils.Link, 0, len(pdoc.Types)+len(pdoc.Imports))
	// Get all types and import packages
	for _, t := range pdoc.Types {
		links = append(links, &utils.Link{
			Name:    t.Name,
			Comment: t.Doc,
		})
	}
	for _, v := range pdoc.Imports {
		links = append(links, &utils.Link{
			Name: path.Base(v) + ".",
			Path: v,
		})
	}

	// Index.
	this.Data["IsHasConst"] = len(pdoc.Consts) > 0
	this.Data["IsHasVar"] = len(pdoc.Vars) > 0
	this.Data["Funcs"] = pdoc.Funcs
	for i, f := range pdoc.Funcs {
		buf.Reset()
		godoc.ToHTML(&buf, f.Doc, nil)
		f.Doc = buf.String()
		buf.Reset()
		utils.FormatCode(&buf, f.Decl, links)
		f.FmtDecl = buf.String()
		buf.Reset()
		utils.FormatCode(&buf, f.Code, links)
		f.Code = buf.String()
		pdoc.Funcs[i] = f
	}
	this.Data["Types"] = pdoc.Types
	for i, t := range pdoc.Types {
		for j, f := range t.Funcs {
			buf.Reset()
			godoc.ToHTML(&buf, f.Doc, nil)
			f.Doc = buf.String()
			buf.Reset()
			utils.FormatCode(&buf, f.Decl, links)
			f.FmtDecl = buf.String()
			buf.Reset()
			utils.FormatCode(&buf, f.Code, links)
			f.Code = buf.String()
			t.Funcs[j] = f
		}
		for j, m := range t.Methods {
			buf.Reset()
			godoc.ToHTML(&buf, m.Doc, nil)
			m.Doc = buf.String()
			buf.Reset()
			utils.FormatCode(&buf, m.Decl, links)
			m.FmtDecl = buf.String()
			buf.Reset()
			utils.FormatCode(&buf, m.Code, links)
			m.Code = buf.String()
			t.Methods[j] = m
		}
		buf.Reset()
		godoc.ToHTML(&buf, t.Doc, nil)
		t.Doc = buf.String()
		buf.Reset()
		utils.FormatCode(&buf, t.Decl, links)
		t.FmtDecl = buf.String()
		pdoc.Types[i] = t
	}

	// Constants.
	this.Data["Consts"] = pdoc.Consts
	for i, v := range pdoc.Consts {
		buf.Reset()
		utils.FormatCode(&buf, v.Decl, links)
		v.FmtDecl = buf.String()
		pdoc.Consts[i] = v
	}

	// Variables.
	this.Data["Vars"] = pdoc.Vars
	for i, v := range pdoc.Vars {
		buf.Reset()
		utils.FormatCode(&buf, v.Decl, links)
		v.FmtDecl = buf.String()
		pdoc.Vars[i] = v
	}

	// Dirs.
	this.Data["IsHasSubdirs"] = len(pdoc.Dirs) > 0
	pinfos := make([]*models.PkgInfo, 0, len(pdoc.Dirs))
	for _, v := range pdoc.Dirs {
		v = pdoc.ImportPath + "/" + v
		if pinfo, err := models.GetPkgInfo(v); err == nil {
			pinfos = append(pinfos, pinfo)
		} else {
			pinfos = append(pinfos, &models.PkgInfo{Path: v})
		}
	}
	this.Data["Subdirs"] = pinfos

	this.Data["Files"] = pdoc.Files
	this.Data["ImportPkgs"] = pdecl.Imports
	this.Data["ImportPkgNum"] = len(pdoc.Imports) - 1
	this.Data["UtcTime"] = pdoc.Created
	this.Data["GOOS"] = pdecl.Goos
	this.Data["GOARCH"] = pdecl.Goarch
	return true
}
Esempio n. 2
0
// generatePage genarates documentation page for project.
// it returns false when its a invaild(empty) project.
func generatePage(this *HomeController, pdoc *doc.Package, q string, lang string) bool {
	// Load project data from database.
	pdecl, err := models.LoadProject(pdoc.ImportPath)
	if err != nil {
		beego.Error("HomeController.generatePage():", err)
		return false
	}

	// Set properties.
	this.TplNames = "docs_" + lang + ".html"

	// Refresh (within 10 seconds).
	this.Data["IsRefresh"] = pdoc.Created.Add(10 * time.Second).UTC().After(time.Now().UTC())

	// Get VCS name, project name, project home page, and Upper level project URL.
	this.Data["VCS"], this.Data["ProName"], this.Data["ProPath"], this.Data["ProDocPath"] = getVCSInfo(q, pdoc)

	if utils.IsGoRepoPath(pdoc.ImportPath) &&
		strings.Index(pdoc.ImportPath, ".") == -1 {
		this.Data["IsGoRepo"] = true
	}

	this.Data["Views"] = pdoc.Views + 1

	// Tags.
	this.Data["Tags"] = getTags(pdoc.Tags, lang)

	// Introduction.
	this.Data["ImportPath"] = pdoc.ImportPath
	byts, _ := base32.StdEncoding.DecodeString(pdecl.Doc)
	this.Data["PkgFullIntro"] = string(byts)

	var buf bytes.Buffer
	// Convert data format.
	err = ConvertDataFormat(pdoc, pdecl)
	if err != nil {
		beego.Error("HomeController.generatePage(): ConvertDataFormat", err)
		return false
	}

	links := make([]*utils.Link, 0, len(pdoc.Types)+len(pdoc.Imports)+len(pdoc.Funcs)+10)
	// Get all types, functions and import packages
	for _, t := range pdoc.Types {
		links = append(links, &utils.Link{
			Name:    t.Name,
			Comment: template.HTMLEscapeString(t.Doc),
		})
		buf.WriteString("&quot;" + t.Name + "&quot;,")
	}

	for _, f := range pdoc.Funcs {
		links = append(links, &utils.Link{
			Name:    f.Name,
			Comment: template.HTMLEscapeString(f.Doc),
		})
		buf.WriteString("&quot;" + f.Name + "&quot;,")
	}

	for _, t := range pdoc.Types {
		for _, f := range t.Funcs {
			links = append(links, &utils.Link{
				Name:    f.Name,
				Comment: template.HTMLEscapeString(f.Doc),
			})
			buf.WriteString("&quot;" + f.Name + "&quot;,")
		}

		for _, m := range t.Methods {
			buf.WriteString("&quot;" + t.Name + "." + m.Name + "&quot;,")
		}
	}

	for _, v := range pdoc.Imports {
		links = append(links, &utils.Link{
			Name: path.Base(v) + ".",
			Path: v,
		})
	}

	exportDataSrc := buf.String()
	if len(exportDataSrc) > 0 {
		this.Data["HasExports"] = true
		exportDataSrc = exportDataSrc[:len(exportDataSrc)-1]
		// Set export keyword type-ahead.
		this.Data["ExportDataSrc"] = exportDataSrc
	}

	// Index.
	this.Data["IsHasConst"] = len(pdoc.Consts) > 0
	this.Data["IsHasVar"] = len(pdoc.Vars) > 0
	this.Data["Funcs"] = pdoc.Funcs
	for i, f := range pdoc.Funcs {
		buf.Reset()
		godoc.ToHTML(&buf, f.Doc, nil)
		f.Doc = buf.String()
		buf.Reset()
		utils.FormatCode(&buf, &f.Decl, links)
		f.FmtDecl = buf.String()
		buf.Reset()
		utils.FormatCode(&buf, &f.Code, links)
		f.Code = buf.String()
		pdoc.Funcs[i] = f
	}
	this.Data["Types"] = pdoc.Types
	for i, t := range pdoc.Types {
		for j, f := range t.Funcs {
			buf.Reset()
			godoc.ToHTML(&buf, f.Doc, nil)
			f.Doc = buf.String()
			buf.Reset()
			utils.FormatCode(&buf, &f.Decl, links)
			f.FmtDecl = buf.String()
			buf.Reset()
			utils.FormatCode(&buf, &f.Code, links)
			f.Code = buf.String()
			t.Funcs[j] = f
		}
		for j, m := range t.Methods {
			buf.Reset()
			godoc.ToHTML(&buf, m.Doc, nil)
			m.Doc = buf.String()
			buf.Reset()
			utils.FormatCode(&buf, &m.Decl, links)
			m.FmtDecl = buf.String()
			buf.Reset()
			utils.FormatCode(&buf, &m.Code, links)
			m.Code = buf.String()
			t.Methods[j] = m
		}
		buf.Reset()
		godoc.ToHTML(&buf, t.Doc, nil)
		t.Doc = buf.String()
		buf.Reset()
		utils.FormatCode(&buf, &t.Decl, links)
		t.FmtDecl = buf.String()
		pdoc.Types[i] = t
	}

	// Constants.
	this.Data["Consts"] = pdoc.Consts
	for i, v := range pdoc.Consts {
		buf.Reset()
		v.Decl = template.HTMLEscapeString(v.Decl)
		v.Decl = strings.Replace(v.Decl, "&#34;", "\"", -1)
		utils.FormatCode(&buf, &v.Decl, links)
		v.FmtDecl = buf.String()
		pdoc.Consts[i] = v
	}

	// Variables.
	this.Data["Vars"] = pdoc.Vars
	for i, v := range pdoc.Vars {
		buf.Reset()
		utils.FormatCode(&buf, &v.Decl, links)
		v.FmtDecl = buf.String()
		pdoc.Vars[i] = v
	}

	// Dirs.
	this.Data["IsHasSubdirs"] = len(pdoc.Dirs) > 0
	pinfos := make([]*models.PkgInfo, 0, len(pdoc.Dirs))
	for _, v := range pdoc.Dirs {
		v = pdoc.ImportPath + "/" + v
		if pinfo, err := models.GetPkgInfo(v); err == nil {
			pinfos = append(pinfos, pinfo)
		} else {
			pinfos = append(pinfos, &models.PkgInfo{Path: v})
		}
	}
	this.Data["Subdirs"] = pinfos

	// Tags.
	this.Data["TagsDataSrc"] = tagSet

	this.Data["Files"] = pdoc.Files
	this.Data["ImportPkgs"] = pdecl.Imports
	this.Data["ImportPkgNum"] = len(pdoc.Imports) - 1
	this.Data["IsImported"] = pdoc.ImportedNum > 0
	this.Data["ImportPid"] = pdoc.ImportPid
	this.Data["ImportedNum"] = pdoc.ImportedNum
	this.Data["UtcTime"] = pdoc.Created
	this.Data["GOOS"] = pdecl.Goos
	this.Data["GOARCH"] = pdecl.Goarch
	return true
}
Esempio n. 3
0
// generatePage genarates documentation page for project.
// it returns false when its a invaild(empty) project.
func generatePage(this *HomeRouter, pdoc *doc.Package, q, tag, lang string) bool {
	// Load project data from database.
	pdecl, err := models.LoadProject(pdoc.ImportPath, tag)
	if err != nil {
		beego.Error("HomeController.generatePage ->", err)
		return false
	}

	// Set properties.
	this.TplNames = "docs_" + lang + ".html"

	// Refresh (within 10 seconds).
	this.Data["IsRefresh"] = pdoc.Created.Add(10 * time.Second).UTC().After(time.Now().UTC())

	// Get VCS name, project name, project home page, and Upper level project URL.
	this.Data["VCS"], this.Data["ProName"], this.Data["ProPath"], this.Data["ProDocPath"] =
		getVCSInfo(q, tag, pdoc)

	if utils.IsGoRepoPath(pdoc.ImportPath) &&
		strings.Index(pdoc.ImportPath, ".") == -1 {
		this.Data["IsGoRepo"] = true
	}

	this.Data["Views"] = pdoc.Views + 1

	// Labels.
	this.Data["Labels"] = getLabels(pdoc.Labels)

	// Introduction.
	this.Data["ImportPath"] = pdoc.ImportPath
	byts, _ := base32.StdEncoding.DecodeString(
		models.LoadPkgDoc(pdoc.ImportPath, lang, "rm"))
	this.Data["PkgDoc"] = string(byts)
	byts, _ = base32.StdEncoding.DecodeString(pdecl.Doc)
	this.Data["PkgFullIntro"] = string(byts)

	var buf bytes.Buffer
	// Convert data format.
	err = ConvertDataFormat(pdoc, pdecl)
	if err != nil {
		beego.Error("HomeController.generatePage -> ConvertDataFormat:", err)
		return false
	}

	links := make([]*utils.Link, 0, len(pdoc.Types)+len(pdoc.Imports)+len(pdoc.Funcs)+10)
	// Get all types, functions and import packages
	for _, t := range pdoc.Types {
		links = append(links, &utils.Link{
			Name:    t.Name,
			Comment: template.HTMLEscapeString(t.Doc),
		})
		buf.WriteString("&quot;" + t.Name + "&quot;,")
	}

	for _, f := range pdoc.Funcs {
		links = append(links, &utils.Link{
			Name:    f.Name,
			Comment: template.HTMLEscapeString(f.Doc),
		})
		buf.WriteString("&quot;" + f.Name + "&quot;,")
	}

	for _, t := range pdoc.Types {
		for _, f := range t.Funcs {
			links = append(links, &utils.Link{
				Name:    f.Name,
				Comment: template.HTMLEscapeString(f.Doc),
			})
			buf.WriteString("&quot;" + f.Name + "&quot;,")
		}

		for _, m := range t.Methods {
			buf.WriteString("&quot;" + t.Name + "." + m.Name + "&quot;,")
		}
	}

	for _, v := range pdoc.Imports {
		links = append(links, &utils.Link{
			Name: path.Base(v) + ".",
			Path: v,
		})
	}

	exportDataSrc := buf.String()
	if len(exportDataSrc) > 0 {
		this.Data["HasExports"] = true
		exportDataSrc = exportDataSrc[:len(exportDataSrc)-1]
		// Set export keyword type-ahead.
		this.Data["ExportDataSrc"] = exportDataSrc
	}

	// Commented and total objects number.
	var comNum, totalNum int

	// Index.
	this.Data["IsHasConst"] = len(pdoc.Consts) > 0
	this.Data["IsHasVar"] = len(pdoc.Vars) > 0

	// Constants.
	this.Data["Consts"] = pdoc.Consts
	for i, v := range pdoc.Consts {
		buf.Reset()
		v.Decl = template.HTMLEscapeString(v.Decl)
		v.Decl = strings.Replace(v.Decl, "&#34;", "\"", -1)
		utils.FormatCode(&buf, &v.Decl, links)
		v.FmtDecl = buf.String()
		pdoc.Consts[i] = v
	}

	// Variables.
	this.Data["Vars"] = pdoc.Vars
	for i, v := range pdoc.Vars {
		buf.Reset()
		utils.FormatCode(&buf, &v.Decl, links)
		v.FmtDecl = buf.String()
		pdoc.Vars[i] = v
	}

	this.Data["Funcs"] = pdoc.Funcs
	for i, f := range pdoc.Funcs {
		if len(f.Doc) > 0 {
			buf.Reset()
			godoc.ToHTML(&buf, f.Doc, nil)
			f.Doc = buf.String()
			comNum++
		}
		buf.Reset()
		utils.FormatCode(&buf, &f.Decl, links)
		f.FmtDecl = buf.String()
		buf.Reset()
		utils.FormatCode(&buf, &f.Code, links)
		f.Code = buf.String()
		if exs := getExamples(pdoc, "", f.Name); len(exs) > 0 {
			f.IsHasExam = true
			f.Exams = exs
		}
		totalNum++
		pdoc.Funcs[i] = f
	}

	this.Data["Types"] = pdoc.Types
	for i, t := range pdoc.Types {
		for j, f := range t.Funcs {
			if len(f.Doc) > 0 {
				buf.Reset()
				godoc.ToHTML(&buf, f.Doc, nil)
				f.Doc = buf.String()
				comNum++
			}
			buf.Reset()
			utils.FormatCode(&buf, &f.Decl, links)
			f.FmtDecl = buf.String()
			buf.Reset()
			utils.FormatCode(&buf, &f.Code, links)
			f.Code = buf.String()
			if exs := getExamples(pdoc, "", f.Name); len(exs) > 0 {
				f.IsHasExam = true
				f.Exams = exs
			}
			totalNum++
			t.Funcs[j] = f
		}
		for j, m := range t.Methods {
			if len(m.Doc) > 0 {
				buf.Reset()
				godoc.ToHTML(&buf, m.Doc, nil)
				m.Doc = buf.String()
				comNum++
			}
			buf.Reset()
			utils.FormatCode(&buf, &m.Decl, links)
			m.FmtDecl = buf.String()
			buf.Reset()
			utils.FormatCode(&buf, &m.Code, links)
			m.Code = buf.String()
			if exs := getExamples(pdoc, t.Name, m.Name); len(exs) > 0 {
				m.IsHasExam = true
				m.Exams = exs
			}
			totalNum++
			t.Methods[j] = m
		}
		if len(t.Doc) > 0 {
			buf.Reset()
			godoc.ToHTML(&buf, t.Doc, nil)
			t.Doc = buf.String()
			comNum++
		}
		buf.Reset()
		utils.FormatCode(&buf, &t.Decl, links)
		t.FmtDecl = buf.String()
		if exs := getExamples(pdoc, "", t.Name); len(exs) > 0 {
			t.IsHasExam = true
			t.Exams = exs
		}
		totalNum++
		pdoc.Types[i] = t
	}

	if !pdoc.IsCmd {
		// Calculate documentation complete %.
		this.Data["DocCPLabel"], this.Data["DocCP"] = calDocCP(comNum, totalNum)

		// Examples.
		this.Data["IsHasExams"] = len(pdoc.Examples)+len(pdoc.UserExamples) > 0
		this.Data["Exams"] = append(pdoc.Examples, pdoc.UserExamples...)

		// Tags.
		this.Data["IsHasTags"] = len(pdoc.Tags) > 1
		if len(tag) == 0 {
			tag = "master"
		}
		this.Data["CurTag"] = tag
		this.Data["Tags"] = pdoc.Tags
	} else {
		this.Data["IsCmd"] = true
	}

	// Dirs.
	this.Data["IsHasSubdirs"] = len(pdoc.Dirs) > 0
	pinfos := make([]*models.PkgInfo, 0, len(pdoc.Dirs))
	for _, v := range pdoc.Dirs {
		v = pdoc.ImportPath + "/" + v
		if pinfo, err := models.GetPkgInfo(v, tag); err == nil {
			pinfos = append(pinfos, pinfo)
		} else {
			pinfos = append(pinfos, &models.PkgInfo{Path: v})
		}
	}
	this.Data["Subdirs"] = pinfos

	// Labels.
	this.Data["LabelDataSrc"] = labelSet

	this.Data["Files"] = pdoc.Files
	this.Data["ImportPkgs"] = pdecl.Imports
	this.Data["ImportPkgNum"] = len(pdoc.Imports) - 1
	this.Data["IsImported"] = pdoc.ImportedNum > 0
	this.Data["ImportPid"] = pdoc.ImportPid
	this.Data["ImportedNum"] = pdoc.ImportedNum
	this.Data["UtcTime"] = pdoc.Created
	return true
}
Esempio n. 4
0
// 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
}
Esempio n. 5
0
// 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
}