Diagrams¶
The mermaid directive draws a Mermaid diagram,
and the uml and plantuml directives (two names for one directive)
draw a PlantUML one.
All three work in reStructuredText and in Markdown (MyST).
Who draws a diagram depends on where you read it:
The editor preview and
ubc build htmldraw Mermaid in the browser, and translate PlantUML to Mermaid first (see PlantUML and UML).ubc build htmlcan instead run a real PlantUML you install, see External PlantUML renderer.ubc build pdfdraws Mermaid inside a Chromium-family browser you already have (Mermaid diagrams), and PlantUML only with a real PlantUML configured; without the browser or the PlantUML, each diagram is a box showing its source.A Sphinx build draws Mermaid with
sphinxcontrib-mermaidin the browser, and PlantUML withsphinxcontrib-plantuml, which runs PlantUML itself.
This site’s PDF has both, so every specimen below is drawn in all three.
ubCode does not draw Graphviz’s graphviz, graph and digraph
(see the unsupported diagrams).
Mermaid¶
A mermaid directive takes its diagram as its body.
See the Mermaid documentation for the diagram syntax itself.
A Mermaid diagram
.. mermaid::
:caption: Firmware boot sequence
sequenceDiagram
Bootloader->>Firmware: verify signature
Firmware-->>Bootloader: ok
sequenceDiagram
Bootloader->>Firmware: verify signature
Firmware-->>Bootloader: ok
Firmware boot sequence¶
```{mermaid}
:caption: Firmware boot sequence
sequenceDiagram
Bootloader->>Firmware: verify signature
Firmware-->>Bootloader: ok
```
The MyST form renders live in the MyST Mermaid diagram.
The diagram can come from a file instead, named by the argument:
a path relative to the page, or to the source directory when it starts with /.
Give the file or a body, not both: a directive with both is reported.
A Mermaid diagram from a file
.. mermaid:: /../specimens/diagrams/boot.mmd
sequenceDiagram
Bootloader->>Firmware: load image
Firmware-->>Bootloader: ready
```{mermaid} /../specimens/diagrams/boot.mmd
```
The MyST form renders live in the MyST Mermaid file.
PlantUML and UML¶
uml and plantuml take PlantUML source as their body, or a file as their argument, as mermaid does.
Write the @startuml / @enduml markers around the body:
ubCode does not add them when it runs a real PlantUML, so without them this site’s PDF prints a box with the source,
and a ubc build html with one configured falls back to the Mermaid conversion with a warning
(the diagram differences).
A sequence diagram
.. uml::
@startuml
Alice -> Bob: Hello
Bob --> Alice: Hi
@enduml
```{uml}
@startuml
Alice -> Bob: Hello
Bob --> Alice: Hi
@enduml
```
The MyST form renders live in the MyST sequence diagram.
This site’s Sphinx build and its PDF draw this one with PlantUML itself;
its ubc build html draws the Mermaid translation.
The same under its other name, with a caption:
A class diagram
.. plantuml::
:caption: Classes of the greeter
@startuml
class Greeter {
+greet(name)
}
class Farewell
Greeter --> Farewell
@enduml
Classes of the greeter¶
```{plantuml}
:caption: Classes of the greeter
@startuml
class Greeter {
+greet(name)
}
class Farewell
Greeter --> Farewell
@enduml
```
The MyST form renders live in the MyST class diagram.
The translation to Mermaid, which the preview and ubc build html draw by default,
is best-effort and covers a subset of PlantUML —
sequence, class, component, state, and activity diagrams.
Other PlantUML diagram families,
and individual lines the translator does not recognise,
fall back to showing the source as a placeholder
rather than a broken diagram.
Note
ubc build html and ubc build pdf can draw the plantuml /
uml directives, and needuml and needarch diagrams,
with a real PlantUML you install and maintain,
which renders them exactly as PlantUML draws them
and keeps the traceability links that the Mermaid conversion drops.
It is opt-in and off by default — see
External PlantUML renderer.
The editor preview always uses the Mermaid conversion, whatever a project configures.
Preprocessing¶
PlantUML preprocessing is applied to the body before it is translated, so a diagram can be generated rather than written out in full. Here a procedure writes one message per call:
A preprocessed diagram
.. uml::
@startuml
!$first = "Bob"
!procedure $hello($to)
Alice -> $to: Hello
!endprocedure
$hello($first)
$hello("Carol")
@enduml
```{uml}
@startuml
!$first = "Bob"
!procedure $hello($to)
Alice -> $to: Hello
!endprocedure
$hello($first)
$hello("Carol")
@enduml
```
The MyST form renders live in the MyST preprocessed diagram.
The translation understands
variables (!$name = …), !define / !definelong,
!function and !procedure,
conditionals (!if / !ifdef / !else),
loops (!while / !foreach)
and the string, list and arithmetic % builtins
(%strlen, %substr, %splitstr, %size, %upper, %lower,
%intval, %dec2hex and the rest).
This needs no configuration and cannot be turned off.
The C4 standard library is bundled,
so !include <C4/C4_Container.puml>
(and the other C4_* files) resolves offline
and the Person() / Container() / Rel() macros expand.
Not yet supported by the translation:
Including your own files, or a URL —
!include,!include_once,!include_manyand!includeurlresolve only against the bundled standard library. Local files are not read and URLs are never fetched; local-file includes are planned.Themes —
!themeis understood but no themes are bundled yet, so it is skipped.Sub-part includes —
!startsub/!endsub/!includesub,!includedefand!importare recognised but not executed.Structured data — a variable holding a JSON object or array cannot be navigated (
$obj.field,$arr[0]), and%load_jsonis not implemented. Lists produced by%splitstrdo work with!foreachand%size.Builtins that read the clock, the environment or the file being built —
%date,%now,%random,%getenv,%filename,%filename_no_extension,%dirpath,%filedateand%file_existsare recognised but return an empty string. A diagram whose content depended on any of them would not be reproducible from its own source, and%getenvwould additionally read the build environment.
Anything on that list is skipped, not fatal:
the rest of the diagram still renders.
If a body preprocesses away to nothing —
every line inside an !if that turned out false, say —
the directive falls back to showing its source.
PlantUML itself would draw an empty diagram there;
showing the source is the more useful of the two.
Options¶
The following options are applied to the rendered diagram:
Option |
Effect |
|---|---|
|
Wraps the diagram as a figure with the caption below it. The caption text is shown as written — inline markup is not interpreted. |
|
Aligns the diagram: |
|
Gives the diagram a stable cross-reference target you can |
|
Sizes the diagram container,
in the preview and in |
|
Accepted so they do not raise errors,
but not yet applied to the rendered diagram
( |
Behaviour notes¶
In the editor preview, diagrams follow the editor theme, and re-render when you switch between light and dark.
A diagram whose source has a syntax error keeps its source visible with an error style, so you can see and fix it, rather than showing a blank space. So does an unrecognised PlantUML family, or a line the translator cannot handle: you always have something to read and correct.
See also
Directives — directives in the preview generally.
Markdown (MyST) — writing the same content in Markdown (MyST).
Diagrams — where diagrams differ from a Sphinx build.