Listing Extension
The listing extension provides a mechanism for creating source code listings. The system allows for the inclusion of local content as well as complete or partial snippets of the desired source code and includes the ability to parse MOOSE input files and separate out blocks. The main purpose is to avoid copying code or input syntax into the documentation to avoid out-of-date content.
The extension provides the !listing
command (see Command Extension), which allows for numbered captions to be applied, the Float Extension provides additional details. The following table list the available configure options for the extension.
Key | Default | Description |
---|---|---|
prefix | Listing | The caption prefix (e.g., Fig.). |
The !listing
command has the capability to include text from local content and arbitrary files (such as source code). files. There is a wide range of settings that are available to specialize how the code is imported. The complete list of available of settings are provided in Table 2 and the sections below provide various examples of using some of these settings.
Local Listing Content
It is possible to create a listing using local content. This is done by using the !listing
command without any agruments with the desired content following the command, see Command Extension for details to how content is defined. The available settings for this command are given in Table 1.
!listing id=local caption=A function for adding 42. language=cpp
double add_forty_two(const double y);
y += 42;
return y;
double add_forty_two(const double y);
y += 42;
return y;
Key | Default | Description |
---|---|---|
style | The style settings that are passed to rendered HTML tag. | |
language | None | The language to use for highlighting, if not supplied it will be inferred from the extension (if possible). |
id | The class settings to be passed to the rendered tag. | |
max-height | 350px | The default height for listing content. |
caption | None | The caption to use for the listing content. |
prefix | None | Text to include prior to the included text. |
class | The class settings to be passed to rendered HTML tag. |
File Content
You can include complete files from the repository. For example, the following creates the code listing in Example 2.
Key | Default | Description |
---|---|---|
strip-header | True | When True the MOOSE header is removed for display. |
style | The style settings that are passed to rendered HTML tag. | |
strip-extra-newlines | True | Removes extraneous new lines from the text. |
indent | 0 | The level of indenting to apply to the included text. |
language | None | The language to use for highlighting, if not supplied it will be inferred from the extension (if possible). |
header | None | Text to include prior to the included text. |
end | None | A portion of text that unique identifies the ending location for including text, if not provided the end of the file is used. By default this line is not included in the display. |
footer | Text to include after to the included text. | |
re-flags | re.M|re.S|re.U | Python re flags. |
start | None | A portion of text that unique identifies the starting location for including text, if not provided the beginning of the file is utilized. |
id | The class settings to be passed to the rendered tag. | |
max-height | 350px | The default height for listing content. |
caption | None | The caption to use for the listing content. |
prefix | None | Text to include prior to the included text. |
include-end | False | When True the texted captured by the 'end' setting is included in the displayed text. |
link | True | Include a link to the filename after the listing. |
re | False | Extract content via a regex, if the 'content' group exists it is used as the desired content, otherwise group 0 is used. |
line | None | A portion of text that unique identifies a single line to include. |
strip-leading-whitespace | False | When True leading white-space is removed from the included text. |
class | The class settings to be passed to rendered HTML tag. | |
include-start | True | When False the texted captured by the 'start' setting is excluded in the displayed text. |
!listing framework/src/kernels/Diffusion.C
// This file is part of the MOOSE framework
// https://www.mooseframework.org
//
// All rights reserved, see COPYRIGHT for full restrictions
// https://github.com/idaholab/moose/blob/master/COPYRIGHT
//
// Licensed under LGPL 2.1, please see LICENSE for details
// https://www.gnu.org/licenses/lgpl-2.1.html
#include "Diffusion.h"
registerMooseObject("MooseApp", Diffusion);
template <>
InputParameters
validParams<Diffusion>()
{
InputParameters params = validParams<Kernel>();
params.addClassDescription("The Laplacian operator ($-\\nabla \\cdot \\nabla u$), with the weak "
"form of $(\\nabla \\phi_i, \\nabla u_h)$.");
return params;
}
Diffusion::Diffusion(const InputParameters & parameters) : Kernel(parameters) {}
Real
Diffusion::computeQpResidual()
{
return _grad_u[_qp] * _grad_test[_i][_qp];
}
Real
Diffusion::computeQpJacobian()
{
return _grad_phi[_j][_qp] * _grad_test[_i][_qp];
}
(../moose/framework/src/kernels/Diffusion.C)Regular Expression Match
Regular expressions can be utilized to extract specific content from files. Example 3 uses a regular expression to extract the content of a class method.
!listing framework/src/kernels/Diffusion.C
re=Real\sDiffusion::computeQpResidual.*?^}
Real
Diffusion::computeQpResidual()
{
return _grad_u[_qp] * _grad_test[_i][_qp];
}
(../moose/framework/src/kernels/Diffusion.C)Single Line Match
It is possible to show a single line of a file by including a snippet that allows the line to be located within the file. If multiple matches occur only the first match will be shown. For example, the call to addClassDescription
can be shown by adding the following.
!listing framework/src/kernels/Diffusion.C line=computeQp
Diffusion::computeQpJacobian()
(../moose/framework/src/kernels/Diffusion.C)Range Line Match
Code starting and ending on lines containing a string is also possible by using the 'start' and 'end' options. If 'start' is omitted then the snippet will start at the beginning of the file. Similarly, if 'end' is omitted the snippet will include the remainder of the file content.
!listing moose/test/tests/kernels/simple_diffusion/simple_diffusion.i
start=Kernels
end=Executioner
[Kernels]
[./diff]
type = Diffusion
variable = u
[../]
[]
[BCs]
[./left]
type = DirichletBC
variable = u
boundary = left
value = 0
[../]
[./right]
type = DirichletBC
variable = u
boundary = right
value = 1
[../]
[]
(../moose/test/tests/kernels/simple_diffusion/simple_diffusion.i)Input File Content
Like for C++ files, MOOSE input files also have additional capability, mainly the "block" setting (see Example 6 for a complete list). Including the block name the included content will be limited to the content matching the supplied name. Notice that the supplied name may be approximate; however, if it is not unique only the first match will appear.
!listing moose/test/tests/kernels/simple_diffusion/simple_diffusion.i
block=Kernels
id=input
caption=Code listing of [MOOSE] input file block.
[Kernels]
[./diff]
type = Diffusion
variable = u
[../]
[]
(../moose/test/tests/kernels/simple_diffusion/simple_diffusion.i)Key | Default | Description |
---|---|---|
strip-header | True | When True the MOOSE header is removed for display. |
strip-extra-newlines | True | Removes extraneous new lines from the text. |
re-flags | re.M|re.S|re.U | Python re flags. |
strip-leading-whitespace | False | When True leading white-space is removed from the included text. |
max-height | 350px | The default height for listing content. |
header | None | Text to include prior to the included text. |
prefix | None | Text to include prior to the included text. |
include-end | False | When True the texted captured by the 'end' setting is included in the displayed text. |
id | The class settings to be passed to the rendered tag. | |
style | The style settings that are passed to rendered HTML tag. | |
end | None | A portion of text that unique identifies the ending location for including text, if not provided the end of the file is used. By default this line is not included in the display. |
re | False | Extract content via a regex, if the 'content' group exists it is used as the desired content, otherwise group 0 is used. |
start | None | A portion of text that unique identifies the starting location for including text, if not provided the beginning of the file is utilized. |
include-start | True | When False the texted captured by the 'start' setting is excluded in the displayed text. |
link | True | Include a link to the filename after the listing. |
line | None | A portion of text that unique identifies a single line to include. |
class | The class settings to be passed to rendered HTML tag. | |
indent | 0 | The level of indenting to apply to the included text. |
language | None | The language to use for highlighting, if not supplied it will be inferred from the extension (if possible). |
footer | Text to include after to the included text. | |
caption | None | The caption to use for the listing content. |
block | None | Space separated list of input file block names to include. |