本文整理汇总了Golang中go/doc.ToText函数的典型用法代码示例。如果您正苦于以下问题:Golang ToText函数的具体用法?Golang ToText怎么用?Golang ToText使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ToText函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: writeHelp
func (c *Application) writeHelp(width int, w io.Writer) {
s := []string{formatArgsAndFlags(c.Name, c.argGroup, c.flagGroup)}
if len(c.commands) > 0 {
s = append(s, "<command>", "[<flags>]", "[<args> ...]")
}
prefix := "usage: "
usage := strings.Join(s, " ")
buf := bytes.NewBuffer(nil)
doc.ToText(buf, usage, "", preIndent, width-len(prefix))
lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")
fmt.Fprintf(w, "%s%s\n", prefix, lines[0])
for _, l := range lines[1:] {
fmt.Fprintf(w, "%*s%s\n", len(prefix), "", l)
}
if c.Help != "" {
fmt.Fprintf(w, "\n")
doc.ToText(w, c.Help, "", preIndent, width)
}
c.flagGroup.writeHelp(width, w)
c.argGroup.writeHelp(width, w)
if len(c.commands) > 0 {
fmt.Fprintf(w, "\nCommands:\n")
c.helpCommands(width, w)
}
}
示例2: godoc
func godoc(member, content string) string {
undocumented := "// " + exportable(member) + " is undocumented.\n"
node, err := html.Parse(strings.NewReader(content))
if err != nil {
return undocumented
}
_, v, err := sandblast.Extract(node)
if err != nil {
return undocumented
}
v = strings.TrimSpace(v)
if v == "" {
return undocumented
}
if member != "" {
v = exportable(member) + " " + strings.ToLower(v[0:1]) + v[1:]
}
out := bytes.NewBuffer(nil)
doc.ToText(out, v, "// ", "", 72)
return out.String()
}
示例3: printText
func (p *docPrinter) printText(s string) {
s = strings.TrimRight(s, " \t\n")
if s != "" {
p.scratch.Reset()
godoc.ToText(&p.scratch, s, textIndent, textIndent+"\t", textWidth)
blank := 0
for _, line := range bytes.Split(p.scratch.Bytes(), []byte{'\n'}) {
if len(line) == 0 {
blank++
} else {
const k = len(textIndent) + 1
if blank == 2 && len(line) > k && line[k] != ' ' {
p.WriteString("\n")
p.PushHighlight(headerGroup)
p.Write(line)
p.PopHighlight()
p.WriteString("\n")
} else {
for i := 0; i < blank; i++ {
p.WriteString("\n")
}
p.Write(line)
p.WriteString("\n")
}
blank = 0
}
}
p.WriteString("\n")
}
}
示例4: packageDoc
// packageDoc prints the docs for the package (package doc plus one-liners of the rest).
func (pkg *Package) packageDoc(showRaml bool) {
defer pkg.flush()
if pkg.showInternals() && !showRaml {
pkg.packageClause(false)
}
doc.ToText(&pkg.buf, pkg.doc.Doc, "", indent, indentedWidth)
pkg.newlines(1)
if !pkg.showInternals() {
// Show only package docs for commands.
return
}
pkg.newlines(1)
if showRaml == true {
pkg.ramls()
} else {
pkg.valueSummary(pkg.doc.Consts)
pkg.valueSummary(pkg.doc.Vars)
pkg.funcSummary(pkg.doc.Funcs)
pkg.typeSummary()
pkg.bugs()
}
}
示例5: commentTextFn
// commentTextFn formats a source code comment as text.
func commentTextFn(v string) string {
const indent = " "
var buf bytes.Buffer
godoc.ToText(&buf, v, indent, "\t", 80-2*len(indent))
p := buf.Bytes()
return string(p)
}
示例6: formatTwoColumns
func formatTwoColumns(w io.Writer, indent, padding, width int, rows [][2]string) {
// Find size of first column.
s := 0
for _, row := range rows {
if c := len(row[0]); c > s && c < 30 {
s = c
}
}
indentStr := strings.Repeat(" ", indent)
offsetStr := strings.Repeat(" ", s+padding)
for _, row := range rows {
buf := bytes.NewBuffer(nil)
doc.ToText(buf, row[1], "", preIndent, width-s-padding-indent)
lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")
fmt.Fprintf(w, "%s%-*s%*s", indentStr, s, row[0], padding, "")
if len(row[0]) >= 30 {
fmt.Fprintf(w, "\n%s%s", indentStr, offsetStr)
}
fmt.Fprintf(w, "%s\n", lines[0])
for _, line := range lines[1:] {
fmt.Fprintf(w, "%s%s%s\n", indentStr, offsetStr, line)
}
}
}
示例7: printMethodDoc
// printMethodDoc prints the docs for matches of symbol.method.
// If symbol is empty, it prints all methods that match the name.
// It reports whether it found any methods.
func (pkg *Package) printMethodDoc(symbol, method string) bool {
defer pkg.flush()
types := pkg.findTypes(symbol)
if types == nil {
if symbol == "" {
return false
}
pkg.Fatalf("symbol %s is not a type in package %s installed in %q", symbol, pkg.name, pkg.build.ImportPath)
}
found := false
for _, typ := range types {
if len(typ.Methods) > 0 {
for _, meth := range typ.Methods {
if match(method, meth.Name) {
decl := meth.Decl
decl.Body = nil
pkg.emit(meth.Doc, decl)
found = true
}
}
continue
}
// Type may be an interface. The go/doc package does not attach
// an interface's methods to the doc.Type. We need to dig around.
spec := pkg.findTypeSpec(typ.Decl, typ.Name)
inter, ok := spec.Type.(*ast.InterfaceType)
if !ok {
// Not an interface type.
// TODO? Maybe handle struct fields here.
continue
}
for _, iMethod := range inter.Methods.List {
// This is an interface, so there can be only one name.
// TODO: Anonymous methods (embedding)
if len(iMethod.Names) == 0 {
continue
}
name := iMethod.Names[0].Name
if match(method, name) {
// pkg.oneLineField(iMethod, 0)
if iMethod.Doc != nil {
for _, comment := range iMethod.Doc.List {
doc.ToText(&pkg.buf, comment.Text, "", indent, indentedWidth)
}
}
s := pkg.oneLineNode(iMethod.Type)
// Hack: s starts "func" but there is no name present.
// We could instead build a FuncDecl but it's not worthwhile.
lineComment := ""
if iMethod.Comment != nil {
lineComment = fmt.Sprintf(" %s", iMethod.Comment.List[0].Text)
}
pkg.Printf("func %s%s%s\n", name, s[4:], lineComment)
found = true
}
}
}
return found
}
示例8: writeEnum
func writeEnum(scope Scope, opt Option, delta int) {
fmt.Println()
if opt.Description != "" {
doc.ToText(os.Stdout, opt.Description, " // ", "", 73)
// fmt.Printf(" // %s\n", opt.Description)
}
fmt.Printf(" %s %s = %d\n", scope.Name+translateName(opt.Name), scope.Name, opt.Code+delta)
}
示例9: comment_textFunc
func comment_textFunc(comment, indent, preIndent string) string {
var buf bytes.Buffer
doc.ToText(&buf, comment, indent, preIndent, punchCardWidth-2*len(indent))
if containsOnlySpace(buf.Bytes()) {
return ""
}
return buf.String()
}
示例10: printParagraph
func printParagraph(indent string, bulletCharacter byte, text string) {
var buf bytes.Buffer
doc.ToText(&buf, text, indent, indent, 79-len(indent))
output := buf.Bytes()
if len(output) > len(indent) && len(indent) > 2 {
bulletPos := len(indent) - 2
output[bulletPos] = bulletCharacter
}
os.Stdout.Write(output)
}
示例11: printText
func (p *docPrinter) printText(s string) {
s = strings.TrimRight(s, " \t\n")
if s != "" {
doc.ToText(&p.buf, s, textIndent, textIndent+"\t", textWidth)
b := p.buf.Bytes()
if b[len(b)-1] != '\n' {
p.buf.WriteByte('\n')
}
p.buf.WriteByte('\n')
}
}
示例12: String
func (d *Doc) String() string {
buf := &bytes.Buffer{}
if d.Import != "" {
fmt.Fprintf(buf, "import \"%s\"\n\n", d.Import)
}
fmt.Fprintf(buf, "%s\n\n", d.Decl)
if d.Doc == "" {
d.Doc = "Undocumented."
}
doc.ToText(buf, d.Doc, indent, preIndent, *linelength)
return buf.String()
}
示例13: helpCommands
func (c *Application) helpCommands(width int, w io.Writer) {
for _, cmd := range c.commandOrder {
fmt.Fprintf(w, " %s\n", formatArgsAndFlags(cmd.name, cmd.argGroup, cmd.flagGroup))
buf := bytes.NewBuffer(nil)
doc.ToText(buf, cmd.help, "", preIndent, width-4)
lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")
for _, line := range lines {
fmt.Fprintf(w, " %s\n", line)
}
fmt.Fprintf(w, "\n")
}
}
示例14: packageDoc
// packageDoc prints the docs for the package (package doc plus one-liners of the rest).
func (pkg *Package) packageDoc() {
defer pkg.flush()
pkg.packageClause(false)
doc.ToText(&pkg.buf, pkg.doc.Doc, "", "\t", 80)
pkg.newlines(2)
pkg.valueSummary(pkg.doc.Consts)
pkg.valueSummary(pkg.doc.Vars)
pkg.funcSummary(pkg.doc.Funcs)
pkg.typeSummary()
pkg.bugs()
}
示例15: emit
// emit prints the node.
func (pkg *Package) emit(comment string, node ast.Node) {
if node != nil {
err := format.Node(&pkg.buf, pkg.fs, node)
if err != nil {
log.Fatal(err)
}
if comment != "" {
pkg.newlines(2) // Guarantee blank line before comment.
doc.ToText(&pkg.buf, comment, " ", indent, indentedWidth)
}
pkg.newlines(1)
}
}