Skip to content

Commit 0c55392

Browse files
authored
docs: add name block examples to showcase.md (#2539)
1 parent a1683b6 commit 0c55392

File tree

2 files changed

+130
-24
lines changed

2 files changed

+130
-24
lines changed

docs/src/man/syntax.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ As with `@index` if `Pages` is not provided then all pages are included. The def
393393

394394
Documenter will then list the contents of the "Subsection" pages, and they will always appear in the same order as they are in the sidebar.
395395

396-
## `@example` block
396+
## [`@example` block](@id reference-at-example)
397397

398398
Evaluates the code block and inserts the result of the last expression into the final document along with the
399399
original source code. If the last expression returns `nothing`, the `stdout`
@@ -689,7 +689,7 @@ Named `@repl <name>` blocks behave in the same way as named `@example <name>` bl
689689
`for` loops etc. When using Documenter with Julia 1.5 or above, Documenter uses the soft
690690
scope in `@repl`-blocks and REPL-type doctests.
691691

692-
## `@setup <name>` block
692+
## [`@setup <name>` block](@id reference-at-setup)
693693

694694
These are similar to `@example` blocks, but both the input and output are hidden from the
695695
final document. This can be convenient if there are several lines of setup code that need to be

docs/src/showcase.md

Lines changed: 128 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,57 @@ Script-style doctests are supported too:
371371
4
372372
```
373373

374+
### Setup code
375+
376+
You can have setup code for doctests that gets executed before the actual doctest.
377+
For example, the following doctest needs to have the `Documenter` module to be present.
378+
379+
```jldoctest; setup=:(using Documenter)
380+
julia> Documenter.splitexpr(:(Foo.Bar.baz))
381+
(:(Foo.Bar), :(:baz))
382+
```
383+
384+
This is achieved by the `setup` keyword to `jldoctest`.
385+
386+
````
387+
```jldoctest; setup=:(using Documenter)
388+
````
389+
390+
The alternative approach is to use the `DocTestSetup` keys in `@meta`-blocks, which will apply across multiple doctests.
391+
392+
````markdown
393+
```@meta
394+
DocTestSetup = quote
395+
f(x) = x^2
396+
end
397+
```
398+
````
399+
```@meta
400+
DocTestSetup = quote
401+
f(x) = x^2
402+
end
403+
```
404+
405+
```jldoctest
406+
julia> f(2)
407+
4
408+
```
409+
410+
The doctests and `@meta` blocks are evaluated sequentially on each page, so you can always unset the test code by setting it back to `nothing`.
411+
412+
````markdown
413+
```@meta
414+
DocTestSetup = nothing
415+
```
416+
````
417+
```@meta
418+
DocTestSetup = nothing
419+
```
420+
421+
374422
## Running interactive code
375423

376-
[`@example` block](@ref) run a code snippet and insert the output into the document.
424+
[`@example` block](@ref reference-at-example) run a code snippet and insert the output into the document.
377425
E.g. the following Markdown
378426

379427
````markdown
@@ -419,7 +467,7 @@ println("Hello World")
419467

420468
### Color output
421469

422-
Output from [`@repl` block](@ref)s and [`@example` block](@ref)s support colored output,
470+
Output from [`@repl` block](@ref)s and [`@example` block](@ref reference-at-example)s support colored output,
423471
transforming ANSI color codes to HTML.
424472

425473
!!! compat "Julia 1.6"
@@ -499,36 +547,94 @@ median(xs)
499547
sum(xs)
500548
```
501549

502-
## Doctest showcase
550+
### Named blocks
503551

504-
Currently exists just so that there would be doctests to run in manual pages of Documenter's
505-
manual. This page does not show up in navigation.
552+
Generally, each blocks gets evaluate in a separate, clean context (i.e. no variables from previous blocks will be polluting the namespace etc).
553+
However, you can also re-use a namespace by giving the blocks a name.
506554

507-
```jldoctest
508-
julia> 2 + 2
509-
4
555+
````markdown
556+
```@example block-name
557+
x = 40
558+
```
559+
will show up like this:
560+
````
561+
```@example block-name
562+
x = 40
510563
```
511564

512-
The following doctests needs doctestsetup:
565+
````markdown
566+
```@example block-name
567+
x + 1
568+
```
569+
will show up like this:
570+
````
571+
```@example block-name
572+
x + 1
573+
```
513574

514-
```jldoctest; setup=:(using Documenter)
515-
julia> Documenter.splitexpr(:(Foo.Bar.baz))
516-
(:(Foo.Bar), :(:baz))
575+
When you need setup code that you do not wish to show in the generated documentation, you can use [an `@setup` block](@ref reference-at-setup):
576+
577+
````markdown
578+
```@setup block-name
579+
x = 42
580+
```
581+
````
582+
```@setup block-name
583+
x = 42
517584
```
518585

519-
Let's also try `@meta` blocks:
586+
The [`@setup` block](@ref reference-at-setup) essentially acts as a hidden [`@example` block](@ref reference-at-example).
587+
Any state it sets up, you can access in subsequent blocks with the same name.
588+
For example, the following `@example` block
520589

521-
```@meta
522-
DocTestSetup = quote
523-
f(x) = x^2
524-
end
590+
````markdown
591+
```@example block-name
592+
x
525593
```
594+
````
526595

527-
```jldoctest
528-
julia> f(2)
529-
4
596+
will show up like this:
597+
598+
```@example block-name
599+
x
530600
```
531601

532-
```@meta
533-
DocTestSetup = nothing
602+
You also have continued blocks which do not evaluate immediately.
603+
604+
````markdown
605+
```@example block-name; continued = true
606+
y = 99
607+
```
608+
````
609+
```@example block-name; continued = true
610+
y = 99
611+
```
612+
613+
The continued evaluation only applies to [`@example` blocks](@ref reference-at-example) and so if you put, for example, an `@repl` block in between, it will lead to an error, because the `y = 99` line of code has not run yet.
614+
615+
````markdown
616+
```@repl block-name
617+
x
618+
y
619+
```
620+
````
621+
622+
```@repl block-name
623+
x
624+
y
625+
```
626+
627+
Another [`@example` block](@ref reference-at-example) with the same name will, however, finish evaluating it.
628+
So a block like
629+
630+
````markdown
631+
```@example block-name
632+
(x, y)
633+
```
634+
````
635+
636+
will lead to
637+
638+
```@example block-name
639+
(x, y)
534640
```

0 commit comments

Comments
 (0)