version: 1.10

package doc

import "go/doc"

Overview

Package doc extracts source code documentation from a Go AST.

Index

Package files

comment.go doc.go example.go exports.go filter.go reader.go synopsis.go

Variables

  1. var IllegalPrefixes = []string{
  2. "copyright",
  3. "all rights",
  4. "author",
  5. }

func Examples

  1. func Examples(files ...*ast.File) []*Example

Examples returns the examples found in the files, sorted by Name field. The Order fields record the order in which the examples were encountered.

Playable Examples must be in a package whose name ends in “_test”. An Example is “playable” (the Play field is non-nil) in either of these circumstances:

  1. - The example function is self-contained: the function references only
  2. identifiers from other packages (or predeclared identifiers, such as
  3. "int") and the test file does not include a dot import.
  4. - The entire test file is the example: the file contains exactly one
  5. example function, zero test or benchmark functions, and at least one
  6. top-level function, type, variable, or constant declaration other
  7. than the example function.

func IsPredeclared

  1. func IsPredeclared(s string) bool

IsPredeclared reports whether s is a predeclared identifier.

func Synopsis

  1. func Synopsis(s string) string

Synopsis returns a cleaned version of the first sentence in s. That sentence ends after the first period followed by space and not preceded by exactly one uppercase letter. The result string has no \n, \r, or \t characters and uses only single spaces between words. If s starts with any of the IllegalPrefixes, the result is the empty string.

func ToHTML

  1. func ToHTML(w io.Writer, text string, words map[string]string)

ToHTML converts comment text to formatted HTML. The comment was prepared by DocReader, so it is known not to have leading, trailing blank lines nor to have trailing spaces at the end of lines. The comment markers have already been removed.

Each span of unindented non-blank lines is converted into a single paragraph. There is one exception to the rule: a span that consists of a single line, is followed by another paragraph span, begins with a capital letter, and contains no punctuation is formatted as a heading.

A span of indented lines is converted into a

  1. block, with the common indent
  2. prefix removed.

  3. URLs in the comment text are converted into links; if the URL also appears in

  4. the words map, the link is taken from the map (if the corresponding map value is

  5. the empty string, the URL is not converted into a link).

  6. Go identifiers that appear in the words map are italicized; if the corresponding

  7. map value is not the empty string, it is considered a URL and the word is

  8. converted into a link.

  9. func ToText

  10. func ToText(w io.Writer, text string, indent, preIndent string, width int)
  11.  
  12. ToText prepares comment text for presentation in textual output. It wraps

  13. paragraphs of text to width or fewer Unicode code points and then prefixes each

  14. line with the indent. In preformatted sections (such as program text), it

  15. prefixes each non-blank line with preIndent.

  16. type Example

  17. type Example struct {
  18.     Name        string // name of the item being exemplified
  19.     Doc         string // example function doc string
  20.     Code        ast.Node
  21.     Play        *ast.File // a whole program version of the example
  22.     Comments    []*ast.CommentGroup
  23.     Output      string // expected output
  24.     Unordered   bool
  25.     EmptyOutput bool // expect empty output
  26.     Order       int  // original source code order
  27. }
  28.  
  29. An Example represents an example function found in a source files.

  30. type Filter

  31. type Filter func(string) bool
  32.  
  33.  
  34. type Func

  35. type Func struct {
  36.     Doc  string
  37.     Name string
  38.     Decl *ast.FuncDecl
  39.     // methods
  40.     // (for functions, these fields have the respective zero value)
  41.     Recv  string // actual   receiver "T" or "*T"
  42.     Orig  string // original receiver "T" or "*T"
  43.     Level int    // embedding level; 0 means not embedded
  44. }
  45.  
  46. Func is the documentation for a func declaration.

  47. type Mode

  48. type Mode int
  49.  
  50. Mode values control the operation of New.

  51. const (
  52.     // extract documentation for all package-level declarations,
  53.     // not just exported ones
  54.     AllDecls Mode = 1 << iota
  55.     // show all embedded methods, not just the ones of
  56.     // invisible (unexported) anonymous fields
  57.     AllMethods
  58. )
  59.  
  60.  
  61. type Note

  62. type Note struct {
  63.     Pos, End token.Pos // position range of the comment containing the marker
  64.     UID      string    // uid found with the marker
  65.     Body     string    // note body text
  66. }
  67.  
  68. A Note represents a marked comment starting with MARKER(uid): note body”. Any

  69. note with a marker of 2 or more upper case [A-Z] letters and a uid of at least

  70. one character is recognized. The “:” following the uid is optional. Notes are

  71. collected in the Package.Notes map indexed by the notes marker.

  72. type Package

  73. type Package struct {
  74.     Doc        string
  75.     Name       string
  76.     ImportPath string
  77.     Imports    []string
  78.     Filenames  []string
  79.     Notes      map[string][]*Note
  80.     // Deprecated: For backward compatibility Bugs is still populated,
  81.     // but all new code should use Notes instead.
  82.     Bugs []string
  83.     // declarations
  84.     Consts []*Value
  85.     Types  []*Type
  86.     Vars   []*Value
  87.     Funcs  []*Func
  88. }
  89.  
  90. Package is the documentation for an entire package.

  91. func New

  92. func New(pkg *ast.Package, importPath string, mode Mode) *Package
  93.  
  94. New computes the package documentation for the given package AST. New takes

  95. ownership of the AST pkg and may edit or overwrite it.

  96. func (*Package) Filter

  97. func (p *Package) Filter(f Filter)
  98.  
  99. Filter eliminates documentation for names that dont pass through the filter f.

  100. TODO(gri): Recognize Type.Method as a name.

  101. type Type

  102. type Type struct {
  103.     Doc  string
  104.     Name string
  105.     Decl *ast.GenDecl
  106.     // associated declarations
  107.     Consts  []*Value // sorted list of constants of (mostly) this type
  108.     Vars    []*Value // sorted list of variables of (mostly) this type
  109.     Funcs   []*Func  // sorted list of functions returning this type
  110.     Methods []*Func  // sorted list of methods (including embedded ones) of this type
  111. }
  112.  
  113. Type is the documentation for a type declaration.

  114. type Value

  115. type Value struct {
  116.     Doc   string
  117.     Names []string // var or const names in declaration order
  118.     Decl  *ast.GenDecl
  119.     // contains filtered or unexported fields
  120. }
  121.  
  122. Value is the documentation for a (possibly grouped) var or const declaration.