예제 #1
0
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)
}
예제 #2
0
func crawlDoc(importPath, etag string) (pdoc *Package, err error) {
	switch {
	case base.IsGoRepoPath(importPath):
		pdoc, err = getGolangDoc(importPath, etag)
	case base.IsGAERepoPath(strings.TrimPrefix(importPath, "google.golang.org/")):
		subPath := strings.TrimPrefix(importPath, "google.golang.org/")
		pdoc, err = getStatic("github.com/golang/"+subPath, etag)
		if pdoc != nil {
			pdoc.ImportPath = importPath
			pdoc.IsGaeRepo = true
		}
	case base.IsValidRemotePath(importPath):
		pdoc, err = getStatic(importPath, etag)
		if err == ErrNoServiceMatch {
			pdoc, err = getDynamic(importPath, etag)
		}
	default:
		err = ErrInvalidRemotePath
	}

	if err != nil {
		return nil, err
	}

	// Render README.
	for name, content := range pdoc.Readme {
		p, err := httplib.Post("https://api.github.com/markdown/raw?"+setting.GitHubCredentials).
			Header("Content-Type", "text/plain").Body(content).Bytes()
		if err != nil {
			return nil, fmt.Errorf("error rendering README: %v", err)
		}
		pdoc.Readme[name] = p
	}

	return pdoc, nil
}
예제 #3
0
파일: docs.go 프로젝트: ewindfall/gowalker
func Docs(ctx *middleware.Context) {
	importPath := ctx.Params("*")

	if base.IsGAERepoPath(importPath) {
		ctx.Redirect("/google.golang.org/" + importPath)
		return
	}

	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)
	}

	// Subdirs.
	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, "|"))
	}

	// Imports and references.
	ctx.Data["ImportNum"] = pinfo.ImportNum
	ctx.Data["RefNum"] = pinfo.RefNum

	// Tools.
	ctx.Data["TimeDuration"] = base.TimeSince(time.Unix(pinfo.Created, 0), ctx.Locale.Language())
	ctx.Data["CanRefresh"] = pinfo.CanRefresh()

	updateHistory(ctx, pinfo.ID)

	ctx.HTML(200, DOCS)
}