Skip to content

Commit f47f46f

Browse files
authored
Render Markdown with Jsonnet natively (#26)
1 parent fc3f9bc commit f47f46f

5 files changed

Lines changed: 423 additions & 35 deletions

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: build test push push-image
1+
.PHONY: build test push push-image docs
22

33
IMAGE_NAME ?= docsonnet
44
IMAGE_PREFIX ?= jsonnetlibs
@@ -14,3 +14,7 @@ push: build test push-image
1414
push-image:
1515
docker push $(IMAGE_PREFIX)/$(IMAGE_NAME):$(IMAGE_TAG)
1616
docker push $(IMAGE_PREFIX)/$(IMAGE_NAME):latest
17+
18+
docs:
19+
jsonnet -J doc-util -S -c -m doc-util/ doc-util/docs.jsonnet
20+

doc-util/README.md

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
---
2-
permalink: /
3-
---
4-
51
# package d
62

73
```jsonnet
8-
local d = import "github.com/jsonnet-libs/docsonnet/doc-util"
4+
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
95
```
106

117
`doc-util` provides a Jsonnet interface for `docsonnet`,
128
a Jsonnet API doc generator that uses structured data instead of comments.
139

14-
1510
## Index
1611

1712
* [`fn arg(name, type, default)`](#fn-arg)
1813
* [`fn fn(help, args)`](#fn-fn)
1914
* [`fn obj(help, fields)`](#fn-obj)
2015
* [`fn pkg(name, url, help)`](#fn-pkg)
16+
* [`fn render(obj, filename)`](#fn-render)
2117
* [`fn val(type, help, default)`](#fn-val)
2218
* [`obj argument`](#obj-argument)
2319
* [`fn new(name, type, default)`](#fn-argumentnew)
@@ -28,10 +24,11 @@ local d = import "github.com/jsonnet-libs/docsonnet/doc-util"
2824
* [`obj object`](#obj-object)
2925
* [`fn new(help, fields)`](#fn-objectnew)
3026
* [`fn withFields(fields)`](#fn-objectwithfields)
31-
* [`obj package`](#obj-package)
32-
* [`fn new(name, url, help)`](#fn-packagenew)
3327
* [`obj value`](#obj-value)
3428
* [`fn new(type, help, default)`](#fn-valuenew)
29+
* [`obj T`](#obj-t)
30+
* [`obj package`](#obj-package)
31+
* [`fn new(name, url, help)`](#fn-packagenew)
3532

3633
## Fields
3734

@@ -67,6 +64,24 @@ pkg(name, url, help)
6764

6865
`new` is a shorthand for `package.new`
6966

67+
### fn render
68+
69+
```ts
70+
render(obj, filename)
71+
```
72+
73+
`render` converts the docstrings to human readable Markdown files.
74+
75+
Usage:
76+
77+
```jsonnet
78+
// docs.jsonnet
79+
d.render(import 'main.libsonnet', 'main.libsonnet')
80+
```
81+
82+
Call with: `jsonnet -S -c -m docs/ docs.jsonnet`
83+
84+
7085
### fn val
7186

7287
```ts
@@ -75,86 +90,97 @@ val(type, help, default)
7590

7691
`val` is a shorthand for `value.new`
7792

78-
## obj argument
93+
### obj argument
7994

8095
Utilities for creating function arguments
8196

82-
### fn argument.new
97+
#### fn argument.new
8398

8499
```ts
85100
new(name, type, default)
86101
```
87102

88103
new creates a new function argument, taking the name, the type and optionally a default value
89104

90-
## obj func
105+
### obj func
91106

92107
Utilities for documenting Jsonnet methods (functions of objects)
93108

94-
### fn func.new
109+
#### fn func.new
95110

96111
```ts
97112
new(help, args)
98113
```
99114

100115
new creates a new function, optionally with description and arguments
101116

102-
### fn func.withArgs
117+
#### fn func.withArgs
103118

104119
```ts
105120
withArgs(args)
106121
```
107122

108123
The `withArgs` modifier overrides the arguments of that function
109124

110-
### fn func.withHelp
125+
#### fn func.withHelp
111126

112127
```ts
113128
withHelp(help)
114129
```
115130

116131
The `withHelp` modifier overrides the help text of that function
117132

118-
## obj object
133+
### obj object
119134

120135
Utilities for documenting Jsonnet objects (`{ }`).
121136

122-
### fn object.new
137+
#### fn object.new
123138

124139
```ts
125140
new(help, fields)
126141
```
127142

128143
new creates a new object, optionally with description and fields
129144

130-
### fn object.withFields
145+
#### fn object.withFields
131146

132147
```ts
133148
withFields(fields)
134149
```
135150

136151
The `withFields` modifier overrides the fields property of an already created object
137152

138-
## obj package
139-
153+
### obj value
140154

155+
Utilities for documenting plain Jsonnet values (primitives)
141156

142-
### fn package.new
157+
#### fn value.new
143158

144159
```ts
145-
new(name, url, help)
160+
new(type, help, default)
146161
```
147162

148-
new creates a new package with given `name`, `import` URL and `help` text
163+
new creates a new object of given type, optionally with description and default value
149164

150-
## obj value
165+
### obj T
166+
167+
168+
* `T.any` (`string`): `"any"` - argument of type "any"
169+
* `T.array` (`string`): `"array"` - argument of type "array"
170+
* `T.boolean` (`string`): `"bool"` - argument of type "boolean"
171+
* `T.func` (`string`): `"function"` - argument of type "func"
172+
* `T.null` (`string`): `"null"` - argument of type "null"
173+
* `T.number` (`string`): `"number"` - argument of type "number"
174+
* `T.object` (`string`): `"object"` - argument of type "object"
175+
* `T.string` (`string`): `"string"` - argument of type "string"
176+
177+
### obj package
151178

152-
Utilities for documenting plain Jsonnet values (primitives)
153179

154-
### fn value.new
180+
#### fn package.new
155181

156182
```ts
157-
new(type, help, default)
183+
new(name, url, help)
158184
```
159185

160-
new creates a new object of given type, optionally with description and default value
186+
new creates a new package with given `name`, `import` URL and `help` text

doc-util/docs.jsonnet

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
local d = import './main.libsonnet';
2+
3+
d.render(import 'main.libsonnet', 'main.libsonnet')

doc-util/main.libsonnet

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,37 +73,68 @@
7373
'#arg': self.argument['#new'] + self.func.withHelp('`arg` is a shorthand for `argument.new`'),
7474
arg:: self.argument.new,
7575

76-
"#value": d.obj("Utilities for documenting plain Jsonnet values (primitives)"),
76+
'#value': d.obj('Utilities for documenting plain Jsonnet values (primitives)'),
7777
value:: {
78-
"#new": d.fn("new creates a new object of given type, optionally with description and default value", [d.arg("type", d.T.string), d.arg("help", d.T.string), d.arg("default", d.T.any)]),
79-
new(type, help='', default=null): { 'value': {
78+
'#new': d.fn('new creates a new object of given type, optionally with description and default value', [d.arg('type', d.T.string), d.arg('help', d.T.string), d.arg('default', d.T.any)]),
79+
new(type, help='', default=null): { value: {
8080
help: help,
8181
type: type,
8282
default: default,
83-
} }
83+
} },
8484
},
8585
'#val': self.value['#new'] + self.func.withHelp('`val` is a shorthand for `value.new`'),
86-
val: self.value.new,
86+
val:: self.value.new,
8787

8888
// T contains constants for the Jsonnet types
8989
T:: {
90+
'#string': d.val(d.T.string, 'argument of type "string"'),
9091
string: 'string',
9192

93+
'#number': d.val(d.T.string, 'argument of type "number"'),
9294
number: 'number',
9395
int: self.number,
9496
integer: self.number,
9597

98+
'#boolean': d.val(d.T.string, 'argument of type "boolean"'),
9699
boolean: 'bool',
97100
bool: self.boolean,
98101

102+
'#object': d.val(d.T.string, 'argument of type "object"'),
99103
object: 'object',
104+
105+
'#array': d.val(d.T.string, 'argument of type "array"'),
100106
array: 'array',
107+
108+
'#any': d.val(d.T.string, 'argument of type "any"'),
101109
any: 'any',
102110

103-
'null': "null",
104-
nil: self["null"],
111+
'#null': d.val(d.T.string, 'argument of type "null"'),
112+
'null': 'null',
113+
nil: self['null'],
105114

115+
'#func': d.val(d.T.string, 'argument of type "func"'),
106116
func: 'function',
107117
'function': self.func,
108118
},
119+
120+
'#render': d.fn(
121+
|||
122+
`render` converts the docstrings to human readable Markdown files.
123+
124+
Usage:
125+
126+
```jsonnet
127+
// docs.jsonnet
128+
d.render(import 'main.libsonnet', 'main.libsonnet')
129+
```
130+
131+
Call with: `jsonnet -S -c -m docs/ docs.jsonnet`
132+
|||,
133+
args=[
134+
d.arg('obj', d.T.object),
135+
d.arg('filename', d.T.string),
136+
]
137+
),
138+
render:: (import './render.libsonnet').render,
139+
109140
}

0 commit comments

Comments
 (0)