Skip to content

Commit fedfb49

Browse files
authored
Extend render template with common usage & install instructions (#27)
* Extend render template with jsonnet-bundler instructions * push usage/install templates up the stack * ensure newline between docs and header * correctly quote default args
1 parent f47f46f commit fedfb49

5 files changed

Lines changed: 119 additions & 34 deletions

File tree

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ push-image:
1616
docker push $(IMAGE_PREFIX)/$(IMAGE_NAME):latest
1717

1818
docs:
19-
jsonnet -J doc-util -S -c -m doc-util/ doc-util/docs.jsonnet
19+
jsonnet -S -c -m doc-util/ \
20+
-e "(import 'doc-util/main.libsonnet').render(import 'doc-util/main.libsonnet')"
2021

doc-util/README.md

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
# package d
22

3-
```jsonnet
4-
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
5-
```
6-
73
`doc-util` provides a Jsonnet interface for `docsonnet`,
84
a Jsonnet API doc generator that uses structured data instead of comments.
95

6+
7+
## Install
8+
9+
```
10+
jb install github.com/jsonnet-libs/docsonnet/doc-util@master
11+
```
12+
13+
## Usage
14+
15+
```jsonnet
16+
local d = import "github.com/jsonnet-libs/docsonnet/doc-util/doc-util/main.libsonnet"
17+
```
18+
1019
## Index
1120

1221
* [`fn arg(name, type, default)`](#fn-arg)
1322
* [`fn fn(help, args)`](#fn-fn)
1423
* [`fn obj(help, fields)`](#fn-obj)
15-
* [`fn pkg(name, url, help)`](#fn-pkg)
16-
* [`fn render(obj, filename)`](#fn-render)
24+
* [`fn pkg(name, url, help, filename='', version='master')`](#fn-pkg)
25+
* [`fn render(obj)`](#fn-render)
1726
* [`fn val(type, help, default)`](#fn-val)
1827
* [`obj argument`](#obj-argument)
1928
* [`fn new(name, type, default)`](#fn-argumentnew)
@@ -28,7 +37,7 @@ local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
2837
* [`fn new(type, help, default)`](#fn-valuenew)
2938
* [`obj T`](#obj-t)
3039
* [`obj package`](#obj-package)
31-
* [`fn new(name, url, help)`](#fn-packagenew)
40+
* [`fn new(name, url, help, filename='', version='master')`](#fn-packagenew)
3241

3342
## Fields
3443

@@ -59,15 +68,15 @@ obj(help, fields)
5968
### fn pkg
6069

6170
```ts
62-
pkg(name, url, help)
71+
pkg(name, url, help, filename='', version='master')
6372
```
6473

6574
`new` is a shorthand for `package.new`
6675

6776
### fn render
6877

6978
```ts
70-
render(obj, filename)
79+
render(obj)
7180
```
7281

7382
`render` converts the docstrings to human readable Markdown files.
@@ -76,7 +85,7 @@ Usage:
7685

7786
```jsonnet
7887
// docs.jsonnet
79-
d.render(import 'main.libsonnet', 'main.libsonnet')
88+
d.render(import 'main.libsonnet')
8089
```
8190

8291
Call with: `jsonnet -S -c -m docs/ docs.jsonnet`
@@ -180,7 +189,16 @@ new creates a new object of given type, optionally with description and default
180189
#### fn package.new
181190

182191
```ts
183-
new(name, url, help)
192+
new(name, url, help, filename='', version='master')
184193
```
185194

186-
new creates a new package with given `name`, `import` URL and `help` text
195+
`new` creates a new package
196+
197+
Arguments:
198+
199+
* given `name`
200+
* source `url` for jsonnet-bundler and the import
201+
* `help` text
202+
* `filename` for the import, defaults to blank for backward compatibility
203+
* `version` for jsonnet-bundler install, defaults to `master` just like jsonnet-bundler
204+

doc-util/docs.jsonnet

Lines changed: 0 additions & 3 deletions
This file was deleted.

doc-util/main.libsonnet

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,54 @@
77
help=|||
88
`doc-util` provides a Jsonnet interface for `docsonnet`,
99
a Jsonnet API doc generator that uses structured data instead of comments.
10-
|||
10+
|||,
11+
filename=std.thisFile,
1112
),
1213

1314
package:: {
14-
'#new':: d.fn('new creates a new package with given `name`, `import` URL and `help` text', [d.arg('name', d.T.string), d.arg('url', d.T.string), d.arg('help', d.T.string)]),
15-
new(name, url, help):: {
16-
name: name,
17-
'import': url,
18-
help: help,
15+
'#new':: d.fn(|||
16+
`new` creates a new package
17+
18+
Arguments:
19+
20+
* given `name`
21+
* source `url` for jsonnet-bundler and the import
22+
* `help` text
23+
* `filename` for the import, defaults to blank for backward compatibility
24+
* `version` for jsonnet-bundler install, defaults to `master` just like jsonnet-bundler
25+
|||, [
26+
d.arg('name', d.T.string),
27+
d.arg('url', d.T.string),
28+
d.arg('help', d.T.string),
29+
d.arg('filename', d.T.string, ''),
30+
d.arg('version', d.T.string, 'master'),
31+
]),
32+
new(name, url, help, filename='', version='master')::
33+
{
34+
name: name,
35+
help: help,
36+
'import':
37+
if filename != ''
38+
then url + '/' + filename
39+
else url,
40+
url: url,
41+
filename: filename,
42+
version: version,
43+
44+
}
45+
+ self.withInstallTemplate(
46+
'jb install %(url)s@%(version)s'
47+
)
48+
+ self.withUsageTemplate(
49+
'local %(name)s = import "%(import)s"'
50+
),
51+
52+
withUsageTemplate(template):: {
53+
usageTemplate: template,
54+
},
55+
56+
withInstallTemplate(template):: {
57+
installTemplate: template,
1958
},
2059
},
2160

@@ -125,14 +164,13 @@
125164
126165
```jsonnet
127166
// docs.jsonnet
128-
d.render(import 'main.libsonnet', 'main.libsonnet')
167+
d.render(import 'main.libsonnet')
129168
```
130169
131170
Call with: `jsonnet -S -c -m docs/ docs.jsonnet`
132171
|||,
133172
args=[
134173
d.arg('obj', d.T.object),
135-
d.arg('filename', d.T.string),
136174
]
137175
),
138176
render:: (import './render.libsonnet').render,

doc-util/render.libsonnet

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
package: |||
66
# package %(name)s
77
8-
```jsonnet
9-
local %(name)s = import '%(import)s/%(filename)s';
10-
```
11-
12-
%(help)s
8+
%(content)s
139
|||,
1410

1511
indexPage: |||
@@ -186,7 +182,11 @@
186182

187183
args: std.join(', ', [
188184
if arg.default != null
189-
then arg.name + '=' + arg.default
185+
then arg.name + '=' + (
186+
if arg.type == 'string'
187+
then "'%s'" % arg.default
188+
else std.toString(arg.default)
189+
)
190190
else arg.name
191191
for arg in self.doc.args
192192
]),
@@ -203,15 +203,46 @@
203203
help: doc.value.help,
204204
value: obj,
205205
},
206+
207+
package(doc, root):: {
208+
name: doc.name,
209+
content:
210+
|||
211+
%(help)s
212+
||| % doc
213+
+ (if 'installTemplate' in doc
214+
then |||
215+
216+
## Install
217+
218+
```
219+
%(install)s
220+
```
221+
||| % doc.installTemplate % doc
222+
else '')
223+
+ (if 'usageTemplate' in doc
224+
then |||
225+
226+
## Usage
227+
228+
```jsonnet
229+
%(usage)s
230+
```
231+
||| % doc.usageTemplate % doc
232+
else ''),
233+
},
206234
},
207235

208-
prepare(obj, filename='', depth=0)::
236+
prepare(obj, depth=0)::
209237
std.foldl(
210238
function(acc, key)
211239
acc +
212240
// Package definition
213241
if key == '#'
214-
then obj[key] { filename: filename }
242+
then root.sections.package(
243+
obj[key],
244+
(depth == 0)
245+
)
215246

216247
// Field definition
217248
else if std.startsWith(key, '#')
@@ -319,6 +350,6 @@
319350
{}
320351
),
321352

322-
render(obj, filename):
323-
self.renderFiles(self.prepare(obj, filename)),
353+
render(obj):
354+
self.renderFiles(self.prepare(obj)),
324355
}

0 commit comments

Comments
 (0)