@@ -223,15 +223,16 @@ SnakeYAML implementation (that clj-yaml uses for low-level encoding and decoding
223223==== Dumper Options [[dumper-options]]
224224Different flow styles (`:auto`, `:block`, `:flow`) allow customization of how YAML is rendered.
225225
226- To demonstrate let's setup `some-data` to play with.
226+ To demonstrate, let's setup `some-data` to play with.
227227
228228[source,clojure]
229229----
230230(def some-yaml "
231231todo:
232- - name: Fix issue
233- responsible:
234- name: Rita
232+ issues:
233+ - name: Fix all the things
234+ responsible:
235+ name: Rita
235236")
236237
237238(def some-data (yaml/parse-string some-yaml))
@@ -247,45 +248,120 @@ results in a string of YAML, that when printed:
247248[source,yaml]
248249----
249250todo:
250- - name: Fix issue
251- responsible:
252- name: Rita
251+ issues:
252+ - name: Fix all the things
253+ responsible:
254+ name: Rita
253255----
254256
255257The same but with the `:flow` style results in:
256258[source,yaml]
257259----
258- {todo: [{name: Fix issue , responsible: {name: Rita}}]}
260+ {todo: {issues: [{name: Fix all the things , responsible: {name: Rita}}]} }
259261----
260262
261263And finally the `:auto` style (the default) renders:
262264[source,yaml]
263265----
264266todo:
265- - name: Fix issue
266- responsible: {name: Rita}
267+ issues:
268+ - name: Fix all the things
269+ responsible: {name: Rita}
267270----
268271
269- Use the `:indent` and `:indicator-indent` options to adjust indentation:
272+ ==== Controlling Indentation
273+
274+ Use `:indent` to control block indentation, to override the default block indent of `2` with `4`:
270275
271276[source,clojure]
272277----
273- (yaml/generate-string some-data :dumper-options {:indent 6
274- :indicator-indent 3
278+ (yaml/generate-string some-data :dumper-options {:indent 4
275279 :flow-style :block})
276280----
277281
278282results in:
279283[source,yaml]
280284----
281285todo:
282- - name: Fix issue
283- responsible:
286+ issues:
287+ - name: Fix all the things
288+ responsible:
284289 name: Rita
285290----
291+ Notice that each block is now indented by `4`.
286292
287- `:indent` must always be larger than `:indicator-indent`.
288- If it is only 1 higher, the indicator will be on a separate line:
293+ Use `:indicator-indent` to change the indentation of the `-` indicator; by default, it is `0`; let's bump it up to `2`:
294+
295+ [source,clojure]
296+ ----
297+ (yaml/generate-string some-data :dumper-options {:indent 4
298+ :indicator-indent 2
299+ :flow-style :block})
300+ ----
301+
302+ results in:
303+ [source,yaml]
304+ ----
305+ todo:
306+ issues:
307+ - name: Fix all the things
308+ responsible:
309+ name: Rita
310+ ----
311+ Notice that the blocks are still indented by 4, but the `-` indicator is now indented by `2`.
312+
313+ Indenting the `-` indicator within the block `:indent` can be limiting.
314+ Sometimes, you'll want to indent `-` blocks more than other blocks.
315+ Specifying `:indent-with-indicator true` makes block indentation for `-` indicators additive; the indicator is still indented by `:indicator-indent`, but its block is indented by `:indent` + `:indicator-indent`.
316+
317+ [source,clojure]
318+ ----
319+ (yaml/generate-string some-data :dumper-options {:indent 4
320+ :indicator-indent 2
321+ :indent-with-indicator true
322+ :flow-style :block})
323+ ----
324+
325+ results in:
326+ [source,yaml]
327+ ----
328+ todo:
329+ issues:
330+ - name: Fix all the things
331+ responsible:
332+ name: Rita
333+ ----
334+ You'll notice that the `-` indicator is indented by `2`, but its block is now indented by `6` (`4` + `2`).
335+
336+ A common usage of `indent-with-indicator true` is to indent arrays like so:
337+
338+ [source,clojure]
339+ ----
340+ (yaml/generate-string some-data :dumper-options {:indent 2
341+ :indicator-indent 2
342+ :indent-with-indicator true
343+ :flow-style :block})
344+ ----
345+ results in:
346+
347+ [source,yaml]
348+ ----
349+ todo:
350+ issues:
351+ - name: Fix all the things
352+ responsible:
353+ name: Rita
354+ ----
355+ We now have:
356+
357+ * a block indentation of `2` by default
358+ * an `-` indicator indentation of `2`
359+ * a block indentation of `4` for `-` indicator content
360+
361+ [TIP]
362+ ====
363+ Unless you are using `:indent-with-indicator`, `:indicator-indent` must always be less than `:indent`.
364+ If `:ident-with-indicator` is 1 less than `:indent`, the `-` indicator will be on a separate line:
289365
290366[source,clojure]
291367----
@@ -297,11 +373,13 @@ results in:
297373[source,yaml]
298374----
299375todo:
300- -
301- name: Fix issue
302- responsible:
303- name: Rita
376+ issues:
377+ -
378+ name: Fix all the things
379+ responsible:
380+ name: Rita
304381----
382+ ====
305383
306384[[keyword-args]]
307385=== Function Options as Keyword Args
0 commit comments