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

add support for code block execution options #14

Merged
merged 58 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
978144b
update README
maxim-uvarov Feb 27, 2024
d0b73fe
update comments
maxim-uvarov Feb 27, 2024
0ae9c38
README edit
maxim-uvarov Mar 3, 2024
4f95d26
update make-release
maxim-uvarov Mar 3, 2024
30ab734
don't use echo with 'use'
maxim-uvarov Mar 4, 2024
4e5fd46
update README
maxim-uvarov Mar 12, 2024
d08fcc0
check the exit code of nu earlier
maxim-uvarov Mar 12, 2024
0ddb437
code style
maxim-uvarov Mar 12, 2024
cccf88e
use guard in match
maxim-uvarov Mar 12, 2024
bec8fbd
assemble-script to not fail with no lines
maxim-uvarov Mar 12, 2024
c78ed81
fix
maxim-uvarov Mar 12, 2024
9672027
classify-lines wip
maxim-uvarov Mar 12, 2024
9149d02
chore: reword code
maxim-uvarov Mar 18, 2024
7ff79eb
add some comments
maxim-uvarov Mar 18, 2024
7ea2f34
chunk-options support
maxim-uvarov Mar 22, 2024
6ce34fb
update examples to chunk-options
maxim-uvarov Mar 22, 2024
5ba38e8
nu-highligh refactor
maxim-uvarov Mar 22, 2024
b3c6452
update examples
maxim-uvarov Mar 22, 2024
867b1da
make no-output instead of print
maxim-uvarov Mar 22, 2024
83cca47
update examples
maxim-uvarov Mar 22, 2024
6ed847f
make execute-code more clear
maxim-uvarov Mar 22, 2024
3aad464
remove redundant --dont-handle-errors
maxim-uvarov Mar 22, 2024
83a204b
rename dont-save into no-save
maxim-uvarov Mar 22, 2024
922fd79
don't substitute original block fence
maxim-uvarov Mar 22, 2024
0c6b2d9
simplify classify-lines
maxim-uvarov Mar 22, 2024
75f06ff
update examples
maxim-uvarov Mar 22, 2024
8450eda
try \n insteado of (char nl)
maxim-uvarov Mar 22, 2024
a14d7b5
Revert "try \n insteado of (char nl)"
maxim-uvarov Mar 22, 2024
449169a
simplify clasify-lines
maxim-uvarov Mar 23, 2024
47fa47d
use Amtoine style
maxim-uvarov Mar 23, 2024
b59d982
replace optout --quiet with optin --echo
maxim-uvarov Mar 23, 2024
8aad47c
remove artifacts
maxim-uvarov Mar 24, 2024
2b1bf69
use long flag names
maxim-uvarov Mar 24, 2024
74af455
rename vars, comments
maxim-uvarov Mar 24, 2024
9016610
add testing.nu
maxim-uvarov Mar 24, 2024
dd58b26
feat: add outputing changes
maxim-uvarov Mar 24, 2024
975e8a4
update testing.nu
maxim-uvarov Mar 24, 2024
17acecc
add tstamp
maxim-uvarov Mar 25, 2024
dc2685f
add path-modify
maxim-uvarov Mar 25, 2024
7b98106
update backup-file
maxim-uvarov Mar 25, 2024
a03151a
big refactor
maxim-uvarov Mar 25, 2024
2bf9ddf
add --stop-on-error
maxim-uvarov Mar 25, 2024
f594a12
add run-itermid-script
maxim-uvarov Mar 25, 2024
aaaf427
add parse-options-from-fence
maxim-uvarov Mar 25, 2024
abc2adb
refactor gen-execute-code
maxim-uvarov Mar 25, 2024
d64dc62
refacto gen-highlight-command
maxim-uvarov Mar 25, 2024
69501ab
refactor gen-intermid-script
maxim-uvarov Mar 25, 2024
1bf93c6
refactor main, rename variables
maxim-uvarov Mar 25, 2024
a6637f6
add command definitions
maxim-uvarov Mar 25, 2024
38d7d96
update testing.nu
maxim-uvarov Mar 25, 2024
be57d8f
update nu-utils
maxim-uvarov Mar 25, 2024
6db0e30
remove nudoc source (move to dotnu)
maxim-uvarov Mar 25, 2024
8c91f40
add table -e to capture
maxim-uvarov Mar 25, 2024
6be5831
add gen-fence-nudoc-output
maxim-uvarov Mar 25, 2024
c4cee8c
move comment out
maxim-uvarov Mar 25, 2024
764e73c
$in.stderr without ?
maxim-uvarov Mar 25, 2024
7652c9a
update exmples
maxim-uvarov Mar 25, 2024
cfe7acb
0.0.6 nupm version
maxim-uvarov Mar 25, 2024
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
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ nudoc is inspired by [R Markdown](https://bookdown.org/yihui/rmarkdown/basics.ht

```
git clone https://github.com/nushell101/nudoc; cd nudoc
# nupm install --force --path . # optionally you can install this module via nupm
use nudoc
nudoc run --quiet README.md
```

🗒 The code above isn't executed and updated by nudoc, as it lacks an opening ` ```nushell ` language idnetifier in the opening code fence tag.
> **Note**
> The code above isn't executed and updated by nudoc, as it lacks an opening ` ```nushell ` language idnetifier in the opening code fence tag.

## How it works

1. The `nudoc run` command opens a file from the first argument.
2. It looks for ` ```nushell ` code chunks.
`nudoc run` parses the initial file, generates a script to execute the found commands, executes this script in a new nushell instance, parses the results, updates the initial document accordingly and output the resulting document into terminal.

### Details of parsing

2. `nudoc` looks for ` ```nushell ` or ` ```nu ` code chunks.
3. In the code chunks, that entirely don't have lines starting with the `>` symbol, nudoc executes the whole code chunks as they are, and if they produce any output (like in `print 'this'`), then the output is written in the ` ```nudoc-output ` chunks, next to the executed code chunks.
4. In the code chunks that contain one or more lines starting with `>` symbol, nudoc filters only lines that start with the `>` or `#` symbol, executes those lines one by one and output their results just after the executed line.
5. nudoc outputs results code chunks and results of their execution into the terminal (if the `--quiet` flag is not used).
6. nudoc updates results in the file, which was provided as the first argument (user needs to confirm overwriting the file, if the `--overwrite` flag wasn't used).

```nushell
# Eventually, the script updates nushell code chunks.
Expand All @@ -46,7 +48,7 @@ Sun, 18 Feb 2024 15:32:52 +0000 (now)
## Examples

```
nudoc run examples/types_of_data.md examples/types_of_data_out.md --overwrite --intermid_script examples/types_of_data_intermid.nu
nudoc run examples/types_of_data.md examples/types_of_data_out.md --overwrite --intermid_script examples/types_of_data.md_intermid.nu
```

The results of the command above are provided in the files of the example folder.
2 changes: 1 addition & 1 deletion examples/types_of_data.md
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ git checkout featurebranch | null

`null` is not the same as the absence of a value! It is possible for a table to be produced that has holes in some of its rows. Attempting to access this value will not produce `null`, but instead cause an error:

```nushell
```nushell try,new-instance
> [{a:1 b:2} {b:1}]
╭───┬────┬───╮
│ # │ a │ b │
Expand Down
163 changes: 97 additions & 66 deletions examples/types_of_data.md_intermid.nu
Original file line number Diff line number Diff line change
@@ -1,141 +1,169 @@
# this script was generated automatically using nudochttps://github.com/nushell-prophet/nudoc

print `###nudoc-block-1`
# this script was generated automatically using nudoc
# https://github.com/nushell-prophet/nudoc
print "###nudoc-block-1"
print "```nushell"
print ("> 42 | describe" | nu-highlight)
do {nu -c "42 | describe"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
42 | describe | echo $in

print `###nudoc-block-4`
print "###nudoc-block-4"
print "```nushell"
print ("> \"-5\" | into int" | nu-highlight)
do {nu -c "\"-5\" | into int"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
"-5" | into int | echo $in

print `###nudoc-block-7`
print "###nudoc-block-7"
print "```nushell"
print ("> \"1.2\" | into float" | nu-highlight)
do {nu -c "\"1.2\" | into float"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
"1.2" | into float | echo $in

print `###nudoc-block-10`
print "###nudoc-block-10"
print "```nushell"
print ("> let mybool = 2 > 1" | nu-highlight)
let mybool = 2 > 1

print ("> $mybool" | nu-highlight)
try {$mybool} catch {|e| $e} | echo $in
$mybool | echo $in

print ("> let mybool = ($env.HOME | path exists)" | nu-highlight)
let mybool = ($env.HOME | path exists)

print ("> $mybool" | nu-highlight)
try {$mybool} catch {|e| $e} | echo $in
$mybool | echo $in

print `###nudoc-block-13`
print "###nudoc-block-13"
print "```nushell"
print ("> 3.14day" | nu-highlight)
do {nu -c "3.14day"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
3.14day | echo $in

print `###nudoc-block-16`
print "###nudoc-block-16"
print "```nushell"
print ("> 30day / 1sec # How many seconds in 30 days?" | nu-highlight)
do {nu -c "30day / 1sec"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
30day / 1sec | echo $in

print `###nudoc-block-19`
print "###nudoc-block-19"
print "```nushell"
print ("> 1Gb / 1b" | nu-highlight)
do {nu -c "1Gb / 1b"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
1Gb / 1b | echo $in

print ("> 1Gib / 1b" | nu-highlight)
do {nu -c "1Gib / 1b"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
1Gib / 1b | echo $in

print ("> (1Gib / 1b) == 2 ** 30" | nu-highlight)
do {nu -c "(1Gib / 1b) == 2 ** 30"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
(1Gib / 1b) == 2 ** 30 | echo $in

print `###nudoc-block-22`
print "###nudoc-block-22"
print "```nushell"
print ("> 0x[1F FF] # Hexadecimal" | nu-highlight)
do {nu -c "0x[1F FF]"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
0x[1F FF] | echo $in

print ("> 0b[1 1010] # Binary" | nu-highlight)
do {nu -c "0b[1 1010]"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
0b[1 1010] | echo $in

print ("> 0o[377] # Octal" | nu-highlight)
do {nu -c "0o[377]"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
0o[377] | echo $in

print `###nudoc-block-25`
print "###nudoc-block-25"
print "```nushell"
print ("> {name: sam rank: 10}" | nu-highlight)
do {nu -c "{name: sam rank: 10}"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
{name: sam rank: 10} | echo $in

print `###nudoc-block-28`
print "###nudoc-block-28"
print "```nushell"
print ("> {x:3 y:1} | insert z 0" | nu-highlight)
do {nu -c "{x:3 y:1} | insert z 0"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
{x:3 y:1} | insert z 0 | echo $in

print `###nudoc-block-31`
print "###nudoc-block-31"
print "```nushell"
print ("> {name: sam, rank: 10} | transpose key value" | nu-highlight)
do {nu -c "{name: sam, rank: 10} | transpose key value"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
{name: sam, rank: 10} | transpose key value | echo $in

print `###nudoc-block-34`
print "###nudoc-block-34"
print "```nushell"
print ("> {x:12 y:4}.x" | nu-highlight)
do {nu -c "{x:12 y:4}.x"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
{x:12 y:4}.x | echo $in

print `###nudoc-block-37`
print "###nudoc-block-37"
print "```nushell"
print ("> {\"1\":true \" \":false}.\" \"" | nu-highlight)
do {nu -c "{\"1\":true \" \":false}.\" \""} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
{"1":true " ":false}." " | echo $in

print `###nudoc-block-40`
print "###nudoc-block-40"
print "```nushell"
print ("> let data = { name: alice, age: 50 }" | nu-highlight)
let data = { name: alice, age: 50 }

print ("> { ...$data, hobby: cricket }" | nu-highlight)
try {{ ...$data, hobby: cricket }} catch {|e| $e} | echo $in
{ ...$data, hobby: cricket } | echo $in

print `###nudoc-block-43`
print "###nudoc-block-43"
print "```nushell"
print ("> [sam fred george]" | nu-highlight)
do {nu -c "[sam fred george]"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
[sam fred george] | echo $in

print `###nudoc-block-46`
print "###nudoc-block-46"
print "```nushell"
print ("> [bell book candle] | where ($it =~ 'b')" | nu-highlight)
try {[bell book candle] | where ($it =~ 'b')} catch {|e| $e} | echo $in
[bell book candle] | where ($it =~ 'b') | echo $in

print `###nudoc-block-49`
print "###nudoc-block-49"
print "```nushell"
print ("> [a b c].1" | nu-highlight)
do {nu -c "[a b c].1"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
[a b c].1 | echo $in

print `###nudoc-block-52`
print "###nudoc-block-52"
print "```nushell"
print ("> [a b c d e f] | range 1..3" | nu-highlight)
do {nu -c "[a b c d e f] | range 1..3"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
[a b c d e f] | range 1..3 | echo $in

print `###nudoc-block-55`
print "###nudoc-block-55"
print "```nushell"
print ("> let x = [1 2]" | nu-highlight)
let x = [1 2]

print ("> [...$x 3 ...(4..7 | take 2)]" | nu-highlight)
try {[...$x 3 ...(4..7 | take 2)]} catch {|e| $e} | echo $in
[...$x 3 ...(4..7 | take 2)] | echo $in

print `###nudoc-block-58`
print "###nudoc-block-58"
print "```nushell"
print ("> [[column1, column2]; [Value1, Value2] [Value3, Value4]]" | nu-highlight)
do {nu -c "[[column1, column2]; [Value1, Value2] [Value3, Value4]]"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
[[column1, column2]; [Value1, Value2] [Value3, Value4]] | echo $in

print `###nudoc-block-61`
print "###nudoc-block-61"
print "```nushell"
print ("> [{name: sam, rank: 10}, {name: bob, rank: 7}]" | nu-highlight)
do {nu -c "[{name: sam, rank: 10}, {name: bob, rank: 7}]"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
[{name: sam, rank: 10}, {name: bob, rank: 7}] | echo $in

print `###nudoc-block-64`
print "###nudoc-block-64"
print "```nushell"
print ("> [{x:12, y:5}, {x:3, y:6}] | get 0" | nu-highlight)
do {nu -c "[{x:12, y:5}, {x:3, y:6}] | get 0"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
[{x:12, y:5}, {x:3, y:6}] | get 0 | echo $in

print `###nudoc-block-67`
print "###nudoc-block-67"
print "```nushell"
print ("> [[x,y];[12,5],[3,6]] | get 0" | nu-highlight)
do {nu -c "[[x,y];[12,5],[3,6]] | get 0"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
[[x,y];[12,5],[3,6]] | get 0 | echo $in

print `###nudoc-block-70`
print "###nudoc-block-70"
print "```nushell"
print ("> [{x:12 y:5} {x:4 y:7} {x:2 y:2}].x" | nu-highlight)
do {nu -c "[{x:12 y:5} {x:4 y:7} {x:2 y:2}].x"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
[{x:12 y:5} {x:4 y:7} {x:2 y:2}].x | echo $in

print `###nudoc-block-73`
print "###nudoc-block-73"
print "```nushell"
print ("> [{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select y z" | nu-highlight)
do {nu -c "[{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select y z"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
[{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select y z | echo $in

print `###nudoc-block-76`
print "###nudoc-block-76"
print "```nushell"
print ("> [{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select 1 2" | nu-highlight)
do {nu -c "[{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select 1 2"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
[{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select 1 2 | echo $in

print `###nudoc-block-79`
print "###nudoc-block-79"
print "```nushell"
print ("> [{foo: 123}, {}].foo?" | nu-highlight)
do {nu -c "[{foo: 123}, {}].foo?"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in
[{foo: 123}, {}].foo? | echo $in

print `###nudoc-block-82`
print "###nudoc-block-82"
print "```nushell"
print ("# Assign a closure to a variable
let greet = { |name| print $\"Hello ($name)\"}
do $greet \"Julian\"" | nu-highlight)
Expand All @@ -145,7 +173,8 @@ print '```
let greet = { |name| print $"Hello ($name)"}
do $greet "Julian" | echo $in

print `###nudoc-block-85`
print "###nudoc-block-85"
print "```nushell"
print ("mut x = 1
if true {
$x += 1000
Expand All @@ -159,13 +188,15 @@ if true {
}
print $x | echo $in

print `###nudoc-block-88`
print "###nudoc-block-88"
print "```nushell"
print ("git checkout featurebranch | null" | nu-highlight)
print '```
```nudoc-output'
git checkout featurebranch | null | echo $in
git checkout featurebranch | null

print `###nudoc-block-91`
print "###nudoc-block-91"
print "```nushell try,new-instance"
print ("> [{a:1 b:2} {b:1}]" | nu-highlight)
do {nu -c "[{a:1 b:2} {b:1}]"} | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | echo $in

Expand Down
6 changes: 2 additions & 4 deletions examples/types_of_data_out.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,8 @@ As with durations, you can make fractional file sizes, and do calculations:
```nushell
> 1Gb / 1b
1000000000

> 1Gib / 1b
1073741824

> (1Gib / 1b) == 2 ** 30
true
```
Expand Down Expand Up @@ -511,7 +509,7 @@ git checkout featurebranch | null

`null` is not the same as the absence of a value! It is possible for a table to be produced that has holes in some of its rows. Attempting to access this value will not produce `null`, but instead cause an error:

```nushell
```nushell try,new-instance
> [{a:1 b:2} {b:1}]
╭───┬────┬───╮
│ # │ a │ b │
Expand All @@ -524,7 +522,7 @@ git checkout featurebranch | null
Error: nu::shell::column_not_found

× Cannot find column
╭─[source:1:1]
╭─[source:1:12]
1 │ [{a:1 b:2} {b:1}].1.a
· ──┬── ┬
· │ ╰── cannot find column 'a'
Expand Down
Loading