github.com/anthonyme00/gomarkdoc@v1.0.0/README.md (about) 1 <!-- Code generated by gomarkdoc. DO NOT EDIT --> 2 # gomarkdoc 3 4 ## NOTICE 5 6 Not the original package of gomarkdoc, this fork is mainly for experimenting with automatically generating hugo documentation. 7 8 This fork adds additional parameter, namely: 9 10 - `--file-only` -> Only definition for the specified file will be included in the generated docs, also filters out doc.go package level documentation 11 - `--override-import-path` -> by default, when specifying local files, the original package adds the following line: 12 13 ```go 14 import "command-line-arguments" 15 ``` 16 17 this overrides the value of "command-line-arguments" 18 19 This repository will only be maintained when i feel like it, please use the original gomarkdoc package instead at https://github.com/princjef/gomarkdoc 20 21 22 --- 23 24 ```go 25 import "github.com/anthonyme00/gomarkdoc" 26 ``` 27 28 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. 29 30 ### Command Line Usage 31 32 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\+: 33 34 ``` 35 go install github.com/princjef/gomarkdoc/cmd/gomarkdoc@latest 36 ``` 37 38 For older versions of go, you can install using the following method instead: 39 40 ``` 41 GO111MODULE=on go get -u github.com/princjef/gomarkdoc/cmd/gomarkdoc 42 ``` 43 44 The command line tool supports configuration for all of the features of the importable package: 45 46 ``` 47 $ gomarkdoc --help 48 generate markdown documentation for golang code 49 50 Usage: 51 gomarkdoc [flags] [package ...] 52 53 Flags: 54 -c, --check Check the output to see if it matches the generated documentation. --output must be specified to use this. 55 --config string File from which to load configuration (default: .gomarkdoc.yml) 56 -e, --embed Embed documentation into existing markdown files if available, otherwise append to file. 57 --exclude-dirs strings List of package directories to ignore when producing documentation. 58 --file-only Only includes definition inside the defined files 59 --footer string Additional content to inject at the end of each output file. 60 --footer-file string File containing additional content to inject at the end of each output file. 61 -f, --format string Format to use for writing output data. Valid options: github (default), azure-devops, plain (default "github") 62 --header string Additional content to inject at the beginning of each output file. 63 --header-file string File containing additional content to inject at the beginning of each output file. 64 -h, --help help for gomarkdoc 65 -u, --include-unexported Output documentation for unexported symbols, methods and fields in addition to exported ones. 66 -o, --output string File or pattern specifying where to write documentation output. Defaults to printing to stdout. 67 --override-import-path string Override the import path of the package. This is useful when the package is not in the GOPATH. 68 --repository.default-branch string Manual override for the git repository URL used in place of automatic detection. 69 --repository.path string Manual override for the path from the root of the git repository used in place of automatic detection. 70 --repository.url string Manual override for the git repository URL used in place of automatic detection. 71 --tags strings Set of build tags to apply when choosing which files to include for documentation generation. 72 -t, --template stringToString Custom template string to use for the provided template name instead of the default template. (default []) 73 --template-file stringToString Custom template file to use for the provided template name instead of the default template. (default []) 74 -v, --verbose count Log additional output from the execution of the command. Can be chained for additional verbosity. 75 --version Print the version. 76 ``` 77 78 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: 79 80 ``` 81 gomarkdoc --output doc.md . 82 ``` 83 84 ### Package Specifiers 85 86 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. 87 88 If you have a project with many packages but you want to skip documentation generation for some, you can use the \-\-exclude\-dirs option. This will remove any matching directories from the list of directories to process. Excluded directories are specified using the same pathing syntax as the packages to process. Multiple expressions may be comma\-separated or specified by using the \-\-exclude\-dirs flag multiple times. 89 90 For example, in this repository we generate documentation for the entire project while excluding our test packages by running: 91 92 ``` 93 gomarkdoc --exclude-dirs ./testData/... ./... 94 ``` 95 96 ### Output Redirection 97 98 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. 99 100 ``` 101 gomarkdoc . > doc.md 102 ``` 103 104 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: 105 106 ``` 107 gomarkdoc --output '{{.Dir}}/README.md' ./... 108 ``` 109 110 You can see all of the data available to the output template in the PackageSpec struct in the github.com/princjef/gomarkdoc/cmd/gomarkdoc package. 111 112 ### Template Overrides 113 114 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: 115 116 - file: generates documentation for a file containing one or more packages, depending on how the tool is configured. This is the root template for documentation generation. 117 118 - package: generates documentation for an entire package. 119 120 - type: generates documentation for a single type declaration, as well as any related functions/methods. 121 122 - func: generates documentation for a single function or method. It may be referenced from within a type, or directly in the package, depending on nesting. 123 124 - value: generates documentation for a single variable or constant declaration block within a package. 125 126 - index: generates an index of symbols within a package, similar to what is seen for godoc.org. The index links to types, funcs, variables, and constants generated by other templates, so it may need to be overridden as well if any of those templates are changed in a material way. 127 128 - example: generates documentation for a single example for a package or one of its symbols. The example is generated alongside whichever symbol it represents, based on the standard naming conventions outlined in https://blog.golang.org/examples#TOC_4. 129 130 - doc: generates the freeform documentation block for any of the above structures that can contain a documentation section. 131 132 - import: generates the import code used to pull in a package. 133 134 Overriding with the \-\-template\-file option uses a key\-value pair mapping a template name to the file containing the contents of the override template to use. Specified template files must exist: 135 136 ``` 137 gomarkdoc --template-file package=custom-package.gotxt --template-file doc=custom-doc.gotxt . 138 ``` 139 140 ### Additional Options 141 142 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. 143 144 ``` 145 gomarkdoc -u -o README.md . 146 ``` 147 148 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. 149 150 ``` 151 gomarkdoc -o README.md -e . 152 ``` 153 154 When running with embed mode enabled, gomarkdoc will look for either this single comment: 155 156 ``` 157 <!-- gomarkdoc:embed --> 158 ``` 159 160 Or the following pair of comments \(in which case all content in between is replaced\): 161 162 ``` 163 <!-- gomarkdoc:embed:start --> 164 165 This content is replaced with the embedded documentation 166 167 <!-- gomarkdoc:embed:end --> 168 ``` 169 170 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. 171 172 ``` 173 gomarkdoc --tags sometag . 174 ``` 175 176 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: 177 178 ``` 179 gomarkdoc -o README.md -c . 180 ``` 181 182 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: 183 184 ``` 185 gomarkdoc -vv -o README.md . 186 ``` 187 188 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: 189 190 ``` 191 gomarkdoc --repository.url "https://github.com/princjef/gomarkdoc" --repository.default-branch master --repository.path / -o README.md . 192 ``` 193 194 ### Configuring via File 195 196 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\). 197 198 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. 199 200 ### Programmatic Usage 201 202 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. 203 204 A common usage will look something like this: 205 206 ``` 207 package main 208 209 import ( 210 "go/build" 211 "fmt" 212 "os" 213 214 "github.com/anthonyme00/gomarkdoc" 215 "github.com/anthonyme00/gomarkdoc/lang" 216 "github.com/anthonyme00/gomarkdoc/logger" 217 ) 218 219 func main() { 220 // Create a renderer to output data 221 out, err := gomarkdoc.NewRenderer() 222 if err != nil { 223 // handle error 224 } 225 226 wd, err := os.Getwd() 227 if err != nil { 228 // handle error 229 } 230 231 buildPkg, err := build.ImportDir(wd, build.ImportComment) 232 if err != nil { 233 // handle error 234 } 235 236 // Create a documentation package from the build representation of our 237 // package. 238 log := logger.New(logger.DebugLevel) 239 pkg, err := lang.NewPackageFromBuild(log, buildPkg) 240 if err != nil { 241 // handle error 242 } 243 244 // Write the documentation out to console. 245 fmt.Println(out.Package(pkg)) 246 } 247 ``` 248 249 ### Examples 250 251 This project uses itself to generate the README files in github.com/princjef/gomarkdoc and its subdirectories. To see the commands that are run to generate documentation for this repository, take a look at the Doc\(\) and DocVerify\(\) functions in magefile.go and the .gomarkdoc.yml file in the root of this repository. To run these commands in your own project, simply replace \`go run ./cmd/gomarkdoc\` with \`gomarkdoc\`. 252 253 Know of another project that is using gomarkdoc? Open an issue with a description of the project and link to the repository and it might be featured here\! 254 255 ## Index 256 257 - [type Renderer](<#Renderer>) 258 - [func NewRenderer\(opts ...RendererOption\) \(\*Renderer, error\)](<#NewRenderer>) 259 - [func \(out \*Renderer\) Example\(ex \*lang.Example\) \(string, error\)](<#Renderer.Example>) 260 - [func \(out \*Renderer\) File\(file \*lang.File\) \(string, error\)](<#Renderer.File>) 261 - [func \(out \*Renderer\) Func\(fn \*lang.Func\) \(string, error\)](<#Renderer.Func>) 262 - [func \(out \*Renderer\) Package\(pkg \*lang.Package\) \(string, error\)](<#Renderer.Package>) 263 - [func \(out \*Renderer\) Type\(typ \*lang.Type\) \(string, error\)](<#Renderer.Type>) 264 - [type RendererOption](<#RendererOption>) 265 - [func WithFormat\(format format.Format\) RendererOption](<#WithFormat>) 266 - [func WithTemplateFunc\(name string, fn any\) RendererOption](<#WithTemplateFunc>) 267 - [func WithTemplateOverride\(name, tmpl string\) RendererOption](<#WithTemplateOverride>) 268 269 270 <a name="Renderer"></a> 271 ## type [Renderer](<https://github.com/princjef/gomarkdoc/blob/master/renderer.go#L16-L21>) 272 273 Renderer provides capabilities for rendering various types of documentation with the configured format and templates. 274 275 ```go 276 type Renderer struct { 277 // contains filtered or unexported fields 278 } 279 ``` 280 281 <a name="NewRenderer"></a> 282 ### func [NewRenderer](<https://github.com/princjef/gomarkdoc/blob/master/renderer.go#L32>) 283 284 ```go 285 func NewRenderer(opts ...RendererOption) (*Renderer, error) 286 ``` 287 288 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. 289 290 <a name="Renderer.Example"></a> 291 ### func \(\*Renderer\) [Example](<https://github.com/princjef/gomarkdoc/blob/master/renderer.go#L135>) 292 293 ```go 294 func (out *Renderer) Example(ex *lang.Example) (string, error) 295 ``` 296 297 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. 298 299 <a name="Renderer.File"></a> 300 ### func \(\*Renderer\) [File](<https://github.com/princjef/gomarkdoc/blob/master/renderer.go#L107>) 301 302 ```go 303 func (out *Renderer) File(file *lang.File) (string, error) 304 ``` 305 306 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. 307 308 <a name="Renderer.Func"></a> 309 ### func \(\*Renderer\) [Func](<https://github.com/princjef/gomarkdoc/blob/master/renderer.go#L121>) 310 311 ```go 312 func (out *Renderer) Func(fn *lang.Func) (string, error) 313 ``` 314 315 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. 316 317 <a name="Renderer.Package"></a> 318 ### func \(\*Renderer\) [Package](<https://github.com/princjef/gomarkdoc/blob/master/renderer.go#L114>) 319 320 ```go 321 func (out *Renderer) Package(pkg *lang.Package) (string, error) 322 ``` 323 324 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. 325 326 <a name="Renderer.Type"></a> 327 ### func \(\*Renderer\) [Type](<https://github.com/princjef/gomarkdoc/blob/master/renderer.go#L128>) 328 329 ```go 330 func (out *Renderer) Type(typ *lang.Type) (string, error) 331 ``` 332 333 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. 334 335 <a name="RendererOption"></a> 336 ## type [RendererOption](<https://github.com/princjef/gomarkdoc/blob/master/renderer.go#L24>) 337 338 RendererOption configures the renderer's behavior. 339 340 ```go 341 type RendererOption func(renderer *Renderer) error 342 ``` 343 344 <a name="WithFormat"></a> 345 ### func [WithFormat](<https://github.com/princjef/gomarkdoc/blob/master/renderer.go#L83>) 346 347 ```go 348 func WithFormat(format format.Format) RendererOption 349 ``` 350 351 WithFormat changes the renderer to use the format provided instead of the default format. 352 353 <a name="WithTemplateFunc"></a> 354 ### func [WithTemplateFunc](<https://github.com/princjef/gomarkdoc/blob/master/renderer.go#L97>) 355 356 ```go 357 func WithTemplateFunc(name string, fn any) RendererOption 358 ``` 359 360 WithTemplateFunc adds the provided function with the given name to the list of functions that can be used by the rendering templates. 361 362 Any name collisions between built\-in functions and functions provided here are resolved in favor of the function provided here, so be careful about the naming of your functions to avoid overriding existing behavior unless desired. 363 364 <a name="WithTemplateOverride"></a> 365 ### func [WithTemplateOverride](<https://github.com/princjef/gomarkdoc/blob/master/renderer.go#L69>) 366 367 ```go 368 func WithTemplateOverride(name, tmpl string) RendererOption 369 ``` 370 371 WithTemplateOverride adds a template that overrides the template with the provided name using the value provided in the tmpl parameter. 372 373 Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)