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 printing markdown text into intermid-script #46

Merged
merged 21 commits into from
Jun 25, 2024
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Usage:

Flags:
-o, --result-md-path <Filepath> - path to a resulting `.md` file; if omitted, updates the original file
--print-block-results - print the block's execution results immediately.
--echo - output resulting markdown to the terminal
--save-ansi - save ANSI formatted version
--no-backup - overwrite the existing `.md` file without backup
Expand Down Expand Up @@ -234,7 +235,7 @@ Input/output types:
╰─────────────────name──────────────────┴─type─╯

> sys host | get boot_time
Fri, 24 May 2024 07:47:15 +0000 (a month ago)
Fri, 24 May 2024 07:47:14 +0000 (a month ago)

> 2 + 2
4
Expand Down
15 changes: 12 additions & 3 deletions examples/1_simple_markdown/simple_markdown.md_intermid.nu
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# this script was generated automatically using numd
# https://github.com/nushell-prophet/numd
const init_numd_pwd_const = '/Users/user/git/numd'
print "#code-block-starting-line-in-original-md-7"
"# This is a simple markdown example

## Example 1

the block below will be executed as it is, but won't yield any output
" | print
print "```nu"
print ("let $var1 = 'foo'" | nu-highlight)

Expand All @@ -11,7 +16,9 @@ let $var1 = 'foo'

print "```"

print "#code-block-starting-line-in-original-md-13"
"
## Example 2
" | print
print "```nu"
print ("# This block will produce some output in a separate block
$var1 | path join 'baz' 'bar'" | nu-highlight)
Expand All @@ -23,7 +30,9 @@ $var1 | path join 'baz' 'bar' | print; print ''

print "```"

print "#code-block-starting-line-in-original-md-23"
"
## Example 3
" | print
print "```nu"
print ("# This block will output results inline" | nu-highlight)

Expand Down
1 change: 0 additions & 1 deletion examples/1_simple_markdown/simple_nu_table.md_intermid.nu
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# this script was generated automatically using numd
# https://github.com/nushell-prophet/numd
const init_numd_pwd_const = '/Users/user/git/numd'
print "#code-block-starting-line-in-original-md-1"
print "```nushell"
print ("[[a b c]; [1 2 3]]" | nu-highlight)

Expand Down
128 changes: 53 additions & 75 deletions examples/2_numd_commands_explanations/numd_commands_explanations.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ Output:
// # this script was generated automatically using numd
// # https://github.com/nushell-prophet/numd
// const init_numd_pwd_const = '/Users/user/git/numd'
// print "#code-block-starting-line-in-original-md-7"
// "# This is a simple markdown example
//
// ## Example 1
//
// the block below will be executed as it is, but won't yield any output
// " | print
// print "```nu"
// print ("let $var1 = 'foo'" | nu-highlight)
//
Expand All @@ -96,7 +101,9 @@ Output:
//
// print "```"
//
// print "#code-block-starting-line-in-original-md-13"
// "
// ## Example 2
// " | print
// print "```nu"
// print ("# This block will produce some output in a separate block
// $var1 | path join 'baz' 'bar'" | nu-highlight)
Expand All @@ -108,7 +115,9 @@ Output:
//
// print "```"
//
// print "#code-block-starting-line-in-original-md-23"
// "
// ## Example 3
// " | print
// print "```nu"
// print ("# This block will output results inline" | nu-highlight)
//
Expand Down Expand Up @@ -140,79 +149,47 @@ $nu_res_stdout_lines
Output:

```
// ╭───────────────────────────────────────────────────────────╮
// │ #code-block-starting-line-in-original-md-7 │
// │ ```nu │
// │ let $var1 = 'foo' │
// │ ``` │
// │ ```output-numd │
// │ ``` │
// │ #code-block-starting-line-in-original-md-13 │
// │ ```nu │
// │ # This block will produce some output in a separate block │
// │ $var1 | path join 'baz' 'bar' │
// │ ``` │
// │ ```output-numd │
// │ foo/baz/bar │
// │ │
// │ ``` │
// │ #code-block-starting-line-in-original-md-23 │
// │ ```nu │
// │ # This block will output results inline │
// │ > whoami │
// │ user │
// │ │
// │ > 2 + 2 │
// │ 4 │
// │ │
// │ ``` │
// ╰───────────────────────────────────────────────────────────╯
// ╭───────────────────────────────────────────────────────────────────────╮
// │ # This is a simple markdown example │
// │ │
// │ ## Example 1 │
// │ │
// │ the block below will be executed as it is, but won't yield any output │
// │ │
// │ ```nu │
// │ let $var1 = 'foo' │
// │ ``` │
// │ ```output-numd │
// │ ``` │
// │ │
// │ ## Example 2 │
// │ │
// │ ```nu │
// │ # This block will produce some output in a separate block │
// │ $var1 | path join 'baz' 'bar' │
// │ ``` │
// │ ```output-numd │
// │ foo/baz/bar │
// │ │
// │ ``` │
// │ │
// │ ## Example 3 │
// │ │
// │ ```nu │
// │ # This block will output results inline │
// │ > whoami │
// │ user │
// │ │
// │ > 2 + 2 │
// │ 4 │
// │ │
// │ ``` │
// ╰───────────────────────────────────────────────────────────────────────╯
```

## parse-block-index

The `parse-block-index` command parses the captured output, and groups them by executed blocks.

```nu indent-output
let $nu_res_with_block_index = parse-block-index $nu_res_stdout_lines
$nu_res_with_block_index
```

Output:

```
// ╭─block_line─┬───────────────────────────line────────────────────────────╮
// │ 7 │ ```nu │
// │ │ let $var1 = 'foo' │
// │ │ ``` │
// │ │ ```output-numd │
// │ │ ``` │
// │ 13 │ ```nu │
// │ │ # This block will produce some output in a separate block │
// │ │ $var1 | path join 'baz' 'bar' │
// │ │ ``` │
// │ │ ```output-numd │
// │ │ foo/baz/bar │
// │ │ │
// │ │ ``` │
// │ 23 │ ```nu │
// │ │ # This block will output results inline │
// │ │ > whoami │
// │ │ user │
// │ │ │
// │ │ > 2 + 2 │
// │ │ 4 │
// │ │ │
// │ │ ``` │
// ╰─block_line─┴───────────────────────────line────────────────────────────╯
```

## assemble-markdown

The `assemble-markdown` command cleans outdated commands outputs in the `$md_orig_table` and combines them with `$nu_res_with_block_index` (the variable from the previous step). Additionally, `prettify-markdown` is used here to remove empty blocks and unnecessary empty lines.

```nu indent-output
let $md_res = assemble-markdown $md_orig_table $nu_res_with_block_index
let $md_res = $nu_res_stdout_lines
| str join (char nl)
| prettify-markdown

$md_res
Expand Down Expand Up @@ -250,6 +227,7 @@ Output:
//
// > 2 + 2
// 4
//
// ```
```

Expand All @@ -267,8 +245,8 @@ Output:
// ╭──────────────────┬────────────────────╮
// │ filename │ simple_markdown.md │
// │ nushell_blocks │ 3 │
// │ levenshtein_dist │ 0
// │ diff_lines │ 0%
// │ levenshtein_dist │ 2
// │ diff_lines │ +1 (3.3%)
// │ diff_words │ 0% │
// │ diff_chars │ 0% │
// ╰──────────────────┴────────────────────╯
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# this script was generated automatically using numd
# https://github.com/nushell-prophet/numd
const init_numd_pwd_const = '/Users/user/git/numd'
print "#code-block-starting-line-in-original-md-5"
"# numd commands explanation

In the code block below, we set settings and variables for executing this entire document.
" | print
print "```nu"
print ("# This setting is for overriding the author's usual small number of `abbreviated_row_count`.
$env.config.table.abbreviated_row_count = 100
Expand All @@ -25,7 +28,13 @@ use ($init_numd_pwd_const | path join numd nu-utils numd-internals.nu) *

print "```"

print "#code-block-starting-line-in-original-md-22"
"
## numd-internals.nu

### detect-code-blocks

This command is used for parsing initial markdown to detect executable code blocks.
" | print
print "```nu indent-output"
print ("# Here we set the `$file` variable (which will be used in several commands throughout this script) to point to `examples/1_simple_markdown/simple_markdown.md`.
let $file = $init_numd_pwd_const | path join examples 1_simple_markdown simple_markdown.md
Expand All @@ -45,7 +54,11 @@ $md_orig_table | table | into string | lines | each {$'// ($in)' | str trim --r

print "```"

print "#code-block-starting-line-in-original-md-69"
"
## gen-intermid-script

The `gen-intermid-script` command generates a script that contains code from all executable blocks and `numd` service commands used for capturing outputs.
" | print
print "```nu indent-output"
print ("# Here we emulate that the `$intermid_script_path` options is not set
let $intermid_script_path = $file
Expand All @@ -69,7 +82,11 @@ open $intermid_script_path | table | into string | lines | each {$'// ($in)' |

print "```"

print "#code-block-starting-line-in-original-md-125"
"
## run-intermid-script

The `run-intermid-script` command runs and captures outputs of the executed intermediate script.
" | print
print "```nu indent-output"
print ("# the flag `$no_fail_on_error` is set to false
let $no_fail_on_error = false
Expand All @@ -89,35 +106,29 @@ $nu_res_stdout_lines | table | into string | lines | each {$'// ($in)' | str tr

print "```"

print "#code-block-starting-line-in-original-md-167"
"" | print
print "```nu indent-output"
print ("let $nu_res_with_block_index = parse-block-index $nu_res_stdout_lines
$nu_res_with_block_index" | nu-highlight)

print "```\n```output-numd"

let $nu_res_with_block_index = parse-block-index $nu_res_stdout_lines
$nu_res_with_block_index | table | into string | lines | each {$'// ($in)' | str trim --right} | str join (char nl) | print; print ''

print "```"

print "#code-block-starting-line-in-original-md-202"
print "```nu indent-output"
print ("let $md_res = assemble-markdown $md_orig_table $nu_res_with_block_index
print ("let $md_res = $nu_res_stdout_lines
| str join (char nl)
| prettify-markdown

$md_res" | nu-highlight)

print "```\n```output-numd"

let $md_res = assemble-markdown $md_orig_table $nu_res_with_block_index
let $md_res = $nu_res_stdout_lines
| str join (char nl)
| prettify-markdown

$md_res | table | into string | lines | each {$'// ($in)' | str trim --right} | str join (char nl) | print; print ''

print "```"

print "#code-block-starting-line-in-original-md-245"
"
## calc-changes-stats

The `calc-changes-stats` command displays stats on the changes made.
" | print
print "```nu indent-output"
print ("calc-changes-stats $file $md_orig $md_res" | nu-highlight)

Expand Down
Loading