Skip to content

Commit 8b292be

Browse files
Update docs with enhanced usage info (#38)
* Update docs with enhanced usage info Signed-off-by: Yoriyasu Yano <430092+yorinasub17@users.noreply.github.com> * Fix incorrect “it’s” with “its” * Make sure to bind /docs in docker docs so that output can be synced to host Signed-off-by: Yoriyasu Yano <430092+yorinasub17@users.noreply.github.com> * docs: use -J vendor --------- Signed-off-by: Yoriyasu Yano <430092+yorinasub17@users.noreply.github.com> Co-authored-by: Jeroen Op 't Eynde <jeroen@simplistic.be>
1 parent cc9df63 commit 8b292be

1 file changed

Lines changed: 62 additions & 9 deletions

File tree

README.md

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Jsonnet itself does not know traditional packages, classes or similar.
3939

4040
For documentation and distribution purposes however, it seems reasonable to introduce a concept of **loose packages**, defined as a single importable file, holding all of your **public API**.
4141

42-
As an example, a hypothetical `url` library could define it's package like above example does.
42+
As an example, a hypothetical `url` library could define its package like above example does.
4343

4444
Packages are defined by including assigning a `d.pkg` call to a key literally named `#` (hash). All fields, including nested packages, of the same object having the `#` key belong to that package.
4545

@@ -54,7 +54,7 @@ Most common part of an API will be functions. These are annotated in a similar f
5454
}
5555
```
5656

57-
Along the actual function definition, a _docsonnet_ key is added, with the functions name prefixed by the familiar `#` as it's name.
57+
Along the actual function definition, a _docsonnet_ key is added, with the functions name prefixed by the familiar `#` as its name.
5858
Above example defines `myFunc` as a function, that greets the user and takes a single argument of type `string`.
5959

6060
### Objects
@@ -73,18 +73,71 @@ Such an object might need a description as well, so you can also annotate it:
7373
}
7474
```
7575

76-
Again, the naming rule `#` joined with the fields name must be followed, so the `docsonnet` utility can automatically join together the contents of your object with it's annotated description.
76+
Again, the naming rule `#` joined with the fields name must be followed, so the `docsonnet` utility can automatically join together the contents of your object with its annotated description.
7777

78-
## FAQ
7978

80-
#### Do my projects need to have `doc-util` installed to vendor/?
79+
## Usage
80+
81+
Once you have a Jsonnet library annotated with `doc-util`, you can generate the docs using one of three ways:
82+
83+
- [Jsonnet renderer](#jsonnet-renderer)
84+
- [docsonnet binary](#docsonnet-binary)
85+
- [docsonnet docker image](#docsonnet-docker-image)
86+
87+
### Jsonnet renderer
88+
89+
The docs can be rendered using Jsonnet with the
90+
[render](https://github.com/jsonnet-libs/docsonnet/tree/master/doc-util#fn-render) function.
91+
92+
In your library source, add a file `docs.jsonnet` (assuming your library entrypoint is `main.libsonnet`) with the
93+
following contents:
94+
95+
```jsonnet
96+
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
97+
d.render(import 'main.libsonnet')
98+
```
99+
100+
Then, you can render the markdown to the `docs/` folder using the following command:
101+
102+
```
103+
jsonnet -J vendor -S -c -m docs/ docs.jsonnet
104+
```
105+
106+
Note that this requires `doc-util` to be installed to `vendor/` to work properly.
107+
108+
### docsonnet binary
109+
110+
Alternatively, the docs can be rendered using the `docsonnet` go binary. The `docsonnet` binary embeds the `doc-util`
111+
library, avoiding the need to have `doc-util` installed.
81112

82-
No! The `docsonnet` binary comes included with it, and during normal Jsonnet use the docsonnet keys will never be accessed, so your Jsonnet runs just fine without.
113+
You can install the `docsonnet` binary using `go install`:
83114

84-
> **Note**
85-
>
115+
```
116+
go install github.com/jsonnet-libs/docsonnet@master
117+
```
118+
119+
Once the binary is installed, you can generate the docs by passing it the main entrypoint to your Jsonnet library:
120+
121+
```
122+
docsonnet main.libsonnet
123+
```
124+
125+
> **Note**
126+
>
86127
> Linters like [jsonnet-lint](https://pkg.go.dev/github.com/google/go-jsonnet/linter) or `tk lint` require the imports to be resolvable, so you should add `doc-util` to `vendor/` when using these linters.
87128
129+
### docsonnet docker image
130+
131+
You can also use the [docker image](https://hub.docker.com/r/jsonnetlibs/docsonnet) which contains the `docsonnet`
132+
binary if you do not wish to set up go or install the binary locally:
133+
134+
```
135+
docker run --rm -v "$(pwd):/src" -v "$(pwd)/docs:/docs" jsonnetlibs/docsonnet /src/main.libsonnet
136+
```
137+
138+
139+
## FAQ
140+
88141
#### What's wrong with comments? Why not parse regular comments?
89142

90143
I had some attempts on this, especially because it feels more natural. However, the language properties of Jsonnet make this quite challenging:
@@ -132,4 +185,4 @@ This will all happen transparently, without any user interaction
132185

133186
Because the Docsonnet gives you the missing logical representation of your Jsonnet library, it enables straight forward implementation of other language tooling, such as **code-completion**.
134187

135-
Instead of inferring what fields are available for a library, we can _just_ look at it's docsonnet and provide the fields specified there, along with nice descriptions and argument types.
188+
Instead of inferring what fields are available for a library, we can _just_ look at its docsonnet and provide the fields specified there, along with nice descriptions and argument types.

0 commit comments

Comments
 (0)