Skip to content

Commit 80124ce

Browse files
committed
feat: make it easier to customize package imports
1 parent 5c06b2c commit 80124ce

File tree

8 files changed

+39
-9
lines changed

8 files changed

+39
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ The documentation information that is output is formatted using a series of text
118118
119119
- doc: generates the freeform documentation block for any of the above
120120
structures that can contain a documentation section.
121+
122+
- import: generates the import code used to pull in a package.
121123
```
122124

123125
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:

doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@
125125
// - doc: generates the freeform documentation block for any of the above
126126
// structures that can contain a documentation section.
127127
//
128+
// - import: generates the import code used to pull in a package.
129+
//
128130
// Overriding with the -t option uses a key-vaule pair mapping a template name
129131
// to the file containing the contents of the override template to use.
130132
// Specified template files must exist:

lang/README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Package lang provides constructs for defining golang language constructs and ext
6161
- [func (pkg *Package) Examples() (examples []*Example)](<#func-package-examples>)
6262
- [func (pkg *Package) Funcs() (funcs []*Func)](<#func-package-funcs>)
6363
- [func (pkg *Package) Import() string](<#func-package-import>)
64+
- [func (pkg *Package) ImportPath() string](<#func-package-importpath>)
6465
- [func (pkg *Package) Level() int](<#func-package-level>)
6566
- [func (pkg *Package) Name() string](<#func-package-name>)
6667
- [func (pkg *Package) Summary() string](<#func-package-summary>)
@@ -487,7 +488,7 @@ func NewPackageFromBuild(log logger.Logger, pkg *build.Package, opts ...PackageO
487488

488489
NewPackageFromBuild creates a representation of a package's documentation from the build metadata for that package\. It can be configured using the provided options\.
489490

490-
### func \(\*Package\) [Consts](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L149>)
491+
### func \(\*Package\) [Consts](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L156>)
491492

492493
```go
493494
func (pkg *Package) Consts() (consts []*Value)
@@ -511,23 +512,23 @@ func (pkg *Package) Dirname() string
511512

512513
Dirname provides the name of the leaf directory in which the package is located\.
513514

514-
### func \(\*Package\) [Doc](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L143>)
515+
### func \(\*Package\) [Doc](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L150>)
515516

516517
```go
517518
func (pkg *Package) Doc() *Doc
518519
```
519520

520521
Doc provides the structured contents of the documentation comment for the package\.
521522

522-
### func \(\*Package\) [Examples](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L187>)
523+
### func \(\*Package\) [Examples](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L194>)
523524

524525
```go
525526
func (pkg *Package) Examples() (examples []*Example)
526527
```
527528

528529
Examples provides the package\-level examples that have been defined\. This does not include examples that are associated with symbols contained within the package\.
529530

530-
### func \(\*Package\) [Funcs](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L167>)
531+
### func \(\*Package\) [Funcs](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L174>)
531532

532533
```go
533534
func (pkg *Package) Funcs() (funcs []*Func)
@@ -543,6 +544,14 @@ func (pkg *Package) Import() string
543544

544545
Import provides the raw text for the import declaration that is used to import code from the package\. If your package's documentation is generated from a local path and does not use Go Modules\, this will typically print \`import "\."\`\.
545546

547+
### func \(\*Package\) [ImportPath](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L138>)
548+
549+
```go
550+
func (pkg *Package) ImportPath() string
551+
```
552+
553+
ImportPath provides the identifier used for the package when installing or importing the package\. If your package's documentation is generated from a local path and does not use Go Modules\, this will typically print \`\.\`\.
554+
546555
### func \(\*Package\) [Level](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L106>)
547556

548557
```go
@@ -559,23 +568,23 @@ func (pkg *Package) Name() string
559568

560569
Name provides the name of the package as it would be seen from another package importing it\.
561570

562-
### func \(\*Package\) [Summary](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L137>)
571+
### func \(\*Package\) [Summary](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L144>)
563572

564573
```go
565574
func (pkg *Package) Summary() string
566575
```
567576

568577
Summary provides the one\-sentence summary of the package's documentation comment\.
569578

570-
### func \(\*Package\) [Types](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L176>)
579+
### func \(\*Package\) [Types](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L183>)
571580

572581
```go
573582
func (pkg *Package) Types() (types []*Type)
574583
```
575584

576585
Types lists the top\-level types provided by the package\.
577586

578-
### func \(\*Package\) [Vars](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L158>)
587+
### func \(\*Package\) [Vars](<https://github.com/princjef/gomarkdoc/blob/master/lang/package.go#L165>)
579588

580589
```go
581590
func (pkg *Package) Vars() (vars []*Value)

lang/package.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ func (pkg *Package) Import() string {
132132
return fmt.Sprintf(`import "%s"`, pkg.doc.ImportPath)
133133
}
134134

135+
// ImportPath provides the identifier used for the package when installing or
136+
// importing the package. If your package's documentation is generated from a
137+
// local path and does not use Go Modules, this will typically print `.`.
138+
func (pkg *Package) ImportPath() string {
139+
return pkg.doc.ImportPath
140+
}
141+
135142
// Summary provides the one-sentence summary of the package's documentation
136143
// comment.
137144
func (pkg *Package) Summary() string {

lang/package_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func TestPackage_dotImport(t *testing.T) {
5757
is.NoErr(err)
5858

5959
is.Equal(pkg.Import(), `import "github.com/princjef/gomarkdoc/testData/lang/function"`)
60+
is.Equal(pkg.ImportPath(), `github.com/princjef/gomarkdoc/testData/lang/function`)
6061
}
6162

6263
func TestPackage_strings(t *testing.T) {
@@ -74,6 +75,7 @@ func TestPackage_strings(t *testing.T) {
7475
is.Equal(pkg.Dirname(), "strings")
7576
is.Equal(pkg.Name(), "strings")
7677
is.Equal(pkg.Import(), `import "strings"`)
78+
is.Equal(pkg.ImportPath(), `strings`)
7779
is.Equal(pkg.Summary(), "Package strings implements simple functions to manipulate UTF-8 encoded strings.")
7880
is.Equal(len(pkg.Consts()), 0) // strings should have no constants
7981
is.Equal(len(pkg.Vars()), 0) // strings should have no vars
@@ -97,6 +99,7 @@ func TestPackage_textScanner(t *testing.T) {
9799
is.Equal(pkg.Dirname(), "scanner")
98100
is.Equal(pkg.Name(), "scanner")
99101
is.Equal(pkg.Import(), `import "text/scanner"`)
102+
is.Equal(pkg.ImportPath(), `text/scanner`)
100103
is.Equal(pkg.Summary(), "Package scanner provides a scanner and tokenizer for UTF-8-encoded text.")
101104
is.True(len(pkg.Consts()) > 0) // text/scanner should have constants
102105
is.Equal(len(pkg.Vars()), 0) // text/scanner should have no vars
@@ -120,6 +123,7 @@ func TestPackage_ioIoutil(t *testing.T) {
120123
is.Equal(pkg.Dirname(), "ioutil")
121124
is.Equal(pkg.Name(), "ioutil")
122125
is.Equal(pkg.Import(), `import "io/ioutil"`)
126+
is.Equal(pkg.ImportPath(), `io/ioutil`)
123127
is.Equal(pkg.Summary(), "Package ioutil implements some I/O utility functions.")
124128
is.Equal(len(pkg.Consts()), 0) // io/ioutil should have no constants
125129
is.True(len(pkg.Vars()) > 0) // io/ioutil should have vars
@@ -143,6 +147,7 @@ func TestPackage_encoding(t *testing.T) {
143147
is.Equal(pkg.Dirname(), "encoding")
144148
is.Equal(pkg.Name(), "encoding")
145149
is.Equal(pkg.Import(), `import "encoding"`)
150+
is.Equal(pkg.ImportPath(), `encoding`)
146151
is.Equal(pkg.Summary(), "Package encoding defines interfaces shared by other packages that convert data to and from byte-level and textual representations.")
147152
is.Equal(len(pkg.Consts()), 0) // encoding should have no constants
148153
is.Equal(len(pkg.Vars()), 0) // encoding should have no vars

templates.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/import.gotxt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{- codeBlock "go" .Import -}}
2+

templates/package.gotxt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{- header .Level .Name -}}
55
{{- end -}}
66

7-
{{- codeBlock "go" .Import -}}
7+
{{- template "import" . -}}
88

99
{{- template "doc" .Doc -}}
1010

0 commit comments

Comments
 (0)