Пример #1
0
func generateHv(importPath string, srcMap map[string]*hv.Source) error {
	w := &hv.Walker{
		Pdoc: &hv.Package{
			PkgInfo: &hv.PkgInfo{
				ImportPath: importPath,
			},
		},
		SrcFiles: srcMap,
	}
	hvs, err := w.Render()
	if err != nil {
		return errors.New("doc.generateHv(" + importPath + ") -> Fail to render: " + err.Error())
	}

	buf := new(bytes.Buffer)
	for name, data := range hvs {
		filePath := "." + utils.HvJsPath + importPath + "/" + name + ".js"
		buf.Reset()
		buf.WriteString("document.write(\"")
		buf.Write(com.Html2JS(data))
		buf.WriteString("\")")

		if _, err := com.SaveFile(filePath, buf.Bytes()); err != nil {
			return errors.New("doc.generateHv(" + importPath + ") -> Save hv: " + err.Error())
		}
	}
	return nil
}
Пример #2
0
// SavePkgDoc saves readered readme.md file data.
func SavePkgDoc(docPath string, readmes map[string][]byte) {
	for lang, data := range readmes {
		if len(data) == 0 {
			continue
		}

		if data[0] == '\n' {
			data = data[1:]
		}

		data = com.Html2JS(data)
		localeDocPath := DocsJsPath + docPath + "_RM_" + lang

		buf := new(bytes.Buffer)
		buf.WriteString("document.write(\"")
		buf.Write(data)
		buf.WriteString("\")")
		if _, err := com.SaveFile("."+localeDocPath+".js", buf.Bytes()); err != nil {
			beego.Error("utils.SavePkgDoc(", localeDocPath, ") ->", err)
		}
	}
}
Пример #3
0
// SavePkgDoc saves readered readme.md file data.
func SavePkgDoc(docPath string, readmes map[string][]byte) {
	for lang, data := range readmes {
		if len(data) == 0 {
			continue
		}

		if data[0] == '\n' {
			data = data[1:]
		}

		data = com.Html2JS(data)
		localeDocPath := setting.DocsJsPath + docPath + "_RM_" + lang
		os.MkdirAll(path.Dir(localeDocPath), os.ModePerm)

		buf := new(bytes.Buffer)
		buf.WriteString("document.write(\"")
		buf.Write(data)
		buf.WriteString("\")")
		if err := ioutil.WriteFile(localeDocPath+".js", buf.Bytes(), 0655); err != nil {
			log.ErrorD(4, "SavePkgDoc( %s ): %v", localeDocPath, err)
		}
	}
}
Пример #4
0
// SaveDocPage saves doc. content to JS file(s),
// it returns max index of JS file(s);
// it returns -1 when error occurs.
func SaveDocPage(docPath string, data []byte) int {
	data = com.Html2JS(data)
	docPath = setting.DocsJsPath + docPath

	buf := new(bytes.Buffer)
	count := 0
	d := string(data)
	l := len(d)
	if l < 80000 {
		buf.WriteString("document.write(\"")
		buf.Write(data)
		buf.WriteString("\")")

		os.MkdirAll(path.Dir(docPath+".js"), os.ModePerm)
		if err := ioutil.WriteFile(docPath+".js", buf.Bytes(), 0655); err != nil {
			log.ErrorD(4, "SaveDocPage( %s ): %v", docPath, err)
			return -1
		}
	} else {
		// Too large, need to sperate.
		start := 0
		end := start + 40000
		for {
			if end >= l {
				end = l
			} else {
				// Need to break in space.
				for {
					if d[end-3:end] == "/b>" {
						break
					}
					end += 1

					if end >= l {
						break
					}
				}
			}

			buf.WriteString("document.write(\"")
			buf.WriteString(d[start:end])
			buf.WriteString("\")\n")

			p := docPath
			if count != 0 {
				p += fmt.Sprintf("-%d", count)
			}

			os.MkdirAll(path.Dir(p+".js"), os.ModePerm)
			if err := ioutil.WriteFile(p+".js", buf.Bytes(), 0655); err != nil {
				log.ErrorD(4, "SaveDocPage( %s ): %v", p, err)
				return -1
			}

			if end >= l {
				break
			}

			buf.Reset()
			start = end
			end += 204800
			count++
		}
	}

	return count
}
Пример #5
0
func renderDoc(this *HomeRouter, pdoc *hv.Package, q, tag, docPath string) bool {
	this.Data["PkgFullIntro"] = pdoc.Doc

	var buf bytes.Buffer
	links := make([]*utils.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, &utils.Link{
			Name:    t.Name,
			Comment: template.HTMLEscapeString(t.Doc),
		})
		buf.WriteString("'" + t.Name + "',")
	}

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

	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("'" + f.Name + "',")
		}

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

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

	// Set exported objects type-ahead.
	exportDataSrc := buf.String()
	if len(exportDataSrc) > 0 {
		pdoc.IsHasExport = true
		this.Data["IsHasExports"] = true
		exportDataSrc = exportDataSrc[:len(exportDataSrc)-1]
		this.Data["ExportDataSrc"] = "<script>$('.search-export').typeahead({local: [" +
			exportDataSrc + "],limit: 10});</script>"
	}

	pdoc.UserExamples = getUserExamples(pdoc.ImportPath)

	pdoc.IsHasConst = len(pdoc.Consts) > 0
	pdoc.IsHasVar = len(pdoc.Vars) > 0
	if len(pdoc.Examples)+len(pdoc.UserExamples) > 0 {
		pdoc.IsHasExample = true
		this.Data["IsHasExample"] = pdoc.IsHasExample
		this.Data["Examples"] = append(pdoc.Examples, pdoc.UserExamples...)
	}

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

	// Constants.
	this.Data["IsHasConst"] = pdoc.IsHasConst
	this.Data["Consts"] = pdoc.Consts
	for i, v := range pdoc.Consts {
		if len(v.Doc) > 0 {
			buf.Reset()
			godoc.ToHTML(&buf, v.Doc, nil)
			v.Doc = buf.String()
		}
		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["IsHasVar"] = pdoc.IsHasVar
	this.Data["Vars"] = pdoc.Vars
	for i, v := range pdoc.Vars {
		if len(v.Doc) > 0 {
			buf.Reset()
			godoc.ToHTML(&buf, v.Doc, nil)
			v.Doc = buf.String()
		}
		buf.Reset()
		utils.FormatCode(&buf, &v.Decl, links)
		v.FmtDecl = buf.String()
		pdoc.Vars[i] = v
	}

	// Dirs.
	pinfos := models.GetSubPkgs(pdoc.ImportPath, tag, pdoc.Dirs)
	if len(pinfos) > 0 {
		pdoc.IsHasSubdir = true
		this.Data["IsHasSubdirs"] = pdoc.IsHasSubdir
		this.Data["Subdirs"] = pinfos
		this.Data["ViewDirPath"] = pdoc.ViewDirPath
	}

	// Files.
	if len(pdoc.Files) > 0 {
		pdoc.IsHasFile = true
		this.Data["IsHasFiles"] = pdoc.IsHasFile
		this.Data["Files"] = pdoc.Files

		var query string
		if i := strings.Index(pdoc.Files[0].BrowseUrl, "?"); i > -1 {
			query = pdoc.Files[0].BrowseUrl[i:]
		}
		this.Data["ViewFilePath"] = path.Dir(pdoc.Files[0].BrowseUrl) + "/" + query
	}

	var err error
	pfuncs := doc.RenderFuncs(pdoc)

	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() + " {"
		if exs := getExamples(pdoc, "", f.Name); len(exs) > 0 {
			f.Examples = exs
		}
		totalNum++
		pdoc.Funcs[i] = f
	}

	this.Data["Types"] = pdoc.Types
	for i, t := range pdoc.Types {
		for j, v := range t.Consts {
			if len(v.Doc) > 0 {
				buf.Reset()
				godoc.ToHTML(&buf, v.Doc, nil)
				v.Doc = buf.String()
			}
			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()
			t.Consts[j] = v
		}
		for j, v := range t.Vars {
			if len(v.Doc) > 0 {
				buf.Reset()
				godoc.ToHTML(&buf, v.Doc, nil)
				v.Doc = buf.String()
			}
			buf.Reset()
			utils.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()
				godoc.ToHTML(&buf, f.Doc, nil)
				f.Doc = buf.String()
				comNum++
			}
			buf.Reset()
			utils.FormatCode(&buf, &f.Decl, links)
			f.FmtDecl = buf.String() + " {"
			if exs := getExamples(pdoc, "", f.Name); len(exs) > 0 {
				f.Examples = 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() + " {"
			if exs := getExamples(pdoc, t.Name, m.Name); len(exs) > 0 {
				m.Examples = 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.Examples = exs
		}
		totalNum++
		pdoc.Types[i] = t
	}

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

	// Examples.
	links = append(links, &utils.Link{
		Name: path.Base(pdoc.ImportPath) + ".",
	})

	for _, e := range pdoc.Examples {
		buf.Reset()
		utils.FormatCode(&buf, &e.Code, links)
		e.Code = buf.String()
	}
	for _, e := range pdoc.UserExamples {
		buf.Reset()
		utils.FormatCode(&buf, &e.Code, links)
		e.Code = buf.String()
	}

	this.Data["ImportPath"] = pdoc.ImportPath

	if len(tag) == 0 && (pdoc.IsCmd || pdoc.IsGoRepo || pdoc.IsGoSubrepo) {
		this.Data["IsHasHv"] = true
	}

	// GitHub redirects non-HTTPS link and Safari loses "#XXX".
	if strings.HasPrefix(pdoc.ImportPath, "github") {
		this.Data["Secure"] = "s"
	}

	this.TplNames = "tpl/docs.tpl"
	data, err := this.RenderBytes()
	if err != nil {
		beego.Error("generatePage(", pdoc.ImportPath, ") -> RenderBytes:", err)
		return false
	}

	n := utils.SaveDocPage(docPath, com.Html2JS(data))
	if n == -1 {
		return false
	}
	pdoc.JsNum = n
	pdoc.Id, err = doc.SaveProject(pdoc, pfuncs)
	if err != nil {
		beego.Error("generatePage(", pdoc.ImportPath, ") -> SaveProject:", err)
		return false
	}

	models.SavePkgDoc(pdoc.ImportPath, pdoc.Readme)

	this.Data["UtcTime"] = pdoc.Created
	this.Data["TimeSince"] = calTimeSince(pdoc.Created)
	return true
}
Пример #6
0
// SaveDocPage saves doc. content to JS file(s),
// it returns max index of JS file(s);
// it returns -1 when error occurs.
func SaveDocPage(docPath string, data []byte) int {
	data = com.Html2JS(data)
	docPath = DocsJsPath + docPath

	buf := new(bytes.Buffer)
	count := 0
	d := string(data)
	l := len(d)
	if l < 80000 {
		buf.WriteString("document.write(\"")
		buf.Write(data)
		buf.WriteString("\")")

		if _, err := com.SaveFile("."+docPath+".js", buf.Bytes()); err != nil {
			beego.Error("utils.SaveDocPage(", docPath, ") ->", err)
			return -1
		}
	} else {
		// Too large, need to sperate.
		start := 0
		end := start + 40000
		for {
			if end >= l {
				end = l
			} else {
				// Need to break in space.
				for {
					if d[end-3:end] == "/b>" {
						break
					}
					end += 1

					if end >= l {
						break
					}
				}
			}

			buf.WriteString("document.write(\"")
			buf.WriteString(d[start:end])
			buf.WriteString("\")\n")

			p := docPath
			if count != 0 {
				p += fmt.Sprintf("-%d", count)
			}

			if _, err := com.SaveFile("."+p+".js", buf.Bytes()); err != nil {
				beego.Error("utils.SaveDocPage(", p, ") ->", err)
				return -1
			}

			if end >= l {
				break
			}

			buf.Reset()
			start = end
			end += 204800
			count++
		}
	}

	return count
}
Пример #7
0
func ExampleHtml2JS() {
	htm := "<div id=\"button\" class=\"btn\">Click me</div>\n\r"
	js := string(com.Html2JS([]byte(htm)))
	fmt.Println(js)
	// Output: <div id=\"button\" class=\"btn\">Click me</div>\n
}