Blocks of computer code, to be typeset verbatim, must appear inside a
"begin code" !bc keyword and an "end code" !ec keyword. Both
keywords must be on a single line and start at the beginning of the
line. Before such a code block there must be a plain sentence
(at least if successful transformation to reST and
ASCII-type formats is desired). For example, a code block cannot come
directly after a section/paragraph heading or a table.
Here is a plain code block:
!bc
% Could be a comment line in some file
% And some data
1.003 1.025
2.204 1.730
3.001 1.198
!ec
which gets rendered as
% Could be a comment line in some file
% And some data
1.003 1.025
2.204 1.730
3.001 1.198
There may be an argument after the !bc tag to specify a certain
environment (for ptex2tex, doconce ptex2tex, or Sphinx) for
typesetting the verbatim code. For instance, !bc dat corresponds to
the data file environment and !bc cod is typically used for a code
snippet. There are some predefined environments explained below. If
there is no argument specifying the environment, one assumes some
plain verbatim typesetting (for ptex2tex this means the ccq
environment, which is defined in the config file .ptex2tex.cfg,
while for Sphinx it defaults to the python environment).
By default, pro is used for complete programs in Python, cod is
for a code snippet in Python, while Xcod and Xpro imply computer
language specific typesetting where X can be f for Fortran, c
for C, cpp for C++, sh for Unix shells, pl for Perl, m for
Matlab, cy for Cython, r for Ruby, js for JavaScript,
latex for LaTeX, html for HTML, and py for Python. The
argument sys means by default console for Sphinx and
CodeTerminal (ptex2tex environent) for LaTeX. Other specifications
are dat for a data file or print out, pyshell for plain interactive Python
shell sessions, and ipy for interactive IPython sessions (the latter
does not introduce any environment in sphinx output, as interactive
sessions are automatically recognized and handled in that format).
(All these definitions of the arguments after !bc can be redefined
in the .ptex2tex.cfg configuration file for ptex2tex and in
the sphinx code-blocks comments for Sphinx, see below.)
Since the config file for ptex2tex and command-line arguments for
the alternative doconce ptex2tex program can define what some environments
map onto with respect to typesetting, a similar possibility is
supported for Sphinx as well. The argument after !bc is in case of
Sphinx output mapped onto a valid Pygments language for typesetting of
the verbatim block by Pygments. This mapping takes place in an
optional comment to be inserted in the DocOnce source file, e.g.,
# sphinx code-blocks: pycod=python cod=fortran cppcod=c++ sys=console
Here, three arguments are defined: !bc pycod maps on to the
Pygments style python for Python code,
!bc cod maps on to the Pygments style fortran for Fortran
code, !bc cppcod maps on to the Pygments style c++ for C++ code,
and !bc sys maps on to the Pygments console style for
terminal sessions. The same arguments would be defined
in .ptex2tex.cfg or on the command line for doconce ptex2tex
for how to typeset the blocks in LaTeX using
various verbatim styles (Pygments can also be used in a LaTeX
context).
!ec tag of verbatim computer code blocks must
be followed by a newline. A common error in list environments is to
forget to indent the plain text surrounding the code blocks. In
general, we recommend to use paragraph headings instead of list items
in combination with code blocks (it usually looks better, and some
common errors are naturally avoided).
Here is a verbatim code block with Python code (pycod style):
!bc pycod
def f(x, y):
return x + y
# Main program
from math import pi
print 'Testing f:', f(pi, 0)
!ec
The typeset result of this block becomes
def f(x, y):
return x + y
# Main program
from math import pi
print 'Testing f:', f(pi, 0)
And here is a C++ code snippet (cppcod style):
!bc cppcod
void myfunc(double* x, const double& myarr) {
for (int i = 1; i < myarr.size(); i++) {
myarr[i] = myarr[i] - x[i]*myarr[i-1]
}
}
!ec
with the rendered result
void myfunc(double* x, const double& myarr) {
for (int i = 1; i < myarr.size(); i++) {
myarr[i] = myarr[i] - x[i]*myarr[i-1]
}
}
Computer code can be copied directly from a file, if desired. The syntax is then
@@@CODE myfile.f
@@@CODE myfile.f fromto: subroutine\s+test@^C\s{5}END1
The first line implies that all lines in the file myfile.f are
copied into a verbatim block, typset in a !bc Xpro environment, where
X is the extension of the filename, here f (i.e., the environment
becomes !bc fpro and will typically lead to some Fortran-style
formatting). The
second line has a fromto: directive, which implies copying code
between two lines in the code, typset within a !`bc Xcod`
environment (again, X is the filename extension, implying the
type of file). Note that the pro and cod arguments are only used for LaTeX
and Sphinx output, all other formats will have the code typeset within
a plain !bc environment.) Two regular expressions, separated by the
@ sign, define the "from" and "to" lines. The "from" line is
included in the verbatim block, while the "to" line is not. In the
example above, we copy code from the line matching subroutine test
(with as many blanks as desired between the two words) and the line
matching C END1 (C followed by 5 blanks and then the text END1). The
final line with the "to" text is not included in the verbatim block.
One can also specify the code environment explictly rather than relying on the file extension:
@@@CODE somefile.py envir=X fromto: def myfunc@def yourfunc
This is the same as writing !bc X and copying parts of the somefile.py
text into the DocOnce source file, followed by !ec.
For example, if the lines between the myfunc and yourfunc functions
actually work as a complete Python program, one could specify envir=pypro
to indicate that it is a complete program that can be run as is.
By default, copying a part of a .py file will lead to
!bc pycod, which indicates a code snippet that normally needs
additional code to be run.
Using envir=None results in a pure include of the file, without any
surrounding code environment (i.e., no !bc or !ec directives around
the contents of the file). The section Example: Nomenclature functionality shows
an example.
Let us demonstrate the result of copying a whole file, as specified in the first line above:
C a comment
subroutine test()
integer i
real*8 r
r = 0
do i = 1, i
r = r + i
end do
return
C END1
program testme
call test()
return
Let us then copy just a piece in the middle as indicated by the fromto:
directive above:
subroutine test()
integer i
real*8 r
r = 0
do i = 1, i
r = r + i
end do
return
fromto and from-to directives
Note that the "to" line is never copied into the DocOnce file, but the
"from" line is. Sometimes it is convenient to also neglect the
"from" line, a feature that is allowed by replacing fromto: by
from-to ("from with minus"). This allows for copying very similar
code segments throughout a file, while still distinguishing between them.
Copying the second set of parameters from the text
# --- Start Example 1 ---
c = -1
A = 2
p0 = 4
simulate_and_plot(c, A, p0)
# --- End Example 1 ---
# --- Start Example 2 ---
c = -1
A = 1
p0 = 0
simulate_and_plot(c, A, p0)
# --- End Example 2 ---
is easy with
from-to: Start Example 2@End Example 2
With only fromto: this would be impossible.
Remark for those familiar with ptex2tex: The from-to
syntax is slightly different from that used in ptex2tex. When
transforming DocOnce to LaTeX, one first transforms the document to a
.p.tex file to be treated by ptex2tex or doconce ptex2tex.
However, note that the @@@CODE line
is always interpreted by DocOnce first.
Remark for those familiar with the listings package in LaTeX: the
listing package can copy code from files, but snippets must be
specified through exact line numbers. The @@@CODE directive
above works with regular expressions which are much less sensitive
to edits of the source code file than the line numbers.
Moreover, copy of code from file works in Dococe across formats
(HTML, Sphinx, Markdown, etc.).
The --code_prefix=text option adds a path text to the filename specified
in the @@@CODE command. For example
@@@CODE src/myfile.py
and --code_prefix=http://some.place.net, the file
http://some.place.net/src/myfile.py
will be included. If source files have a header with author, email, etc.,
one can remove this header by the option '--code_skip_until=# ---'.
The lines up to and including (the first) # --- will then be excluded.