4. Output Formats
When the source is parsed, and have been processed it is time to render the output. We will consider the following source snippet:
# # Rational numbers
#
# In julia rational numbers can be constructed with the `//` operator.
# Lets define two rational numbers, `x` and `y`:
x = 1//3
#-
y = 2//5
# When adding `x` and `y` together we obtain a new rational number:
z = x + y
and see how this is rendered in each of the output formats.
4.1. Markdown Output
Markdown output is generated by Literate.markdown
. The (default) markdown output of the source snippet above is as follows:
```@meta
EditURL = "https://github.com/fredrikekre/Literate.jl/blob/master/docs/src/outputformats.jl"
```
# Rational numbers
In julia rational numbers can be constructed with the `//` operator.
Lets define two rational numbers, `x` and `y`:
```@example name
x = 1//3
```
```@example name
y = 2//5
```
When adding `x` and `y` together we obtain a new rational number:
```@example name
z = x + y
```
We note that lines starting with #
are printed as regular markdown, and the code lines have been wrapped in @example
blocks. We also note that an @meta
block have been added, that sets the EditURL
variable. This is used by Documenter to redirect the "Edit on GitHub" link for the page, see Interaction with Documenter.
It possible to configure Literate.markdown
to also evaluate code snippets, capture the result and include it in the output, by passing execute=true
as a keyword argument. The result of the first code-block in the example above would then become
```julia
x = 1//3
```
```
1//3
```
In this example the output is just plain text. However, if the resulting value of the code block can be displayed as an image (png or jpeg) Literate will include the image representation of the output.
Since Documenter executes and captures results of @example
block it is not necessary to use execute=true
for markdown output that is meant to be used as input to Documenter.
Code execution of markdown output requires at least Literate version 2.4.
See the section about Configuration for more information about how to configure the behavior and resulting output of Literate.markdown
.
Literate.markdown
— FunctionLiterate.markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
Generate a markdown file from inputfile
and write the result to the directory outputdir
.
Default output directory pwd
requires at least Literate version 2.5.
See the manual section on Configuration for documentation of possible configuration with config
and other keyword arguments.
4.2. Notebook Output
Notebook output is generated by Literate.notebook
. The (default) notebook output of the source snippet can be seen here: notebook.ipynb.
We note that lines starting with #
are placed in markdown cells, and the code lines have been placed in code cells. By default the notebook is also executed and output cells populated. The current working directory is set to the specified output directory the notebook is executed.
See the section about Configuration for how to configure the behavior and resulting output of Literate.notebook
.
Literate.notebook
— FunctionLiterate.notebook(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
Generate a notebook from inputfile
and write the result to outputdir
.
Default output directory pwd
requires at least Literate version 2.5.
See the manual section on Configuration for documentation of possible configuration with config
and other keyword arguments.
Notebook metadata
Jupyter notebook cells (both code cells and markdown cells) can contain metadata. This is enabled in Literate by the %%
token, similar to Jupytext. The format is as follows
%% optional ignored text [type] {optional metadata JSON}
Cell metadata can, for example, be used for nbgrader and the reveal.js notebook extension RISE.
The following would create a 3 slide deck with RISE:
#nb # %% A slide [markdown] {"slideshow": {"slide_type": "slide"}}
# # Some title
#
# We're using `#nb` so the metadata is only included in notebook output
#nb %% A slide [code] {"slideshow": {"slide_type": "fragment"}}
x = 1//3
y = 2//5
#nb # %% A slide [markdown] {"slideshow": {"slide_type": "subslide"}}
# For more information about RISE, see [the docs](https://rise.readthedocs.io/en/stable/usage.html)
4.3. Script Output
Script output is generated by Literate.script
. The (default) script output of the source snippet above is as follows:
x = 1//3
y = 2//5
z = x + y
We note that lines starting with #
are removed and only the code lines have been kept.
See the section about Configuration for how to configure the behavior and resulting output of Literate.script
.
Literate.script
— FunctionLiterate.script(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
Generate a plain script file from inputfile
and write the result to outputdir
.
Default output directory pwd
requires at least Literate version 2.5.
See the manual section on Configuration for documentation of possible configuration with config
and other keyword arguments.
4.4. Configuration
The behavior of Literate.markdown
, Literate.notebook
and Literate.script
can be configured by keyword arguments. There are two ways to do this; pass config::Dict
as a keyword argument, or pass individual keyword arguments.
Passing configuration as a dictionary requires at least Literate version 2.2.
Individual keyword arguments takes precedence over the config
dictionary, so for e.g. Literate.markdown(...; config = Dict("name" => "hello"), name = "world")
the resulting configuration for name
will be "world"
. Both individual keyword arguments and the config
dictionary takes precedence over the default.
Available configurations with description and default values are given in the reference for Literate.DEFAULT_CONFIGURATION
just below.
Literate.DEFAULT_CONFIGURATION
— ConstantDEFAULT_CONFIGURATION
Default configuration for Literate.markdown
, Literate.notebook
and Literate.script
which is used for everything not specified by the user. See the manual section about Configuration for more information.
Configuration key | Description | Default value | Comment |
---|---|---|---|
name | Name of the output file (excluding file extension). | filename(inputfile) | |
preprocess | Custom preprocessing function mapping String to String . | identity | See Custom pre- and post-processing. |
postprocess | Custom preprocessing function mapping String to String . | identity | See Custom pre- and post-processing. |
documenter | Boolean signaling that the source contains Documenter.jl elements. | true | See Interaction with Documenter. |
credit | Boolean for controlling the addition of This file was generated with Literate.jl ... to the bottom of the page. If you find Literate.jl useful then feel free to keep this. | true | |
keep_comments | When true , keeps markdown lines as comments in the output script. | false | Only applicable for Literate.script . |
execute | Whether to execute and capture the output. | true (notebook), false (markdown) | Only applicable for Literate.notebook and Literate.markdown . For markdown this requires at least Literate 2.4. |
codefence | Pair containing opening and closing fence for wrapping code blocks. | "```julia" => "```" | If documenter is true the default is "```@example"=>"```" . |
devurl | URL for "in-development" docs. | "dev" | See Documenter docs. Unused if repo_root_url /nbviewer_root_url /binder_root_url are set. |
repo_root_url | URL to the root of the repository. | - | Determined automatically on Travis CI, GitHub Actions and GitLab CI. Used for @__REPO_ROOT_URL__ . |
nbviewer_root_url | URL to the root of the repository as seen on nbviewer. | - | Determined automatically on Travis CI, GitHub Actions and GitLab CI. Used for @__NBVIEWER_ROOT_URL__ . |
binder_root_url | URL to the root of the repository as seen on mybinder. | - | Determined automatically on Travis CI, GitHub Actions and GitLab CI. Used for @__BINDER_ROOT_URL__ . |
repo_root_path | Filepath to the root of the repository. | - | Determined automatically on Travis CI, GitHub Actions and GitLab CI. Used for computing Documenters EditURL . |