forked from mongodb/docs-meta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-blocks.txt
More file actions
470 lines (326 loc) · 12.1 KB
/
code-blocks.txt
File metadata and controls
470 lines (326 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
.. meta::
:robots: noindex, nosnippet
=============
Code Examples
=============
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Snooty supports the following directives for code samples:
- ``code-block``
- ``io-code-block``
- ``literalinclude``
These directives feature a monospace font and syntax highlighting that makes
source code easier to interpret.
Code Blocks
-----------
The ``code-block`` directive formats a code sample with the following options.
Syntax
~~~~~~
.. code-block::
.. code-block:: <language>
:copyable: <boolean>
:emphasize-lines: <line numbers>
:linenos:
:caption: <caption text>
:source: <url>
<code sample>
Options
~~~~~~~
All of the following options are optional.
.. list-table::
* - ``<language>``
- Language to use for syntax highlighting.
For a complete list of supported languages, see the
`leafygreen-ui GitHub repository. <https://github.com/mongodb/leafygreen-ui/blob/main/packages/code/src/languages.ts>`__
**Default:** None
* - ``:copyable: <boolean>``
- Option that indicates whether to show the :guilabel:`copy icon`.
**Default:** ``true``
* - ``:emphasize-lines: <line number>``
- Lines to highlight. You can specify a range
of line numbers, individual line numbers, or both.
**Default:** None
* - ``:linenos:``
- Option that indicates whether to show line numbers.
**Default:** None
* - ``:caption: <caption text>``
- Brief description.
**Default:** None
* - ``:source: <url>``
- Option that indicates whether to show the :guilabel:`source icon` and
:guilabel:`View full source` tooltip.
This icon is hidden if the ``copyable`` option is set to ``false``.
**Default:** None
Example 1
~~~~~~~~~
.. code-block::
.. code-block:: python
:linenos:
:copyable: true
:emphasize-lines: 1, 4-5
:caption: An example Python code block
print("Hello Docs team!")
print("This is Python code...")
print("...inside a code-block.")
print("The line numbers are over here <-")
print("And the copy icon is over there ->")
The previous ``code-block`` directive creates the following output:
.. code-block:: python
:linenos:
:copyable: true
:emphasize-lines: 1, 4-5
:caption: An example Python code block
print("Hello Docs team!")
print("This is Python code...")
print("...inside a code-block.")
print("The line numbers are over here <-")
print("And the copy icon is over there ->")
Example 2
~~~~~~~~~
.. code-block::
.. code-block:: python
:source: https://github.com/mongodb/docs-ecosystem/blob/36f9cc7260a246d47cb05ac35276a2c92734b028/conf.py#L120
html_theme = sconf.theme.name
html_theme_path = [os.path.join(conf.paths.buildsystem, 'themes')]
html_title = conf.project.title
htmlhelp_basename = 'MongoDBdoc'
The previous ``code-block`` directive creates the following output:
.. code-block:: python
:source: https://github.com/mongodb/docs-ecosystem/blob/36f9cc7260a246d47cb05ac35276a2c92734b028/conf.py#L120
html_theme = sconf.theme.name
html_theme_path = [os.path.join(conf.paths.buildsystem, 'themes')]
html_title = conf.project.title
htmlhelp_basename = 'MongoDBdoc'
I/O Code Blocks
---------------
The ``io-code-block`` directive formats a code sample and
includes an expandable code block for the output and a button to
toggle its visibility.
Syntax
~~~~~~
.. code-block::
.. io-code-block::
:copyable: <boolean>
:caption: <caption text>
:source: <url>
.. input::
:language: <input language>
:emphasize-lines: <line numbers>
:linenos:
<input code>
.. output::
:language: <output language>
:emphasize-lines: <line numbers>
:linenos:
:visible: <boolean>
<output code>
Options
~~~~~~~
All of the following options are optional.
.. list-table::
* - ``:copyable: <boolean>``
- Option that indicates whether to show the :guilabel:`copy icon` for the input
code.
**Default:** ``false``
* - ``:language:``
- Language to use for syntax highlighting. You can specify
different languages for the input and output code.
For a complete list of supported languages, see the
`leafygreen-ui GitHub repository. <https://github.com/mongodb/leafygreen-ui/blob/main/packages/code/src/languages.ts>`__
**Default:** None
* - ``:linenos:``
- Option that indicates whether to show line numbers.
**Default:** None
* - ``:emphasize-lines: <line number>``
- Lines to highlight. You can specify a range
of line numbers, individual line numbers, or both.
**Default:** None
* - ``:visible: <boolean>``
- Option that indicates whether the output code is initially visible. If ``false``,
the user must click a button to see the output.
**Default:** ``true``
* - ``:caption: <caption text>``
- Brief description.
**Default:** None
* - ``:source: <url>``
- Option that indicates whether to show the :guilabel:`source icon` and
:guilabel:`View full source` tooltip.
This icon is hidden if the ``copyable`` option is set to ``false``.
**Default:** None
Example 1
~~~~~~~~~
.. code-block::
.. io-code-block::
:copyable: true
:caption: An example Python code block with output
.. input::
:language: python
:emphasize-lines: 1
:linenos:
print("Hello Docs team!")
print("This is Python code...")
print("...inside an io-code-block.")
.. output::
:language: shell
:emphasize-lines: 1
:linenos:
:visible: false
"Hello Docs team!"
"This is Python code..."
"...inside an io-code-block."
.. io-code-block::
:copyable: true
:caption: An example Python code block with output
.. input::
:language: python
:emphasize-lines: 1
:linenos:
print("Hello Docs team!")
print("This is Python code...")
print("...inside an io-code-block.")
.. output::
:language: shell
:emphasize-lines: 1
:linenos:
"Hello Docs team!"
"This is Python code..."
"...inside an io-code-block."
Example 2
~~~~~~~~~
.. code-block::
.. io-code-block::
:source: https://github.com/mongodb/docs-node/blob/0cf95d4eac536ed0ae1a2085948dd98f24ac48af/source/quick-reference.txt#L473
:copyable: true
.. input::
:language: javascript
coll.find({ $text: { $search: 'zissou' } });
.. output::
:language: javascript
[
{ title: 'The Life Aquatic with Steve Zissou', ... }
]
The previous ``io-code-block`` directive creates the following output:
.. io-code-block::
:source: https://github.com/mongodb/docs-node/blob/0cf95d4eac536ed0ae1a2085948dd98f24ac48af/source/quick-reference.txt#L473
:copyable: true
.. input::
:language: javascript
coll.find({ $text: { $search: 'zissou' } });
.. output::
:language: javascript
[
{ title: 'The Life Aquatic with Steve Zissou', ... }
]
Literal Includes
----------------
The ``literalinclude`` directive includes and formats a code example from another file.
You can specify the comments or other lines of code on which to start and end the example.
This directive allows you to maintain the code in a separate single file and display the
same code example in multiple places.
.. note:: include vs. literalinclude
Use the ``literalinclude`` directive to include a code file in the docs.
Use the ``include`` directive to include an ``rst`` file in the docs.
Syntax
~~~~~~
.. code-block::
.. literalinclude:: <file path>
:language: <language>
:copyable: <boolean>
:start-after: <line of code>
:end-before: <line of code>
:linenos:
:lineno-start: <line number>
:emphasize-lines: <line numbers>
:caption: <caption>
:dedent: <number>
Options
~~~~~~~
.. list-table::
* - ``<file path>``
- **Required.** Path to the file in the repository that contains the code to show.
**Default:** None
* - ``:language: <language>``
- Language to use for syntax highlighting.
For a complete list of supported languages, see the
`leafygreen-ui GitHub repository. <https://github.com/mongodb/leafygreen-ui/blob/main/packages/code/src/languages.ts>`__
**Default:** None
* - ``:copyable: <boolean>``
- Option that indicates whether to show the :guilabel:`copy icon`.
**Default:** ``true``
* - ``:start-after: <line of code>``
- Line of code before the first line to include in the sample.
You can use a comment to mark the point where the ``literalinclude`` directive
starts including code. If you do, omit the comment marker (``#`` or ``//``) from
the attribute value.
By using comments, you can unambiguously
specify the start of the code example, regardless of how many examples you're
including from the file. You also won't need to update the directive if the
extracted code changes.
**Default:** Beginning of file
* - ``:end-before: <line of code>``
- Line of code after the last line to include in the sample.
You can use a comment to mark the point where the ``literalinclude`` directive
stops including code. If you do, you can omit the comment marker (``#`` or ``//``)
from the attribute value.
By using comments, you can unambiguously
specify the end of the code sample, regardless of how many examples you're
including from the file. You also won't need to update the directive if the
extracted code changes.
**Default:** End of file
* - ``:linenos:``
- Option that indicates whether to show line numbers.
**Default:** None
* - ``:lineno-start:``
- Number of the line at which to start showing line numbers. This option requires the
``:linenos:`` option.
**Default:** 1
* - ``:emphasize-lines: <line number>``
- Lines to highlight. You can specify a range
of line numbers, individual line numbers, or both.
**Default:** None
* - ``:caption: <caption text>``
- Brief description.
**Default:** None
* - ``:dedent: <number of spaces>``
- Number of spaces to remove from the beginning of each line of code.
Use this option if the code to show is indented in the code file.
**Default:** None
Example 1
~~~~~~~~~
Suppose you want to include samples from the following Python file in a documentation page:
.. literalinclude:: /includes/python-sample.py
:language: python
The following ``literalinclude`` directive starts with the first line of the file and
ends before the line that reads ``print("...but this line won't.")``:
.. code-block::
.. literalinclude:: /includes/python-sample.py
:copyable: false
:end-before: print("...but this line won't.")
:caption: Sample Python code from another file
The previous ``literalinclude`` directive creates the following output:
.. literalinclude:: /includes/python-sample.py
:copyable: false
:end-before: print("...but this line won't.")
:caption: Sample Python code from another file
Example
~~~~~~~~~
In the Python file in the previous example, the code inside ``sample_function()`` is
indented two spaces. The following ``literalinclude`` directive uses the ``:dedent:``
option to remove these spaces:
.. code-block::
.. literalinclude:: /includes/python-sample.py
:start-after: start-function-sample
:end-before: end-function-sample
:linenos:
:lineno-start: 9
:dedent: 2
The previous directive creates the following output:
.. literalinclude:: /includes/python-sample.py
:start-after: start-function-sample
:end-before: end-function-sample
:linenos:
:lineno-start: 9
:dedent: 2