github.com/huner2/gomarkdoc@v0.3.6/README.md (about)

     1  <!-- Code generated by gomarkdoc. DO NOT EDIT -->
     2  
     3  [![Go Report Card](https://goreportcard.com/badge/github.com/huner2/gomarkdoc)](https://goreportcard.com/report/github.com/huner2/gomarkdoc)
     4  [![GitHub Actions](https://github.com/huner2/gomarkdoc/workflows/Test/badge.svg)](https://github.com/huner2/gomarkdoc/actions?query=workflow%3ATest+branch%3Amaster)
     5  [![Go Reference](https://pkg.go.dev/badge/github.com/huner2/gomarkdoc.svg)](https://pkg.go.dev/github.com/huner2/gomarkdoc)
     6  [![codecov](https://codecov.io/gh/huner2/gomarkdoc/branch/master/graph/badge.svg?token=171XNH5XLT)](https://codecov.io/gh/huner2/gomarkdoc)
     7  
     8  # gomarkdoc
     9  
    10  ```go
    11  import "github.com/huner2/gomarkdoc"
    12  ```
    13  
    14  Package gomarkdoc formats documentation for one or more packages as markdown for usage outside of the main https://pkg.go.dev site\. It supports custom templates for tweaking representation of documentation at fine\-grained levels\, exporting both exported and unexported symbols\, and custom formatters for different backends\.
    15  
    16  ### Command Line Usage
    17  
    18  If you want to use this package as a command\-line tool\, you can install the command by running the following on go 1\.16\+:
    19  
    20  ```
    21  go install github.com/huner2/gomarkdoc/cmd/gomarkdoc@latest
    22  ```
    23  
    24  For older versions of go\, you can install using the following method instead:
    25  
    26  ```
    27  GO111MODULE=on go get -u github.com/huner2/gomarkdoc/cmd/gomarkdoc
    28  ```
    29  
    30  The command line tool supports configuration for all of the features of the importable package:
    31  
    32  ```
    33  $ gomarkdoc --help
    34  generate markdown documentation for golang code
    35  
    36  Usage:
    37    gomarkdoc [flags] [package ...]
    38  
    39  Flags:
    40    -c, --check                              Check the output to see if it matches the generated documentation. --output must be specified to use this.
    41        --config string                      File from which to load configuration (default: .gomarkdoc.yml)
    42    -e, --embed                              Embed documentation into existing markdown files if available, otherwise append to file.
    43        --footer string                      Additional content to inject at the end of each output file.
    44        --footer-file string                 File containing additional content to inject at the end of each output file.
    45    -f, --format string                      Format to use for writing output data. Valid options: github (default), azure-devops, plain (default "github")
    46        --header string                      Additional content to inject at the beginning of each output file.
    47        --header-file string                 File containing additional content to inject at the beginning of each output file.
    48    -h, --help                               help for gomarkdoc
    49    -u, --include-unexported                 Output documentation for unexported symbols, methods and fields in addition to exported ones.
    50    -o, --output string                      File or pattern specifying where to write documentation output. Defaults to printing to stdout.
    51        --repository.default-branch string   Manual override for the git repository URL used in place of automatic detection.
    52        --repository.path string             Manual override for the path from the root of the git repository used in place of automatic detection.
    53        --repository.url string              Manual override for the git repository URL used in place of automatic detection.
    54        --tags strings                       Set of build tags to apply when choosing which files to include for documentation generation.
    55    -t, --template stringToString            Custom template string to use for the provided template name instead of the default template. (default [])
    56        --template-file stringToString       Custom template file to use for the provided template name instead of the default template. (default [])
    57    -v, --verbose count                      Log additional output from the execution of the command. Can be chained for additional verbosity.
    58        --version                            Print the version.
    59  ```
    60  
    61  The gomarkdoc command processes each of the provided packages\, generating documentation for the package in markdown format and writing it to console\. For example\, if you have a package in your current directory and want to send it to a documentation markdown file\, you might do something like this:
    62  
    63  ```
    64  gomarkdoc --output doc.md .
    65  ```
    66  
    67  ### Package Specifiers
    68  
    69  The gomarkdoc tool supports generating documentation for both local packages and remote ones\. To specify a local package\, start the name of the package with a period \(\.\) or specify an absolute path on the filesystem\. All other package signifiers are assumed to be remote packages\. You may specify both local and remote packages in the same command invocation as separate arguments\.
    70  
    71  ### Output Redirection
    72  
    73  By default\, the documentation generated by the gomarkdoc command is sent to standard output\, where it can be redirected to a file\. This can be useful if you want to perform additional modifications to the documentation or send it somewhere other than a file\. However\, keep in mind that there are some inconsistencies in how various shells/platforms handle redirected command output \(for example\, Powershell encodes in UTF\-16\, not UTF\-8\)\. As a result\, the \-\-output option described below is recommended for most use cases\.
    74  
    75  ```
    76  gomarkdoc . > doc.md
    77  ```
    78  
    79  If you want to redirect output for each processed package to a file\, you can provide the \-\-output/\-o option\, which accepts a template specifying how to generate the path of the output file\. A common usage of this option is when generating README documentation for a package with subpackages \(which are supported via the \.\.\. signifier as in other parts of the golang toolchain\)\. In addition\, this option provides consistent behavior across platforms and shells:
    80  
    81  ```
    82  gomarkdoc --output '{{.Dir}}/README.md' ./...
    83  ```
    84  
    85  You can see all of the data available to the output template in the PackageSpec struct in the github\.com/huner2/gomarkdoc/cmd/gomarkdoc package\.
    86  
    87  ### Template Overrides
    88  
    89  The documentation information that is output is formatted using a series of text templates for the various components of the overall documentation which get generated\. Higher level templates contain lower level templates\, but any template may be replaced with an override template using the \-\-template/\-t option\. The full list of templates that may be overridden are:
    90  
    91  ```
    92  - file:    generates documentation for a file containing one or more
    93             packages, depending on how the tool is configured. This is the
    94             root template for documentation generation.
    95  
    96  - package: generates documentation for an entire package.
    97  
    98  - type:    generates documentation for a single type declaration, as well
    99             as any related functions/methods.
   100  
   101  - func:    generates documentation for a single function or method. It may
   102             be referenced from within a type, or directly in the package,
   103             depending on nesting.
   104  
   105  - value:   generates documentation for a single variable or constant
   106             declaration block within a package.
   107  
   108  - index:   generates an index of symbols within a package, similar to what
   109             is seen for godoc.org. The index links to types, funcs,
   110             variables, and constants generated by other templates, so it may
   111             need to be overridden as well if any of those templates are
   112             changed in a material way.
   113  
   114  - example: generates documentation for a single example for a package or
   115             one of its symbols. The example is generated alongside whichever
   116             symbol it represents, based on the standard naming conventions
   117             outlined in https://blog.golang.org/examples#TOC_4.
   118  
   119  - doc:     generates the freeform documentation block for any of the above
   120             structures that can contain a documentation section.
   121  ```
   122  
   123  Overriding with the \-t option uses a key\-vaule pair mapping a template name to the file containing the contents of the override template to use\. Specified template files must exist:
   124  
   125  ```
   126  gomarkdoc -t package=custom-package.gotxt -t doc=custom-doc.gotxt .
   127  ```
   128  
   129  ### Additional Options
   130  
   131  As with the godoc tool itself\, only exported symbols will be shown in documentation\. This can be expanded to include all symbols in a package by adding the \-\-include\-unexported/\-u flag\.
   132  
   133  ```
   134  gomarkdoc -u -o README.md .
   135  ```
   136  
   137  If you want to blend the documentation generated by gomarkdoc with your own hand\-written markdown\, you can use the \-\-embed/\-e flag to change the gomarkdoc tool into an append/embed mode\. When documentation is generated\, gomarkdoc looks for a file in the location where the documentation is to be written and embeds the documentation if present\. Otherwise\, the documentation is appended to the end of the file\.
   138  
   139  ```
   140  gomarkdoc -o README.md -e .
   141  ```
   142  
   143  When running with embed mode enabled\, gomarkdoc will look for either this single comment:
   144  
   145  ```
   146  <!-- gomarkdoc:embed -->
   147  ```
   148  
   149  Or the following pair of comments \(in which case all content in between is replaced\):
   150  
   151  ```
   152  <!-- gomarkdoc:embed:start -->
   153  
   154  This content is replaced with the embedded documentation
   155  
   156  <!-- gomarkdoc:embed:end -->
   157  ```
   158  
   159  If you would like to include files that are part of a build tag\, you can specify build tags with the \-\-tags flag\. Tags are also supported through GOFLAGS\, though command line and configuration file definitions override tags specified through GOFLAGS\.
   160  
   161  ```
   162  gomarkdoc --tags sometag .
   163  ```
   164  
   165  You can also run gomarkdoc in a verification mode with the \-\-check/\-c flag\. This is particularly useful for continuous integration when you want to make sure that a commit correctly updated the generated documentation\. This flag is only supported when the \-\-output/\-o flag is specified\, as the file provided there is what the tool is checking:
   166  
   167  ```
   168  gomarkdoc -o README.md -c .
   169  ```
   170  
   171  If you're experiencing difficulty with gomarkdoc or just want to get more information about how it's executing underneath\, you can add \-v to show more logs\. This can be chained a second time to show even more verbose logs:
   172  
   173  ```
   174  gomarkdoc -vv -o README.md .
   175  ```
   176  
   177  Some features of gomarkdoc rely on being able to detect information from the git repository containing the project\. Since individual local git repositories may be configured differently from person to person\, you may want to manually specify the information for the repository to remove any inconsistencies\. This can be achieved with the \-\-repository\.url\, \-\-repository\.default\-branch and \-\-repository\.path options\. For example\, this repository would be configured with:
   178  
   179  ```
   180  gomarkdoc --repository.url "https://github.com/huner2/gomarkdoc" --repository.default-branch master --repository.path / -o README.md .
   181  ```
   182  
   183  ### Configuring via File
   184  
   185  If you want to reuse configuration options across multiple invocations\, you can specify a file in the folder where you invoke gomarkdoc containing configuration information that you would otherwise provide on the command line\. This file may be a JSON\, TOML\, YAML\, HCL\, env\, or Java properties file\, but the name is expected to start with \.gomarkdoc \(e\.g\. \.gomarkdoc\.yml\)\.
   186  
   187  All configuration options are available with the camel\-cased form of their long name \(e\.g\. \-\-include\-unexported becomes includeUnexported\)\. Template overrides are specified as a map\, rather than a set of key\-value pairs separated by =\. Options provided on the command line override those provided in the configuration file if an option is present in both\.
   188  
   189  ### Programmatic Usage
   190  
   191  While most users will find the command line utility sufficient for their needs\, this package may also be used programmatically by installing it directly\, rather than its command subpackage\. The programmatic usage provides more flexibility when selecting what packages to work with and what components to generate documentation for\.
   192  
   193  A common usage will look something like this:
   194  
   195  ```
   196  package main
   197  
   198  import (
   199  	"go/build"
   200  	"fmt"
   201  	"os"
   202  
   203  	"github.com/huner2/gomarkdoc"
   204  	"github.com/huner2/gomarkdoc/lang"
   205  	"github.com/huner2/gomarkdoc/logger"
   206  )
   207  
   208  func main() {
   209  	// Create a renderer to output data
   210  	out, err := gomarkdoc.NewRenderer()
   211  	if err != nil {
   212  		// handle error
   213  	}
   214  
   215  	wd, err := os.Getwd()
   216  	if err != nil {
   217  		// handle error
   218  	}
   219  
   220  	buildPkg, err := build.ImportDir(wd, build.ImportComment)
   221  	if err != nil {
   222  		// handle error
   223  	}
   224  
   225  	// Create a documentation package from the build representation of our
   226  	// package.
   227  	log := logger.New(logger.DebugLevel)
   228  	pkg, err := lang.NewPackageFromBuild(log, buildPkg)
   229  	if err != nil {
   230  		// handle error
   231  	}
   232  
   233  	// Write the documentation out to console.
   234  	fmt.Println(out.Package(pkg))
   235  }
   236  ```
   237  
   238  ## Index
   239  
   240  - [type Renderer](<#type-renderer>)
   241    - [func NewRenderer(opts ...RendererOption) (*Renderer, error)](<#func-newrenderer>)
   242    - [func (out *Renderer) Example(ex *lang.Example) (string, error)](<#func-renderer-example>)
   243    - [func (out *Renderer) File(file *lang.File) (string, error)](<#func-renderer-file>)
   244    - [func (out *Renderer) Func(fn *lang.Func) (string, error)](<#func-renderer-func>)
   245    - [func (out *Renderer) Package(pkg *lang.Package) (string, error)](<#func-renderer-package>)
   246    - [func (out *Renderer) Type(typ *lang.Type) (string, error)](<#func-renderer-type>)
   247  - [type RendererOption](<#type-rendereroption>)
   248    - [func WithFormat(format format.Format) RendererOption](<#func-withformat>)
   249    - [func WithTemplateOverride(name, tmpl string) RendererOption](<#func-withtemplateoverride>)
   250  
   251  
   252  ## type [Renderer](<https://github.com/huner2/gomarkdoc/blob/master/renderer.go#L15-L19>)
   253  
   254  Renderer provides capabilities for rendering various types of documentation with the configured format and templates\.
   255  
   256  ```go
   257  type Renderer struct {
   258      // contains filtered or unexported fields
   259  }
   260  ```
   261  
   262  ### func [NewRenderer](<https://github.com/huner2/gomarkdoc/blob/master/renderer.go#L30>)
   263  
   264  ```go
   265  func NewRenderer(opts ...RendererOption) (*Renderer, error)
   266  ```
   267  
   268  NewRenderer initializes a Renderer configured using the provided options\. If nothing special is provided\, the created renderer will use the default set of templates and the GitHubFlavoredMarkdown\.
   269  
   270  ### func \(\*Renderer\) [Example](<https://github.com/huner2/gomarkdoc/blob/master/renderer.go#L140>)
   271  
   272  ```go
   273  func (out *Renderer) Example(ex *lang.Example) (string, error)
   274  ```
   275  
   276  Example renders an example's documentation to a string\. You can change the rendering of the example by overriding the "example" template or one of the templates it references\.
   277  
   278  ### func \(\*Renderer\) [File](<https://github.com/huner2/gomarkdoc/blob/master/renderer.go#L112>)
   279  
   280  ```go
   281  func (out *Renderer) File(file *lang.File) (string, error)
   282  ```
   283  
   284  File renders a file containing one or more packages to document to a string\. You can change the rendering of the file by overriding the "file" template or one of the templates it references\.
   285  
   286  ### func \(\*Renderer\) [Func](<https://github.com/huner2/gomarkdoc/blob/master/renderer.go#L126>)
   287  
   288  ```go
   289  func (out *Renderer) Func(fn *lang.Func) (string, error)
   290  ```
   291  
   292  Func renders a function's documentation to a string\. You can change the rendering of the package by overriding the "func" template or one of the templates it references\.
   293  
   294  ### func \(\*Renderer\) [Package](<https://github.com/huner2/gomarkdoc/blob/master/renderer.go#L119>)
   295  
   296  ```go
   297  func (out *Renderer) Package(pkg *lang.Package) (string, error)
   298  ```
   299  
   300  Package renders a package's documentation to a string\. You can change the rendering of the package by overriding the "package" template or one of the templates it references\.
   301  
   302  ### func \(\*Renderer\) [Type](<https://github.com/huner2/gomarkdoc/blob/master/renderer.go#L133>)
   303  
   304  ```go
   305  func (out *Renderer) Type(typ *lang.Type) (string, error)
   306  ```
   307  
   308  Type renders a type's documentation to a string\. You can change the rendering of the type by overriding the "type" template or one of the templates it references\.
   309  
   310  ## type [RendererOption](<https://github.com/huner2/gomarkdoc/blob/master/renderer.go#L22>)
   311  
   312  RendererOption configures the renderer's behavior\.
   313  
   314  ```go
   315  type RendererOption func(renderer *Renderer) error
   316  ```
   317  
   318  ### func [WithFormat](<https://github.com/huner2/gomarkdoc/blob/master/renderer.go#L102>)
   319  
   320  ```go
   321  func WithFormat(format format.Format) RendererOption
   322  ```
   323  
   324  WithFormat changes the renderer to use the format provided instead of the default format\.
   325  
   326  ### func [WithTemplateOverride](<https://github.com/huner2/gomarkdoc/blob/master/renderer.go#L88>)
   327  
   328  ```go
   329  func WithTemplateOverride(name, tmpl string) RendererOption
   330  ```
   331  
   332  WithTemplateOverride adds a template that overrides the template with the provided name using the value provided in the tmpl parameter\.
   333  
   334  
   335  
   336  Generated by [gomarkdoc](<https://github.com/huner2/gomarkdoc>)