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) v.FmtDecl = buf.String() pdoc.Vars[i] = v } // Files. if len(pdoc.Files) > 0 { pdoc.IsHasFile = true data["IsHasFiles"] = pdoc.IsHasFile data["Files"] = pdoc.Files var query string if i := strings.Index(pdoc.Files[0].BrowseUrl, "?"); i > -1 { query = pdoc.Files[0].BrowseUrl[i:] } viewFilePath := path.Dir(pdoc.Files[0].BrowseUrl) + "/" + query // GitHub URL change. if strings.HasPrefix(viewFilePath, "github.com") { viewFilePath = strings.Replace(viewFilePath, "blob/", "tree/", 1) } data["ViewFilePath"] = viewFilePath } var err error renderFuncs(pdoc) data["Funcs"] = pdoc.Funcs for i, f := range pdoc.Funcs { if len(f.Doc) > 0 { buf.Reset() doc.ToHTML(&buf, f.Doc, nil) f.Doc = buf.String() } buf.Reset() FormatCode(&buf, &f.Decl, links) f.FmtDecl = buf.String() + " {" if exs := getExamples(pdoc, "", f.Name); len(exs) > 0 { f.Examples = exs } pdoc.Funcs[i] = f } data["Types"] = pdoc.Types for i, t := range pdoc.Types { for j, v := range t.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() t.Consts[j] = v } for j, v := range t.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) v.FmtDecl = buf.String() t.Vars[j] = v } for j, f := range t.Funcs { if len(f.Doc) > 0 { buf.Reset() doc.ToHTML(&buf, f.Doc, nil) f.Doc = buf.String() } buf.Reset() FormatCode(&buf, &f.Decl, links) f.FmtDecl = buf.String() + " {" if exs := getExamples(pdoc, "", f.Name); len(exs) > 0 { f.Examples = exs } t.Funcs[j] = f } for j, m := range t.Methods { if len(m.Doc) > 0 { buf.Reset() doc.ToHTML(&buf, m.Doc, nil) m.Doc = buf.String() } buf.Reset() FormatCode(&buf, &m.Decl, links) m.FmtDecl = buf.String() + " {" if exs := getExamples(pdoc, t.Name, m.Name); len(exs) > 0 { m.Examples = exs } t.Methods[j] = m } if len(t.Doc) > 0 { buf.Reset() doc.ToHTML(&buf, t.Doc, nil) t.Doc = buf.String() } buf.Reset() FormatCode(&buf, &t.Decl, links) t.FmtDecl = buf.String() if exs := getExamples(pdoc, "", t.Name); len(exs) > 0 { t.Examples = exs } pdoc.Types[i] = t } // Examples. links = append(links, &Link{ Name: path.Base(pdoc.ImportPath) + ".", }) for _, e := range pdoc.Examples { buf.Reset() FormatCode(&buf, &e.Code, links) e.Code = buf.String() } data["ProjectPath"] = pdoc.ProjectPath data["ImportPath"] = pdoc.ImportPath // GitHub redirects non-HTTPS link and Safari loses "#XXX". if strings.HasPrefix(pdoc.ProjectPath, "github") { data["Secure"] = "s" } result, err := render.HTMLBytes("docs/tpl", data) if err != nil { return fmt.Errorf("error rendering HTML: %v", err) } pdoc.JsNum = SaveDocPage(docPath, result) if pdoc.JsNum == -1 { return errors.New("Save JS file wasn't successful") } SavePkgDoc(pdoc.ImportPath, pdoc.Readme) data["UtcTime"] = time.Unix(pdoc.Created, 0).UTC() // data["TimeSince"] = calTimeSince(time.Unix(pdoc.Created, 0)) return nil }