本文整理汇总了Golang中go/doc.ToHTML函数的典型用法代码示例。如果您正苦于以下问题:Golang ToHTML函数的具体用法?Golang ToHTML怎么用?Golang ToHTML使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ToHTML函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: pageView
func pageView(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
id := strings.TrimSpace(r.FormValue("id"))
if id != "" {
db := getDatabase()
d, found := db.FindFullPackage(id)
if !found {
pageNotFound(w, r)
return
}
if d.StarCount < 0 {
d.StarCount = 0
}
var descHTML bytesp.Slice
doc.ToHTML(&descHTML, d.Description, nil)
if err := templates.ExecuteTemplate(w, "view.html", struct {
UIUtils
gcse.HitInfo
DescHTML template.HTML
TotalDocCount int
StaticRank int
ShowReadme bool
}{
HitInfo: d,
DescHTML: template.HTML(descHTML),
TotalDocCount: db.PackageCount(),
StaticRank: d.StaticRank + 1,
ShowReadme: len(d.Description) < 10 && len(d.ReadmeData) > 0,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
示例2: main
func main() {
pkg, err := build.Import("robpike.io/ivy", "", build.ImportComment)
if err != nil {
log.Fatal(err)
}
fs := token.NewFileSet()
pkgs, err := parser.ParseDir(fs, pkg.Dir, nil, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
astPkg := pkgs[pkg.Name]
if astPkg == nil {
log.Fatalf("failed to locate %s package", pkg.Name)
}
docPkg := doc.New(astPkg, pkg.ImportPath, doc.AllDecls)
htmlBuf := new(bytes.Buffer)
fmt.Fprintln(htmlBuf, `<!-- auto-generated from robpike.io/ivy package doc -->`)
fmt.Fprintln(htmlBuf, head)
fmt.Fprintln(htmlBuf, `<body>`)
doc.ToHTML(htmlBuf, docPkg.Doc, nil)
fmt.Fprintln(htmlBuf, `</body></html>`)
goBuf := new(bytes.Buffer)
fmt.Fprintf(goBuf, "package mobile\n\n")
fmt.Fprintf(goBuf, "// GENERATED; DO NOT EDIT\n")
fmt.Fprintf(goBuf, "const help = `%s`\n", sanitize(htmlBuf.Bytes()))
buf, err := format.Source(goBuf.Bytes())
if err != nil {
log.Fatalf("failed to gofmt: %v", err)
}
os.Stdout.Write(buf)
}
示例3: htmlCommentFmt
// Template formatter for "html-comment" format.
func htmlCommentFmt(w io.Writer, format string, x ...interface{}) {
var buf bytes.Buffer
writeAny(&buf, fileset(x), x[0])
// TODO(gri) Provide list of words (e.g. function parameters)
// to be emphasized by ToHTML.
doc.ToHTML(w, buf.Bytes(), nil) // does html-escaping
}
示例4: commentFn
// commentFn formats a source code comment as HTML.
func commentFn(v string) htemp.HTML {
var buf bytes.Buffer
godoc.ToHTML(&buf, v, nil)
p := buf.Bytes()
p = replaceAll(p, h3Pat, func(out, src []byte, m []int) []byte {
out = append(out, src[m[0]:m[1]-1]...)
out = append(out, '4')
return out
})
p = replaceAll(p, rfcPat, func(out, src []byte, m []int) []byte {
out = append(out, `<a href="http://tools.ietf.org/html/rfc`...)
out = append(out, src[m[2]:m[3]]...)
out = append(out, `">`...)
out = append(out, src[m[0]:m[1]]...)
out = append(out, `</a>`...)
return out
})
p = replaceAll(p, packagePat, func(out, src []byte, m []int) []byte {
path := bytes.TrimRight(src[m[2]:m[3]], ".!?:")
if !doc.IsValidPath(string(path)) {
return append(out, src[m[0]:m[1]]...)
}
out = append(out, src[m[0]:m[2]]...)
out = append(out, `<a href="/`...)
out = append(out, path...)
out = append(out, `">`...)
out = append(out, path...)
out = append(out, `</a>`...)
out = append(out, src[m[2]+len(path):m[1]]...)
return out
})
return htemp.HTML(p)
}
示例5: html
func (p *Package) html(text string, scope string, ignored map[string]struct{}) template.HTML {
var buf bytes.Buffer
doc.ToHTML(&buf, text, nil)
var out bytes.Buffer
p.linkify(&out, buf.String(), scope, ignored)
return template.HTML(out.String())
}
示例6: comment_htmlFunc
func comment_htmlFunc(comment string) string {
var buf bytes.Buffer
// TODO(gri) Provide list of words (e.g. function parameters)
// to be emphasized by ToHTML.
doc.ToHTML(&buf, comment, nil) // does html-escaping
return buf.String()
}
示例7: commentFn
// commentFn formats a source code comment as HTML.
func commentFn(v string) string {
var buf bytes.Buffer
godoc.ToHTML(&buf, v, nil)
p := buf.Bytes()
p = bytes.Replace(p, h3Open, h4Open, -1)
p = bytes.Replace(p, h3Close, h4Close, -1)
p = rfcRE.ReplaceAll(p, rfcReplace)
return string(p)
}
示例8: pageView
func pageView(w http.ResponseWriter, r *http.Request) {
id := strings.TrimSpace(r.FormValue("id"))
if id != "" {
c := appengine.NewContext(r)
ddb := NewCachedDocDB(c, "doc")
var doc DocInfo
err, exists := ddb.Get(id, &doc)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if !exists {
fmt.Fprintf(w, `<html><body>No such entry!`)
ent, _ := findCrawlingEntry(c, kindCrawlerPackage, id)
if ent != nil {
fmt.Fprintf(w, ` Scheduled to be crawled at %s`,
ent.ScheduleTime.Format("2006-01-02 15:04:05"))
} else {
fmt.Fprintf(w, ` Not found yet!`)
}
fmt.Fprintf(w, ` Click to <a href="crawl?id=%s">crawl</a>.</body></html>`,
template.URLQueryEscaper(id))
return
}
if doc.StarCount < 0 {
doc.StarCount = 0
}
var descHTML villa.ByteSlice
godoc.ToHTML(&descHTML, doc.Description, nil)
showReadme := len(doc.Description) < 10 && len(doc.ReadmeData) > 0
err = templates.ExecuteTemplate(w, "view.html", struct {
DocInfo
DescHTML template.HTML
ShowReadme bool
}{
DocInfo: doc,
DescHTML: template.HTML(descHTML),
ShowReadme: showReadme,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
示例9: commentFn
// commentFn formats a source code comment as HTML.
func commentFn(v string) htemp.HTML {
var buf bytes.Buffer
godoc.ToHTML(&buf, v, nil)
p := buf.Bytes()
p = replaceAll(p, h3Pat, func(out, src []byte, m []int) []byte {
out = append(out, `<h4 id="`...)
out = append(out, src[m[2]:m[3]]...)
out = append(out, `">`...)
out = append(out, src[m[4]:m[5]]...)
out = append(out, ` <a class="permalink" href="#`...)
out = append(out, src[m[2]:m[3]]...)
out = append(out, `">¶</a></h4>`...)
return out
})
p = replaceAll(p, rfcPat, func(out, src []byte, m []int) []byte {
out = append(out, `<a href="http://tools.ietf.org/html/rfc`...)
out = append(out, src[m[2]:m[3]]...)
// If available, add major section fragment
if m[6] != -1 {
out = append(out, `#section-`...)
out = append(out, src[m[8]:m[9]]...)
// If available, add minor section fragment
if m[13] != -1 {
out = append(out, src[m[12]:m[13]]...)
}
}
out = append(out, `">`...)
out = append(out, src[m[0]:m[1]]...)
out = append(out, `</a>`...)
return out
})
p = replaceAll(p, packagePat, func(out, src []byte, m []int) []byte {
path := bytes.TrimRight(src[m[2]:m[3]], ".!?:")
if !gosrc.IsValidPath(string(path)) {
return append(out, src[m[0]:m[1]]...)
}
out = append(out, src[m[0]:m[2]]...)
out = append(out, `<a href="/`...)
out = append(out, path...)
out = append(out, `">`...)
out = append(out, path...)
out = append(out, `</a>`...)
out = append(out, src[m[2]+len(path):m[1]]...)
return out
})
return htemp.HTML(p)
}
示例10: toHTML
// toHTML returns Go documention in HTML format for the main package
// in the current directory. It returns an error if it finds multiple
// packages or a non-main package.
func toHTML() ([]byte, error) {
dir := "."
// create a build.Package
bpkg, err := build.ImportDir(dir, build.ImportComment)
if err != nil {
return nil, err
}
// create an ast.Package
fset := token.NewFileSet()
goFiles := map[string]struct{}{}
for _, f := range bpkg.GoFiles {
goFiles[f] = struct{}{}
}
for _, f := range bpkg.CgoFiles {
goFiles[f] = struct{}{}
}
filter := func(fi os.FileInfo) bool {
_, ok := goFiles[fi.Name()]
return ok
}
apkgs, err := parser.ParseDir(fset, dir, filter, parser.ParseComments)
if err != nil {
return nil, err
}
if len(apkgs) > 1 {
return nil, errors.New("multiple packages in directory")
}
var apkg *ast.Package
var ok bool
if apkg, ok = apkgs["main"]; !ok {
return nil, errors.New("package is a non-main package")
}
// create a doc.Package
dpkg := doc.New(apkg, bpkg.ImportPath, 0)
// return html
b := new(bytes.Buffer)
doc.ToHTML(b, dpkg.Doc, nil)
return b.Bytes(), nil
}
示例11: pageView
func pageView(w http.ResponseWriter, r *http.Request) {
id := strings.TrimSpace(r.FormValue("id"))
if id != "" {
var doc gcse.HitInfo
if !findPackage(id, &doc) {
http.Error(w, fmt.Sprintf("Package %s not found!", id), http.StatusNotFound)
return
}
indexDB, _ := indexDBBox.Get().(*index.TokenSetSearcher)
if doc.StarCount < 0 {
doc.StarCount = 0
}
var descHTML bytesp.Slice
godoc.ToHTML(&descHTML, doc.Description, nil)
showReadme := len(doc.Description) < 10 && len(doc.ReadmeData) > 0
docCount := 0
if indexDB != nil {
docCount = indexDB.DocCount()
}
if err := templates.ExecuteTemplate(w, "view.html", struct {
UIUtils
gcse.HitInfo
DescHTML template.HTML
TotalDocCount int
StaticRank int
ShowReadme bool
}{
HitInfo: doc,
DescHTML: template.HTML(descHTML),
TotalDocCount: docCount,
StaticRank: doc.StaticRank + 1,
ShowReadme: showReadme,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
示例12: commentToHTML
func commentToHTML(comment string) string {
b := bytes.NewBuffer(make([]byte, 0, 128))
doc.ToHTML(b, []byte(comment), nil)
return b.String()
}
示例13: renderDoc
func renderDoc(render macaron.Render, pdoc *Package, docPath string) error {
data := make(map[string]interface{})
data["PkgFullIntro"] = pdoc.Doc
exports := make([]exportSearchObject, 0, 10)
var buf bytes.Buffer
links := make([]*Link, 0, len(pdoc.Types)+len(pdoc.Imports)+len(pdoc.TestImports)+
len(pdoc.Funcs)+10)
// Get all types, functions and import packages
for _, t := range pdoc.Types {
links = append(links, &Link{
Name: t.Name,
Comment: template.HTMLEscapeString(t.Doc),
})
exports = append(exports, exportSearchObject{t.Name})
// buf.WriteString("'" + t.Name + "',")
}
for _, f := range pdoc.Funcs {
f.Code = template.HTMLEscapeString(f.Code)
links = append(links, &Link{
Name: f.Name,
Comment: template.HTMLEscapeString(f.Doc),
})
exports = append(exports, exportSearchObject{f.Name})
// buf.WriteString("'" + f.Name + "',")
}
for _, t := range pdoc.Types {
for _, f := range t.Funcs {
links = append(links, &Link{
Name: f.Name,
Comment: template.HTMLEscapeString(f.Doc),
})
exports = append(exports, exportSearchObject{f.Name})
// buf.WriteString("'" + f.Name + "',")
}
for _, m := range t.Methods {
exports = append(exports, exportSearchObject{t.Name + "." + m.Name})
// buf.WriteString("'" + t.Name + "_" + m.Name + "',")
}
}
// Ignore C.
for _, v := range append(pdoc.Imports, pdoc.TestImports...) {
if v != "C" {
links = append(links, &Link{
Name: path.Base(v) + ".",
Path: v,
})
}
}
// Set exported objects type-ahead.
// exportDataSrc := buf.String()
if len(exports) > 0 {
pdoc.IsHasExport = true
data["IsHasExports"] = true
exportDataSrc, _ := json.Marshal(exports)
data["ExportDataSrc"] = "<script>var exportDataSrc = " + string(exportDataSrc) + ";</script>"
}
pdoc.IsHasConst = len(pdoc.Consts) > 0
pdoc.IsHasVar = len(pdoc.Vars) > 0
if len(pdoc.Examples) > 0 {
pdoc.IsHasExample = true
data["IsHasExample"] = pdoc.IsHasExample
data["Examples"] = pdoc.Examples
}
// Constants.
data["IsHasConst"] = pdoc.IsHasConst
data["Consts"] = pdoc.Consts
for i, v := range pdoc.Consts {
if len(v.Doc) > 0 {
buf.Reset()
doc.ToHTML(&buf, v.Doc, nil)
v.Doc = buf.String()
}
buf.Reset()
v.Decl = template.HTMLEscapeString(v.Decl)
v.Decl = strings.Replace(v.Decl, """, "\"", -1)
FormatCode(&buf, &v.Decl, links)
v.FmtDecl = buf.String()
pdoc.Consts[i] = v
}
// Variables.
data["IsHasVar"] = pdoc.IsHasVar
data["Vars"] = pdoc.Vars
for i, v := range pdoc.Vars {
if len(v.Doc) > 0 {
buf.Reset()
doc.ToHTML(&buf, v.Doc, nil)
v.Doc = buf.String()
}
buf.Reset()
FormatCode(&buf, &v.Decl, links)
//.........这里部分代码省略.........
示例14: htmlCommentFmt
// Template formatter for "html-comment" format.
func htmlCommentFmt(w io.Writer, x interface{}, format string) {
var buf bytes.Buffer
writeAny(&buf, x, false)
doc.ToHTML(w, buf.Bytes()) // does html-escaping
}
示例15: generatePage
// 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
//.........这里部分代码省略.........