@doccident/doccident 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +195 -3
- package/README.md +333 -7
- package/bin/cmd.js +12 -1
- package/dist/doctest.js +189 -7
- package/dist/languages/basic.js +51 -0
- package/dist/languages/c.js +123 -12
- package/dist/languages/cobol.js +8 -3
- package/dist/languages/csharp.js +77 -0
- package/dist/languages/fortran.js +119 -10
- package/dist/languages/go.js +124 -10
- package/dist/languages/java.js +92 -0
- package/dist/languages/javascript.js +31 -4
- package/dist/languages/pascal.js +88 -0
- package/dist/languages/perl.js +36 -0
- package/dist/languages/python.js +20 -3
- package/dist/languages/r.js +37 -0
- package/dist/languages/rust.js +58 -10
- package/dist/languages/shell.js +12 -3
- package/dist/parse-code-snippets-from-markdown.js +102 -3
- package/dist/reporter.js +39 -0
- package/package.json +6 -6
package/LICENSE
CHANGED
|
@@ -1,9 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright (c) 2025 Billaud Cipher
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
|
191
|
+
|
|
192
|
+
--------------------------------------------------------------------------------
|
|
193
|
+
|
|
194
|
+
This project is based on work originally licensed under the MIT License:
|
|
195
|
+
|
|
1
196
|
The MIT License (MIT)
|
|
2
197
|
|
|
3
198
|
Copyright (c) 2015 Nick Johnstone
|
|
4
199
|
|
|
5
|
-
Copyright (c) 2025 Billaud Cipher
|
|
6
|
-
|
|
7
200
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
201
|
of this software and associated documentation files (the "Software"), to deal
|
|
9
202
|
in the Software without restriction, including without limitation the rights
|
|
@@ -21,4 +214,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
21
214
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
215
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
216
|
SOFTWARE.
|
|
24
|
-
|
package/README.md
CHANGED
|
@@ -9,6 +9,12 @@
|
|
|
9
9
|
|
|
10
10
|
As an open source developer, few things are more frustrating for users than encountering broken examples in a README. `doccident` ensures your documentation remains accurate by treating your examples as testable code. It parses your Markdown files, extracts code blocks for multiple languages, and executes them in a sandboxed environment to verify they run without errors.
|
|
11
11
|
|
|
12
|
+
### The "Why"
|
|
13
|
+
|
|
14
|
+
This project was born out of frustration. The author once wrote a massive technical book, only to be haunted by the realization that subtle bugs had crept into the code examples and output listings during the editing process.
|
|
15
|
+
|
|
16
|
+
`doccident` solves this by automating the verification of your documentation. It doesn't just check if your code compiles; it can **execute** your snippets and **verify their output** matches what you claim in your docs. This "living documentation" approach guarantees that your readers always see correct, working code and accurate output.
|
|
17
|
+
|
|
12
18
|
**Supported Languages:**
|
|
13
19
|
* JavaScript (Node.js)
|
|
14
20
|
* TypeScript
|
|
@@ -19,6 +25,12 @@ As an open source developer, few things are more frustrating for users than enco
|
|
|
19
25
|
* Fortran
|
|
20
26
|
* COBOL
|
|
21
27
|
* C
|
|
28
|
+
* BASIC
|
|
29
|
+
* Java
|
|
30
|
+
* Perl
|
|
31
|
+
* C#
|
|
32
|
+
* R
|
|
33
|
+
* Pascal
|
|
22
34
|
|
|
23
35
|
> **Note**: `doccident` primarily verifies that your code *runs* without throwing exceptions. While you can add assertions to your examples to test correctness, its main goal is to ensure your documentation examples are valid and runnable.
|
|
24
36
|
|
|
@@ -45,6 +57,17 @@ You can also target specific files or directories:
|
|
|
45
57
|
npx doccident docs/**/*.md
|
|
46
58
|
```
|
|
47
59
|
|
|
60
|
+
### Timeout
|
|
61
|
+
|
|
62
|
+
By default, each code snippet has a 5-minute (300,000ms) timeout to prevent infinite loops or hangs. You can customize this with the `--timeout` flag (in milliseconds):
|
|
63
|
+
|
|
64
|
+
<!-- skip-example -->
|
|
65
|
+
```bash
|
|
66
|
+
npx doccident --timeout 60000 # 60 second timeout
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
If a snippet exceeds the timeout, it will fail with an error message indicating the timeout was reached.
|
|
70
|
+
|
|
48
71
|
### Language Support & Recipes
|
|
49
72
|
|
|
50
73
|
`doccident` executes code inside fenced code blocks for the following languages:
|
|
@@ -58,6 +81,13 @@ npx doccident docs/**/*.md
|
|
|
58
81
|
* **Fortran**: `fortran`, `f90`, `f95`
|
|
59
82
|
* **COBOL**: `cobol`, `cob`
|
|
60
83
|
* **C**: `c`
|
|
84
|
+
* **BASIC**: `basic`
|
|
85
|
+
* **Java**: `java`
|
|
86
|
+
* **Perl**: `perl`, `pl`
|
|
87
|
+
* **C#**: `csharp`, `cs`
|
|
88
|
+
* **R**: `r`
|
|
89
|
+
* **Pascal**: `pascal`, `pas`
|
|
90
|
+
* **Text/Output**: `text`, `txt`, `output` (for output verification)
|
|
61
91
|
|
|
62
92
|
It automatically transforms modern JavaScript and TypeScript using **esbuild** before execution.
|
|
63
93
|
|
|
@@ -167,9 +197,10 @@ Writes code to a temporary file and executes it via `go run`.
|
|
|
167
197
|
1. Use `go` fenced code blocks.
|
|
168
198
|
2. You can provide a full program (including `package main`) OR a simple snippet.
|
|
169
199
|
3. Simple snippets (without `package main`) are automatically wrapped in a `main` function and include `import "fmt"`.
|
|
170
|
-
4. **Note**: `<!-- share-code-between-examples -->` is
|
|
200
|
+
4. **Note**: `<!-- share-code-between-examples -->` is supported for Go snippets (auto-wrapped). Declarations (`func`, `type`, `import`) are extracted to top-level.
|
|
171
201
|
|
|
172
202
|
**Simple Snippet:**
|
|
203
|
+
<!-- skip-example -->
|
|
173
204
|
```go
|
|
174
205
|
fmt.Println("Hello Go")
|
|
175
206
|
```
|
|
@@ -191,9 +222,10 @@ Compiles the code using `rustc` into a temporary binary, then executes the binar
|
|
|
191
222
|
1. Use `rust` fenced code blocks.
|
|
192
223
|
2. You can provide a full program (including `fn main()`) OR a simple snippet.
|
|
193
224
|
3. Simple snippets (without `fn main`) are automatically wrapped in a `main` function.
|
|
194
|
-
4. **Note**: `<!-- share-code-between-examples -->` is
|
|
225
|
+
4. **Note**: `<!-- share-code-between-examples -->` is supported for Rust. Attributes like `#![...]` and `extern crate` are hoisted to the crate root. Other code is wrapped in `fn main()`.
|
|
195
226
|
|
|
196
227
|
**Simple Snippet:**
|
|
228
|
+
<!-- skip-example -->
|
|
197
229
|
```rust
|
|
198
230
|
println!("Hello Rust");
|
|
199
231
|
let x = 5;
|
|
@@ -217,9 +249,10 @@ Compiles the code using `gfortran` into a temporary binary, then executes the bi
|
|
|
217
249
|
1. Use `fortran` fenced code blocks.
|
|
218
250
|
2. You can provide a full program (starting with `program name`) OR a simple snippet.
|
|
219
251
|
3. Simple snippets are automatically wrapped in a `program main ... end program main` block.
|
|
220
|
-
4. **Note**: `<!-- share-code-between-examples -->` is
|
|
252
|
+
4. **Note**: `<!-- share-code-between-examples -->` is supported for Fortran. Modules are extracted to the top level; `use` statements are moved to the top of `program main`.
|
|
221
253
|
|
|
222
254
|
**Simple Snippet:**
|
|
255
|
+
<!-- skip-example -->
|
|
223
256
|
```fortran
|
|
224
257
|
print *, "Hello Fortran"
|
|
225
258
|
```
|
|
@@ -243,6 +276,7 @@ Compiles the code using `cobc -x -free` into a temporary executable, then runs i
|
|
|
243
276
|
3. The compiler is run in free-format mode (`-free`).
|
|
244
277
|
4. **Note**: `<!-- share-code-between-examples -->` is **not** supported for COBOL.
|
|
245
278
|
|
|
279
|
+
<!-- skip-example -->
|
|
246
280
|
```cobol
|
|
247
281
|
IDENTIFICATION DIVISION.
|
|
248
282
|
PROGRAM-ID. HELLO.
|
|
@@ -268,13 +302,186 @@ Compiles the code using `gcc` into a temporary binary, then executes the binary.
|
|
|
268
302
|
1. Use `c` fenced code blocks.
|
|
269
303
|
2. You can provide a full program (including `main()`) OR a simple snippet.
|
|
270
304
|
3. Simple snippets are automatically wrapped in a `main` function and include `stdio.h`.
|
|
271
|
-
4. **Note**: `<!-- share-code-between-examples -->` is
|
|
305
|
+
4. **Note**: `<!-- share-code-between-examples -->` is supported for C. Includes and definitions (`struct`, `func`) are hoisted; statements are placed in `main()`.
|
|
272
306
|
|
|
273
307
|
**Simple Snippet:**
|
|
274
308
|
```c
|
|
275
309
|
printf("Hello C\n");
|
|
276
310
|
```
|
|
277
311
|
|
|
312
|
+
#### BASIC
|
|
313
|
+
|
|
314
|
+
Use `basic` for BASIC examples.
|
|
315
|
+
|
|
316
|
+
**Prerequisites:**
|
|
317
|
+
* `cbmbasic` must be installed and available in your system path.
|
|
318
|
+
* **macOS**: `brew install cbmbasic`
|
|
319
|
+
* **Ubuntu/Debian**: Build from source (see `test.yml` for example).
|
|
320
|
+
|
|
321
|
+
**Execution Model:**
|
|
322
|
+
Spawns a `cbmbasic` subprocess with the code piped to stdin.
|
|
323
|
+
|
|
324
|
+
**Recipe:**
|
|
325
|
+
|
|
326
|
+
1. Use `basic` fenced code blocks.
|
|
327
|
+
2. Write standard Commodore BASIC V2 code.
|
|
328
|
+
3. State can be shared between blocks using `<!-- share-code-between-examples -->`.
|
|
329
|
+
|
|
330
|
+
<!-- skip-example -->
|
|
331
|
+
```basic
|
|
332
|
+
10 PRINT "HELLO BASIC"
|
|
333
|
+
20 END
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
#### Java
|
|
337
|
+
|
|
338
|
+
Use `java` for Java examples.
|
|
339
|
+
|
|
340
|
+
**Prerequisites:**
|
|
341
|
+
* `javac` and `java` must be installed and available in your system path.
|
|
342
|
+
* **macOS**: `brew install openjdk`
|
|
343
|
+
* **Ubuntu/Debian**: `sudo apt-get install default-jdk`
|
|
344
|
+
|
|
345
|
+
**Execution Model:**
|
|
346
|
+
Compiles the code using `javac` into a temporary class file in a unique directory, then executes it with `java`.
|
|
347
|
+
|
|
348
|
+
**Recipe:**
|
|
349
|
+
|
|
350
|
+
1. Use `java` fenced code blocks.
|
|
351
|
+
2. You can provide a full class (e.g. `public class MyClass { ... }`) OR a simple snippet.
|
|
352
|
+
3. Simple snippets are automatically wrapped in a `public class Main { public static void main(String[] args) { ... } }`.
|
|
353
|
+
4. **Note**: `<!-- share-code-between-examples -->` is **not** supported for Java.
|
|
354
|
+
|
|
355
|
+
**Simple Snippet:**
|
|
356
|
+
<!-- skip-example -->
|
|
357
|
+
```java
|
|
358
|
+
System.out.println("Hello Java");
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
**Full Class:**
|
|
362
|
+
<!-- skip-example -->
|
|
363
|
+
```java
|
|
364
|
+
public class Greeting {
|
|
365
|
+
public static void main(String[] args) {
|
|
366
|
+
System.out.println("Hello from Greeting class");
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
#### Perl
|
|
372
|
+
|
|
373
|
+
Use `perl` or `pl` for Perl examples.
|
|
374
|
+
|
|
375
|
+
**Prerequisites:**
|
|
376
|
+
* `perl` must be installed and available in your system path.
|
|
377
|
+
* **macOS**: Pre-installed or `brew install perl`
|
|
378
|
+
* **Ubuntu/Debian**: Pre-installed or `sudo apt-get install perl`
|
|
379
|
+
|
|
380
|
+
**Execution Model:**
|
|
381
|
+
Spawns a `perl` subprocess with the code piped to stdin.
|
|
382
|
+
|
|
383
|
+
**Recipe:**
|
|
384
|
+
|
|
385
|
+
1. Use `perl` fenced code blocks.
|
|
386
|
+
2. Standard library imports work out of the box.
|
|
387
|
+
3. State can be shared between blocks using `<!-- share-code-between-examples -->`.
|
|
388
|
+
|
|
389
|
+
```perl
|
|
390
|
+
print "Hello Perl\n";
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
#### C#
|
|
394
|
+
|
|
395
|
+
Use `csharp` or `cs` for C# examples.
|
|
396
|
+
|
|
397
|
+
**Prerequisites:**
|
|
398
|
+
* `mono` (includes `mcs` compiler) must be installed and available in your system path.
|
|
399
|
+
* **macOS**: `brew install mono`
|
|
400
|
+
* **Ubuntu/Debian**: `sudo apt-get install mono-devel`
|
|
401
|
+
|
|
402
|
+
**Execution Model:**
|
|
403
|
+
Compiles the code using `mcs` into a temporary executable, then runs it with `mono`.
|
|
404
|
+
|
|
405
|
+
**Recipe:**
|
|
406
|
+
|
|
407
|
+
1. Use `csharp` fenced code blocks.
|
|
408
|
+
2. You can provide a full class (e.g. `public class Program { ... }`) OR a simple snippet.
|
|
409
|
+
3. Simple snippets are automatically wrapped in a `public class Program { public static void Main(string[] args) { ... } }` and include `using System;`.
|
|
410
|
+
4. **Note**: `<!-- share-code-between-examples -->` is **not** supported for C#.
|
|
411
|
+
|
|
412
|
+
**Simple Snippet:**
|
|
413
|
+
<!-- skip-example -->
|
|
414
|
+
```csharp
|
|
415
|
+
Console.WriteLine("Hello C#");
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
**Full Class:**
|
|
419
|
+
<!-- skip-example -->
|
|
420
|
+
```csharp
|
|
421
|
+
using System;
|
|
422
|
+
public class Test {
|
|
423
|
+
public static void Main(string[] args) {
|
|
424
|
+
Console.WriteLine("Hello from Test class");
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
#### R
|
|
430
|
+
|
|
431
|
+
Use `r` for R examples.
|
|
432
|
+
|
|
433
|
+
**Prerequisites:**
|
|
434
|
+
* `Rscript` must be installed and available in your system path.
|
|
435
|
+
* **macOS**: `brew install r`
|
|
436
|
+
* **Ubuntu/Debian**: `sudo apt-get install r-base`
|
|
437
|
+
|
|
438
|
+
**Execution Model:**
|
|
439
|
+
Spawns an `Rscript` subprocess with the code piped to stdin.
|
|
440
|
+
|
|
441
|
+
**Recipe:**
|
|
442
|
+
|
|
443
|
+
1. Use `r` fenced code blocks.
|
|
444
|
+
2. Standard R syntax works out of the box.
|
|
445
|
+
3. State can be shared between blocks using `<!-- share-code-between-examples -->`.
|
|
446
|
+
|
|
447
|
+
```r
|
|
448
|
+
print("Hello R")
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
#### Pascal
|
|
452
|
+
|
|
453
|
+
Use `pascal` or `pas` for Pascal examples.
|
|
454
|
+
|
|
455
|
+
**Prerequisites:**
|
|
456
|
+
* `fpc` (Free Pascal Compiler) must be installed and available in your system path.
|
|
457
|
+
* **macOS**: `brew install fpc`
|
|
458
|
+
* **Ubuntu/Debian**: `sudo apt-get install fpc`
|
|
459
|
+
|
|
460
|
+
**Execution Model:**
|
|
461
|
+
Compiles the code using `fpc` into a temporary executable, then runs it.
|
|
462
|
+
|
|
463
|
+
**Recipe:**
|
|
464
|
+
|
|
465
|
+
1. Use `pascal` fenced code blocks.
|
|
466
|
+
2. You can provide a full program (including `program Name;`) OR a simple snippet.
|
|
467
|
+
3. Simple snippets are automatically wrapped in a `program TestProgram; begin ... end.` block.
|
|
468
|
+
4. **Note**: `<!-- share-code-between-examples -->` is **not** supported for Pascal.
|
|
469
|
+
|
|
470
|
+
**Simple Snippet:**
|
|
471
|
+
<!-- skip-example -->
|
|
472
|
+
```pascal
|
|
473
|
+
writeln('Hello Pascal');
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
**Full Program:**
|
|
477
|
+
<!-- skip-example -->
|
|
478
|
+
```pascal
|
|
479
|
+
program Hello;
|
|
480
|
+
begin
|
|
481
|
+
writeln('Hello from Pascal program');
|
|
482
|
+
end.
|
|
483
|
+
```
|
|
484
|
+
|
|
278
485
|
### Skipping Examples
|
|
279
486
|
|
|
280
487
|
To skip a specific code block, add the `<!-- skip-example -->` comment immediately before it:
|
|
@@ -289,7 +496,7 @@ To skip a specific code block, add the `<!-- skip-example -->` comment immediate
|
|
|
289
496
|
|
|
290
497
|
By default, each code block is executed in isolation. To share state (variables, functions, classes) between multiple code blocks in the same file, add the `<!-- share-code-between-examples -->` comment. This applies to the entire file.
|
|
291
498
|
|
|
292
|
-
> **Note**: This feature is currently supported for JavaScript, TypeScript, Python, and
|
|
499
|
+
> **Note**: This feature is currently supported for JavaScript, TypeScript, Python, Shell, BASIC, Perl, R, Go, Rust, Fortran, and C, but **not** compiled languages like COBOL, Java, C#, and Pascal.
|
|
293
500
|
|
|
294
501
|
<!-- share-code-between-examples -->
|
|
295
502
|
|
|
@@ -302,6 +509,93 @@ By default, each code block is executed in isolation. To share state (variables,
|
|
|
302
509
|
assert x == 10
|
|
303
510
|
```
|
|
304
511
|
|
|
512
|
+
### Configuration Comments
|
|
513
|
+
|
|
514
|
+
You can configure execution behavior for specific snippets using comments immediately preceding the code block.
|
|
515
|
+
|
|
516
|
+
**Arguments:** Pass arguments to the compiler or interpreter.
|
|
517
|
+
|
|
518
|
+
<!-- args: -v -->
|
|
519
|
+
```python
|
|
520
|
+
import sys
|
|
521
|
+
print("Running with verbose output")
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
**Environment Variables:** Set environment variables for the execution.
|
|
525
|
+
|
|
526
|
+
<!-- env: API_KEY=secret123 MODE=test -->
|
|
527
|
+
```bash
|
|
528
|
+
echo "Key: $API_KEY"
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
### Output Verification
|
|
532
|
+
|
|
533
|
+
You can verify that a code snippet produces specific output by assigning it an ID and then referencing that ID in a subsequent block.
|
|
534
|
+
|
|
535
|
+
1. Assign an ID to the code snippet using `<!-- id: my-snippet-name -->`.
|
|
536
|
+
2. Create an output block (usually `text` or `json`) and reference the ID using `<!-- output: my-snippet-name -->`.
|
|
537
|
+
|
|
538
|
+
`doccident` will execute the first snippet, capture its stdout, and verify that it matches the content of the output block.
|
|
539
|
+
|
|
540
|
+
**Example:**
|
|
541
|
+
|
|
542
|
+
<!-- id: hello-world -->
|
|
543
|
+
```js
|
|
544
|
+
console.log("Hello Output");
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
<!-- output: hello-world -->
|
|
548
|
+
```text
|
|
549
|
+
Hello Output
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
This is useful for ensuring your documentation's "Output:" sections stay in sync with the actual code behavior.
|
|
553
|
+
|
|
554
|
+
### Output Matching Modes
|
|
555
|
+
|
|
556
|
+
When verifying output, you can optionally specify a matching mode to handle whitespace or dynamic content.
|
|
557
|
+
|
|
558
|
+
* `exact` (default): Exact string match (trailing whitespace is trimmed).
|
|
559
|
+
* `ignore-whitespace`: Collapses all whitespace sequences to a single space and trims ends before comparing.
|
|
560
|
+
* `match:regex`: Treats the output block content as a regular expression.
|
|
561
|
+
|
|
562
|
+
**Examples:**
|
|
563
|
+
|
|
564
|
+
**Ignore Whitespace:**
|
|
565
|
+
|
|
566
|
+
<!-- id: fuzzy-output -->
|
|
567
|
+
```js
|
|
568
|
+
console.log(" A B \n C ");
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
<!-- output: fuzzy-output ignore-whitespace -->
|
|
572
|
+
```text
|
|
573
|
+
A B C
|
|
574
|
+
```
|
|
575
|
+
|
|
576
|
+
**Regex Matching:**
|
|
577
|
+
|
|
578
|
+
<!-- id: dynamic-output -->
|
|
579
|
+
```js
|
|
580
|
+
console.log("Timestamp: " + Date.now());
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
<!-- output: dynamic-output match:regex -->
|
|
584
|
+
```text
|
|
585
|
+
^Timestamp: \d+$
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
### Updating Output (Snapshots)
|
|
589
|
+
|
|
590
|
+
If you want `doccident` to automatically update your output blocks with the actual output from execution, run with the `--update-output` flag.
|
|
591
|
+
|
|
592
|
+
<!-- skip-example -->
|
|
593
|
+
```bash
|
|
594
|
+
npx doccident --update-output
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
This will replace the content of any `<!-- output: ... -->` block with the latest captured output from the corresponding ID. This is extremely useful when writing documentation: you can write the code example, add a placeholder output block, and let `doccident` fill it in for you.
|
|
598
|
+
|
|
305
599
|
## Configuration
|
|
306
600
|
|
|
307
601
|
Create a `.doccident-setup.js` file in your project root to configure the test environment.
|
|
@@ -310,6 +604,7 @@ Create a `.doccident-setup.js` file in your project root to configure the test e
|
|
|
310
604
|
|
|
311
605
|
If your examples use external libraries, provide them here. This allows your examples to `require` modules just like users would:
|
|
312
606
|
|
|
607
|
+
<!-- skip-example -->
|
|
313
608
|
```javascript
|
|
314
609
|
// .doccident-setup.js
|
|
315
610
|
module.exports = {
|
|
@@ -325,6 +620,7 @@ module.exports = {
|
|
|
325
620
|
|
|
326
621
|
Define global variables available to all snippets:
|
|
327
622
|
|
|
623
|
+
<!-- skip-example -->
|
|
328
624
|
```javascript
|
|
329
625
|
module.exports = {
|
|
330
626
|
globals: {
|
|
@@ -350,13 +646,13 @@ module.exports = {
|
|
|
350
646
|
* Reads Markdown content line-by-line.
|
|
351
647
|
* Uses a robust state machine to identify code fences and control comments (like `skip-example`).
|
|
352
648
|
* Extracts valid snippets into structured objects containing code, file paths, and line numbers.
|
|
353
|
-
* **Multi-Language Support**: Recognizes `js`, `ts`, `python`, `shell`, `go`, `rust`, `fortran`, `cobol`, and `
|
|
649
|
+
* **Multi-Language Support**: Recognizes `js`, `ts`, `python`, `shell`, `go`, `rust`, `fortran`, `cobol`, `c`, `basic`, `java`, `perl`, `csharp`, `r`, and `pascal` blocks.
|
|
354
650
|
|
|
355
651
|
2. **Test Runner (`src/doctest.ts`)**
|
|
356
652
|
* The orchestrator of the application.
|
|
357
653
|
* Iterates through parsed snippets and manages the execution lifecycle.
|
|
358
654
|
* **Sandboxing**: Uses Node.js's `vm` module (`runInNewContext`) to execute JS/TS code in isolation.
|
|
359
|
-
* **Subprocess Execution**: Spawns `python3`, `go`, `rustc`, `gfortran`, `cobc`, `gcc`, or shell subprocesses for non-JS languages.
|
|
655
|
+
* **Subprocess Execution**: Spawns `python3`, `go`, `rustc`, `gfortran`, `cobc`, `gcc`, `cbmbasic`, `javac`/`java`, `perl`, `mcs`/`mono`, `Rscript`, `fpc`, or shell subprocesses for non-JS languages.
|
|
360
656
|
* **Transformation**: Uses **esbuild** to compile modern JavaScript and TypeScript code down to a compatible format before execution.
|
|
361
657
|
|
|
362
658
|
3. **Reporter (`src/reporter.ts`)**
|
|
@@ -369,3 +665,33 @@ module.exports = {
|
|
|
369
665
|
* Utility functions (`src/utils.ts`) provide common helpers.
|
|
370
666
|
|
|
371
667
|
This separation of concerns allows `doccident` to be easily extended—for example, by adding new parsers for different documentation formats or custom reporters for CI environments.
|
|
668
|
+
|
|
669
|
+
## License & History
|
|
670
|
+
|
|
671
|
+
This project is licensed under the Apache License, Version 2.0.
|
|
672
|
+
|
|
673
|
+
Copyright (c) 2025 Tim O'Brien
|
|
674
|
+
|
|
675
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
676
|
+
you may not use this file except in compliance with the License.
|
|
677
|
+
You may obtain a copy of the License at
|
|
678
|
+
|
|
679
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
680
|
+
|
|
681
|
+
Unless required by applicable law or agreed to in writing, software
|
|
682
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
683
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
684
|
+
See the License for the specific language governing permissions and
|
|
685
|
+
limitations under the License.
|
|
686
|
+
|
|
687
|
+
---
|
|
688
|
+
|
|
689
|
+
**Note**: This project was originally forked from [markdown-doctest](https://github.com/nick-johnstone/markdown-doctest) by Nick Johnstone. While the original inspiration and some core concepts remain, the **entire codebase has been replaced** and significantly expanded by Tim O'Brien to support a wide range of compiled and interpreted languages, shared state mechanisms, and output verification. All subsequent additions and modifications are covered by the Apache 2.0 Software License.
|
|
690
|
+
|
|
691
|
+
## Agentic AI Guide
|
|
692
|
+
|
|
693
|
+
This repository contains a dedicated guide for AI agents to understand how to use `doccident`. If you are an AI model tasked with maintaining this project or using it to verify documentation, please refer to:
|
|
694
|
+
|
|
695
|
+
[**doccident/guide/index.md**](./guide/index.md)
|
|
696
|
+
|
|
697
|
+
This directory contains instructions on capabilities, verification workflows, and configuration specifically structured for machine consumption.
|