Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown code blocks part 4 #20189

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions doc/docstyle.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ General Guidelines
* (debatable) In nim sources, for links, prefer ``[link text](link.html)`` to `\`link text<link.html>\`_`:code:
since the syntax is simpler and markdown is more common (likewise, `nim rst2html`:cmd: also supports it in ``rst`` files).

.. code-block:: nim

```nim
proc someproc*(s: string, foo: int) =
## Use single backticks for inline code, e.g.: `s` or `someExpr(true)`.
## Use a backlash to follow with alphanumeric char: `int8`\s are great.
```


Module-level documentation
Expand All @@ -32,23 +32,24 @@ Documentation of a module is placed at the top of the module itself. Each line o
Sometimes `##[ multiline docs containing code ]##` is preferable, see ``lib/pure/times.nim``.
Code samples are encouraged, and should follow the general RST syntax:

.. code-block:: Nim

````Nim
## The `universe` module computes the answer to life, the universe, and everything.
##
## .. code-block::
## doAssert computeAnswerString() == 42
## ```
## doAssert computeAnswerString() == 42
## ```
````


Within this top-level comment, you can indicate the authorship and copyright of the code, which will be featured in the produced documentation.

.. code-block:: Nim

```Nim
## This is the best module ever. It provides answers to everything!
##
## :Author: Steve McQueen
## :Copyright: 1965
##
```

Leave a space between the last line of top-level documentation and the beginning of Nim code (the imports, etc.).

Expand All @@ -57,28 +58,29 @@ Procs, Templates, Macros, Converters, and Iterators

The documentation of a procedure should begin with a capital letter and should be in present tense. Variables referenced in the documentation should be surrounded by single tick marks:

.. code-block:: Nim

```Nim
proc example1*(x: int) =
## Prints the value of `x`.
echo x
```

Whenever an example of usage would be helpful to the user, you should include one within the documentation in RST format as below.

.. code-block:: Nim

````Nim
proc addThree*(x, y, z: int8): int =
## Adds three `int8` values, treating them as unsigned and
## truncating the result.
##
## .. code-block::
## # things that aren't suitable for a `runnableExamples` go in code-block:
## echo execCmdEx("git pull")
## drawOnScreen()
## ```
## # things that aren't suitable for a `runnableExamples` go in code-block:
## echo execCmdEx("git pull")
## drawOnScreen()
## ```
runnableExamples:
# `runnableExamples` is usually preferred to ``code-block``, when possible.
doAssert addThree(3, 125, 6) == -122
result = x +% y +% z
````

The command `nim doc`:cmd: will then correctly syntax highlight the Nim code within the documentation.

Expand All @@ -87,21 +89,20 @@ Types

Exported types should also be documented. This documentation can also contain code samples, but those are better placed with the functions to which they refer.

.. code-block:: Nim

```Nim
type
NamedQueue*[T] = object ## Provides a linked data structure with names
## throughout. It is named for convenience. I'm making
## this comment long to show how you can, too.
name*: string ## The name of the item
val*: T ## Its value
next*: ref NamedQueue[T] ## The next item in the queue
```


You have some flexibility when placing the documentation:

.. code-block:: Nim

```Nim
type
NamedQueue*[T] = object
## Provides a linked data structure with names
Expand All @@ -110,11 +111,11 @@ You have some flexibility when placing the documentation:
name*: string ## The name of the item
val*: T ## Its value
next*: ref NamedQueue[T] ## The next item in the queue
```

Make sure to place the documentation beside or within the object.

.. code-block:: Nim

```Nim
type
## Bad: this documentation disappears because it annotates the `type` keyword
## above, not `NamedQueue`.
Expand All @@ -123,40 +124,41 @@ Make sure to place the documentation beside or within the object.
## is not what we want.
val*: T ## Its value
next*: ref NamedQueue[T] ## The next item in the queue
```

Var, Let, and Const
-------------------

When declaring module-wide constants and values, documentation is encouraged. The placement of doc comments is similar to the `type` sections.

.. code-block:: Nim

```Nim
const
X* = 42 ## An awesome number.
SpreadArray* = [
[1,2,3],
[2,3,1],
[3,1,2],
] ## Doc comment for `SpreadArray`.
```

Placement of comments in other areas is usually allowed, but will not become part of the documentation output and should therefore be prefaced by a single hash (`#`).

.. code-block:: Nim

```Nim
const
BadMathVals* = [
3.14, # pi
2.72, # e
0.58, # gamma
] ## A bunch of badly rounded values.
```

Nim supports Unicode in comments, so the above can be replaced with the following:

.. code-block:: Nim

```Nim
const
BadMathVals* = [
3.14, # π
2.72, # e
0.58, # γ
] ## A bunch of badly rounded values (including π!).
```
16 changes: 8 additions & 8 deletions doc/drnim.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ DrNim's command-line options are the same as the Nim compiler's.
DrNim currently only checks the sections of your code that are marked
via `staticBoundChecks: on`:

.. code-block:: nim

```nim
{.push staticBoundChecks: on.}
# <--- code section here ---->
{.pop.}
```

DrNim currently only tries to prove array indexing or subrange checks,
overflow errors are *not* prevented. Overflows will be checked for in
Expand All @@ -53,8 +53,7 @@ Motivating Example
The follow example highlights what DrNim can easily do, even
without additional annotations:

.. code-block:: nim

```nim
{.push staticBoundChecks: on.}

proc sum(a: openArray[int]): int =
Expand All @@ -64,6 +63,7 @@ without additional annotations:
{.pop.}

echo sum([1, 2, 3])
```

This program contains a famous "index out of bounds" bug. DrNim
detects it and produces the following error message::
Expand Down Expand Up @@ -125,8 +125,7 @@ Example: insertionSort

**Note**: This example does not yet work with DrNim.

.. code-block:: nim

```nim
import std / logic

proc insertionSort(a: var openArray[int]) {.
Expand All @@ -142,6 +141,7 @@ Example: insertionSort
{.invariant: forall(j in 1..k, i in 0..<j, j == t or a[i] <= a[j]).}
swap a[t], a[t-1]
dec t
```

Unfortunately, the invariants required to prove that this code is correct take more
code than the imperative instructions. However, this effort can be compensated
Expand All @@ -155,14 +155,14 @@ This is required, but not sufficient to describe that a `sort` operation
was performed. For example, the same postcondition is true for this proc
which doesn't sort at all:

.. code-block:: nim

```nim
import std / logic

proc insertionSort(a: var openArray[int]) {.
ensures: forall(i in 1..<a.len, a[i-1] <= a[i]).} =
# does not sort, overwrites `a`'s contents!
for i in 0..<a.len: a[i] = i
```



Expand Down
3 changes: 2 additions & 1 deletion doc/effects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ difficult is the ``newString`` proc: If it is simply wrapped, it
should not be evaluated at compile time! On other occasions it can
and should be evaluted:

.. code-block:: nim
```nim
proc toUpper(s: string): string =
result = newString(len(s))
for i in 0..len(s) - 1:
result[i] = toUpper(s[i])
```

No, it really can always be evaluated. The code generator should transform
``s = "\0\0\0..."`` back into ``s = newString(...)``.
Expand Down
6 changes: 4 additions & 2 deletions doc/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ recognize it as Nim source file).
If we use `generateXML` code shown above and call the SCF file `xmlGen.nimf`
In your `main.nim`:

.. code-block:: nim
```nim
include "xmlGen.nimf"

echo generateXML("John Smith","42")
```

Pipe operator
=============
Expand Down Expand Up @@ -150,7 +151,7 @@ Example::

The filter transforms this into:

.. code-block:: nim
```nim
proc generateHTMLPage(title, currentTab, content: string,
tabs: openArray[string]): string =
result = ""
Expand All @@ -173,6 +174,7 @@ The filter transforms this into:
" A dollar: $.\n" &
" </div>\n" &
"</body>\n")
```


Each line that does not start with the meta character (ignoring leading
Expand Down
Loading